/* (non-Javadoc)
   * @see javax.swing.SwingWorker#doInBackground()
   */
  @Override
  protected Void doInBackground() throws Exception {

    String cmd = null;

    if (isMale) { // If male

      try { // Writes speech to text file
        PrintWriter out = new PrintWriter(new FileWriter(".PetulantWaffle/Speech.txt"));
        out.println(speech);
        out.close();
      } catch (IOException e) {
        System.out.println("Couldn't write to Speech.txt");
      }

      // Create the wav file from the text in Speech.txt
      cmd = "text2wave .PetulantWaffle/Speech.txt -o .PetulantWaffle/speech.wav;";
    } else { // If female
      SchemeCreator scheme = new SchemeCreator(speech);
      scheme.createMP3Scheme();

      cmd = "festival -b .PetulantWaffle/Scheme.scm;";
    }

    // Convert the wav file to mp3
    cmd = cmd + "ffmpeg -i .PetulantWaffle/speech.wav -y .PetulantWaffle/audio.mp3;";

    if (speechTimeInMS != 0.0) { // Merge video at specified time
      // Replace with merged audio and create file specified by the user
      cmd =
          cmd
              + "ffmpeg -i "
              + Video.getVideoName()
              + " -i .PetulantWaffle/audio.mp3 -filter_complex '[1:a]adelay="
              + speechTimeInMS
              + "[aud2];[0:a][aud2]amix=inputs=2' -map 0:v -map 1:a "
              + chooser.getSelectedFile().getAbsolutePath()
              + ".avi";
    } else { // Merge video at beginning
      cmd =
          cmd
              + "ffmpeg -i "
              + Video.getVideoName()
              + " -i .PetulantWaffle/audio.mp3 -filter_complex '[0:a][1:a]amix=inputs=2' -map 0:v -map 1:a "
              + chooser.getSelectedFile().getAbsolutePath()
              + ".avi";
    }

    ProcessBuilder makeWav = new ProcessBuilder("/bin/bash", "-c", cmd);
    Process processMW;
    processMW = makeWav.start();
    processMW.waitFor();

    return null;
  }