Skip to content

Audio

Updated

Sound tells players what is happening before they see it: a door creaks behind them, a fountain bubbles in the next courtyard, a button clicks under their finger. In MIRA every sound comes from an audio entity placed in the scene: music and narration heard everywhere, a radio that plays from its desk, a 360-degree forest ambience, interface blips and melodies generated by scripts, and zones that keep each room’s sound in its room. A Behavior script finds that entity, reads its audio component and calls methods such as play(), pause() and setVolume(). The same scene sounds right in a desktop browser, on a phone and in a VR or AR headset.

  • Audio entity. An entity whose job is to make one sound. There is one entity type per kind of source: Audio, Positional Audio, Audio Ambisonic, Audio Tone and Positional Audio Tone. Each carries exactly one source component that it requires and that cannot be removed.
  • Source component. The component that produces the sound and that your script controls. AudioComponent plays a sound file heard the same everywhere. PositionalAudioComponent plays a file from a point in the world. AudioAmbisonicComponent plays a 360-degree soundfield. AudioToneComponent and PositionalAudioToneComponent are small synthesizers that play notes a script asks for.
  • Listener. The ear of the scene. It rides the active camera (in a headset, the user’s head), so what you hear always matches what you see. Scripts can reach it as the audioListener global.
  • Global and positional sound. A global source (Audio, Audio Tone, Audio Ambisonic) sounds the same wherever the player stands: music, narration, interface clicks. A positional source comes from its entity’s position, turns as the player turns, and gets quieter with distance according to refDistance, rolloffFactor, distanceModel and maxDistance.
  • Optional audio components. Components you add next to a source to extend it: Audio Fade ramps the sound in and out, Audio Analyzer reports live frequency data for visuals, and audio filters (low pass, high pass, echo, reverb, chorus, distortion) change how it sounds.
  • Audio Zone. A box that confines positional sound to a room: while the listener is inside, only the sources in the same zone are heard. Only positional sources are confined by Audio Zones.
  • Events. Sources report what happens to them (ready, play, pause, stop, end, error) through a Signal Emitter on the same entity. Scripts listen with on(signal, …) and compare event.eventType with an EventType constant.

Every audio entity follows the same life:

  1. Loading. When the scene loads, sampled sources load their sound files. The scene waits for them, so an autoplaying sound is ready the moment the scene appears. A tone has nothing to load.
  2. Entering the scene. Sources with autoplay on start playing. Sources with an Audio Fade fade in.
  3. Playing. Your scripts call methods on the source: play(), pause(), stop(), setVolume(v), or play(note) on a tone. Prop changes go through setProps({ … }). Every change is reported to the entity’s Signal Emitters as an event.
  4. The sound path. The source’s sound passes through its volume, its distance falloff (positional sources), its fade, any audio zone mute, then each enabled filter in order, and finally reaches the listener.
  5. Leaving the scene. Sources stop, or fade out first when they have an Audio Fade.

Browsers keep audio silent until the user interacts with the page. MIRA unlocks sound on the first click, tap or key press. Sources that started before that moment simply become audible then; you do not need to play them again.

This walkthrough adds background music and a button that pauses and resumes it.

  1. Add an Audio entity to the scene and name it Music. Set its src to a music file, leave repeat on repeat and autoplay on.

  2. Add a Signal Emitter to Music with the signal music. The source now reports its events on that signal.

  3. Add a Selectable object to click, for example a cube named Radio, and give it a Signal Emitter with the signal radio/clicked.

  4. Add a Behavior entity and paste this script:

    MusicToggle.jx
    #pragma lifecycle(startup)
    let music;
    function startup() {
    music = findEntity('Music').getComponent(AudioComponent);
    // play() after pause() resumes from where it stopped.
    on('radio/clicked', (event) => {
    if (event.eventType !== EventType.Selectable.Select) return;
    findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
    if (music.isPlaying()) music.pause();
    else music.play();
    });
    on('music', (event) => {
    if (event.eventType === EventType.Audio.Ready) log(`Music loaded: ${event.data.duration.toFixed(1)} s`);
    if (event.eventType === EventType.Audio.Play) log('Music playing');
    if (event.eventType === EventType.Audio.Pause) log('Music paused');
    });
    }
  5. Enter play and click the radio. The music pauses; click again and it resumes where it stopped.

How it works:

  • findEntity('Music').getComponent(AudioComponent) looks the source up once, in startup, and keeps it in a variable.
  • isPlaying() tells whether the sound is playing now, so one click handler can both pause and resume.
  • pause() keeps the playback position; play() resumes from it. stop() would rewind to the start instead.
  • The second listener shows the source’s own events. event.data.duration on Audio::ready is the length of the file in seconds.
