Skip to content

Audio Tone

Updated

A synthesizer scripts play notes on: one-shots, held notes and chords.

Type
ent-audio-tone
Script class
AudioToneEntity
Family
Audio

An Audio Tone entity is a small synthesizer that your scripts play notes on: interface blips, a success chime, a short jingle, a held chord, even a whole melody. There is no file to load and no file to host: the sound is generated as it plays. It is heard the same everywhere, like the global Audio entity. The entity draws nothing in play.

Its sound comes from the required Audio Tone component (AudioToneComponent in scripts). A tone has no src and no autoplay: it is silent until a script plays a note. In the editor, selecting the entity plays its note once, so you can hear how it is set up.

Each note is a voice: an oscillator shaped by an envelope in four stages. Attack rises from silence to full level, decay falls to the sustain level, which is held while the note is down, and release fades to silence once the note is let go.

Use it whenever a script needs a sound it can pitch freely. Pick a sibling for recorded sounds (Audio), or for notes that come from a point in the world (Audio Tone Positional).

The props that matter most:

  • oscillator: the waveform, which gives the note its colour: sine is pure and soft, triangle (the default) is mellow, square is hollow and retro, sawtooth is bright and buzzy.
  • attack, decay, sustain, release: the envelope, in seconds, except sustain, which is a level from 0 to 1. A short attack and release give a percussive blip; a long attack and release give a soft pad.
  • voices: the most notes that sound at once. When all are busy, the notes already fading out give way first, then the oldest.
  • note and duration: the note and the hold time play() uses when a script gives none.
  • volume: the loudness, from 0 to 1.

A note is a name with its octave, such as 'C4' (middle C), 'F#3' or 'Bb2', or a frequency in hertz such as 440. An array of notes is a chord. The main methods are play(note?, duration?, delay?), attack(note, delay?), release(note?, delay?), stop(), isPlaying() and setVolume(v), plus the static AudioToneComponent.frequencyOf(note).

  1. Add an Audio Tone entity to the scene and name it, for example Blip.
  2. Choose an oscillator and shape the envelope: for a click, a very short attack and release and a low sustain; for a soft pad, longer attack and release.
  3. Set note and duration to the sound play() makes with no arguments. Select the entity to hear it.
  4. Set voices to the number of notes you play together; the default of 8 suits chords and quick melodies.
  5. Add a Signal Emitter with a signal if scripts need to know when notes are scheduled or cut.

Give every UI button the same short sound, without any sound file.

ButtonBlip.jx
#pragma lifecycle(startup)
// Scene: an Audio Tone entity "Blip" (oscillator: sine, attack: 0.001,
// decay: 0.08, sustain: 0, release: 0.05).
// Every UI Button has a Signal Emitter on the signal 'ui/press'.
let blip;
function startup() {
blip = findEntity('Blip').getComponent(AudioToneComponent);
on('ui/press', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
blip.play('E6', 0.05);
});
}

How it works:

  • play('E6', 0.05) plays a high E, held for 50 ms, then released. Because sustain is 0, the note dies away during its decay: a short, crisp blip.
  • One signal serves every button: each button’s Signal Emitter uses ui/press, and the script keeps only UIButton::press events.
  • Notes can overlap: a quick double press plays two blips, each on its own voice.

Play a rising arpeggio and a final chord, scheduled all at once so it stays in time.

SuccessJingle.jx
#pragma lifecycle(startup)
// Scene: an Audio Tone entity "Jingle" (oscillator: triangle).
// Another script emits 'level/complete'.
let jingle;
function startup() {
jingle = findEntity('Jingle').getComponent(AudioToneComponent);
on('level/complete', () => {
jingle.stop(); // cut a jingle that is still ringing
jingle.play('C5', 0.12, 0);
jingle.play('E5', 0.12, 0.12);
jingle.play('G5', 0.12, 0.24);
jingle.play(['C5', 'E5', 'G5', 'C6'], 0.8, 0.36);
});
}

How it works:

  • The third argument of play is a delay in seconds from now. All the notes are scheduled immediately, on the audio clock, so a slow frame never makes the melody stumble.
  • The last call passes an array: the four notes start together as a chord.
  • stop() cuts the notes sounding and those still scheduled, so triggering the jingle twice never stacks two copies.

Sustain a chord for as long as the player holds the space bar.

HoldChord.jx
#pragma lifecycle(startup, update)
// Scene: an Audio Tone entity "Pad".
const CHORD = ['D3', 'F3', 'A3'];
let pad;
let held = false;
function startup() {
pad = findEntity('Pad').getComponent(AudioToneComponent);
pad.setProps({ oscillator: 'sawtooth', attack: 0.4, sustain: 0.6, release: 1.5 });
}
function update() {
const down = Input.keyboard.isKeyPressed(Key.Space);
if (down && !held) pad.attack(CHORD);
if (!down && held) pad.release(CHORD);
held = down;
}

How it works:

  • setProps shapes the pad from the script: a slow attack fades the chord in, a long release lets it ring after the key comes up. Prop changes apply to the next notes.
  • isKeyPressed is true every frame the key is down. The script compares it with last frame’s state, so attack runs once on the press and release once on the release.
  • attack(notes) holds the notes until release(notes) lets them go; they then fade over release seconds.

Play the notes of a scale on the number keys and report each note through the tone’s events.

NumberKeyPiano.jx
#pragma lifecycle(startup, update)
// Scene: an Audio Tone entity "Piano" with a Signal Emitter on the signal 'piano'.
const KEYS = [Key.Digit1, Key.Digit2, Key.Digit3, Key.Digit4, Key.Digit5, Key.Digit6, Key.Digit7, Key.Digit8];
const NOTES = ['C4', 'D4', 'E4', 'F4', 'G4', 'A4', 'B4', 'C5'];
let piano;
function startup() {
piano = findEntity('Piano').getComponent(AudioToneComponent);
on('piano', (event) => {
if (event.eventType !== EventType.AudioTone.Play) return;
const hz = AudioToneComponent.frequencyOf(event.data.note);
log('Note', event.data.note, '=', hz.toFixed(1), 'Hz');
});
}
function update() {
for (let i = 0; i < KEYS.length; i++) {
if (Input.keyboard.isKeyReleased(KEYS[i])) piano.play(NOTES[i], 0.3);
}
}

How it works:

  • isKeyReleased is true once per press, so each press plays one note of the C major scale.
  • Each play sends an AudioTone::play event, one per note, with data.note and data.delay (0 for a note that starts now).
  • AudioToneComponent.frequencyOf(note) is a static method: it is called on the class, and turns a note name into hertz, handy for driving visuals from the music.
  • A tone costs almost nothing when idle, and each note is freed as soon as it falls silent. You can keep several tone entities for different instruments.
  • Prop changes shape the next notes only. A note already sounding keeps its waveform and envelope.
  • Starting a note while the same pitch is held lets go of the held one first, so repeated notes never pile up.

What a new Audio Tone 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 comp-audio-tone Required One A synthesizer scripts play notes on: one-shots, held notes and chords.

You can also add 15 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
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.

  • AudioTone::ready
  • AudioTone::play
  • AudioTone::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.

AudioToneEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My Audio Tone');
if (!(entity instanceof AudioToneEntity)) return;
// Its Audio Tone component holds the settings listed under Props.
const component = entity.getComponent(AudioToneComponent);
log('attack is', component.props.attack);
component.setProps({ attack: 0.01 });
}