-
Notifications
You must be signed in to change notification settings - Fork 154
Add partial support to Audio through Miniaudio #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuripourre
wants to merge
6
commits into
BabylonJS:master
Choose a base branch
from
yuripourre:miniaudio
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+5,733
−67
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bcb08b0
Add miniaudio-backed WebAudio polyfill.
yuripourre 5ff7204
Integrate WebAudio into Embedding with mobile audio session hooks.
yuripourre 37441db
Add WebAudio coverage to JavaScript unit tests.
yuripourre 0e43544
Add Playground audio demo for on-device validation.
yuripourre ffb1fd6
Document WebAudio as partially supported in project status.
yuripourre 73acf9c
Improve desktop audio demo startup and document stub.
yuripourre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...yground/Android/BabylonNative/src/main/java/com/babylonjs/embedding/AudioFocusHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.babylonjs.embedding; | ||
|
|
||
| import android.content.Context; | ||
| import android.media.AudioAttributes; | ||
| import android.media.AudioFocusRequest; | ||
| import android.media.AudioManager; | ||
| import android.os.Build; | ||
|
|
||
| /** | ||
| * Requests Android audio focus before WebAudio playback starts. | ||
| */ | ||
| public final class AudioFocusHelper { | ||
| private static AudioManager audioManager; | ||
| private static AudioFocusRequest audioFocusRequest; | ||
| private static final AudioManager.OnAudioFocusChangeListener focusListener = focusChange -> { | ||
| switch (focusChange) { | ||
| case AudioManager.AUDIOFOCUS_LOSS: | ||
| case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: | ||
| case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: | ||
| nativeNotifyAudioInterruption(true); | ||
| break; | ||
| case AudioManager.AUDIOFOCUS_GAIN: | ||
| nativeNotifyAudioInterruption(false); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| static { | ||
| System.loadLibrary("BabylonNativeEmbedding"); | ||
| } | ||
|
|
||
| private AudioFocusHelper() {} | ||
|
|
||
| public static void initialize(Context context) { | ||
| audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); | ||
| } | ||
|
|
||
| public static void requestPlaybackFocus() { | ||
| if (audioManager == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| if (audioFocusRequest == null) { | ||
| AudioAttributes attributes = new AudioAttributes.Builder() | ||
| .setUsage(AudioAttributes.USAGE_GAME) | ||
| .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) | ||
| .build(); | ||
| audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) | ||
| .setAudioAttributes(attributes) | ||
| .setOnAudioFocusChangeListener(focusListener) | ||
| .build(); | ||
| } | ||
|
|
||
| audioManager.requestAudioFocus(audioFocusRequest); | ||
| } else { | ||
| audioManager.requestAudioFocus( | ||
| focusListener, | ||
| AudioManager.STREAM_MUSIC, | ||
| AudioManager.AUDIOFOCUS_GAIN); | ||
| } | ||
| } | ||
|
|
||
| private static native void nativeNotifyAudioInterruption(boolean began); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /// <reference path="../../node_modules/babylonjs/babylon.module.d.ts" /> | ||
|
|
||
| (function () { | ||
| if (typeof document === "undefined") { | ||
| globalThis.document = { | ||
| addEventListener: function () { }, | ||
| removeEventListener: function () { }, | ||
| createElement: function (tag) { | ||
| if (tag === "audio") { | ||
| return new Audio(); | ||
| } | ||
| return {}; | ||
| }, | ||
| createTextNode: function (text) { | ||
| return { nodeValue: text }; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function createBeepWavArrayBuffer(sampleRate, durationSeconds, frequency, amplitude) { | ||
| const frameCount = Math.max(1, Math.floor(sampleRate * durationSeconds)); | ||
| const bytesPerSample = 2; | ||
| const channelCount = 1; | ||
| const dataSize = frameCount * channelCount * bytesPerSample; | ||
| const buffer = new ArrayBuffer(44 + dataSize); | ||
| const view = new DataView(buffer); | ||
|
|
||
| const writeString = function (offset, value) { | ||
| for (let i = 0; i < value.length; i++) { | ||
| view.setUint8(offset + i, value.charCodeAt(i)); | ||
| } | ||
| }; | ||
|
|
||
| writeString(0, "RIFF"); | ||
| view.setUint32(4, 36 + dataSize, true); | ||
| writeString(8, "WAVE"); | ||
| writeString(12, "fmt "); | ||
| view.setUint32(16, 16, true); | ||
| view.setUint16(20, 1, true); | ||
| view.setUint16(22, channelCount, true); | ||
| view.setUint32(24, sampleRate, true); | ||
| view.setUint32(28, sampleRate * channelCount * bytesPerSample, true); | ||
| view.setUint16(32, channelCount * bytesPerSample, true); | ||
| view.setUint16(34, 8 * bytesPerSample, true); | ||
| writeString(36, "data"); | ||
| view.setUint32(40, dataSize, true); | ||
|
|
||
| for (let frame = 0; frame < frameCount; frame++) { | ||
| const sample = Math.sin((2 * Math.PI * frequency * frame) / sampleRate) * amplitude; | ||
| const intSample = Math.max(-32768, Math.min(32767, Math.round(sample * 32767))); | ||
| view.setInt16(44 + frame * bytesPerSample, intSample, true); | ||
| } | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
| let clickAudioContext = null; | ||
| let clickAudioBuffer = null; | ||
|
|
||
| async function playClickBeep() { | ||
| if (typeof AudioContext === "undefined") { | ||
| BABYLON.Tools.Warn("AudioContext is not available"); | ||
| return; | ||
| } | ||
|
|
||
| if (!clickAudioContext) { | ||
| clickAudioContext = new AudioContext(); | ||
| } | ||
|
|
||
| if (clickAudioContext.state === "suspended") { | ||
| await clickAudioContext.resume(); | ||
| } | ||
|
|
||
| if (!clickAudioBuffer) { | ||
| const wavBytes = createBeepWavArrayBuffer(44100, 0.25, 880, 0.4); | ||
| clickAudioBuffer = await clickAudioContext.decodeAudioData(wavBytes.slice(0)); | ||
| } | ||
|
|
||
| const source = clickAudioContext.createBufferSource(); | ||
| source.buffer = clickAudioBuffer; | ||
| source.connect(clickAudioContext.destination); | ||
| source.start(clickAudioContext.currentTime); | ||
| BABYLON.Tools.Log("Click beep"); | ||
| } | ||
|
|
||
| function createScene() { | ||
| const engine = new BABYLON.NativeEngine({ adaptToDeviceRatio: true }); | ||
| const scene = new BABYLON.Scene(engine); | ||
| scene.createDefaultCamera(true, true, true); | ||
|
|
||
| const beepBuffer = createBeepWavArrayBuffer(44100, 0.25, 880, 0.4); | ||
| let spatialSource = null; | ||
| let spatialStarted = false; | ||
|
|
||
| scene.onPointerObservable.add(function (pointerInfo) { | ||
| if (pointerInfo.type !== BABYLON.PointerEventTypes.POINTERDOWN) { | ||
| return; | ||
| } | ||
|
|
||
| playClickBeep().catch(function (error) { | ||
| BABYLON.Tools.Warn("Click beep failed: " + error); | ||
| }); | ||
|
|
||
| if (!spatialStarted && typeof BABYLON.Sound === "function") { | ||
| spatialStarted = true; | ||
| const legacySound = new BABYLON.Sound("beep", beepBuffer, scene, function () { | ||
| BABYLON.Tools.Log("Spatial beep started after click"); | ||
| }, { | ||
| autoplay: true, | ||
| loop: true, | ||
| spatialSound: true, | ||
| maxDistance: 20 | ||
| }); | ||
| spatialSource = legacySound; | ||
| } else if (spatialSource && typeof spatialSource.play === "function") { | ||
| spatialSource.play(); | ||
| } | ||
| }); | ||
|
|
||
| let angle = 0; | ||
| scene.onBeforeRenderObservable.add(function () { | ||
| if (!spatialSource) { | ||
| return; | ||
| } | ||
|
|
||
| angle += 0.02; | ||
| const x = Math.cos(angle) * 3; | ||
| const z = Math.sin(angle) * 3; | ||
|
|
||
| if (typeof spatialSource.setPosition === "function") { | ||
| spatialSource.setPosition(new BABYLON.Vector3(x, 0, z)); | ||
| } else if (spatialSource._soundSource) { | ||
| spatialSource._soundSource.position = new BABYLON.Vector3(x, 0, z); | ||
| } | ||
| }); | ||
|
|
||
| BABYLON.Tools.Log("Click anywhere to play a beep (and start spatial audio)"); | ||
|
|
||
| return scene; | ||
| } | ||
|
|
||
| globalThis.createScene = createScene; | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.