Seeking an Audio Tutorial

Seeking an Audio Tutorial
0.0 0

#1

Good evening everybody ! I was starting on my Simon game Project but quickly realised how tedious it would be without a proper tutorial, I know very little about audio context and everything and was wondering if someont could link me a good tutorial about all the command and function of tha audio API, so that I tackle it down and get working .

Thanks a lot for your help <3


#2

I can’t link you a tutorial, but usage is pretty basic.

// play mp3
const fail = new Audio('fail.mp3')
fail.play()

// play a pitch
const context = new AudioContext()
const note = context.createOscillator()
note.start(0) // reset note

// provide frequency for note
// you can use a switch statement to do a different frequency for each button
note.frequency.value = 240

// start sound
note.connect(context.destination)

// stop sound
// you can run this with a setTimeout or use an async/promise delay function
note.disconnect()

So, I did something like this:

const note      = context.createOscillator()
note.start(0) // reset note

const delay = time => new Promise(resolve => setTimeout(resolve, time))

function playSound() {
  switch (currentBtn) {
    case '#green':
      note.frequency.value = 240
      break
    case '#red':
      note.frequency.value = 301
      break
    case '#blue':
      note.frequency.value = 318
      break
    case '#yellow':
      note.frequency.value = 360
  }
  note.connect(context.destination)
}

playSound()
await delay(1000)
note.disconnect()