I have my 8 bit computer emulator written in JavaScript, it uses WebAudio and createScriptProcessor to genarate sound based on the internal state of the "virtual" sound chip emulated (even digital samples, so it's not webaudio oscillator based solution). It works nicely on Chrome, but not on Firefox, there is no sound at all with Firefox (tested with versions 30 and 31). The strange thing: it seems the function given by the onaudioprocess is not called at all on Firefox, but it is on Chrome. I've tested it with putting a console.log() into that callback function, but not a single line produced by that is displayed within the JavaScript console. The minimal skeleton of my solution I tested then separately:
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
/* comment: see later */
var audioNode = audioCtx.createScriptProcessor(4096, 2, 2);
audioNode.connect(audioCtx.destination);
audioNode.onaudioprocess = function (e) { console.log("process: " + e.toString()); };
Of course it does not produce too much sound, but it should print messages onto the javascript console periodically (but for sure, at least once ...). And indeed it does, on both of firefox (!!) and chrome. Now the odd thing what I can't understand: the very same scheme is used in my emulator but there, the callback function registered with audioNode.onaudioprocess is not called ever. I can't imagine why, because it works in a simple example like the one above. But as far as I can see, this causes the situation that my code does not produce any sound on Firefox. The only difference between the code above and my emulator, that the lines after the "comment" is given separately, when you start the emulator with the "run!" button, the parts before is executed on loading the emulator (page).
My not so precise question: what kind of problems can cause this? If that the test code fragment above works, and the very same is used in my emulator, I can't understand what can be the difference. My emulator uses setTimeout()
for the timing of the speed of the emulated machine, can it mess the onaudioprocess up somehow?
UPDATE, WORKAROUND
It seems the solution is to put new AudioContext()
at the same part where creating audioNode
and .connect()
is done, then it works. However I don't understand why it is a problem: audioCtx
was stored in the global variable scope (as I've read it can be a problem to have some garbage collection messing up webaudio a bit). Does anybody have any idea what is the reason of this behaviour?
Thanks a lot in advance, any feedback is welcome!