MP3 loops with an audible gap, the equivalent WAV doesn't

I’m integrating fmod into my engine right now and am trying to loop an MP3 sound. I have a WAV and an MP3 version of this sound.

When playing the MP3 version of the file as a looping sound (opened with FMOD_2D | FMOD_CREATESAMPLE | FMOD_ACCURATETIME | FMOD_LOOP_NORMAL), there is an audible gap between the loops. When playing the WAV version of the file, this gap is not there.

I was under the impression that FMOD_CREATESAMPLE would cause fmod to do all the necessary preprocessing at load time, so that there should just be raw PCM data in the engine, for both the MP3 file and the WAV file.

How can I get rid of the gap between the loops of the MP3 version?

The MP3 file is exported with LAME 3.99.5, from Reaper 7.06/win64 at 192kbps CBR. I’m using FMOD Engine 2.02. The loop is 0-crossing. Both MP3 and WAV files loop perfectly when re-imported into reaper. The sound is 100ms long.

The audio files in question can be found here: FMOD problem sounds

I tested an OGG file, loading only with FMOD_2D | FMOD_LOOP_NORMAL, and it looped without gaps. I understand that MP3 processing might incur some overhead as it is not a recommended format, but why does FMOD_CREATESAMPLE not cause this cost to be payed only once, at load time?

Unfortunately, what you’re observing is a limitation of the MP3 format. MP3 encoders will often add padding to the start and/or end of files for a number of reasons, though the typical culprit is usually matching the audio to a set frame size. Even if FMOD decompresses the audio to PCM data, the padding will remain. If you’re interested in technical info on this padding, I would recommend taking a look at LAME’s technical FAQ.

I am unsure how exactly Reaper handles the reimported MP3 file such that it loops seamlessly, so I can’t comment on that, but the FSBank API and FMOD Studio’s bank building process work around this limitation by resampling the source audio in the last MP3 frame to make sure the audio aligns without padding.

That said, the best fix for this is to encode to a different format that does come with such a limitation, such as OGG which you’ve already tested.