Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join two WAV files from Java?

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.)

like image 645
krosenvold Avatar asked Mar 17 '09 11:03

krosenvold


People also ask

Can you concatenate WAV files?

Combine WAV Files Online This audio merger is the handy tool that you need – consolidate them without getting a pesky audio watermark! The web tool works fast and is totally user-friendly. No need to download apps or programs, simply upload your files on the page – choose them from your PC, Google Drive, or Dropbox.

Can WAV be multichannel?

Audacity will allow you to create and subsequently output a multichannel wav file based on an original stereo input wav file. You simply copy and paste or duplicate a track as many times as you require. Or simply create empty (silent) tracks where necessary.


1 Answers

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();
        }
    }
}
like image 55
James Van Huis Avatar answered Oct 17 '22 13:10

James Van Huis