0

This util method helps with creating, silencing and concatenating selected .wav files using bash commands within unix. For some reason when I'm running the methods given a list of .wav files in a list, the concatenation of .wav files only appears when I run the same method twice. Thus the playAudio() method gives me an error of:

java.io.FileNotFoundException: temp/combine.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 java.io.FileInputStream.<init>(FileInputStream.java:93)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:35)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:30)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
java.io.FileNotFoundException: temp/combine.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 java.io.FileInputStream.<init>(FileInputStream.java:93)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:35)
at namesayer.util.PlayAudio$1.call(PlayAudio.java:30)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)

My class is:

//Input of a string with destination of audio file to play - uses AudioStream and AudioPlayer for .wav files
public class PlayAudio {
private String audio;
private List<String> combineAudio = new ArrayList<>();
private AudioStream audioStream;

public PlayAudio(String audio) {
    this.audio = audio;
}

public PlayAudio(List<String> combineAudio) {
    this.combineAudio = combineAudio;
    audio = "temp/combine.wav";
}

public void playAudio() {
    Task task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            InputStream in = null;
            try {
                in = new FileInputStream(audio);
                audioStream = new AudioStream(in);
                AudioPlayer.player.start(audioStream); // play the audio clip with the audio player class
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    new Thread(task).start();
}

public void playCombinedAudio() {
    //Silence noise sections of each audio
    for (String audio : combineAudio) {
        String silenceAudio = "cd data/names\n" +
                "cp " + audio + ".wav ../../temp/" + audio + ".wav\n" +
                "cd ../../temp\n" +
                "ffmpeg -hide_banner -i " + audio + ".wav -af silenceremove=1:0:-30dB:1:5:-30dB " + audio + "CONCAT.wav\n";
        ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", silenceAudio);
        try {
            processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Combine audio clips together
    FileWriter writer = null;
    try {
        writer = new FileWriter("temp/combineAudio.txt");
        for (String str : combineAudio) {
            writer.write("file '" + str + "CONCAT.wav'\n");
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Concatenate from txt file
    String concatAudio = "cd temp/\n" +
            "ffmpeg -f concat -safe 0 -i combineAudio.txt -c copy combine.wav\n";
    ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", concatAudio);
    try {
        processBuilder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

    playAudio();
}
borrible
  • 17,120
  • 7
  • 53
  • 75
KleinESK
  • 85
  • 7
  • Possible duplicate of [Java new File() says FileNotFoundException but file exists](https://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists) – SedJ601 Oct 04 '18 at 13:31
  • I understand their situation, but where would I throw and catch the exception explicitly in my case? – KleinESK Oct 05 '18 at 02:05
  • I am going to guess that your file path is incorrect – SedJ601 Oct 05 '18 at 02:15
  • The problem is, I run the method two to three times and then the "Combine.wav" shows up. – KleinESK Oct 05 '18 at 02:22

0 Answers0