Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waveform visualization in JavaScript from audio [duplicate]

I'm trying to use JavaScript to display the waveform for and audio file, but I don't even know how to get started. I found the Audio Data API, but am unfamiliar with most audio terms and don't really know what is provided or how to manipulate it. I found examples of waveforms in JavaScript, but they are too complicated/I can't comprehend what is going on. Then my question is: how can you use JavaScript to create a waveform of a song on canvas, and what exactly is the process behind it?

like image 842
Alex Avatar asked May 29 '11 20:05

Alex


4 Answers

Here's some sample code from my book (HTML5 Multimedia: Develop and Design) that does exactly that; Audio Waveform. It uses the Mozilla Audio Data API.

The code simply takes snapshots of the audio data and uses it to draw on the canvas.

like image 132
Ian Devlin Avatar answered Nov 10 '22 01:11

Ian Devlin


Here's an article from the BBC's R&D team showing how they did exactly that to build a couple of JS libraries and more besides. The results all seem to be openly available and rather good.

Rather than use the Audio Data API, which you cannot be sure is supported by all your users' browsers, it might be better if you generate your waveform data server-side (the BBC team created a C++ app to do that) and then at least you are decoupling the client-side display aspect from the playback aspect. Also, bear in mind that the entire audio file has to reach the browser before you can calculate peaks and render a waveform. I am not sure if streaming files (eg MP3) can be used to calculate peaks as the file is coming in. But overall it is surely better to calculate your peaks once, server-side, then simply send the data via JSON (or even create + cache your graphics server-side - there are numerous PHP chart libraries or you can do it natively with GD).

For playback on the browser, there are several good (non-Flash!) options. Personally I like SoundManager 2 as the code is completely decoupled from display, meaning that I am free to create whatever UI / display that I like (or that the client wants). I have found it robust and reliable although I had some initial difficulty on one project with multiple players on the same page. The examples on their site are not great (imho) but with imagination you can do some cool things. SM2 also has an optional Flash fallback option for antique browsers.

like image 40
Coder Avatar answered Nov 10 '22 00:11

Coder


I did just that with the web audio api and I used a project called wavesurfer. http://www.html5audio.org/2012/10/interactive-navigable-audio-visualization-using-webaudio-api-and-canvas.html

What it does is, it draws tiny rectangles and uses an audio buffer to determine the height of each rectangle. Also possible in wavesurfer is playing and pausing using space bar and clicking on the wave to start playing at that point.

Update: This POC website no longer exists.

To check out what I made go to this site: Update: This POC website no longer exists. This only works in a google chrome browser and maybe safari but I'm not sure about that.

Let me know if you want more info.

like image 25
Silver Avatar answered Nov 10 '22 00:11

Silver


Not well supported yet but take a look at this Firefox tone generator.

like image 36
tnt-rox Avatar answered Nov 10 '22 01:11

tnt-rox