javascript - Web Audio Oscillator unable to create new oscillator -
javascript - Web Audio Oscillator unable to create new oscillator -
in below code expect create new oscillator node assigned o each time button element pressed allowing multiple presses resulting in multiple 1 sec tones @ 440. finding button can pressed 1 time yield tone, after goes dead if not creating new oscillator nodes. i'm bootstrapping javascript , suspect must trivial. have idea?
<html> <body> <button id='but' label='button'/> <script> function secondcontext(){ play([440],0,1); } function play(freqs, delay, duration) { freqs.foreach(function(freq){ o = audio_context.createoscillator(); o.frequency.value = freq; o.connect(audio_context.destination); o.start(0); o.stop(1); }); } var audio_context = new (audiocontext || webkitaudiocontext); button_ele = document.getelementbyid('but') button_ele.addeventlistener('click',function(){secondcontext()}); </script> </body> </html>
edit:
just found this question addresses issue. solution add together audio_context.currenttime() offset so:
o.start(audio_context.currenttime + 0); o.stop(audio_context.currenttime + 1);
as mentioned, start()
, stop()
methods' first argument timestamp based on clock maintained audiocontext.currenttime
.
if timestamp less current time, start/stop method executed immediately. otherwise methods executed (oscillator started or stopped respectively) when clock @ same time timestamp.
the when parameter describes @ time (in seconds) sound should start playing. in same time coordinate scheme audiocontext's currenttime attribute. if 0 passed in value or if value less currenttime, sound start playing immediately. start may called 1 time , must called before stop....
http://webaudio.github.io/web-audio-api/#widl-audiobuffersourcenode-start-void-double-when-double-offset-double-duration
javascript web-audio
Comments
Post a Comment