Skip to content

Commit

Permalink
feat: add the ability of loadSound with AudioBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Sep 2, 2024
1 parent 045ecaf commit 9a84cc3
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/assets/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class SoundData {
this.buf = buf;
}

static fromAudioBuffer(buf: AudioBuffer): SoundData {
return new SoundData(buf);
}

static fromArrayBuffer(buf: ArrayBuffer): Promise<SoundData> {
return new Promise((resolve, reject) =>
audio.ctx.decodeAudioData(buf, resolve, reject)
Expand Down Expand Up @@ -61,15 +65,22 @@ export function getSound(name: string): Asset<SoundData> | null {
// load a sound to asset manager
export function loadSound(
name: string | null,
src: string | ArrayBuffer,
src: string | ArrayBuffer | AudioBuffer,
): Asset<SoundData> {
src = fixURL(src);
return assets.sounds.add(
name,
typeof src === "string"
? SoundData.fromURL(src)
: SoundData.fromArrayBuffer(src),
);
const fixedSrc = fixURL(src);
let sound: Promise<SoundData> | SoundData;

if (typeof fixedSrc === "string") {
sound = SoundData.fromURL(fixedSrc);
}
else if (fixedSrc instanceof ArrayBuffer) {
sound = SoundData.fromArrayBuffer(fixedSrc);
}
else {
sound = Promise.resolve(SoundData.fromAudioBuffer(fixedSrc));
}

return assets.sounds.add(name, sound);
}

export function loadMusic(
Expand Down

0 comments on commit 9a84cc3

Please sign in to comment.