-
Notifications
You must be signed in to change notification settings - Fork 4
mp3, flac, ogg, aiff or wav What should I use?
This article exists because many people still rely on mp3. We'll take a look at why using mp3 is not a good idea when you have alternatives, and what advantages or disadvantages the other formats have.
I advise everyone not to use mp3 if you can avoid it. It's still a great format, but unfortunately a bit outdated nowadays and the alternatives simply have fewer problems and win in comparison in file size, quality and decoding speed. Mp3 also adds gaps to the start and the end of each file which makes looping and perfect duration resolution difficult/problematic.
Ogg for example is smaller than mp3 at the same quality and faster to decode. Or the other way around, ogg has a higher quality at the same file size and is faster to decode. Note that we're talking about the decoders included in TuningFork here, so this is not a statement that is true in general.
Let's take a look at a few benchmark numbers of the different formats and decoders to then decide which format is particularly well suited for what.
The benchmark sound file includes both noise and a single tone to have a potentially easy and a hard to compress part. The sound is 4 min long and I use a NVMe SSD to load it from, therefore results may differ completely on slower disks. It may not be the most scientific approach, but it should suffice for this test.
You can run this benchmark on your hardware with .\gradlew jmh
, it's included in this repository and the results will show up in core\build\reports\jmh
.
Format | Codec | Bit Rate | Load Time | File Size |
---|---|---|---|---|
wav/aiff | PCM | 8 per sample | 2 ms | 20.7 MB |
wav/aiff | PCM | 16 per sample | 4 ms | 41.4 MB |
wav/aiff | u-Law/a-Law | 8 per sample | 5 ms | 20.7 MB |
flac | FLAC | 8 per sample | 23 ms | 11.9 MB |
wav | IMA ADPCM | 4 per sample | 26 ms | 10.4 MB |
ogg | Vorbis | 187k per second | 33 ms | 5.0 MB |
flac | FLAC | 16 per sample | 34 ms | 26.9 MB |
mp3 | MPEG-Layer-3 | 187k per second | 71 ms | 5.5 MB |
Here you can easily see that Ogg outperforms Mp3 in all respects, which basically makes mp3 obsolete. Flac offers a nice compromise between file size and decoding speed with lossless compression. And wav or aiff is the undefeated king in terms of file size in the negative sense and loading time in the positive sense.
Which format is the right one depends on the intended use and whether the focus is on short loading times or low memory consumption.
If you want to be on the safe side, choose a common combination like:
- buffered sounds: 16 bit PCM wav or aiff
- streamed sounds/music: ogg
If you want to save both memory and loading time, with a little effort in mastering you can get almost indistinguishable quality out of
- 8-bit PCM
- 8-bit FLAC
I wouldn't recommend it for music though.