Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dev/core/src/Audio/Interfaces/ISoundOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ISoundOptions {
* Define the distance attenuation model the sound will follow.
* @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic#creating-a-spatial-3d-sound
*/
distanceModel?: string;
distanceModel?: "linear" | "inverse" | "exponential";
/**
* Defines the playback speed (1 by default)
*/
Expand Down
23 changes: 13 additions & 10 deletions packages/dev/core/src/Audio/audioEngine.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Analyser } from "./analyser";

import type { Nullable } from "../types";
import { Observable } from "../Misc/observable";
import { AbstractEngine } from "../Engines/abstractEngine";
import type { IAudioEngine } from "./Interfaces/IAudioEngine";
import { _WebAudioEngine } from "../AudioV2/webAudio/webAudioEngine";
import type { _WebAudioMainBus } from "../AudioV2/webAudio/webAudioMainBus";
import { AbstractEngine } from "../Engines/abstractEngine";
import { Observable } from "../Misc/observable";
import type { Nullable } from "../types";
import type { Analyser } from "./analyser";
import type { IAudioEngine } from "./Interfaces/IAudioEngine";

// Sets the default audio engine to Babylon.js
AbstractEngine.AudioEngineFactory = (
Expand All @@ -22,7 +21,6 @@ AbstractEngine.AudioEngineFactory = (
* @see https://doc.babylonjs.com/features/featuresDeepDive/audio/playingSoundsMusic
*/
export class AudioEngine implements IAudioEngine {
private _audioContext: Nullable<AudioContext> = null;
private _masterGain: GainNode;
private _tryToRun = false;
private _useCustomUnlockedButton: boolean = false;
Expand Down Expand Up @@ -176,7 +174,7 @@ export class AudioEngine implements IAudioEngine {
* This is helpful to resume play once browser policies have been satisfied.
*/
public unlock() {
if (this._audioContext?.state === "running") {
if (this._v2._audioContext?.state === "running") {
if (!this.unlocked) {
// Notify users that the audio stack is unlocked/unmuted
this.unlocked = true;
Expand All @@ -192,10 +190,10 @@ export class AudioEngine implements IAudioEngine {

/** @internal */
public _resumeAudioContextOnStateChange(): void {
this._audioContext?.addEventListener(
this._v2._audioContext?.addEventListener(
"statechange",
() => {
if (this.unlocked && this._audioContext?.state !== "running") {
if (this.unlocked && this._v2._audioContext?.state !== "running") {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._resumeAudioContextAsync();
}
Expand All @@ -214,6 +212,10 @@ export class AudioEngine implements IAudioEngine {
return Promise.resolve();
}

if (this._v2._audioContext.state === "suspended" && !this._useCustomUnlockedButton) {
this._v2._unmuteUIEnabled = true;
}

return this._v2._audioContext.resume();
}

Expand Down Expand Up @@ -261,6 +263,7 @@ export class AudioEngine implements IAudioEngine {

private async _triggerRunningStateAsync() {
if (this._tryToRun) {
void this._v2._audioContext.resume();
return;
}
this._tryToRun = true;
Expand Down
Loading
Loading