If you want… Use Read
Music, narration or UI sounds heard the same everywhere Audio (AudioComponent) Audio sources
A sound that comes from an object and fades with distance Positional Audio (PositionalAudioComponent) Spatial audio
A full 360-degree recorded ambience that surrounds the player Audio Ambisonic (AudioAmbisonicComponent) Audio sources
Blips, chimes or melodies generated by a script, with no file Audio Tone, or Positional Audio Tone for notes that come from a point in the world Tones
Sound that fades in and out when the scene starts or the entity switches Audio Fade Audio sources
Visuals that pulse to the music Audio Analyzer Audio sources
Each room to have its own soundscape, its sound staying in that room Audio Zone Spatial audio
A muffled, echoing, roomy or distorted sound Audio filters Audio filters

Each audio entity type holds exactly one sound source; Audio Zone holds the zone. Each type has a page of its components, props, events and a first script:

EntityTypeWhat it is
Audioent-audioPlays a sound heard the same everywhere, for music or UI.
Audio Ambisonicent-audio-ambisonicPlays a 360 soundfield as binaural audio.
Audio Positionalent-audio-positional3D sound that gets quieter with distance.
Audio Toneent-audio-toneA synthesizer scripts play notes on: one-shots, held notes and chords.
Audio Tone Positionalent-audio-tone-positionalA synthesizer placed in the world, quieter with distance.
Audio Zoneent-audio-zoneInside the box, only sounds in the same zone are heard.

These globals are the classes and values audio scripts use most. Pass a component class to entity.getComponent(...) to reach the component, and use EventType to recognize the events.

