Hi all,
I’m currently writing a little game in java of course
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.