简体   繁体   中英

JAVA - iterate through a folder of audio files, concatenating another audio file to each one

I am working on a small Java script to iterate through the .wav files in a folder named 'audiofiles', and concatenate each one individually with another audio file (named "silence_2sec.wav" in this case) So far I have:-

import java.io.File;
import java.util.*;
import java.io.*;
import javax.sound.sampled.*;

public class silapp_2b{

public static void main(String[]args){

String wavFile2 = "silence_2sec.wav";

            // get list of file names from audio directory

File audDir = new File("audiofiles");

       //define a list to contain the audio files names and path

 File[] filesList = audDir.listFiles();

       // assign contents of each wav file from filesList to a string     


try {


    for(File f : filesList){

        if(f.isFile()){

            AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(f.getName()));
            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("output" + "(f.getName()).wav"));

        }
    }

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


}
}

This compiles, but returns the following error on execution:-

java.io.FileNotFoundException: glitch2.wav (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(WaveFloatFileReader.java:164)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1181)
    at silapp_2b.main(silapp_2b.java:32)

Any suggestions?

The error

AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(f.getName()));

This tries to create a file with the given file name, and looks for it in the current directory.

You should be using File.getAbsolutePath() to get the full absolute path to the file

AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(f.getAbsolutePath()));

or more simply

AudioInputStream clip1 = AudioSystem.getAudioInputStream(f);

Same problem with wavFile2

I suspect you will have the same issue with the second file:

AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));

You will need to provide an absolute path here too. If it also is in the audDir then do

File wavFile2 = new File(audDir.getAbsolutePath() + "/silence_2sec.wav");
AudioInputStream clip2 = AudioSystem.getAudioInputStream(wavFile2);

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