diff options
author | Joris | 2022-06-19 16:31:38 +0200 |
---|---|---|
committer | Joris | 2022-06-19 16:31:38 +0200 |
commit | 6571d1a72c1828d7c2ed902d07ec412110787bcf (patch) | |
tree | ec3aabd86ff4ccda2e61251e44a872d475e39b42 /src/sounds.ts | |
parent | 91438efeff2cdad7997687ebf4139ce0723982b9 (diff) |
Add snare and hit hat closed sounds
Diffstat (limited to 'src/sounds.ts')
-rw-r--r-- | src/sounds.ts | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/src/sounds.ts b/src/sounds.ts index 9ce8d2e..5e4c68a 100644 --- a/src/sounds.ts +++ b/src/sounds.ts @@ -1,5 +1,13 @@ -export interface Sounds { - kick: AudioBuffer +export type Sounds = Record<Sound, AudioBuffer> + +export enum Sound { + Bass, + Snare, + HitHatClosed, +} + +export function all(): Array<Sound> { + return [Sound.HitHatClosed, Sound.Snare, Sound.Bass] } const audioContext = new AudioContext() @@ -9,21 +17,30 @@ export async function load(): Promise<Sounds> { if (lazy !== undefined) { return lazy } else { + let [bass, snare, hitHatClosed] = await Promise.all([ + fetchSound('/sounds/bass.opus'), + fetchSound('/sounds/snare.opus'), + fetchSound('/sounds/hit-hat-closed.opus') + ]) - const kick = await fetch('/sounds/kick.opus') - .then(res => res.arrayBuffer()) - .then(ArrayBuffer => audioContext.decodeAudioData(ArrayBuffer)) - - lazy = { - kick + lazy = { + [Sound.Bass]: bass, + [Sound.Snare]: snare, + [Sound.HitHatClosed]: hitHatClosed } return lazy } } -export function playKick(sounds: Sounds) { +async function fetchSound(name: string): Promise<AudioBuffer> { + return await fetch(name) + .then(res => res.arrayBuffer()) + .then(ArrayBuffer => audioContext.decodeAudioData(ArrayBuffer)) +} + +export function play(sounds: Sounds, sound: Sound): void { const source = audioContext.createBufferSource() - source.buffer = sounds.kick + source.buffer = sounds[sound] source.connect(audioContext.destination) source.start() } |