audio - Play specific frequency with javascript -
i want function works this:
playsound(345, 1000) which play tone of 345 hz 1000 milliseconds. simplest way achieve in javascript? don't mind if uses sample (maybe of sin wave, or piano), or uses computer's hardware generate sound.
as pointed out in comments, way through oscillatornode.
var audioctx = new (window.audiocontext || window.webkitaudiocontext)(); function playnote(frequency, duration) { // create oscillator node var oscillator = audioctx.createoscillator(); oscillator.type = 'square'; oscillator.frequency.value = frequency; // value in hertz oscillator.connect(audioctx.destination); oscillator.start(); settimeout( function(){ oscillator.stop(); }, duration); } also, made simple fiddle playing star wars imperial march
Comments
Post a Comment