I'm trying to programmatically plot a sound wave that I have placed in a 2d array, however, I cannot consistently get the sound wave to reach the end of the window. For example, when you load a sound in audacity, you get this:

enter image description here

In contrast, when I load a sound into my program I will often get something like this (red is the play head that moves at speed of one blue tick mark every second. The Green line is the end of the song in seconds):

enter image description here

As you can see, I can plot the complete wave form, but it does not sync up with the end of the song. Oddly enough, there are times during this song that the wave form is an accurate representation of what's being played. There are also a handful of songs that do not seem to have this issue (but more than a handful of others that do have this issue).

This is how I'm drawing the wave form:

    float drawStep = songLengthInSeconds / numberOfSamples;
    for(int i = 0; i < waveForm.length ; i++){
      for(int j =0 ; j < waveForm.length - 1; j++){
         renderer.line(x, waveForm[i][j], x + drawStep, waveForm[i][j+1]);

         x+= drawStep;    


The renderer.line method signature is

renderer.line(beginningX, beginningY, endingX, endingY);

And I get the number of samples by just adding all of the waveForm[x].length together.

Finally, the top and bottom wave forms in the picture below are being drawn differently which is why they don't match. The bottom wave form is the one that is represented by the code above.

I've been fighting with this for a couple weeks now and would be very grateful if anyone has any suggestions that would push me in the right direction.


I took @maxime.bochon 's advice and posted to signal processing and got the answer there, so I'm posting it here as well.

Full question and answer can be found here : https://dsp.stackexchange.com/questions/25830/how-can-i-accurately-represent-an-mp3s-wave-form-in-respects-to-time

It turns out that drawStep had to be a double instead of a float. The smaller memory amount of a float was cutting off digits in the drawStep and causing rounding errors.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.