Why are wav and mp3 files different lengths in some tools and frameworks?

0

Using Audacity, I have exported a mp3 file from a very short wav file.

Bringing the mp3 file back into Audacity and viewing the wav and mp3 together shows the files as different lengths with (mostly!) silent audio added at the beginning and end of the waveform. This is the same on both Windows and Mac.

They are also different lengths when using the NAudio API.

However, if I view in Sonic Visualiser on Windows, both the wav and mp3 files are the same length.

They are also the same length on the Mac when using AVFoundation API components.

I expect some difference in the sample amplitudes due to lossy encoding, but not the length of the file. Why do some tools and frameworks load the mp3 file incorrectly, having a different length and spurious audio sections at the beginning and end?

(The erroneous lengths are different in Audacity and NAudio by the way.)

WAV and MP3 in Audacity on Windows

fractor

Posted 2019-04-24T15:32:46.507

Reputation: 141

1

How long is this "short" WAV file? depending on your sample rate you might be at the limit of the smallest mp3 frame size https://stackoverflow.com/questions/6220660/calculating-the-length-of-mp3-frames-in-milliseconds at your sample rate it looks like you should have a frame of approximately 26 milliseconds and if your sample is shorter than that it could be getting padded up to it.

– Mokubai – 2019-04-24T16:31:49.497

The wav file is 56ms long. When the mp3 file is pulled into Audacity it is 131ms. When loaded into Sonic Visualizer (for example) the mp3 length is the same as the original wav. – fractor – 2019-04-25T08:45:15.407

1Well 131/5 does equal 26.2 so it does appear to be padding. 56ms would need a minimum of 3 frames and one of those frames would have to be padded up anyway. After that it looks like it has added a frame at the start and the end to "smooth" the data a bit. I know that later mp3 encoders were able to add data to state when the actual music data started and finished in order to facilitate gapless playback which would explain why some programs show the "proper" length... While I can explain each individual feature of what you are seeing I can't really explain why overall... – Mokubai – 2019-04-25T09:19:10.057

No answers