05 May 2007

Mixing two audio files using Java Sound

There are no special methods in the Java Sound API to do this. However, mixing is a trivial signal processing task, it can be accomplished with plain Java code .
So manually mixing the two or more audioinputstreams using some attitional api
note: each Audiofile length must be same

Download the Sample Sourcecode with API : Sample Source code ais_mixer.rar
Alternate Downloading Mirror :mirror(ais_mixer.rar)

or


Example:

first convert audiofile to audioinputstream

audioInputStream = AudioSystem.getAudioInputStream(soundFile);
audioInputStream2 = AudioSystem.getAudioInputStream(soundFile2);

Create one collection list object using arraylist then add all audioinputstream's

Collection list=new ArrayList();
list.add(audioInputStream2);
list.add(audioInputStream);

then pass the audioformat and collection list to MixingAudioInputStream constructor

MixingAudioInputStream mixer=new MixingAudioInputStream(audioFormat, list);

finaly read data from mixed audioninputstream and give it to sourcedataline

nBytesRead =mixer.read(abData, 0,abData.length);

int nBytesWritten = line.write(abData, 0, nBytesRead);