diff --git a/src/audio.ts b/src/audio.ts index 220f94f..9cc0f6f 100644 --- a/src/audio.ts +++ b/src/audio.ts @@ -1,33 +1,9 @@ import { AudioSource, Entity, EntityState, IEngine, PBAudioSource, engine } from "@dcl/sdk/ecs"; import { priority } from "./priority"; -function getAudioDuration(audio: HTMLAudioElement): Promise { - return new Promise((resolve, reject) => { - let loaded: boolean = false - const error = new Error('Failed getting sound duration') +export type Sounds = ReturnType - const timeout = setTimeout(() => { - if (!loaded) reject(error) - audio.removeEventListener("error", onError) - audio.removeEventListener("loadedmetadata", onLoadedMetadata) - }, 100) - - function onError() { - clearTimeout(timeout) - reject(error) - } - - function onLoadedMetadata() { - loaded = true - resolve(audio.duration) - } - - audio.addEventListener("loadedmetadata", onLoadedMetadata) - audio.addEventListener("error", onError) - }) -} - -function assertSound(start: number, end: number, duration: number) { +function assertSound(start: number, end: number) { if (start < 0) { throw new Error('Invalid "start" parameter provided. "start" parameter should be >= 0.') } @@ -35,14 +11,10 @@ function assertSound(start: number, end: number, duration: number) { if (start >= end) { throw new Error('Invalid "start" & "end" parameters provided. "start" parameter should be lower than "end" parameter.') } - - if (end > duration) { - throw new Error('Invalid "end" & "duration" parameters provided. "end" parameter should be lower than the duration of the sound.') - } } function createSounds(targetEngine: IEngine) { - const soundMap = new Map() + const soundMap = new Map() function makeSystem(dt: number) { const deadSounds = []; @@ -70,19 +42,14 @@ function createSounds(targetEngine: IEngine) { } function playSoundSegment(entity: Entity, value: PBAudioSource, start: number, end: number) { - const audio = new Audio(value.audioClipUrl) - getAudioDuration(audio) - .then(duration => { - assertSound(start, end, duration) - - soundMap.set(entity, { currentTime: start, end, duration }) - AudioSource.createOrReplace(entity, { - ...value, - playing: true, - currentTime: start - }) - }) - .catch((e) => console.error(e)) + assertSound(start, end) + + soundMap.set(entity, { currentTime: start, end }) + AudioSource.createOrReplace(entity, { + ...value, + playing: true, + currentTime: start + }) } targetEngine.addSystem(makeSystem, priority.TweenSystemPriority) diff --git a/src/index.ts b/src/index.ts index fc5cef3..5b87c2d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,6 +23,11 @@ export { tweens } from './tween' +export { + Sounds, + sounds +} from './audio' + export { PerpetualMotions, perpetualMotions diff --git a/tsconfig.json b/tsconfig.json index 9312928..6a985ac 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -31,8 +31,7 @@ "strictFunctionTypes": true, "strictPropertyInitialization": true, "lib": [ - "ES2020", - "DOM" + "ES2020" ], "types": [ "@dcl/js-runtime",