tags:

views:

335

answers:

2

I wanted to plot the wave-form of the .wav file for the specific plotting width.

Which method should I use to display correct waveform plot ?

Any Suggestions , tutorial , links are welcomed....

+1  A: 

Almost any kind of plotting is platform specific. That said, .wav files are most commonly used on Windows, so it's probably a fair guess that you're interested primarily (or exclusively) in code for Windows as well. In this case, it mostly depends on your speed requirements. If you want a fairly static display, you can just draw with MoveTo and (mostly) LineTo. If that's not fast enough, you can gain a little speed by using something like PolyLine.

If you want it substantially faster, chances are that your best bet is to use something like OpenGL or DirectX graphics. Either of these does the majority of real work on the graphics card. Given that you're talking about drawing a graph of sound waves, even a low-end graphics card with little or no work on optimizing the drawing will probably keep up quite easily with almost anything you're likely to throw at it.

Edit: As far as reading the .wav file itself goes, the format is pretty simple. Most .wav files are uncompressed PCM samples, so drawing them is a simple matter of reading the headers to figure out the sample size and number of channels, then scaling the data to fit in your window.

Edit2: You have a couple of choices for handling left and right channels. One is to draw them in two separate plots, typically one above the other. Another is to draw them superimposed, but in different colors. Which is more suitable depends on what you're trying to accomplish -- if it's mostly to look cool, a superimposed, multi-color plot will probably work nicely. If you want to allow the user to really examine what's there in detail, you'll probably want two separate plots.

Jerry Coffin
I have parsed the wav file. But i dont know whether I average out the windows size samples to single value. How to handle left and right channel sample values.
Ashish
I haven't added anything about how to down-sample, because it looks to me like kauppi has covered that quite reasonably already.
Jerry Coffin
+3  A: 

Basic algorithm:

  1. Find number of samples to fit into draw-window
  2. Determine how many samples should be presented by each pixel
  3. Calculate RMS (or peak) value for each pixel from a sample block. Averaging does not work for audio signals.
  4. Draw the values.

Let's assume that n(number of samples)=44100, w(width)=100 pixels:

then each pixel should represent 44100/100 == 441 samples (blocksize)

for (x = 0; x < w; x++)
    draw_pixel(x_offset + x,
               y_baseline - rms(&mono_samples[x * blocksize], blocksize));

Stuff to try for different visual appear:

  • rms vs max value from block
  • overlapping blocks (blocksize x but advance x/2 for each pixel etc)

Downsampling would not probably work as you would lose peak information.

kauppi
what to do in case of stereo sampels ?
Ashish
Stereo samples are usually interleaved so you just take even/odd numbered samples from the sample array. Plot one waveform / channel.
kauppi