Simple API for JavaSound use

Hi all,
I’m currently writing a little game in java of course :wink:

At this time, i had completed the “game-logic” and i had started an UI implementation with Java2D.
At the end, i want to use an UI implementation with Slick, but doing it with Java2D force to me to preserve game-logic from the ui implementation and it’s a good exercice as well.

So, in this implementation, i’m starting to add some sounds on my game… and i’m a strong newbie on this domain.
When studying JavaSound, OpenAL, NativeFmodEx, … my first suprise is that all of theses API are low-level and we must manage I/O buffers, decoding streams, etc…

For my game, i have simple needs like:

  • Reading compressed file format like OGG Vorbis
  • Playing multiples sounds at the same time that should be mixed inside the hardware
  • Playing mutiples times the same sound at the same time.
  • Doing fade-In fade-Out effect
  • Change speed playback of a sound. (it’s seems to be complex, and i’m not be able to implement it)

So, for theses simple needs, i’m writing a simple API upon JavaSound.
I know this API can’t fullfill more complex needs (like 3D sounds, sync, complex effects, …)
But in case it can help someone else, i put it here.

As i said, i’m a newbie in sound dev, and all remarks are welcome if it can improve this basis.
But i don’t want to make a full API with a billion of features !!!

Here an exemple of use:

        // create a manager that will use a dedicated thread for perform I/O
        SoundManager manager  = new SoundManager();

        // get a sound definition
        Sound        sound    = manager.getBufferedSound( getClass().getResource("/audio.ogg") );

        // start a playback from the sound
        PlayBack     playback = sound.play();

Playback can be started at any times from any sounds and there I/O will be handled by the manager.

We can by example run a sound with a fade-in like it:

       Sound        sound    = manager.getBufferedSound( getClass().getResource("/audio.ogg") );

       SoundEffect effect    = SoundSequence.from(sound).playback().volume(0f).setLoop(true).resume().fadeIn( 3 , TimeUnit.SECONDS );
 
       SoundTimeLine timeline = manager.getTimeLine("example");
       Future future = timeline.submit(effect);

SoundSequence is a convenient way to create delayed operations upon a sound that can be executed by a timeline sequentially.
For more information, see sources or javadoc.

When your application should end, don’t forget to close your SoundManager (that will close the dedicated thread)

      
  manager.close();

PS: For using OGG Vorbis, you just need to have JOrbis and VorbisSpi on your classpath.

Best regards,
André Sébastien.

Hi

There is already this:
http://www.java-gaming.org/index.php/topic,20271.0.html
It is fine to reinvent the wheel as it allows to learn a lot of things :wink:

Yes i saw it.
I just figure it’s main purpose was 3D sound and was a little more complex that what i need.
But i will try it if i had some free time this week.

Writing your own sound API might have a pedagogical interest but nothing else as Paul (the author of the API that I quoted) has made a great job, really. I have seen lots of tiny useless wrappers of JavaSound. Paul’s sound engine takes into account several known bugs of Java Sound Audio Engine. If you don’t want to lose all your hairs by writing your own sound API, use his API (well documented with some examples to start very quickly) that is neither complicated nor only for 3D, you can play some samples of music without using the 3D features.

[quote]I just figure it’s main purpose was 3D sound and was a little more complex that what i need.
[/quote]
His sound library is the easiest and best I’ve ever used. Basically just works without hassle. Reading his pdf you can get it working in your game in 5 minutes.

If the SoundSystem is too large for your needs, you are also free copy and paste what you need from the source code (I’ve basically given it a “do whatever you like” license, so feel free to hack away at it). If you need help understanding parts of it, let me know and I can explain how different things work.

I had a quick look through your code, though haven’t tried actually compiling it. For the purpose you’ve got, I really like the API. The method chaining looks clean and intuitive (reminds me of jQuery), and I like the timeline.submit() and Future mechanism.

The one thing that may let you down is the underlying JavaSound library, which can be a real PITA. It’s behaviour can be inconsistent, and the best performing mixers don’t necessarily do hardware mixing or provide you with the Controls you want. If it’s working for you, great, but you might be able to get better performance by going for a software mixing strategy. I pointed to a few projects / sources of info in another thread - http://www.java-gaming.org/index.php/topic,22847.msg188955.html#msg188955 Also, if you haven’t come across it yet, http://www.jsresources.org/ is quite useful even if a bit dated (well, it’s about as current as JS itself! :slight_smile: )

Best wishes, Neil

Thanks Neil for your return.
Features covered by this API are quite small and my goal was to offer this small assets on a very simple design.

Java Sound was attractive for a newbie like me because it’s embedded on the JVM and don’t need a lot of third parties.
But i surprise to hear that this API implementation seem to be not robust as we can expect for a standard API.

So, i haven’t knowledge to do more complex stuff (especially reimplementing software mixing and so one).
I will start to try this evening the SoundSystem (i’m starting to install theses projects inside my maven repository).

Sébastien.