3

how can i mix two audio files into one file so that the resultant file can play two files simultaneously? please help.. here what i am doing is that i am taking two files and concat them into another file.. but i want the file to be played simultaneously..

    private void saveAudio1() {
    try {                                      

        AudioInputStream clip1 = AudioSystem.getAudioInputStream(file1);
        AudioInputStream clip2 = AudioSystem.getAudioInputStream(file2);
        Collection list=new ArrayList();

        AudioInputStream appendedFiles =
                new AudioInputStream(
                new SequenceInputStream(clip1, clip2),
                clip1.getFormat(),
                clip1.getFrameLength() + clip2.getFrameLength());
        if (dlgOpenFile == null) {
            dlgOpenFile = new FileDialog(this, "Save As...", FileDialog.SAVE);
        }
        if (cfgJMApps != null) {
            nameFile = cfgJMApps.getLastOpenFile();
        }
        if (nameFile != null) {
            dlgOpenFile.setFile(nameFile);
        }

        dlgOpenFile.show();
        nameFile = dlgOpenFile.getFile();
        if (nameFile == null) {
            return;
        }

        nameFile = dlgOpenFile.getDirectory() + nameFile;
        if (cfgJMApps != null) {
            cfgJMApps.setLastOpenFile(nameFile);
        }


        AudioSystem.write(appendedFiles,
                AudioFileFormat.Type.WAVE,
                new File(nameFile));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
pal sarkar
  • 105
  • 1
  • 9

4 Answers4

1

I just found a link http://www.jsresources.org/examples/AudioConcat.html

It seems like he is doing it.. The source code can be found on the page! Hope this helps you out.

Vossi
  • 231
  • 2
  • 8
  • 1
    he is using a package that is not present in netbeans. So i need another source.. import org.tritonus.share.sampled.TConversionTool; this is the package – pal sarkar Feb 10 '12 at 11:45
0

what @Vossi said is true. Use jresources.org to help use. You could use their example: http://www.jsresources.org/examples/MixingAudioInputStream.java.html. For the org.tritonus.share.sampled.TConversionTool package, if you have problems using their library, then download their source code: it's open source, and try using it. I tried it and it worked for me :D http://sourceforge.net/projects/tritonus/files/ I'm grateful for those guys!

Winnifred
  • 1,202
  • 1
  • 8
  • 10
0

You need to read sample by sample from both streams, perfrom addition of those samples (be carefull of overflow) and then store new added sample to new AudioInputStream. Check here to see how to convert OutputStream to InputStream, when you'll be able to make another AudioInputStream and save your audio file.

Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
Rade_303
  • 905
  • 11
  • 28
  • i am supposed to play both the files simultaneously... can i do that in this way? i am not sure.. plz help me.. thx – pal sarkar Feb 15 '12 at 11:12
  • Sorry for late answer. This is the nature of sound. Sampling is the mean to convert physical sound to numbers. When you hear two sounds simultaneously, the physics and mathematics say that you actually add the numbers of the first sound with the numbers of the second sound. Now, the ear itself works as a cushion, so after addition it actually lowers the volume of the sum. You may or may not perform that "lowering". – Rade_303 Feb 29 '12 at 11:47
  • Or, more easily, you can use `Thread`s, such as [here](http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml) and [here](http://stackoverflow.com/questions/3093687/play-wav-files-one-after-the-other-in-java). – Rade_303 Feb 29 '12 at 12:05
  • you can do interleaving to desired number of channels and play this all to one SourceDataLine – Cynichniy Bandera Oct 14 '12 at 11:53
-1

Here is a class I made :

class MixedSound extends Thread {
    protected AudioFormat format; //Both streams must have same format
    protected AudioInputStream sound1;
    protected AudioInputStream sound2;
    protected static SourceDataLine ausgabe;
    protected DataLine.Info data;

    public MixedSound(String path1, String path2) {
        try {
        sound1 = AudioSystem.getAudioInputStream(new File(path1));
        sound2 = AudioSystem.getAudioInputStream(new File(path2));
        format=sound1.getFormat(); //I assume both streams have same format
        data=new DataLine.Info(SourceDataLine.class,format);
        ausgabe=(SourceDataLine) AudioSystem.getLine(data);
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException bug) {
            System.err.println(bug);
        }
    }
    public synchronized void play() throws IOException, LineUnavailableException {
        ausgabe.open(format,1024);
        ausgabe.start();
        byte[] buffer1=new byte[1024];
        byte[] buffer2=new byte[1024];
        byte[] mixed=new byte[1024];
        int bytes_sound1=sound1.read(buffer1,0,buffer1.length);
        int bytes_sound2=sound2.read(buffer2,0,buffer2.length);
        while (bytes_sound1 != -1 || bytes_sound2 != -1) {
            for (int i=0; i < mixed.length; i++) {
                mixed[i]=(byte)Math.min(0.999f,((float)buffer1[i]+(float)buffer2[i])); //Mix them
            }
            ausgabe.write(mixed, 0, Math.max(bytes_sound1, bytes_sound2));
            buffer1=new byte[1024]; //Clean buffer
            buffer2=new byte[1024]; //Clean buffer
            bytes_sound1=sound1.read(buffer1,0,buffer1.length);
            bytes_sound2=sound2.read(buffer2,0,buffer2.length);
        }
        ausgabe.drain();
        ausgabe.close();
    }
    @Override
    public synchronized void run() {
        try {
            play();
        } catch (IOException | LineUnavailableException ex) {
            System.err.println(ex);
        }
    }
}

It mixes two sounds by adding their volumes.

Use it like that :

MixedSound sound=new Sound("sound1.wav","sound2.wav");
sound.start(); //Play it
System.out.println("Started playing sound"); //Do stuff at same time

Hope it helps.

Luatic
  • 8,513
  • 2
  • 13
  • 34
  • where to get AudioSystem.getAudioInputStream ? – Ahmad Arslan Aug 31 '18 at 09:40
  • I'm sorry for not being able to tell you the exact library name ATM. You could either google it, but I recommend using an IDE, such as NetBeans, it'll help you with the appropriate imports ;) – Luatic Aug 31 '18 at 14:57
  • @AhmadArslan You would need to do `import javax.sound.sampled.AudioSystem;` (searched for "java AudioSystem", [see the docs](https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html)). – Yeti Nov 10 '18 at 01:21