aboutsummaryrefslogtreecommitdiff
path: root/src/sounds.ts
blob: 9ce8d2e37d2418aac6de02063866145f4e90e981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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()
}