aboutsummaryrefslogtreecommitdiff
path: root/src/sounds.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/sounds.ts')
-rw-r--r--src/sounds.ts77
1 files changed, 60 insertions, 17 deletions
diff --git a/src/sounds.ts b/src/sounds.ts
index 5e4c68a..f906a70 100644
--- a/src/sounds.ts
+++ b/src/sounds.ts
@@ -1,13 +1,46 @@
+import * as dict from 'lib/dict'
+
export type Sounds = Record<Sound, AudioBuffer>
export enum Sound {
- Bass,
- Snare,
+ Crash,
+ Ride,
+ TomHigh,
+ TomMedium,
+ TomFloor,
+ HitHatOpen,
HitHatClosed,
+ Snare,
+ Kick,
}
export function all(): Array<Sound> {
- return [Sound.HitHatClosed, Sound.Snare, Sound.Bass]
+ return dict.values(Sound)
+}
+
+export function record<T>(f: (sound: Sound) => T): Record<Sound, T> {
+ let res: any = {}
+
+ all().forEach(async sound => {
+ res[sound] = f(sound)
+ })
+
+ return (res as Record<Sound, T>)
+}
+
+export function toString(sound: Sound): string {
+ switch (sound) {
+ case Sound.Kick: return 'Kick'
+ case Sound.Snare: return 'Snare'
+ case Sound.HitHatOpen: return 'Hit-hat open'
+ case Sound.HitHatClosed: return 'Hit-hat closed'
+ case Sound.Ride: return 'Ride'
+ case Sound.Crash: return 'Crash'
+ case Sound.TomFloor: return 'Tom floor'
+ case Sound.TomMedium: return 'Tom medium'
+ case Sound.TomHigh: return 'Tom high'
+ default: throw `Sound ${sound} is unknown.`
+ }
}
const audioContext = new AudioContext()
@@ -17,27 +50,37 @@ 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')
- ])
-
- lazy = {
- [Sound.Bass]: bass,
- [Sound.Snare]: snare,
- [Sound.HitHatClosed]: hitHatClosed
- }
- return lazy
+ let sounds = dict.fromList(await Promise.all(dict.toList(record(fetchSound)).map(async obj => {
+ let value = await obj.value
+ return { key: obj.key, value }
+ }))) as Sounds
+
+ lazy = sounds
+ return sounds
}
}
-async function fetchSound(name: string): Promise<AudioBuffer> {
- return await fetch(name)
+async function fetchSound(sound: Sound): Promise<AudioBuffer> {
+ return await fetch(path(sound))
.then(res => res.arrayBuffer())
.then(ArrayBuffer => audioContext.decodeAudioData(ArrayBuffer))
}
+function path(sound: Sound): string {
+ switch (sound) {
+ case Sound.Kick: return 'sounds/kick.opus'
+ case Sound.Snare: return 'sounds/snare.opus'
+ case Sound.HitHatOpen: return 'sounds/hit-hat-open.opus'
+ case Sound.HitHatClosed: return 'sounds/hit-hat-closed.opus'
+ case Sound.Ride: return 'sounds/ride.opus'
+ case Sound.Crash: return 'sounds/crash.opus'
+ case Sound.TomFloor: return 'sounds/tom-floor.opus'
+ case Sound.TomMedium: return 'sounds/tom-medium.opus'
+ case Sound.TomHigh: return 'sounds/tom-high.opus'
+ default: throw `Sound ${sound} is unknown.`
+ }
+}
+
export function play(sounds: Sounds, sound: Sound): void {
const source = audioContext.createBufferSource()
source.buffer = sounds[sound]