I am working with a web audio API and I am trying to create a JavaScriptNode that outputs momentum. That is, I want a node that produces 1, followed by a whole bunch of zeros, and nothing more.
I thought the code below would be a reasonable way to do this. I initialize a variable called "timeForAnImpulse" to true and use this variable to trigger a pulse output on the sound callback. In the callback, I set the timeForAnImpulse value to false.
It seems that it should work, but it is not. Instead of a single pulse, I get a pulse train (1 at the beginning of each buffer). Any idea what I'm doing wrong?
<script type="text/javascript"> window.onload = init; function impulseNodeCB(evt){ if(timeForAnImpulse){ var buf = evt.outputBuffer.getChannelData(0); buf[0] = 1; timeForAnImpulse = false; } } var timeForAnImpulse = true; function init() { var context = new webkitAudioContext(); impulseNode = context.createJavaScriptNode(2048,0,1); impulseNode.onaudioprocess = impulseNodeCB; impulseNode.connect(context.destination); } </script> </head>
source share