Join two WAV files from Java?

28,813

Solution 1

Here is the barebones code:

import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class WavAppender {
    public static void main(String[] args) {
        String wavFile1 = "D:\\wav1.wav";
        String wavFile2 = "D:\\wav2.wav";

        try {
            AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
            AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));

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

            AudioSystem.write(appendedFiles, 
                            AudioFileFormat.Type.WAVE, 
                            new File("D:\\wavAppended.wav"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Solution 2

The WAV header should be not be too hard to parse, and if I read this header description correctly, you can just strip the first 44 bytes from the second WAV and simply append the bytes to the first one. After that, you should of course change some of the header fields of the first WAV so that they contain the correct new length.

Solution 3

I found this (AudioConcat) via the "Code Samples & Apps" link on here.

Solution 4

Your challenge though occurs if the two WAV files don't have the exact same format in the wave header.

If the wave formats on the two files aren't the same, you're going to have to find a way to transmogrify them so they match.

That may involve an MP3 transcode or other kinds of transcoding (if one of them is encoded with an MP3 codec).

Share:
28,813

Related videos on Youtube

krosenvold
Author by

krosenvold

Agile pragmatic programmer in C#, Java, Web-technologies. Twitter kristian rosenvold.com (spot the missing character to get a valid email address)

Updated on July 09, 2022

Comments

  • krosenvold
    krosenvold almost 2 years

    What's the simplest way to concatenate two WAV files in Java 1.6? (Equal frequency and all, nothing fancy.)

    (This is probably sooo simple, but my Google-fu seems weak on this subject today.)

    • Gueorgui Obregon
      Gueorgui Obregon over 8 years
      I learn what's Google-fu +1 :)
  • TOMKA
    TOMKA over 15 years
    Assuming they are of the same bit-rate, sample-rate, and have the same number of channels.
  • krosenvold
    krosenvold over 15 years
    But do I really have to do this myself ? There must be a simpler solution ??
  • Dijar
    Dijar over 15 years
    You could have a look at javax.sound.sampled.AudioFileFormat - at least it's able to read WAV files, perhaps you can write WAVs with it, too.
  • krosenvold
    krosenvold over 15 years
    Thank you! There had to be a simple way.
  • Alex Kapustian
    Alex Kapustian about 13 years
    This doesn't work anymore cause android doesn't support javax.sound.sampled.* package anymore. Is their any other way to do it?
  • dharmendra
    dharmendra almost 12 years
    dear Alex the question is About in java not for Android
  • Jalal Sordo
    Jalal Sordo over 10 years
    i this won't work if the two files differ in length, what do you say ?
  • James Van Huis
    James Van Huis over 10 years
    The length of each clip should not matter. However, both files must be of the same format (mono/stereo, sampling rate, etc.).
  • onluiz
    onluiz over 8 years
    The problem with AudioInputStream is that the .close() method does not work :)