Created
April 26, 2023 23:17
-
-
Save zabbarob/f0fe5dc142959c81fb93d3896f25a922 to your computer and use it in GitHub Desktop.
Play Note in Web Browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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