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.
Key concepts
Section titled “Key concepts”- 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.
AudioComponentplays a sound file heard the same everywhere.PositionalAudioComponentplays a file from a point in the world.AudioAmbisonicComponentplays a 360-degree soundfield.AudioToneComponentandPositionalAudioToneComponentare 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
audioListenerglobal. - 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,distanceModelandmaxDistance. - 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 withon(signal, …)and compareevent.eventTypewith anEventTypeconstant.
How it works
Section titled “How it works”Every audio entity follows the same life:
- 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.
- Entering the scene. Sources with
autoplayon start playing. Sources with an Audio Fade fade in. - Playing. Your scripts call methods on the source:
play(),pause(),stop(),setVolume(v), orplay(note)on a tone. Prop changes go throughsetProps({ … }). Every change is reported to the entity’s Signal Emitters as an event. - 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.
- 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.
Your first sound
Section titled “Your first sound”This walkthrough adds background music and a button that pauses and resumes it.
-
Add an Audio entity to the scene and name it
Music. Set itssrcto a music file, leaverepeatonrepeatandautoplayon. -
Add a Signal Emitter to
Musicwith the signalmusic. The source now reports its events on that signal. -
Add a Selectable object to click, for example a cube named
Radio, and give it a Signal Emitter with the signalradio/clicked. -
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 againif (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');});} -
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, instartup, 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.durationonAudio::readyis the length of the file in seconds.
Your first sound: background music on an Audio entity, and a radio that pauses and resumes it when clicked, with the source’s own events in the log.
With your sound on, click the radio to pause the music, and again to resume it.
Choosing
Section titled “Choosing”| If you want… | Use | Read |
|---|---|---|
| Music, narration or UI sounds heard the same everywhere | Audio (Audio) |
Audio sources |
| A sound that comes from an object and fades with distance | Positional Audio (Positional) |
Spatial audio |
| A full 360-degree recorded ambience that surrounds the player | Audio Ambisonic (Audio) |
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 |
Entity types
Section titled “Entity types”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:
| Entity | Type | What it is |
|---|---|---|
| Audio | ent-audio | Plays a sound heard the same everywhere, for music or UI. |
| Audio Ambisonic | ent-audio-ambisonic | Plays a 360 soundfield as binaural audio. |
| Audio Positional | ent-audio-positional | 3D sound that gets quieter with distance. |
| Audio Tone | ent-audio-tone | A synthesizer scripts play notes on: one-shots, held notes and chords. |
| Audio Tone Positional | ent-audio-tone-positional | A synthesizer placed in the world, quieter with distance. |
| Audio Zone | ent-audio-zone | Inside the box, only sounds in the same zone are heard. |
In scripts
Section titled “In scripts”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.
| Global | What it is |
|---|---|
AudioComponent | A 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. |
PositionalAudioComponent | A 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. |
AudioAmbisonicComponent | Plays 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. |
AudioToneComponent | A 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. |
PositionalAudioToneComponent | A 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. |
AudioFadeComponent | Ramps 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. |
AudioAnalyzerComponent | Wraps 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. |
AudioZoneComponent | A 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. |
AudioLowPassFilterComponent | Lets the lows through and cuts what is above frequency (default 5000 Hz): muffles. |
AudioHighPassFilterComponent | Lets the highs through and cuts what is below frequency (default 200 Hz): thins. |
AudioEchoFilterComponent | Repeats 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. |
AudioReverbFilterComponent | Puts 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. |
AudioChorusFilterComponent | Thickens 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. |
AudioDistortionFilterComponent | Overdrives 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. |
AudioEntity | A 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. |
PositionalAudioEntity | An AudioEntity with a 3D, distance-attenuated source in place of the global one. |
AmbisonicAudioEntity | An AudioEntity with a binaural soundfield source in place of the global one. |
AudioToneEntity | An AudioEntity with a synthesizer in place of the sampled source: scripts play its notes. |
PositionalAudioToneEntity | An AudioEntity with a 3D, distance-attenuated synthesizer in place of the sampled source. |
AudioZoneEntity | An 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. |
audioListener | Shared listener attached to the active camera; live, undefined until rendering starts. |
EventType | Every event type a script can hear, by who sends it: compare them rather than typing the strings. |
Components
Section titled “Components”| Component | Type | What it does |
|---|---|---|
| Audio | comp-audio | Plays a sound heard the same everywhere, for music or UI. |
| Audio Ambisonic | comp-audio-ambisonic | Plays a 360 soundfield as binaural audio. |
| Audio Analyzer | comp-audio-analyzer | Gives live frequency data for audio-reactive visuals. |
| Audio Chorus Filter | comp-audio-chorus-filter | Thickens and widens the sound with two wavering delayed copies of itself. |
| Audio Distortion Filter | comp-audio-distortion-filter | Overdrives the sound, from a warm edge to a hard crunch, at a steady level. |
| Audio Echo Filter | comp-audio-echo-filter | Repeats the sound at an interval, each repeat quieter and darker than the last. |
| Audio Fade | comp-audio-fade | Fades a sound in on start and/or out on stop. |
| Audio High Pass Filter | comp-audio-high-pass-filter | Cuts the lows below the cutoff, thinning the sound. |
| Audio Low Pass Filter | comp-audio-low-pass-filter | Cuts the highs above the cutoff, muffling the sound. |
| Audio Positional | comp-audio-positional | 3D sound that gets quieter with distance. |
| Audio Reverb Filter | comp-audio-reverb-filter | Adds the reflections of a room, with a tail that darkens and dies away over the decay time. |
| Audio Tone | comp-audio-tone | A synthesizer scripts play notes on: one-shots, held notes and chords. |
| Audio Tone Positional | comp-audio-tone-positional | A synthesizer placed in the world, quieter with distance. |
| Audio Zone | comp-audio-zone | Inside the box, only sounds in the same zone are heard. |
In this section
Section titled “In this section”| 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. |