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:
- load all your 30–40 different sounds into memory
- 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.