Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple API for JavaSound use  (Read 4470 times)
0 Members and 1 Guest are viewing this topic.
Offline divxdede
« Posted 2010-09-26 19:41:26 »

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 =;

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)


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

Best regards,
André Sébastien. 
Offline gouessej
« Reply #1 - Posted 2010-09-26 20:33:54 »


There is already this:
It is fine to reinvent the wheel as it allows to learn a lot of things Wink

Offline divxdede
« Reply #2 - Posted 2010-09-26 21:49:24 »


There is already this:
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #3 - Posted 2010-09-26 22:12:49 »

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.

Offline zammbi

JGO Coder

Medals: 4

« Reply #4 - Posted 2010-09-27 02:03:58 »

I just figure it's main purpose was 3D sound and was a little more complex that what i need.
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.

Current project - Rename and Sort
Offline paulscode

Senior Devvie

Medals: 12

Staff Sergeant

« Reply #5 - Posted 2010-09-27 09:26:59 »

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.

We love death.  The US loves life.  That is the difference between us.  -Osama bin Laden, mass murderer
Offline nsigma
« Reply #6 - Posted 2010-09-27 10:53:07 »

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 -  Also, if you haven't come across it yet, is quite useful even if a bit dated (well, it's about as current as JS itself!  Smiley )

Best wishes, Neil

Praxis LIVE - hybrid visual IDE for creative coding
Digital Prisoners - interactive spaces and projections
Offline divxdede
« Reply #7 - Posted 2010-09-27 16:21:31 »

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).

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (3 views)
2015-10-07 02:11:23

KaiHH (12 views)
2015-10-06 20:22:20

KaiHH (11 views)
2015-10-06 19:41:59

BurntPizza (23 views)
2015-10-06 19:04:48

basil_ (44 views)
2015-09-30 17:04:40

shadowstryker (22 views)
2015-09-29 15:55:06

TheSpaceHedgehog (28 views)
2015-09-29 01:58:48

GamerC4 (54 views)
2015-09-24 21:10:38

GamerC4 (71 views)
2015-09-24 21:09:48

htuy (28 views)
2015-09-24 04:57:24
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!