Concatenate WAV files in Java

Here is a small snippet that will let you concatenate a list of WAV files. This only works for WAV files and is based on the assumption that all files are of the same format.

While the core code is good for most people, I’m sure the nuances of your needs are different than mine so you can modify or extend that above as necessary.

Happy coding!

 

One thought on “Concatenate WAV files in Java

  1. Boris

    Hi Karthic,

    I wrote a very similar code, but it is not working 100%.

    I have some wav samples (drum sounds, basically the same sound with small variations) and I am trying to concatenate them randomly in one big wav. If I set it to let’s say 12 iterations (in the for loop) it will result in a wav with only first 4 samples in it but with correct total length of all 12 summed up. It behaves differently at each run, sometimes it only concatenetes only the first 3 of them.

    The samples are really short and small, each 63k, so the resulting file should be under 1 MB.

    I used the same methods as you in principle.

    It seems to me like the SequenceInputStream has some kind of limitation or bug…because it seems to stumble over something once he has concatenated 3 or 4 of the samples.

    Do you have any idea what it could be? Any help would be greatly appreciated.

    Here is my code (I’m really a beginner in java programming, so please excuse my beginner’s mistakes:

    Random randomVal = new Random();
    AudioInputStream Track;
    int noOfBeats = 10;
    SequenceInputStream JoinedHits;
    //Vector Hits = new Vector();
    List Hits = null;
    long TrackLength=0;

    try {
    File TrackFile = new File(FileString);
    TrackFile.delete();

    AudioInputStream[] clip = new AudioInputStream[4];
    clip[0] = AudioSystem.getAudioInputStream(new File(wavFile[0]));
    clip[1] = AudioSystem.getAudioInputStream(new File(wavFile[1]));
    clip[2] = AudioSystem.getAudioInputStream(new File(wavFile[2]));
    clip[3] = AudioSystem.getAudioInputStream(new File(wavFile[3]));
    AudioFormat format= clip[0].getFormat();

    for (int i=1;i<=noOfBeats;i++){

    int r = randomVal.nextInt(4);

    System.out.println("loop: "+i+"("+r+")");

    if (Hits==null){
    Hits=new ArrayList();
    }
    Hits.add(clip[r]);
    TrackLength=TrackLength+clip[r].getFrameLength();

    }

    JoinedHits = new SequenceInputStream(Collections.enumeration(Hits));

    Track = new AudioInputStream(JoinedHits,format,TrackLength);

    TrackFile = new File(FileString);

    AudioSystem.write(Track,AudioFileFormat.Type.WAVE,TrackFile);

    System.out.println("Done");

    AudioInputStream SnareTrackClipAIS = AudioSystem.getAudioInputStream(TrackFile);
    Clip SnareTrackClip = AudioSystem.getClip();

    SnareTrackClip.open(SnareTrackClipAIS);
    SnareTrackClip.start();

    try {
    Thread.sleep(100);
    } catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
    }

    } catch (Exception e) {
    e.printStackTrace();
    }

    Thanks a lot
    Boris

     
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *