Multiple clips on the same line

Is this possible? I have been googling and javagaming.orging but I’m not sure. Is it possible with Java sound to have multiple clips going through the same line?

My problem is that my game has about 30-40 different sounds, and when the program initializes, I open a different line for each clip, and they remain open throughout the entire game because they all get used over and over. This works fine on Windows systems.

However, the Raspberry Pi only has 8 lines, which throws a LineUnavailableException. Right now, I’m re-doing the audio so all sound effects are in the same audio file, one after the other, and I have to go and figure out the frame positions for each sound and play them accordingly. I have done this with the background music which isn’t too hard. When I go to do the sound effects, it will take a long ass time.

So is it possible to play all the sound effects through the same line instead of opening a separate line for each sound?

Neat idea, to place several audio clips on the same audio file! I hadn’t thought of doing that. How do you stop the cue at the right place? Are you counting frames somehow?

To collect all sounds and send them out a single line, you have to write a mixing algorithm yourself or make use of a library such as TinySound which does exactly this. (By mixer I mean the equivalent of an audio mixer as studio gear, not the javax.sound.sampled.Mixer class which is something different.)

To mix, it is matter of getting at the individual PCM sound frames (can be done by first loading the cues into memory arrays or using an AudioinputStream if you are playing back from a file), and summing them, then outputting the sum on a SourceDataLine.

I have been working pretty hard, actually, on a class I’m calling an AudioCue. There is a weak first pass already posted here at JGO. It will allow concurrent sound effects for any sound effect line that is “opened”, where all the concurrent plays will be summed into a single output for that cue. (Example: variable number of bullets in quick succession for simulating a semi-automatic rifle, all come from one cue and all output on single line, despite the cues being played in a concurrent, overlapping fashion.) The new version I am about to post has reliable and smooth volume, pan, and frequency faders, so the same sound can be played back at different speeds at the same time and still be counted as a single output line.

I could just go ahead and post it now if it would be useful, or send you a preview, but I am still working on the JavaDocs for it and want to rewrite the panning into a lambda form, so different types of panning could be used. Also want to put in some input argument safety checks and clamping on the output. It is easy to overdrive and create wretched noises if you aren’t careful. (But not so bad if you know what you are doing and understand audio.)

This class would allow you to do:

  1. load all your 30–40 different sounds into memory
  2. easily open and shut the lines as needed (assuming different segments of the game use different sounds).

If some of the sounds can do double duty by varying the speed or if some of your 30-40 are implementations of concurrent uses of clips, this could get you a long way towards reducing the number of lines that need to be open at once.

I haven’t timed the lag for the combo open/play with this class yet as I hadn’t anticipated that use. But if you can run an open method a good 100 millis prior to play as a preparatory step, that would easily be a good safety margin.

Anyway, you probably just need to go to a library.
How far along is your game, otherwise?
Is it possible to check out what you have so far?
I do have a mixer library I wrote that does the mixing I described–maybe I can rig up a version that will handle your needs.

Yeah, I’m just using if(getFramePosition() == xxx), stop the clip (although I had to go through and figure out the start/stop frame position for the sounds. It would be cool to try out AudioCue! No rush though as I have some time so you can do whatever you need to before you post it. I can post my audio class when I get home from work so you can take a look. The game is almost done, I just need to program the last boss battle and do some bug fixes. Thanks for the reply! It kinda made my day that you said I have a neat idea about sound lol.

I ended up using TinySound and wow was that easy to implement. I never really realized it would be so easy. I learned how to use and ported all the java sound to tiny sound in about 2 hours.

@philfrei, I would still be interested in trying out AudioCue whenever you have it good and ready. Thanks for helping me out with this!

Finished a draft of the API. Am working on html page for posting, and figuring out license (worst case scenario: requirement to post a single link on an “About” page or “Credits” page).

TinySound is a good library! Glad you gave it a try.

AudioCue will have some advantages and disadvantages. It doesn’t mix everything together into a single output line (advantage TinySound) but I can’t recall if TinySound has real time volume, panning and frequency changes, or if it lets you position the “playhead” to different points. I probably should have looked closely at its API before designing mine, though. I think AudioCue will be similarly easy to use (or at least in the ballpark).

All right, I actually have a hour to work on it this morning–better stop procrastinating.