HTML5 gives us a couple new toys to play with, such as <audio> and <video> tags. On the visual side, we've already seen live green-screening with Canvas and JS, and in terms of audio there's been several JS drum machines already. But the question I was interested in was: can you use JavaScript to stream live data into these media tags?
Enter the JavaScript audio synth. It generates a handful of samples using very basic time-domain synthesis, wraps them up in a WAVE file header and embeds them in <audio> tags using base64-encoded data URIs. Each sample is then triggered using timers to play the drum pattern. It's quite simple to do and runs fast enough in HTML5 capable browsers to be unnoticeable. Yes, it sounds tinny, but that's just because I'm too lazy to design proper filters for toys like this.
Unfortunately, while the synthesis is fast enough to run real-time, you can't actually use it for a full live audio stream, as there is no way to queue up chunks of synthesized audio for seamless playback. I tried triggering multiple <audio> tags in parallel to address this, but that didn't work either.
My final attempt was to generate tons of periodic audio loops only a couple of ms long, and to play them back with looping turned on while altering each tag's volume in real time, hence doing a sort of additive wavetable synthesis. Unfortunately, looping is not a fully supported feature, and the only browser I found that does it (Safari) doesn't loop seamlessly at all.
All in all, my first brush with the <audio> tag was a major disappointment. The <video> tag's high-level approach leads to similar limitations, but they are offset by the flexibility and power of the <canvas> tag. Unfortunately, there is no 'audio canvas' to solve similar problems with audio.
Edit: fine, I made the bass a bit punchier and added moar dubsteps.


Very interesting
Very interesting example!
And I also didn't know about the possibilities of video + canvas. Looks very cool.
This doesn't seem to work in
This doesn't seem to work in chrome, which is a bit of a problem. This could be a very useful tool, but only so long as it is something that can work in all HTML5 browsers.
HTML5 tissues
Browsers are slow to catch up with new specs, but don't cry, some are ahead of the game. Steve, I'd been playing with
<canvas>and was about to generate some wav-files, but I could never have matched your code style. To describe sounds with an array of commands is a nice idea. Any idea how streaming audio feeds work? Could you just keep appending to audio.src?@Nicholas: Appending/streaming audio
Appending to the data URL wouldn't work... the browser would treat it as a whole new URL, and replay the sample from the beginning.
Apparently though, upcoming versions of Firefox will provide stream access.
direct audio access
Yeah, you should have a look at David's work on bringing real time audio access to Firefox:
http://vocamus.net/dave/?cat=25
There's also a W3C group spinning up to handle it.
Post new comment