diff --git a/packages/dev/core/src/Audio/sound.ts b/packages/dev/core/src/Audio/sound.ts index 9d2e5d218a3..00bc3bbc18a 100644 --- a/packages/dev/core/src/Audio/sound.ts +++ b/packages/dev/core/src/Audio/sound.ts @@ -158,7 +158,7 @@ export class Sound { private _registerFunc: Nullable<(connectedMesh: TransformNode) => void>; private _isOutputConnected = false; private _htmlAudioElement: HTMLAudioElement; - private _urlType: "Unknown" | "String" | "Array" | "ArrayBuffer" | "MediaStream" | "MediaElement" = "Unknown"; + private _urlType: "Unknown" | "String" | "Array" | "ArrayBuffer" | "MediaStream" | "AudioBuffer" | "MediaElement" = "Unknown"; private _length?: number; private _offset?: number; @@ -173,7 +173,7 @@ export class Sound { /** * Create a sound and attach it to a scene * @param name Name of your sound - * @param urlOrArrayBuffer Url to the sound to load async or ArrayBuffer, it also works with MediaStreams + * @param urlOrArrayBuffer Url to the sound to load async or ArrayBuffer, it also works with MediaStreams and AudioBuffers * @param scene defines the scene the sound belongs to * @param readyToPlayCallback Provide a callback function if you'd like to load your code once the sound is ready to be played * @param options Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel, streaming @@ -238,6 +238,8 @@ export class Sound { this._urlType = "MediaElement"; } else if (urlOrArrayBuffer instanceof MediaStream) { this._urlType = "MediaStream"; + } else if (urlOrArrayBuffer instanceof AudioBuffer) { + this._urlType = "AudioBuffer"; } else if (Array.isArray(urlOrArrayBuffer)) { this._urlType = "Array"; } @@ -278,6 +280,9 @@ export class Sound { this._soundLoaded(urlOrArrayBuffer); } break; + case "AudioBuffer": + this._audioBufferLoaded(urlOrArrayBuffer); + break; case "String": urls.push(urlOrArrayBuffer); // eslint-disable-next-line no-fallthrough @@ -443,6 +448,20 @@ export class Sound { return "Sound"; } + private _audioBufferLoaded(buffer: AudioBuffer) { + if (!Engine.audioEngine?.audioContext) { + return; + } + this._audioBuffer = buffer; + this._isReadyToPlay = true; + if (this.autoplay) { + this.play(0, this._offset, this._length); + } + if (this._readyToPlayCallback) { + this._readyToPlayCallback(); + } + } + private _soundLoaded(audioData: ArrayBuffer) { if (!Engine.audioEngine?.audioContext) { return; @@ -450,14 +469,7 @@ export class Sound { Engine.audioEngine.audioContext.decodeAudioData( audioData, (buffer) => { - this._audioBuffer = buffer; - this._isReadyToPlay = true; - if (this.autoplay) { - this.play(0, this._offset, this._length); - } - if (this._readyToPlayCallback) { - this._readyToPlayCallback(); - } + this._audioBufferLoaded(buffer); }, (err: any) => { Logger.Error("Error while decoding audio data for: " + this.name + " / Error: " + err);