简体   繁体   中英

How can I append a WAV file y to an existing WAV file x, s.t. x += y?

I'm trying to continuously add WAV files together, using a try/catch block in a for loop. I have an existing snippet of code that can create a new file that combines two existing ones, and I'm trying to take that and turn it into something that can write over an existing file and use it's contents.

So far, however, this is the only thing that's happening. Given an array of preexisting WAV file[] of size n , the last entry ( file[n-1] ) is the only one stored on to the sum file x . I want all the files in the array to be added together into x .

My basic method has two parts: adding the first two files together to initialize x ( x = file[0] + file[1] ), and adding all subsequent files to x ( x+=file[2]+...+file[n-1] ). The first part works fine; I can initialize a new WAV file that combines the first two files easily. It's when I loop that I encounter issues. I can't seem to manipulate the file writing to make it where x doesn't just become file[n-1] .

Here's the code that can take two files and add them into a new file:

try {
      AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File("filepath"));
      AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File("filepath"));

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

            AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, 
                          new File("newFileFilepath"));
} catch (Exception e) { e.printStackTrace(); }

And here's how I'm trying to approach the second half of my problem (the looped appending):

// filepaths in the array need to be initialized, these are simply placeholders.
String[] filePathClips = {"filepath1", "filepath2", "filepath3", "filepath4"};


// Subsequent appends to the first and second clips, if necessary.
for (int n = 2; n < filePathClips.length; n++) {
    try {
        String stitchAppend = filePathClips[n];

        // clip1 represents the stored and preexisting, previously combined files.
        AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File("combinedFiles"));
        // clip2 is the upcoming file to be added to clip1. 
        AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(stitchAppend));

        // appendedFiles is the AudioInputStream of the combined files.
        AudioInputStream appendedFiles = new AudioInputStream(
                       new SequenceInputStream(clip1, clip2),     
                       clip1.getFormat(), 
                       clip1.getFrameLength() + clip2.getFrameLength());

        // This line generates the new file, which should just write over the
        // file of the same name (in this case, "combinedFiles"). 
        AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, 
        new File("combinedFiles"));
    }
    catch (Exception e) { e.printStackTrace(); } 
}

I expect the written file to be the sum of all files, but in stead, the resulting file is simply the last one to be added on the loop. Anyone with heavy experience with Java's sound library, or WAV file management will likely be able to help, and I'd be very grateful if you did! Thanks for reading my problem over, and for the myriads of answers I've already read that gave me my degree.

So, I figured it out, and it seems very obvious in retrospect. Basically, overwriting the file each time causes some issues, as jakub_d pointed out. What fixed it, was to update the AudioInputStream appended files. Resulting code is:

try {
// filepaths in the array need to be initialized, these are simply placeholders.
String[] filePathClips = {"filepath1", "filepath2", "filepath3", "filepath4"};

// The first two clips are combined to created the appendedFiles AIS.
AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(filePathClips[0]));
AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(filePathClips[1]));

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

// Subsequent appends to the first and second clips, if necessary.
for (int n = 2; n < filePathClips.length; n++) {
    String stitchAppend = filePathClips[n];

    // clip2 is the upcoming file to be added to appendedFiles. 
    clip2 = AudioSystem.getAudioInputStream(new File(stitchAppend));

    // appendedFiles is the AudioInputStream of the combined files.
    appendedFiles = new AudioInputStream(
                   new SequenceInputStream(appendedFiles, clip2),     
                   appendedFiles.getFormat(), 
                   appendedFiles.getFrameLength() + clip2.getFrameLength());
}
// This line generates the new file.
AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, 
    new File("combinedFilesFilepath"));
} catch (Exception e) { e.printStackTrace(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM