From 299233d3a030ded0745eb20a71a24fba2a7e8b6d Mon Sep 17 00:00:00 2001 From: Timmy Kokke Date: Fri, 2 Sep 2022 22:04:22 +0200 Subject: [PATCH 1/3] Added passing of existing AudioBuffer to Sound --- packages/dev/core/src/Audio/sound.ts | 32 +++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/dev/core/src/Audio/sound.ts b/packages/dev/core/src/Audio/sound.ts index 9d2e5d218a3..e984cf7a6e6 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; @@ -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"; } @@ -272,12 +274,15 @@ export class Sound { this._readyToPlayCallback(); } break; - case "ArrayBuffer": + case "ArrayBuffer": if ((urlOrArrayBuffer).byteLength > 0) { codecSupportedFound = true; 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); From abfbec89e0c613d81f81b925d6b5156cf95dc2ed Mon Sep 17 00:00:00 2001 From: Timmy Kokke Date: Fri, 2 Sep 2022 22:06:15 +0200 Subject: [PATCH 2/3] Fixed comment --- packages/dev/core/src/Audio/sound.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/core/src/Audio/sound.ts b/packages/dev/core/src/Audio/sound.ts index e984cf7a6e6..a1f491a4454 100644 --- a/packages/dev/core/src/Audio/sound.ts +++ b/packages/dev/core/src/Audio/sound.ts @@ -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 From 8a7d8549b2a2eea333e585c44588c7a26d5f7143 Mon Sep 17 00:00:00 2001 From: Timmy Kokke Date: Fri, 2 Sep 2022 22:40:49 +0200 Subject: [PATCH 3/3] removed whitespace --- packages/dev/core/src/Audio/sound.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dev/core/src/Audio/sound.ts b/packages/dev/core/src/Audio/sound.ts index a1f491a4454..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" | "AudioBuffer" |"MediaElement" = "Unknown"; + private _urlType: "Unknown" | "String" | "Array" | "ArrayBuffer" | "MediaStream" | "AudioBuffer" | "MediaElement" = "Unknown"; private _length?: number; private _offset?: number; @@ -274,7 +274,7 @@ export class Sound { this._readyToPlayCallback(); } break; - case "ArrayBuffer": + case "ArrayBuffer": if ((urlOrArrayBuffer).byteLength > 0) { codecSupportedFound = true; this._soundLoaded(urlOrArrayBuffer); @@ -451,7 +451,7 @@ export class Sound { private _audioBufferLoaded(buffer: AudioBuffer) { if (!Engine.audioEngine?.audioContext) { return; - } + } this._audioBuffer = buffer; this._isReadyToPlay = true; if (this.autoplay) {