Generating and Playing Sound in Javascript

Written by on November 25th, 2008 in Uncategorized.

Another experiment with sound, data: URIs, and the embed tag.

SK has shown that it’s possible to generate wave file in Javascript and play it. All this happens in the browser and without requiring Flash. He’s built a sine wave generator and a song player.

I came across a post about putting .wav files in data: URIs (via an Ajaxian post) so my immediate question was whether I could generate .wav files on the fly. There’s nothing particularly complicated about the .wav format, and I had done it before in PHP, so I applied the idea to JavaScript.

There’s a limit to data URIs, so you’re not able to do too much with it, but at least I was successful in generating a sine waveform with JavaScript. I had to lower the sample rate significantly so that I could fit the data in. I’ve found it to work on Firefox, Opera, Safari, and Google Chrome, but I didn’t test it in Internet Explorer 8 (not installed). You can increase the size of the generated .wav file by changing around the parameters, but be aware that it will stop working if the URI grows to be too large.

Javascript Song Player

Source: Ajaxian » Front Page
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/465082627/generating-and-playing-sound-in-javascript

Comments are closed.



Site Navigation