Skip to content

Audio Tone Positional

Updated

A synthesizer placed in the world, quieter with distance.

Type
ent-audio-tone-positional
Script class
PositionalAudioToneEntity
Family
Audio

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), square or sawtooth.
  • attack, decay, sustain, release: the envelope that shapes each note, in seconds (sustain is a level from 0 to 1).
  • note and duration: what play() plays when a script gives no arguments.
  • refDistance: the distance in metres within which the notes are at full volume.
  • rolloffFactor, distanceModel and maxDistance: how the volume falls with distance, exactly as on Audio Positional. With linear, the notes fall silent at maxDistance.

It has the same methods as the Audio Tone: play(note?, duration?, delay?), attack(note, delay?), release(note?, delay?), stop(), isPlaying() and setVolume(v).

  1. Add an Audio Tone Positional entity to the scene. To attach the sound to an object, place it as a child of that object.
  2. Choose an oscillator and shape the envelope; select the entity to hear its note.
  3. Set refDistance to the radius where the notes are at full volume, for example 2 for a music box, 10 for a large bell.
  4. For a sound that must stop at a known range, set distanceModel to linear and maxDistance to that range.
  5. Add a Signal Emitter with a signal if scripts need its events.

Ring a bell from where it hangs each time the player clicks it.

Bell.jx
#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: 6 keeps the bell loud within 6 m; beyond, it fades naturally.

Play a short melody from an object, and report each note as it is scheduled.

MusicBox.jx
#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 t seconds 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::play event with data.note and data.delay, the seconds until it sounds.
  • stop() first cuts a tune still playing, so winding the box twice restarts it cleanly.

Beep faster and higher as the player gets closer to a hidden object.

Beacon.jx
#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:

  • getWorldPosition on the beacon and on camera gives both positions in world space; distanceTo measures 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.
  • wait counts down by the frame time dt; 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 inverse model, maxDistance changes nothing. Use linear for 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.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Audio Tone Positional comp-audio-tone-positional 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.01 ≤ x ≤ 10000
maxDistance 10000 0.001 ≤ x ≤ 10000
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

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • AudioTonePositional::ready
  • AudioTonePositional::play
  • AudioTonePositional::stop

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

PositionalAudioToneEntity.jx
#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 });
}