Should I Prefer OGG, Wav, or FLAC for Game Audio

Note: This is maybe the wrong place to ask, and if a moderator thinks this is too offtopic just let me know or feel free to delete this.

In my game library I'm using an audio crate that supports flac, wav, and ogg file formats. Which one is more suitable for using as the source for games?

I know flac is lossless, like wav is, but that it's compressed, so it's better for size, and I know that ogg is lossy, but I don't know how the playback or loading performance compares for any of them. Is there any general rule of thumb?

I would go with ogg.

I noticed a few AAA titles mentioned ogg among their open source licenses. That's obviously not a guarantee that they actually use it, but I would assume they do -- and if it's good enough for them, then surely it's good enough for me.

Oh, there's this: Games that use Vorbis - XiphWiki

2 Likes

This is kinda subjective when it comes to lossy vs. lossy formats. I'd definitely pick a lossy format, if you also include music, because actual music tends to take up quite a bit of space and decompression usually takes less time than loading the files from the hard drive.

<rant>I hate long loading times.</rant>

2 Likes

OK cool, my instinct was OGG ( maybe just because minecraft used it, but... :man_shrugging: )

If by Ogg you mean Ogg Vorbis, then there's a newer, much better codec: Opus. It's basically the codec to end all codecs.

There are some Rust bindings for libopus.

12 Likes

Ogg is an audio container, not a codec. It can contain Vorbis but others as well.

1 Like

I'd just like to interject for a moment. Actually, it's not Vorbis, but Vorbis' Monster, or as I’ve recently taken to calling it, GNU plus Vorbis' Monster.

3 Likes

Year ago I heared that some formats like wav are quicker and resource savy to execute by the system, like in case of multiple overlapping bullet sound effects, no idea about actual details these days.

"wav" is a raw (uncompressed) waveform. Assuming you don't need to resample, you just need to push the raw data (sans header) to the sound card.

But whatever other formats you have end up being raw waveforms as well. Unless you have special hardware that can take in a compressed stream and play it, you always run a decoder that outputs the encoded audio to a raw waveform which you postprocess/mix and then pass to the "sound card" (via some audio API).

If you decode everything in "realtime" to waveforms, then you may run into some unwanted latencies and unnecessary CPU load. This can pretty easily be solved by doing the decoding up-front: When a level loads, pre-decode all the audio and keep the raw waveforms in memory. Or you can even use an LRU list and "cache" the raw waveforms of the latest used sounds.

Back in the 1990:ies there was a fad where people used disk compression tools. Most techies argued against using these, noting that compressing/decompressing would slow your system down. To most people's surprise, some benchmarks in fact showed an increased "disk speed", which was explained using something along the line of "the bottleneck is the disk I/O. The PATA interface is slower than the CPU/RAM by so many order of magnitude that the decompressor easily has time to decompress the data as it arrives, so you get the illusion of "faster disk access" by virtue of transferring less data from the disk.".

This is basically true to this day; as mentioned in another post. If you have long audio (like music, rather than simple sound effects) you want to compress it to cut down on disk transfer times (if you're fetching a lot of other data as well).

In short: Use compressed format for storage. Use raw waveforms in memory for short repeatedly played sounds.

7 Likes

@blonk That is very insightful read. thanks

Great post but just a nitpick: WAV is not necessarily raw audio data, it's an IFF-like container and even though it probably isn't used that way anymore, it can also store compressed audio data (e.g. ADPCM.)

2 Likes

I knew that wav is a container format, but I didn't think I've seen it encode anything but raw waveforms so I assumed it was de-facto raw waveforms. However, I did not know it was IFF .. :exploding_head: . Reading the wiki you linked to and I'm amazed to see that WebP uses an IFF container as well. Amiga lives! :slight_smile:

1 Like

There was also the very real increased risk of corruption.

1 Like