Skip to content

Commit

Permalink
Merge pull request #1 from BabylonJS/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
james-pre committed Sep 3, 2022
2 parents 72610af + 19e98d3 commit a461644
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/dev/core/src/Audio/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -443,21 +448,28 @@ 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;
}
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);
Expand Down

0 comments on commit a461644

Please sign in to comment.