I've got some simple sound playing using AudioClips, but I noticed in the byteArray thread a mention of AudioClip causing a stutter. Can someone clarify playing audio streams and clips for me?
For some reason, AudioClips are slow to play, and have to drain the entire sound buffer before restarting. This behavior tends to cause hiccups and pauses in gameplay when sounds are repeated at a very fast rate. (e.g. a spaceship cannon firing)GAGESound
and many other JavaSound libraries handle this problem by pre-loading the decompressed sound into a byte buffer, then playing back the sound via a SourceDataLine. A SourceDataLine is nothing more than an open mixer channel, so latency is very low and the stream can be interrupted in an instant.
- If I have audio of about 30s or more and its an ogg, then I can decompress all of it into ram, but that will suck up lots of memory. Mostly true?
The formula is actually pretty easy to figure out:
Uncompressed Size = Bits per Sample * Channels / 8 * Sample Rate * Seconds
Thus a 16 bit, Stereo, 44.1KHz sound that lasts 30 seconds would take:
16 * 2 / 8 * 44100 * 30 = 5292000 bytes = ~5 MB
Tremendous sizes like this are much of the reason why most sound effects are kept small, short, and low quality. e.g. An 8 bit Mono, 11Khz, 2 second sample would be:
8 * 1 / 8 * 11000 * 2 = 176000 = ~171kb
- If instead I read the file in as a stream, then I risk eating more cpu time as its playing, since it has to decode on the fly? Still mostly true?
More or less. It's slightly more complex because streaming compressed audio is a very tricky thing. Not only does it take more CPU, but it also takes more I/O, more interrupts, and more bus bandwidth. The end result is that you can pretty easily outstrip a machine's resources in attempting to stream a file from disk. Modern 2GHz+ computers have an ungodly amount of internal bandwidth to handle this sort of problem, but older machines might not be able to keep up. There are a number of ways to get around this problem, but pretty much all of them amount to stealing from Peter to pay Paul.
What if I read in an ogg and decompress it to an aiff, then I stream it in?
I'd actually recommend writing out raw data to disk. This would allow you to stream it back in with zero decompression or parsing costs. Also, you may want to experiment with memory mapping the file or using large, staggered buffers. This way the OS can perform a DMA load on the data while your game runs in parallel.