Best way to play sound?

Hi guys,

I’m currently creating a desktop version of one of my Android games (Blob Wars : Attrition, if anyone’s interested), and everything’s gone pretty smoothly; it plays much better on the desktop, as I the resolution is obviously higher, there’s more screen real estate, and you can use the keyboard and game controllers.

I’m now stuck on how best to handle audio. I’m making use of LWJGL3, so have access to OpenAL, but can’t decide on the best approach.

My sounds are a mixture of MP3s and OGGs (willing to convert the MP3s to OGGs if needed).

Suggestions?

For ogg you can use jogg / jvorbis from jcraft (be aware the code isn’t great), you just need to grab the PCM data in a byte buffer and pass this to openal. For me, I wrote my own ogg decoder in java and this fits in quite nicely with openal (plus its very easy to use)

For mp3 you can use http://www.javazoom.net/javalayer/javalayer.html. I have integrated this with openal. If I remember the api converts the mp3 to wav so you just need to convert this to pcm.

Hi

You can use Paul Lamb Sound Library with its codec for OGG and its plugin based on your favorite OpenAL binding.

I’ve been using tinysound and I’m very impressed with it. I’ve only been using wav format but I believe it handles ogg and mp3 as well. It’s main feature is how easy it is to use.

MP3 at least not out of the box, but I use it too for WAV, OGG and MIDI because of its easiness and tininess :smiley:

Thanks for the tips, guys. I’ve managed to get sound working with LWJGL3 and OpenAL. Now just working on the music side of things. Hopefully have a demo of the game out in the next few days :slight_smile:

Huzzah! Got all the sounds working. Had to convert the MP3s to OGGs, but I’ve not noticed any dip in sound quality, so all good.

If anyone’s interested in giving the demo a quick go, you can grab it here:

http://www.stephenjsweeney.com/downloads/attrition/blobwars-attrition-1.042.tar.gz

Simply unarchive, and either use run.sh or run.bat to start the game on Linux or Windows. Note: this demo version won’t save games…! It’ll save the config, but nothing else. Feedback on the demo appreciated (especially crashes, etc.).

Good job on the port.

It would make your time much easier by simply reusing all the code and just tweaking it - the power of java. It appears that you are talking like you are rewriting the game to be exact.

My initial thoughts were to redo it in C, but using Java allowed me to reuse 90% of the code. Going from Android to the desktop, I had to:

  • Remove all Android specific stuff (like touch input)
  • Replacing the drawing code
  • Increase the screen resolution, and subsequently tweak the layout of certain things
  • Implement the sound engine
  • Add keyboard and controller support

The sound too the longest to do, which is why I left it until last.

I used OpenAL and Java to do my sounds.

youtube.com/thinmatrix

has a good tutorial.
But… watch out. Some of his stuff is extensively wrote in his tutorials. Such as rendering.

@StephenJSweeney - I understand that you have a solution and I don’t mean to suggest you change anything. I just wanted to add this for people coming across your topic, that there is another option that seems quite viable: using JavaFX’s AudioClip. AudioClips have more capabilities than javax.sound.sampled.Clip.

I was reading about them in the following book: Beginning Java 8 Games Development This book also has a good section on optimizing PCM files using Audacity, worth checking out, regardless of sound library.

I just saw this thread…
I’m using libGDX and so far very happy with it. It uses OpenAL on the sound backend. One great advantage is the cross platform compatibility. For example, the sound will also work on Android without code modifications.
https://libgdx.badlogicgames.com/

OpenAL has been the way i have done it for a while. It is robust and gets the job done.

As for formats/codecs. Ogg is a container so I assume you mean Vorbis. Vorbis is younger and technically more advanced that mp3. Transcoding does cost you, but without some serious equipment, i doubt most humans could tell the difference between them.

The codec to look out for is Opus. It is like Vorbis on extra drugs so that is can do pure speech to a complete Symphony well. Also is is part of the HTML5 video tag stuff so i would expect wide support (ie javaFX).

the best way to play sound in a game if FMOD.

http://www.fmod.org/

Have fun selling the game :stuck_out_tongue:

heh, not the best to do that. :slight_smile: