export interface Sounds { kick: AudioBuffer } const audioContext = new AudioContext() let lazy: undefined | Sounds = undefined export async function load(): Promise { 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() }