1 Reply Latest reply on Jan 7, 2011 6:17 PM by captfoss

    Combining 2+ WAV Files into a single WAV file

      Hello there,

      I am trying to combine at least 2 WAV files (my example uses 3) into a single file. I have tried several examples (http://www.jsresources.org/examples/AudioConcat.html and http://stackoverflow.com/questions/653861/join-two-wav-files-from-java) but both have the same problem: the output file is an exact copy of the last file to have been joined.

      Here is the code for the simpler of the 2 examples which I have altered to meet my needs.
              File outputFile = new File("concat.wav");
              for (int i = 0; i < files.length - 1; i++) {
                  try {
                      AudioInputStream clip1 = AudioSystem.getAudioInputStream(files);
      AudioInputStream clip2 = AudioSystem.getAudioInputStream(files[i + 1]);

      AudioInputStream appendedFiles =
      new AudioInputStream(
      new SequenceInputStream(clip1, clip2),
      clip1.getFrameLength() + clip2.getFrameLength());


      files[i + 1] = outputFile;//first file read in next loop, will contain joined file so far

      } catch (Exception e) {
      return outputFile;

      I am unsure as where the problem lies, so would be grateful for anyone to point out the (no doubt obvious) mistake i've made :p
        • 1. Re: Combining 2+ WAV Files into a single WAV file
          I don't see any problem with your actual code in terms of JavaSound, but...

          The first run through, you'd be concating "file1.wav" and "file2.wav" to a file called "concat.wav". The second time through, you'd be concating "concat.wav" and "file3.wav" to a file called "concat.wav"...

          On the second iteration, "concat.wav" will have a read-lock put on it until such time as the sequence input stream releases it... which will occur as soon as it's done reading it. At that point, the read lock is released and you can obtain a write-lock on it, but, you've already dumped all of the data from concat.wav into the bit bucket...so the only thing that gets to go in there now is "file3.wav".

          So, don't try to read from and write to the same file at the same time...