OscillatorNode example to play beep sounds in JavaScript

I am using my interval timer tool more often in the last couple of month. Mostly for lifting weights with my fingers. And to improve the usage of this tool I would like to play a beep sound before and after every break. One possible solution could be to load and play a audio file in JavaScript. An other solution are OscillatorNodes. I like about this approach that I do not need to buy a audio or create it on my own. And I am even more flexible on sound timing. So I created a small post to play around with OscillatorNodes.

JavaScript OscillatorNode player

The following form let you define the type, frequency and detune of the OscillatorNode. Just click afterwards on the start button and you should hear your sound.

JavaScript code to play a beep sound

Here a small JavaScript example to play a OscillatorNode with type sine and frequency of 440 hertz. The example has copied from BaseAudioContext: createOscillator() method - Web APIs | MDN with a small modification.

const audioCtx = new AudioContext();
const oscillator = audioCtx.createOscillator();

oscillator.type = "sine";
oscillator.frequency.setValueAtTime(440, audioCtx.currentTime);
oscillator.connect(audioCtx.destination);
oscillator.start();
Next Previous