diff options
Diffstat (limited to 'src/sounds.ts')
-rw-r--r-- | src/sounds.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/sounds.ts b/src/sounds.ts new file mode 100644 index 0000000..9ce8d2e --- /dev/null +++ b/src/sounds.ts @@ -0,0 +1,29 @@ +export interface Sounds { + kick: AudioBuffer +} + +const audioContext = new AudioContext() +let lazy: undefined | Sounds = undefined + +export async function load(): Promise<Sounds> { + if (lazy !== undefined) { + return lazy + } else { + + const kick = await fetch('/sounds/kick.opus') + .then(res => res.arrayBuffer()) + .then(ArrayBuffer => audioContext.decodeAudioData(ArrayBuffer)) + + lazy = { + kick + } + return lazy + } +} + +export function playKick(sounds: Sounds) { + const source = audioContext.createBufferSource() + source.buffer = sounds.kick + source.connect(audioContext.destination) + source.start() +} |