Audio Tone Positional
Updated
A synthesizer placed in the world, quieter with distance.
- Type
ent-audio-tone-positional- Script class
PositionalAudioToneEntity- Family
- Audio
Overview
Section titled “Overview”An Audio Tone Positional entity is a script-played synthesizer placed in the world. It plays notes exactly like the Audio Tone, but the notes come from where the entity stands and get quieter with distance, like an Audio Positional sound: a bell that rings on a tower, a music box on a shelf, a console that beeps when touched, an instrument the player can walk around.
Its sound comes from the required Positional Audio Tone component (PositionalAudioToneComponent in scripts). It has no file and no autoplay: it is silent until a script plays a note, and in the editor selecting it plays its note once. Its events are named AudioTonePositional::…. Because it is positional, an Audio Zone can confine it to a room.
Pick the plain Audio Tone for interface sounds that should be heard the same everywhere, and an Audio Positional for a recorded sound at a point.
The props that matter most:
oscillator: the waveform:sine,triangle(the default),squareorsawtooth.attack,decay,sustain,release: the envelope that shapes each note, in seconds (sustainis a level from 0 to 1).noteandduration: whatplay()plays when a script gives no arguments.refDistance: the distance in metres within which the notes are at full volume.rolloffFactor,distanceModelandmaxDistance: how the volume falls with distance, exactly as on Audio Positional. Withlinear, the notes fall silent atmaxDistance.
It has the same methods as the Audio Tone: play(note?, duration?, delay?), attack(note, delay?), release(note?, delay?), stop(), isPlaying() and setVolume(v).
Set it up
Section titled “Set it up”- Add an Audio Tone Positional entity to the scene. To attach the sound to an object, place it as a child of that object.
- Choose an
oscillatorand shape the envelope; select the entity to hear itsnote. - Set
refDistanceto the radius where the notes are at full volume, for example2for a music box,10for a large bell. - For a sound that must stop at a known range, set
distanceModeltolinearandmaxDistanceto that range. - Add a Signal Emitter with a
signalif scripts need its events.
Examples
Section titled “Examples”A bell that rings when clicked
Section titled “A bell that rings when clicked”Ring a bell from where it hangs each time the player clicks it.
#pragma lifecycle(startup)
// Scene: a mesh "Bell" with a Selectable and a Signal Emitter on 'bell/clicked'.// As its child, an Audio Tone Positional entity "Bell Tone"// (oscillator: sine, attack: 0.002, decay: 1.5, sustain: 0.1, release: 2, refDistance: 6).let tone;
function startup() { tone = findEntity('Bell Tone').getComponent(PositionalAudioToneComponent);
on('bell/clicked', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again tone.play(['G3', 'D4', 'G4'], 0.2); });}How it works:
- The tone entity is a child of the bell, so the ring comes from the bell wherever it hangs.
- A very short attack and a long decay and release make a struck, ringing sound. The chord of a root, a fifth and an octave gives the bell its body.
refDistance: 6keeps the bell loud within 6 m; beyond, it fades naturally.
A music box that plays a tune
Section titled “A music box that plays a tune”Play a short melody from an object, and report each note as it is scheduled.
#pragma lifecycle(startup)
// Scene: an Audio Tone Positional entity "Music Box" (oscillator: triangle,// attack: 0.005, decay: 0.3, sustain: 0.2, release: 0.6, refDistance: 1.5)// with a Signal Emitter on 'musicbox'. Another script emits 'musicbox/wind'.const TUNE = [ ['E5', 0.25], ['D5', 0.25], ['C5', 0.25], ['D5', 0.25], ['E5', 0.25], ['E5', 0.25], ['E5', 0.5],];let box;
function playTune() { box.stop(); let t = 0; for (const [note, length] of TUNE) { box.play(note, length * 0.9, t); t += length; }}
function startup() { box = findEntity('Music Box').getComponent(PositionalAudioToneComponent); on('musicbox/wind', playTune);
on('musicbox', (event) => { if (event.eventType === EventType.AudioTonePositional.Play) log('note', event.data.note, 'in', event.data.delay.toFixed(2), 's'); });}How it works:
- Each note is a pair of a name and a length in seconds. The loop schedules every note at once, each
tseconds from now, on the audio clock, so the tune stays in time. - Holding each note for 90 % of its length leaves a tiny gap, so repeated notes are heard separately.
- Every scheduled note sends an
AudioTonePositional::playevent withdata.noteanddata.delay, the seconds until it sounds. stop()first cuts a tune still playing, so winding the box twice restarts it cleanly.
A proximity beacon
Section titled “A proximity beacon”Beep faster and higher as the player gets closer to a hidden object.
#pragma lifecycle(startup, update)
// Scene: an Audio Tone Positional entity "Beacon" (oscillator: square,// attack: 0.001, decay: 0.05, sustain: 0, release: 0.03, refDistance: 2).const RANGE = 20;const beaconPos = new THREE.Vector3();const eyePos = new THREE.Vector3();let beacon;let tone;let wait = 0;
function startup() { beacon = findEntity('Beacon'); tone = beacon.getComponent(PositionalAudioToneComponent);}
function update(dt) { if (!camera || !tone) return; beacon.getWorldPosition(beaconPos); camera.getWorldPosition(eyePos); const distance = eyePos.distanceTo(beaconPos); if (distance > RANGE) return;
wait -= dt; if (wait > 0) return; const closeness = 1 - distance / RANGE; // 0 far away, 1 on top of it tone.play(400 + closeness * 800, 0.05); wait = 1 - closeness * 0.9; // 1 s apart far away, 0.1 s close by}How it works:
getWorldPositionon the beacon and oncameragives both positions in world space;distanceTomeasures how far the player is.- A note can be a number: a frequency in hertz. The pitch rises from 400 Hz far away to 1200 Hz on top of the beacon.
waitcounts down by the frame timedt; when it reaches zero a beep plays and the next wait is set, shorter as the player gets closer.- Because the tone is positional, the beep also comes from the beacon’s direction and gets louder as the player approaches.
- With the default
inversemodel,maxDistancechanges nothing. Uselinearfor a hard edge, or an Audio Zone to keep the notes inside a room. - An idle tone costs almost nothing, and each note is freed when it falls silent, so many small tone entities in a scene are fine.
- Notes played before the user’s first click, tap or key press sound once that first gesture unlocks audio.
Components
Section titled “Components”What a new Audio Tone Positional is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Audio Tone Positional | comp- |
Required | One | A synthesizer placed in the world, quieter with distance. |
You can also add 14 optional components, such as AI Speech Capture, Animation Graph, Animation Timeline, Attribution, Audio Analyzer, Audio Chorus Filter.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
distanceModel |
"inverse" |
|
refDistance |
1 |
0. |
maxDistance |
10000 |
0. |
rolloffFactor |
1 |
0 ≤ x ≤ 10 |
oscillator |
"triangle" |
|
attack |
0.005 |
0 ≤ x ≤ 10 |
decay |
0.1 |
0 ≤ x ≤ 10 |
sustain |
0.3 |
0 ≤ x ≤ 1 |
release |
1 |
0 ≤ x ≤ 10 |
voices |
8 |
1 ≤ x ≤ 64 |
note |
"C4" |
|
duration |
0.5 |
0 ≤ x ≤ 60 |
volume |
1 |
0 ≤ x ≤ 1 |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
AudioTonePositional::readyAudioTonePositional::playAudioTonePositional::stop
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
Five stones each carry a child Positional Audio Tone: bouncing balls land on them, and the stones' collision events play each one's note from where it sits.
With your sound on, click once to start the audio, then listen: every bounce plays the stone's note, and the log shows its frequency. Run the example again to replay. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Audio Tone Positional.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Audio Tone Positional'); if (!(entity instanceof PositionalAudioToneEntity)) return;
// Its Audio Tone Positional component holds the settings listed under Props. const component = entity.getComponent(PositionalAudioToneComponent); log('refDistance is', component.props.refDistance); component.setProps({ refDistance: 1.5 });}