GlobalWhat it is
AudioComponentA global (non-positional) source of sound. Owns a flat THREE.Audio node added to the host entity, loads the buffer once through the AssetManager, and drives play/stop across the fragment activate/deactivate lifecycle. The host AudioEntity is a thin container that delegates preload/dispose and the play/pause/stop controls here.
PositionalAudioComponentA 3D, distance-attenuated source of sound: a THREE.PositionalAudio node that follows the host entity's transform and gets quieter with distance from the listener. Shares all buffer-loading and play/fade/stop lifecycle logic with its base AudioComponent; it only swaps the node type and layers on the distance params.
AudioAmbisonicComponentPlays an ambisonic (B-format, AmbiX ACN-SN3D) soundfield — first, second, or third order — and renders it to binaural headphone audio for the XR runtime. The (order+1)² B-format channels are decoded to a near-uniform sphere of virtual loudspeakers, each fed through a native Web Audio HRTF PannerNode; the listener's head rotation is applied for free by the global audio listener, so there is no per-frame cost — the node graph is built once and static.
AudioToneComponentA synthesizer heard the same wherever the listener is: each note is an oscillator with an attack-decay-sustain-release envelope, all on the shared listener's context. Scripts play notes — one-shots with play, held notes and chords with attack and release — at an offset from now, so a melody is a handful of calls scheduled at once. Nothing runs per frame and no timer is kept: every change is scheduled on the audio clock, and a finished voice frees itself when its oscillator ends.
PositionalAudioToneComponentA 3D synthesizer: an AudioToneComponent whose THREE.PositionalAudio node follows the host entity's transform and gets quieter with distance from the listener. Shares all voice and lifecycle logic with its base; it only swaps the node type, layers on the distance params and joins the audio zones.
AudioFadeComponentRamps a sibling audio source's gain across the fragment lifecycle: fading up from silence on activation (fade-in), down to silence on deactivation (fade-out), or both (fade-in-out). Works with any AudioSourceComponent — the flat/positional AudioComponent family and AudioAmbisonicComponent.
AudioAnalyzerComponentWraps THREE.AudioAnalyser around the sibling audio source's node, exposing real-time frequency-domain data for reactive visuals (equalizers, audio-driven scale/color, etc.). Purely on-demand — the analyzer only samples when a caller pulls data, so there is no per-frame cost unless something reads it.
AudioZoneComponentA box volume that clamps positional audio. While the listener is inside one or more zones, it hears only the PositionalAudioComponent sources inside the same zone(s); sources elsewhere are muted. With the listener outside every zone — or no zones in the scene — everything plays unclamped.
AudioLowPassFilterComponentLets the lows through and cuts what is above frequency (default 5000 Hz): muffles.
AudioHighPassFilterComponentLets the highs through and cuts what is below frequency (default 200 Hz): thins.
AudioEchoFilterComponentRepeats the sibling source: a delay line whose output is darkened by a high cut and fed back into itself, so each repeat is quieter and duller than the last, mixed with the untouched sound.
AudioReverbFilterComponentPuts the sibling source in a room: a convolver fed a generated stereo impulse — decorrelated noise on each side, dying away exponentially over decay and darkening as it goes, after preDelay of silence — mixed with the untouched sound. Each side of the impulse is scaled to unit energy, so the reverberated sound is as loud as the sound itself at any decay: a longer decay is longer, not louder, and wet alone sets the balance. (The convolver's own normalization is off: it leaves a 2 s tail about 9 dB down and makes longer tails louder.) The impulse is built once, and again only when decay or preDelay change.
AudioChorusFilterComponentThickens the sibling source into a wide stereo image: each side is a delayed copy whose delay wavers with one low-frequency oscillator, the right side in opposite phase to the left, so the two copies detune against each other. A mono source is spread to both sides first. Mixed with the untouched sound.
AudioDistortionFilterComponentOverdrives the sibling source: a wave shaper with a tanh saturation curve, driven harder the higher amount. The curve is scaled so material at -12 dBFS comes out at the same level at any amount — more drive is more grit, not more volume. Shaped at 4× oversampling, so the harmonics it adds do not fold back as aliasing.
AudioEntityA global (non-positional) sound: one AudioComponent, heard the same wherever the listener is — music, ambience, narration. PositionalAudioEntity, AmbisonicAudioEntity and the tone entities extend it, each swapping in its own source: every audio entity has exactly one.
PositionalAudioEntityAn AudioEntity with a 3D, distance-attenuated source in place of the global one.
AmbisonicAudioEntityAn AudioEntity with a binaural soundfield source in place of the global one.
AudioToneEntityAn AudioEntity with a synthesizer in place of the sampled source: scripts play its notes.
PositionalAudioToneEntityAn AudioEntity with a 3D, distance-attenuated synthesizer in place of the sampled source.
AudioZoneEntityAn audio zone volume: an invisible box (its AudioZoneComponent) that clamps positional audio to whoever shares the zone with the listener. Renders a framed translucent box in the editor — gated on the global gizmos toggle — so the volume is placeable and visible; in play mode it has no geometry of its own.
audioListenerShared listener attached to the active camera; live, undefined until rendering starts.
EventTypeEvery event type a script can hear, by who sends it: compare them rather than typing the strings.
ComponentTypeWhat it does
Audiocomp-audioPlays a sound heard the same everywhere, for music or UI.
Audio Ambisoniccomp-audio-ambisonicPlays a 360 soundfield as binaural audio.
Audio Analyzercomp-audio-analyzerGives live frequency data for audio-reactive visuals.
Audio Chorus Filtercomp-audio-chorus-filterThickens and widens the sound with two wavering delayed copies of itself.
Audio Distortion Filtercomp-audio-distortion-filterOverdrives the sound, from a warm edge to a hard crunch, at a steady level.
Audio Echo Filtercomp-audio-echo-filterRepeats the sound at an interval, each repeat quieter and darker than the last.
Audio Fadecomp-audio-fadeFades a sound in on start and/or out on stop.
Audio High Pass Filtercomp-audio-high-pass-filterCuts the lows below the cutoff, thinning the sound.
Audio Low Pass Filtercomp-audio-low-pass-filterCuts the highs above the cutoff, muffling the sound.
Audio Positionalcomp-audio-positional3D sound that gets quieter with distance.
Audio Reverb Filtercomp-audio-reverb-filterAdds the reflections of a room, with a tail that darkens and dies away over the decay time.
Audio Tonecomp-audio-toneA synthesizer scripts play notes on: one-shots, held notes and chords.
Audio Tone Positionalcomp-audio-tone-positionalA synthesizer placed in the world, quieter with distance.
Audio Zonecomp-audio-zoneInside the box, only sounds in the same zone are heard.
Topic Description
Audio sources The Audio and Audio Ambisonic sources, their props, methods and events, autoplay, hiding and disabling, Audio Fade and Audio Analyzer.
Spatial audio Positional Audio, its distance props and distance models, and Audio Zones that keep each room’s sound in its room.
Tones The Audio Tone synthesizers: envelopes, notes, chords, held notes and melodies played from scripts.
Audio filters Low pass, high pass, echo, reverb, chorus and distortion, how they chain, and how to change them smoothly.