views:

775

answers:

3

Hi,

I am looking for a way to create a graphical waveform of mp3 files uploaded to a server. From a little research i beleive the mp3 would need converting to a raw format first...but i have no idea on how to create a .gif format of the waveform for this sound file.

90% of the sound files would be over 60 minutes in length.

I understand this wouldn't be able to done right after it is uploaded, it would need to be placed into a queue and processed.

I have looked at several libaries such as lame, but none seem to be able to achieve what i am looking for.

Any pointers into the right direction would be greatly appreciated!

Thank you very much! Paul

+2  A: 

The first step is to uncompress the mp3. As long as you're doing this as a batch job, rather than use LAME as a library, just use an existing command-line program to convert the mp3 to a temporary WAV file, that will be much easier. Then find a library to read WAV files - it's a relatively simple format and you should find lots of sample code online, or you could write your own in an afternoon.

Suppose your song is 60 minutes long: 60 minutes * 60 seconds/minute * 44100 samples/second = 158,760,000 samples. (Twice that if it's a stereo song.) If your image is 1000 pixels wide, you only want to display one sample for every 158,760 samples.

(As an aside, you won't see much detail at that resolution. Perhaps a better solution would be to show a waveform of just the first 5 minutes, or render a larger image that the user can scroll?)

Anyway, you want to read the audio samples for each block of 158,760 samples (in this example), and render it as a vertical line representing the strength of the signal over that portion of the audio. There are two ways to do this:

  1. The maximum value over that region
  2. The root-mean-squared (RMS) value over that region

Maximum will show you peaks, while RMS will show you the overall perceived loudness. Both are easy to implement; try both and see which one looks best.

Then you just need to turn that resulting image into a gif. Since this is a batch job anyway, if I were you, I would write out a BMP file (a really easy file format) and then use a command-line program like ImageMagick's "convert" to turn that into a GIF.

Finally, one last note: if you're really tricky, you could read the MP3 frames and extract the gain directly from the bitstream without decoding the whole thing. That's what I did here, and you're welcome to use it - but it's not for the faint of heart. It's roughly 100x faster than decoding the full MP3, but the waveform you get will be a crude approximation.

dmazzoni
Thanks for the detailed answer, explains in a nutshell what needs to be done to process this.The image i am after does not have to be perfect, it's just a rough guide of the peaks and lows during a 60 minute DJ mix.The code you provided looks promising, however it's java which i have no idea about, but may still be able to decipher some information from.Thanks!
Paul Hinett
Not to be rude, but if you like my answer I'd appreciate it if you would accept it or at least upvote it. That's how this site works, you know.
dmazzoni
Sorry, i didnt know i had the privelages to do so...i have only just joined and the first time i tried it said i didn't have enough points...vote added as it was a useful resource! thank you!
Paul Hinett
+1  A: 

You can use the C# code here to generate the wave form image. Afterward, you should use the common System.Drawing library to save it to a GIF file.

Eran Betzalel
I've had a look through this code and tried to get it working, however it crashed the application each time i tried to test it with a 16bit WAV file.Have you used this code before by any chance?
Paul Hinett
No, I didn't, but I think it'll worth your time to resolve these errors.
Eran Betzalel
+2  A: 

Hi, You may find FMOD or BASS can do this for you, quite easily. I believe the FMOD download comes with a visualisation example, so you can extrapolate from that. They both read in MP3's and perform the necessary calculations.

keyboardP
I am using the BASS api to create my waveforms now, works absolutely perfectly with lots of customisation options available.Thank you very much this has helped tremendously!
Paul Hinett
You're welcome :). Don't forget to look at the license for its use, if you're going to use it for commercial purposes.
keyboardP
Sounds like an overkill for his purposes... I mean - these are some heavy duty audio libraries and not a cheap ones.
Eran Betzalel
I have just sen the license costs...wow yes these are some heavy duty libraries!!I will try and hunt down a cheaper/alternative version, but the waveforms it creates are perfect and very simply to do!
Paul Hinett
I agree the licensing cost can be quite expensive (I guess you're looking at the 100 euro one), but that does assume this is being used for commercial purposes. If not, then they're free to use. I guess it could be used as a temporary measure to ensure all parts of the application work. I just came across this post: http://stackoverflow.com/questions/1215326/open-source-c-code-to-present-wave-formIf you're able to get the wave information into a byte array, the link will help you display it in C#.
keyboardP
Ah, found a .NET library which looks like what you need:http://naudio.codeplex.com/I haven't used it, but there should be a demo that shows how to draw the spectrum:http://naudio.codeplex.com/Thread/View.aspx?ThreadId=60833
keyboardP