Put the following variable with your other variables
let audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext)
Put the following function with your other functions (copied from
Stack Overflow)
// All arguments are optional:
// duration of the tone in milliseconds. Default is 500
// frequency of the tone in hertz. default is 440
// volume of the tone. Default is 1, off is 0.
// type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine.
// callback to use on end of tone
function beep(duration, frequency, volume, type, callback) {
let oscillator = audioCtx.createOscillator()
let gainNode = audioCtx.createGain()
oscillator.connect(gainNode)
gainNode.connect(audioCtx.destination)
if (volume) { gainNode.gain.value = volume }
if (frequency) { oscillator.frequency.value = frequency }
if (type) { oscillator.type = type }
if (callback) { oscillator.onended = callback }
oscillator.start(audioCtx.currentTime)
oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000))
}
Call the beep function where you want to play a sound
beep(someMilliseconds, someFrequency)