Skip to content

Instantly share code, notes, and snippets.

@zabbarob
Created April 26, 2023 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabbarob/f0fe5dc142959c81fb93d3896f25a922 to your computer and use it in GitHub Desktop.
Save zabbarob/f0fe5dc142959c81fb93d3896f25a922 to your computer and use it in GitHub Desktop.
Play Note in Web Browser
// playNote(440, 10) will play a 440 Hz sine wave for 10 seconds
//
// Don't forget that the browser requires some user interaction before
// calling the function, otherwise it will block audio output.
//
// Clicking on the web page once should be enough, though.
//
function playNote(freq, duration = 1) {
const context = new AudioContext();
const oscillator = context.createOscillator();
oscillator.type = "sine";
oscillator.frequency.value = freq;
oscillator.connect(context.destination);
oscillator.start();
oscillator.stop(context.currentTime + duration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment