Skip to content

Commit 7375134

Browse files
committed
Reimplement legacy Sound class with new audio engine
1 parent 2849d39 commit 7375134

26 files changed

+1090
-1324
lines changed

packages/dev/core/src/Audio/Interfaces/ISoundOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface ISoundOptions {
4040
* Define the distance attenuation model the sound will follow.
4141
* @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
4242
*/
43-
distanceModel?: string;
43+
distanceModel?: "linear" | "inverse" | "exponential";
4444
/**
4545
* Defines the playback speed (1 by default)
4646
*/

packages/dev/core/src/Audio/audioEngine.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import type { Analyser } from "./analyser";
2-
3-
import type { Nullable } from "../types";
4-
import { Observable } from "../Misc/observable";
5-
import { AbstractEngine } from "../Engines/abstractEngine";
6-
import type { IAudioEngine } from "./Interfaces/IAudioEngine";
71
import { _WebAudioEngine } from "../AudioV2/webAudio/webAudioEngine";
82
import type { _WebAudioMainBus } from "../AudioV2/webAudio/webAudioMainBus";
3+
import { AbstractEngine } from "../Engines/abstractEngine";
4+
import { Observable } from "../Misc/observable";
5+
import type { Nullable } from "../types";
6+
import type { Analyser } from "./analyser";
7+
import type { IAudioEngine } from "./Interfaces/IAudioEngine";
98

109
// Sets the default audio engine to Babylon.js
1110
AbstractEngine.AudioEngineFactory = (
@@ -22,7 +21,6 @@ AbstractEngine.AudioEngineFactory = (
2221
* @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic
2322
*/
2423
export class AudioEngine implements IAudioEngine {
25-
private _audioContext: Nullable<AudioContext> = null;
2624
private _masterGain: GainNode;
2725
private _tryToRun = false;
2826
private _useCustomUnlockedButton: boolean = false;
@@ -176,7 +174,7 @@ export class AudioEngine implements IAudioEngine {
176174
* This is helpful to resume play once browser policies have been satisfied.
177175
*/
178176
public unlock() {
179-
if (this._audioContext?.state === "running") {
177+
if (this._v2._audioContext?.state === "running") {
180178
if (!this.unlocked) {
181179
// Notify users that the audio stack is unlocked/unmuted
182180
this.unlocked = true;
@@ -192,10 +190,10 @@ export class AudioEngine implements IAudioEngine {
192190

193191
/** @internal */
194192
public _resumeAudioContextOnStateChange(): void {
195-
this._audioContext?.addEventListener(
193+
this._v2._audioContext?.addEventListener(
196194
"statechange",
197195
() => {
198-
if (this.unlocked && this._audioContext?.state !== "running") {
196+
if (this.unlocked && this._v2._audioContext?.state !== "running") {
199197
// eslint-disable-next-line @typescript-eslint/no-floating-promises
200198
this._resumeAudioContextAsync();
201199
}
@@ -214,6 +212,10 @@ export class AudioEngine implements IAudioEngine {
214212
return Promise.resolve();
215213
}
216214

215+
if (this._v2._audioContext.state === "suspended" && !this._useCustomUnlockedButton) {
216+
this._v2._unmuteUIEnabled = true;
217+
}
218+
217219
return this._v2._audioContext.resume();
218220
}
219221

0 commit comments

Comments
 (0)