libgdx: Ogg files load way more slowly than wav files
Issue details
The only change I make is to switch my sound effect folder all to .ogg rather than .wav and the load time difference is:
All wav sounds: assets 229 screen 441
All ogg sounds: assets 1548 screen 1749
The difference in loading time is somewhat noticeable anyway, but I’ve provided the actual times for clarity.
Reproduction steps/code
Load sound effects as ordinary Sound files as wav and then all as ogg and test the loading time of each.
Version of libGDX and/or relevant dependencies
1.10.0
Please select the affected platforms
- MacOS
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 26 (14 by maintainers)
I feel like I’ve been to Hell and back, figuring out Unity. Think I’ve managed to conjure up an equivalent test - loading an
AudioClipfrom outside the project (libGDX test was also external). I think it might be possible to shave off ~50ms from this, but you get the picture. Here’s the current score on the three:libGDX is still the loser, but you need to be loading in an awful lot of audio in one go on desktop for it to really matter.
Oh, hey, entitlement city! So, first of all. As Nate sad, the decoders for both OGG and MP3 are pure Java decoders. That means they will be slow until the hot code paths have been run a few thousand times until the JIT compiler of the JVM kicks in. No mystery, conspiracy, etc.
What could be done:
And since libGDX is such a nice open-source project, you can totally send a PR that swaps out the Java decoder for OGG for the stb vorbis decoder in LWJGL 3 instead of telling people actually contributing code to and maintaining libgdx that their comments are useless.
I wanted to see if there’s anything more behind this issue, and I kind of lost track of what direction I was going in with this comment.
Tests
This isn’t super scientific, but I’ve tested the decode times of various codecs on my computer with ffmpeg. While blatantly ignoring the fact sound effects will be lots of small files (which will be slower than loading one large file) it gives us an idea for what kind of difference is realistic.
The source file used was the 2010 remaster of Supertramp’s Breakfast in America album, ripped from CD - 46m19s in length, and unsurprisingly 16-bit stereo 44.1kHz.
All test files are 128kbps (±1kbps) with the exception of HE-AAC (64kbps) and of course (AD)PCM and FLAC. Three tests for each. Time is in milliseconds.
ffmpeg: libGDX-supported codecs
libGDX: libGDX-supported codecs
Tested on libGDX 1.10.0 with Java 11 and LWJGL3.
ffmpeg: Other codecs
Some of the above codecs can be used on mobile and web, and I hope will be supported on desktop one day. But the table’s partly a digression.
Conclusion
While I’m not surprised the compressed formats take longer, being ~3× slower suggests something suboptimal is going on here in libGDX land.
If I were in charge, I’d have been likely to close this issue with the reasoning of “of course OGGs load more slowly than WAVs”, but now I’m thinking this raises the point that libGDX’s desktop audio decoding speed could be improved upon, so it could be worth leaving open for that alone.
I tested with my game and i can load 351 ogg files (total 3MB on SSD) in 1500ms which is totally OK for me considering that :
So i would say that OGG is totally fine for most use cases. If loading time is much more important to you than storage (i can understand that), then WAV format is better.
MP3 is another story, it has some huge drawback (padding making them not perfectly loopable, etc)
Haha, you actually did a Unity test, you mad man! Cool to see the numbers.
libgdx uses OggInputStream by Kevin Glass which was used in an old Java game toolkit called Slick (ah the memories). It uses the JOrbis library. I vaguely remember hacking on OggInputStream long ago to make it a little more efficient. If it’s JOrbis that is slow, it’s a bigger issue.
MP3 uses javazoom. I’m not surprised that is slow. MP3 decoding in general seems like quite a mess. I would avoid it if possible and use OGG.
He didn’t say anything about a game only working on one or the other. libGDX games can run on both Intel and M1 on macOS (albeit more prone to technical issues on M1 until we no longer need Rosetta) so we wouldn’t be able to figure out which you’re using on our own.
So that this argument has one fewer direction to head in, my relevant specs are:
I could easily enough change any of the above, but I don’t see what for. I expect WAV to always be faster, and typically by a similar percentage, except in the case of slow storage such as loading from floppy disk.
You still haven’t provided any basic info on your hardware (such as “year built” or “Intel processor” or “a new M1 Mac”) other than “MacOS”. I feel like asking repeatedly is a waste of my time. I would rather not waste my time! The AARCH64 vs. x86-64 difference is actually quite significant, and you could quite reasonably be running either in 2021. It’s entirely possible that this issue doesn’t appear on M1 Macs, but does on Intel, or vice versa; it’s even more likely that the magnitude of severity is different on different architectures, OSes, eras of production, etc.
Please remember, you are using tools built by volunteers in their spare time, and @mgsx-dev is such a volunteer. He has helped this project immensely over several years, and our 3D code basically depends on him. He’s earned our respect. @Frosty-J has recently contributed some really in-depth reporting on various issues that would have otherwise stagnated, and he’s shown a keen eye for detail. He’s also earned our respect.
You can only ever compare it with how long it takes with other engines and with how long WAV takes to load on LibGDX, which you strangely didn’t mention. Saying it’s ok for you on a fast desktop with SSD is pretty useless information since there’s different desktop systems/hard drives and even mobile systems where file size will be even more important and it will be even slower…
Frosty-J’s tests were infinitely more useful. You could at least add the time for loading them in wav format and the size of the oggs since I loaded only 49 in about the same time. I also use an SSD so we can expect slower times on a normal hard drive.
Music: Streams the file from disk while it plays. A good fit for long things such as music and ambience.Sound: Decodes the entire audio file into memory at once. So it’ll take longer to load but can be played immediately once loaded without the latency of having to access storage and decode the file. The obvious choice for short sound effects such as attack sounds. Not really weird.