src/js/audio.js
// start frequency HZ, frequency change, delay between changes, number of changes, volume, type
start frequency HZ, frequency change, delay between changes, number of changes, volume, type
start frequency HZ frequency change delay between changes number of changes volume type
function playSound (frequency, increment, delay, times, volume, type = 0) {
  if (!audioContext) return
  const oscillatorTypes = ['square', 'sawtooth', 'triangle', 'sine']
  const oscillator = audioContext.createOscillator()
  oscillator.frequency.value = frequency
  oscillator.type = oscillatorTypes[type]
  const modulationGain = audioContext.createGain()
  modulationGain.gain.value = 0
  oscillator.connect(modulationGain)
  modulationGain.connect(audioContext.destination)
  oscillator.start()
  let i = 0
  const interval = setInterval(playTune, delay)
  function playTune () {
    oscillator.frequency.value = frequency + increment * i
    modulationGain.gain.value = (1 - (i / times)) * volume
    i++
    if (i > times) {
      clearInterval(interval)
      setTimeout(stopTune, delay + times) 
    }
  }
  function stopTune () {
    oscillator.stop()
  }
}
function playSound ( frequency , increment , delay , times , volume , type = 0 ) { if ( ! audioContext ) return const oscillatorTypes = [ 'square' , 'sawtooth' , 'triangle' , 'sine' ] const oscillator = audioContext . createOscillator ( ) oscillator . frequency . value = frequency oscillator . type = oscillatorTypes [ type ] const modulationGain = audioContext . createGain ( ) modulationGain . gain . value = 0 oscillator . connect ( modulationGain ) modulationGain . connect ( audioContext . destination ) oscillator . start ( ) let i = 0 const interval = setInterval ( playTune , delay ) function playTune ( ) { oscillator . frequency . value = frequency + increment * i modulationGain . gain . value = ( 1 - ( i / times ) ) * volume i ++ if ( i > times ) { clearInterval ( interval ) setTimeout ( stopTune , delay + times ) } } function stopTune ( ) { oscillator . stop ( ) } }
start frequency HZ, frequency change, delay between changes, number of changes, volume, type
start frequency HZ frequency change delay between changes number of changes volume type
// modulation for sound volume control // prevents the click when stopping the oscillator