Skip to content

Commit

Permalink
remove get audio duration method
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza committed Mar 13, 2024
1 parent 8988ec5 commit e656cc0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 46 deletions.
55 changes: 11 additions & 44 deletions src/audio.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
import { AudioSource, Entity, EntityState, IEngine, PBAudioSource, engine } from "@dcl/sdk/ecs";
import { priority } from "./priority";

function getAudioDuration(audio: HTMLAudioElement): Promise<number> {
return new Promise((resolve, reject) => {
let loaded: boolean = false
const error = new Error('Failed getting sound duration')
export type Sounds = ReturnType<typeof createSounds>

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.')
}

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<Entity, { currentTime: number; end: number; duration: number; }>()
const soundMap = new Map<Entity, { currentTime: number; end: number; }>()

function makeSystem(dt: number) {
const deadSounds = [];
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export {
tweens
} from './tween'

export {
Sounds,
sounds
} from './audio'

export {
PerpetualMotions,
perpetualMotions
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"lib": [
"ES2020",
"DOM"
"ES2020"
],
"types": [
"@dcl/js-runtime",
Expand Down

0 comments on commit e656cc0

Please sign in to comment.