Skip to content

Add partial support to Audio through Miniaudio#1765

Open
yuripourre wants to merge 6 commits into
BabylonJS:masterfrom
yuripourre:miniaudio
Open

Add partial support to Audio through Miniaudio#1765
yuripourre wants to merge 6 commits into
BabylonJS:masterfrom
yuripourre:miniaudio

Conversation

@yuripourre

Copy link
Copy Markdown

This PR adds a miniaudio-backed Web Audio polyfill so Babylon.js audio works on native Embedding hosts (desktop and mobile).

New Polyfills/WebAudio

Implements the Web Audio subset Babylon.js uses: AudioContext (starts suspended), decodeAudioData, buffer sources with scheduled start(when), gain/panner/stereo panner, AudioListener, AudioParam automation, AnalyserNode, and HTMLAudioElement.canPlayType. Playback device is opened lazily on first resume().

Platform hooks

PreparePlayback / interruption callbacks for iOS (AVAudioSession) and Android (audio focus via JNI). Any app linking BabylonNativeEmbedding gets session setup without custom native code.

Embedding integration

WebAudio::Initialize from Runtime; Suspend/Resume pauses the shared audio device when the app backgrounds; built-in document stub when globalThis.document is missing.

CI / headless

BABYLON_NATIVE_WEBAUDIO_BACKEND=null uses a software clock so graph/lifecycle tests run without hardware.

Tests

New JavaScript.All WebAudio cases (suspended context, resume(), automation, analysers, natural ended, scheduled start(when)).

Playground harness

New Scripts/audio.js (click-to-beep + optional spatial BABYLON.Sound). iOS/Android Playground entry points load audio.js + playground_runner.js instead of experience.js for on-device audio validation. Android Gradle also copies missing babylonjs.addons.js into assets.

Design notes

  • Single shared native engine/clock across AudioContext instances (one playback device).
  • Browser-like lifecycle: contexts start suspended; clock advances only while at least one context is running.
  • Panner uses simplified equal-power azimuth, not browser HRTF.
  • URL streaming / MediaElement sources remain out of scope; buffer + decodeAudioData is the supported path.

Introduce the WebAudio polyfill with browser-aligned AudioContext lifecycle,
scheduled buffer-source start times, a built-in document stub, and platform
PreparePlayback hooks for mobile session setup.
Initialize the polyfill from Runtime, suspend/resume playback with app
lifecycle, and wire iOS AVAudioSession plus Android audio-focus handling.
Cover suspended-by-default contexts, resume, automation, analysers, natural
ended events, and scheduled buffer-source start times for headless CI.
Bundle and load audio.js on iOS and Android, copy babylonjs.addons.js into
Android assets, and resume AudioContext on user gesture for mobile autoplay.
Point readers to the WebAudio polyfill readme for platform coverage and
mobile validation guidance.
Resume AudioContext on audio.js load so legacy Sound autoplay works on
desktop, and add createTextNode to the built-in document stub for Babylon
v9 audio engine initialization.
@yuripourre

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant