views:

1394

answers:

4

For the computer game I'm making, I obviously want to play sound. So far, I've been using AudioClip to play WAV files. While this approach works fine, the WAV files tend to be gigantic. A few seconds of sound end up being hundreds of kB. I'm faced with having a game download that's 95% audio!

The obvious option here would be to use MP3 or Ogg Vorbis. But I've had limited success with this - I can play MP3 using JLayer (but it plays in the same thread). As for Ogg, I've had no luck at all. Worse, JLayer's legal status is a bit on the dubious side.

So my question is to both Java developers and generally people who actually know something about sound: What do I do? Can I somehow "trim the fat" off my WAVs? Is there some way of playing Ogg in Java? Is there some other sound format I should use instead?

+2  A: 

These may be outdated, but they are officially recognized by the Xiph.org team (who maintain Ogg and Vorbis, among others). http://www.vorbis.com/software/#java

sep332
+2  A: 

The problem you describe is addressed by the Service Provider Interface (SPI) for sound in Java. The result is that simply adding JAR files to your classpath will add functionality to the default Java Sound API. Thus enabling the handling of more sound formats without changing code.

Last time I tried this the Javazoom people offered a working MP3 SPI JAR. Which was based on the JLayer you mentioned.

For Vorbis OGG there now also seems to be an SPI library. Check out the docs on the Vorbis SPI on Javazoom.

Rene
+2  A: 

If you decide to stay with wav format...

It is probably not very important to have high quality sound files. You can use your favorite wav editor to lower the bit rate, switch to mono, or both. That will save tons of space and you won't notice the difference in quality while playing the game.

Hope this helps.

BoltBait
+2  A: 

You could use JOrbis library to play back OGG music. For working sample usage you can look at these files here.

I also experimented with some lossless audio compression, for example I had a 16 bit mono sound. I separated the upper and lower bytes of each sample and put them after each other. Then I applied a differential replacement where each byte is replaced by its difference from the last byte. And finally used GZIP to compress the data. I was able to reduce the data size to 50-60% of the original size. Unfortunately this was not enough so I turned to the OGG format.

One thing I noticed with 8 bit music is that if I change the audio volume, the playback becomes very noisy. I solved this problem by upsampling the audio data to 16 bit right before the SourceDataLine.write().

kd304
Brilliant! I previously looked at JOrbis, but its seeming total lack of documentation made me unable to figure out how to actually point it at a .ogg file and make sounds come out of the speaker.
Zarkonnen