Example usage for java.lang Runtime exec

List of usage examples for java.lang Runtime exec

Introduction

In this page you can find the example usage for java.lang Runtime exec.

Prototype

public Process exec(String cmdarray[]) throws IOException 

Source Link

Document

Executes the specified command and arguments in a separate process.

Usage

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Initialize HTK Training process//www.j a  v  a2  s.c  o  m
 * @throws Exception
 */
private void initialiseHTKTrain() throws Exception {

    String hcompv = getProp(HTKDIR) + File.separator + "HCompV";
    File htkFile = new File(hcompv);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";
    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

    System.out.println("( cd " + getProp(HTDIR) + " ; mkdir hmm/hmm-dummy ; " + " mkdir hmm/hmm-final ; "
            + hcompv + " " + HTK_SO + " -C " + configFile + " -f 0.01 -m -S " + listFile + " -M "
            + getProp(HTDIR) + File.separator + "hmm/hmm-dummy " + getProp(HTDIR) + File.separator + "config"
            + File.separator + "htk.proto" + " > log_initialiseHTKTrain.txt" + "; exit )\n");
    pw.print("( cd " + getProp(HTDIR) + " ; mkdir hmm/hmm-dummy ; " + " mkdir hmm/hmm-final ; " + hcompv + " "
            + HTK_SO + " -C " + configFile + " -f 0.01 -m -S " + listFile + " -M " + getProp(HTDIR)
            + File.separator + "hmm/hmm-dummy " + getProp(HTDIR) + File.separator + "config" + File.separator
            + "htk.proto" + " > log_initialiseHTKTrain.txt" + "; exit )\n");
    pw.flush();
    //shut down
    pw.close();

    process.waitFor();

    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
* Setup the HTK directory/*from www. j av  a 2 s .c  o  m*/
* @throws IOException, InterruptedException
  * @throws MaryConfigurationException 
*/
private void setup() throws IOException, InterruptedException, MaryConfigurationException {

    htk.mkdir();
    File lab = new File(htk.getAbsolutePath() + "/lab");
    //call setup of HTK in this directory
    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    //go to htk directory and setup Directory Structure 
    pw.print("( cd " + htk.getAbsolutePath() + "; mkdir -p hmm" + "; mkdir -p etc" + "; mkdir -p feat"
            + "; mkdir -p config" + "; mkdir -p lab" + "; mkdir -p htk-full/lab" + "; mkdir -p htk-full/wrd"
            + "; exit )\n");
    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

    // TODO: temporary: at the moment fix path to load the dictionary
    lexicon = new FSTLookup(new FileInputStream(
            "/home/fabio/voice_building_5_0/marytts-it/marytts-lang-it/src/main/resources/marytts/language/it/lexicon/it_lexicon.fst"),
            "it_lexicon.fst");

    /*  System.out.print("Starting builtin MARY TTS...");
    Mary.startup();
    System.out.println(" MARY TTS started.");
            
    e poi 
    */

}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Create HMMs for each phone from Global HMMs 
 * @throws Exception// w  w  w .  j  a  v a 2s.co  m
 */
private void createTrainFile() throws Exception {

    String script;
    String hmmDir = getProp(HTDIR) + File.separator + "hmm" + File.separator;

    /**TODO:
     * Replace below 'gawk' script with Java method.
     */

    script = "mkdir hmm/hmm0\n" + "head -3 hmm/hmm-dummy/htk > hmm/hmm0/hmmdefs\n"
            + "for s in `cat etc/htk.phone.list`\n" + "do\n" + "echo \"~h \\\"$s\\\"\" >> hmm/hmm0/hmmdefs\n"
            + "gawk '/BEGINHMM/,/ENDHMM/ { print $0 }' hmm/hmm-dummy/htk >> hmm/hmm0/hmmdefs\n" + "done\n";
    // creating list of training files
    File file = new File(getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrainScript.sh");
    PrintWriter pw = new PrintWriter(new FileWriter(file));
    pw.println(script);
    pw.flush();
    pw.close();

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell
    pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

    System.out.println("( cd " + getProp(HTDIR) + "; sh etc" + File.separator + "htkTrainScript.sh"
            + " > log_htkTrainScript.txt" + "; exit )\n");
    pw.print("( cd " + getProp(HTDIR) + "; sh etc" + File.separator + "htkTrainScript.sh"
            + " > log_htkTrainScript.txt" + "; exit )\n");

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

    PrintWriter macroFile = new PrintWriter(
            new FileOutputStream(new File(hmmDir + "hmm0" + File.separator + "macros")));
    macroFile.println("~o\n" + "<VecSize> 13\n" + "<" + Train_FEAT + ">");
    macroFile.println(
            FileUtils.getFileAsString(new File(hmmDir + "hmm-dummy" + File.separator + "vFloors"), "ASCII"));
    macroFile.flush();
    macroFile.close();

}

From source file:marytts.tools.voiceimport.HTKLabeler.java

private void saveHTKWordDictionary() throws Exception {

    String dict0 = outputDir + File.separator + "htk.words0.dict";
    String dict = outputDir + File.separator + "htk.words.dict";
    PrintWriter wordDictOut = new PrintWriter(new FileOutputStream(new File(dict0)));

    HTKdictionary.add("sil sil");
    HTKdictionary.add("ssil ssil");
    HTKdictionary.add("sp sp");

    Iterator<String> itr = HTKdictionary.iterator();
    while (itr.hasNext()) {
        wordDictOut.println(itr.next());
    }/* w w w .j a  v  a2 s .c o m*/

    wordDictOut.flush();
    wordDictOut.close();

    String fileded = getProp(HTDIR) + File.separator + "config" + File.separator + "dict.ded";
    PrintWriter dedDictOut = new PrintWriter(new FileOutputStream(new File(fileded)));

    dedDictOut.println("AS sp");
    dedDictOut.println("MP sil sil sp");
    dedDictOut.println("MP ssil ssil sp");
    dedDictOut.println("MP sp sp sp");

    dedDictOut.flush();
    dedDictOut.close();

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell

    //when no sp use (-m)!

    String hdman = getProp(HTKDIR) + File.separator + "HDMan";

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

    String cmd = "( cd " + getProp(HTDIR) + "; " + hdman + " -g " + fileded + " " + dict + " " + dict0
            + "; exit )\n";

    System.out.println(cmd);
    pw.println(cmd);

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Force Align database for Automatic labels 
 * @throws Exception//from   w w  w.j a  v  a2 s. c om
 */
private void hviteAligning() throws Exception {

    String hvite = getProp(HTKDIR) + File.separator + "HVite"; // -A -D -V -T 1 "; // to add -A -D -V -T 1 in every function
    File htkFile = new File(hvite);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";

    // Virtual sp change_ phoneList should be a member? 
    // Without sp: 
    /*String phoneList = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phone2.list";*/

    // Whit sp:

    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

    String hmmDef = getProp(HTDIR) + File.separator + "hmm" + File.separator + "hmm-final" + File.separator
            + "hmmdefs";
    String macros = getProp(HTDIR) + File.separator + "hmm" + File.separator + "hmm-final" + File.separator
            + "macros";

    // Virtual sp change_ phoneMlf should be a member?

    // Without sp: 
    /*String phoneMlf = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phones2.mlf";*/
    // Whit sp:
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones3.mlf";

    String alignedMlf = getProp(HTDIR) + File.separator + "aligned.mlf";
    String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.dict";
    String labDir = getProp(HTDIR) + File.separator + "lab";

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell

    //when no sp use (-m)!

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println("( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir
            + " -o W -C " + configFile + " -a -H " + macros + " -H " + hmmDef + " -i " + alignedMlf
            + " -t 250.0 -y lab" + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList
            + " > log_hviteAligning.txt" + "; exit )\n");

    pw.println("( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -o W -C "
            + configFile + " -a -H " + macros + " -H " + hmmDef + " -i " + alignedMlf + " -t 250.0 -y lab"
            + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList
            + " > log_hviteAligning.txt" + "; exit )\n");

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:marytts.tools.voiceimport.HTKLabeler.java

private void htkExtraModels() throws Exception {

    String hlstats = getProp(HTKDIR) + File.separator + "HLStats";
    String hbuild = getProp(HTKDIR) + File.separator + "HBuild";

    File htkFile = new File(hlstats);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }/*ww  w  . ja va 2  s . c o m*/
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String bigFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones.big";
    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.list";
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones.mlf";
    String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.dict";
    String phoneAugDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.aug.phone.dict";
    String phoneAugList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.aug.phone.list";

    String netFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones.net";

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println("( cd " + getProp(HTDIR) + "; " + hlstats + " -T 1 -C " + configFile + " -b " + bigFile
            + " -o " + phoneList + " " + phoneMlf + " > log_hlstats.txt" + "; exit )\n");

    pw.println("( cd " + getProp(HTDIR) + "; " + hlstats + " -T 1 -C " + configFile + " -b " + bigFile + " -o "
            + phoneList + " " + phoneMlf + " > log_hlstats.txt" + "; exit )\n");

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

    String fileDict = FileUtils.getFileAsString(new File(phoneDict), "ASCII");
    PrintWriter augPhoneDict = new PrintWriter(new FileWriter(phoneAugDict));
    augPhoneDict.println("!ENTER sil");
    augPhoneDict.print(fileDict);
    augPhoneDict.println("!EXIT sil");
    augPhoneDict.flush();
    augPhoneDict.close();

    String fileList = FileUtils.getFileAsString(new File(phoneList), "ASCII");
    PrintWriter augPhoneList = new PrintWriter(new FileWriter(phoneAugList));
    augPhoneList.println("!ENTER");
    augPhoneList.print(fileList);
    augPhoneList.println("!EXIT");
    augPhoneList.flush();
    augPhoneList.close();

    rtime = Runtime.getRuntime();
    //get a shell
    process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell
    pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println("( cd " + getProp(HTDIR) + "; " + hbuild + " -T 1 -C " + configFile + " -n " + bigFile
            + " " + phoneAugList + " " + netFile + " > log_hbuild.txt" + "; exit )\n");

    pw.println("( cd " + getProp(HTDIR) + "; " + hbuild + " -T 1 -C " + configFile + " -n " + bigFile + " "
            + phoneAugList + " " + netFile + " > log_hbuild.txt" + "; exit )\n");

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Force Align database for Automatic labels 
 * @throws Exception/*from  ww  w  . j  a  v a 2 s  .  c o  m*/
 */
private void hviteMultiplePronunciationAligning(String hmmNumber, String alignedMlf, boolean labOutput,
        String labDir, boolean full, String logfile, boolean cmp) throws Exception {

    String hvite = getProp(HTKDIR) + File.separator + "HVite"; // -A -D -V -T 1 "; // to add -A -D -V -T 1 in every function
    File htkFile = new File(hvite);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }
    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String listFile = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";

    // Virtual sp change_ phoneList should be a member? 
    // Without sp: 
    /*String phoneList = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phone2.list";*/

    // Whit sp:

    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

    String hmmDef = getProp(HTDIR) + File.separator + "hmm" + File.separator + hmmNumber + File.separator
            + "hmmdefs";
    String macros = getProp(HTDIR) + File.separator + "hmm" + File.separator + hmmNumber + File.separator
            + "macros";

    // Virtual sp change_ phoneMlf should be a member?

    // Without sp: 
    /*String phoneMlf = getProp(HTDIR)+File.separator
        +"etc"+File.separator+"htk.phones2.mlf";*/
    // Whit sp:
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.words3.mlf";

    String phoneDict = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.words.dict";

    Runtime rtime = Runtime.getRuntime();
    //get a shell
    Process process = rtime.exec("/bin/bash");
    //get an output stream to write to the shell

    //when no sp use (-m)!
    String cmd;

    String alignout, mOptioon, oOption;

    if (labOutput)
        alignout = "";
    else
        alignout = " -i " + alignedMlf;

    if (full) {
        if (labOutput)
            mOptioon = "";
        else
            mOptioon = " -m";

        cmd = "( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + " -C "
                + configFile + mOptioon + " -a -H " + macros + " -H " + hmmDef + alignout + " -t 250.0 -y lab"
                + " -I " + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > " + logfile
                + "; exit )\n";
    } else {
        if (cmp)
            oOption = " -o TS";
        else
            oOption = " -o W";

        cmd = "( cd " + getProp(HTDIR) + "; " + hvite + " " + HTK_SO + " -b sil -l " + labDir + oOption + " -C "
                + configFile + " -m -a -H " + macros + " -H " + hmmDef + alignout + " -t 250.0 -y lab" + " -I "
                + phoneMlf + " -S " + listFile + " " + phoneDict + " " + phoneList + " > " + logfile
                + "; exit )\n";
    }

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
    System.out.println(cmd);
    pw.println(cmd);

    pw.flush();
    //shut down
    pw.close();
    process.waitFor();
    // check exit value
    if (process.exitValue() != 0) {
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        throw new MaryConfigurationException(errorReader.readLine());
    }

}

From source file:edu.harvard.iq.dvn.core.web.servlet.FileDownloadServlet.java

private boolean generateImageThumb(StudyFile file) {

    String fileLocation = file.getFileSystemLocation();
    if (fileLocation == null || fileLocation.trim().equals("")) {
        return false;
    }/* ww  w .  ja  va2s  .  c  om*/
    String thumbFileLocation = fileLocation + ".thumb";

    // see if the thumb is already generated and saved:

    if (new File(thumbFileLocation).exists()) {
        return true;
    }

    // let's attempt to generate the thumb:

    // the default size of the thumbnail is 64 pixels horizontally.
    // The number 64 was picked arbitrarily; if a different size is 
    // desired, it can be configured via the dvn.image.thumbnail.size 
    // JVM option.

    Long thumbSize = Long.valueOf(64);

    String thumbSizeOption = System.getProperty("dvn.image.thumbnail.size");

    if (thumbSizeOption != null) {
        Long thumbSizeOptionValue = null;
        try {
            thumbSizeOptionValue = new Long(thumbSizeOption);
        } catch (NumberFormatException nfe) {
            // if the supplied option value is invalid/unparseable, we
            // ignore it and fall back to the default value. 
        }
        if (thumbSizeOptionValue != null && thumbSizeOptionValue.longValue() > 0) {
            thumbSize = thumbSizeOptionValue;
        }
    }

    // it is also possible to configure the thumbnail size for a 
    // specific dataverse: 

    VDC vdc = file.getStudy().getOwner();

    if (vdc != null) {
        thumbSizeOption = System.getProperty("dvn.image.thumbnail.size." + vdc.getAlias());

        if (thumbSizeOption != null) {
            Long thumbSizeOptionValue = null;
            try {
                thumbSizeOptionValue = new Long(thumbSizeOption);
            } catch (NumberFormatException nfe) {
                // if the supplied option value is invalid/unparseable, we
                // ignore it and fall back to the default value. 
            }
            if (thumbSizeOptionValue != null && thumbSizeOptionValue.longValue() > 0) {
                thumbSize = thumbSizeOptionValue;
            }
        }
    }

    // This is the default location of the "convert" executable from the
    // ImageMagick package. If it's installed in a different locaiton, 
    // it can be configured via the dvn.image.convert.exec JVM option. 

    String imageMagickConvertExec = "/usr/bin/convert";

    String imageMagickConvertExecOption = System.getProperty("dvn.image.convrt.exec");

    if (imageMagickConvertExecOption != null) {
        if (!imageMagickConvertExecOption.trim().equals("")) {
            imageMagickConvertExec = imageMagickConvertExecOption.trim();
        }
    }

    if (new File(imageMagickConvertExec).exists()) {
        String sizeOption = " -size " + thumbSize + "x" + thumbSize + " ";

        String ImageMagickCommandLine = imageMagickConvertExec + sizeOption + fileLocation + " -resize "
                + thumbSize + " -flatten png:" + thumbFileLocation;
        int exitValue = 1;

        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(ImageMagickCommandLine);
            exitValue = process.waitFor();
        } catch (Exception e) {
            exitValue = 1;
        }

        if (exitValue == 0) {
            return true;
        }
    }

    // For whatever reason, creating the thumbnail with ImageMagick
    // has failed.
    // Let's try again, this time with Java's standard Image
    // library:

    try {
        BufferedImage fullSizeImage = ImageIO.read(new File(fileLocation));

        if (fullSizeImage == null) {
            return false;
        }

        double scaleFactor = (thumbSize.doubleValue()) / (double) fullSizeImage.getWidth(null);
        int thumbHeight = (int) (fullSizeImage.getHeight(null) * scaleFactor);

        // We are willing to spend a few extra CPU cycles to generate
        // better-looking thumbnails, hence the SCALE_SMOOTH flag. 
        // SCALE_FAST would trade quality for speed. 

        java.awt.Image thumbImage = fullSizeImage.getScaledInstance(thumbSize.intValue(), thumbHeight,
                java.awt.Image.SCALE_SMOOTH);

        ImageWriter writer = null;
        Iterator iter = ImageIO.getImageWritersByFormatName("png");
        if (iter.hasNext()) {
            writer = (ImageWriter) iter.next();
        } else {
            return false;
        }

        BufferedImage lowRes = new BufferedImage(thumbSize.intValue(), thumbHeight, BufferedImage.TYPE_INT_RGB);
        lowRes.getGraphics().drawImage(thumbImage, 0, 0, null);

        ImageOutputStream ios = ImageIO.createImageOutputStream(new File(thumbFileLocation));
        writer.setOutput(ios);

        // finally, save thumbnail image:
        writer.write(lowRes);
        writer.dispose();

        ios.close();
        thumbImage.flush();
        fullSizeImage.flush();
        lowRes.flush();
        return true;
    } catch (Exception e) {
        // something went wrong, returning "false":
        dbgLog.info("ImageIO: caught an exception while trying to generate a thumbnail for " + fileLocation);

        return false;
    }
}

From source file:marytts.tools.voiceimport.HTKLabeler.java

/**
 * Flat-start initialization for automatic labeling
 * @throws Exception/*from  w w w .  j  a  v a  2 s  . c o m*/
 */
private void herestTraining() throws Exception {

    String herest = getProp(HTKDIR) + File.separator + "HERest";
    String hhed = getProp(HTKDIR) + File.separator + "HHEd";

    File htkFile = new File(herest);
    if (!htkFile.exists()) {
        throw new RuntimeException("File " + htkFile.getAbsolutePath() + " does not exist");
    }

    String configFile = getProp(HTDIR) + File.separator + "config" + File.separator + "htkTrain.conf";
    String hhedconf = getProp(HTDIR) + File.separator + "config" + File.separator + "sil.hed";

    String hhedconf_vp = getProp(HTDIR) + File.separator + "config" + File.separator + "sil_vp.hed";

    String trainList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htkTrain.list";
    String phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone.list";

    String hmmDir = getProp(HTDIR) + File.separator + "hmm" + File.separator;
    String phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones.mlf";

    int BEST_ITERATION = MAX_ITERATIONS;
    int SP_ITERATION = -1;
    int VP_ITERATION = -1;
    int FA_ITERATION = -1;
    int change_mix_iteration = -1;
    for (int iteration = 1; iteration <= MAX_ITERATIONS; iteration++) {

        System.out.println("Iteration number: " + iteration);

        File hmmItDir = new File(hmmDir + "hmm" + iteration);
        if (!hmmItDir.exists())
            hmmItDir.mkdir();

        Runtime rtime = Runtime.getRuntime();
        //get a shell
        Process process = rtime.exec("/bin/bash");
        //get an output stream to write to the shell
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));

        if (PHASE_NUMBER == 0) {

            if (iteration == (SP_ITERATION + 1)) {

                phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones2.mlf";
                phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone2.list";

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);

                //now we enter in PHASE 1 
                PHASE_NUMBER = 1;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                continue;
            }

            // check epsilon_array  
            if (iteration > 2) {
                if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                        || iteration == MAX_SP_ITERATION) {
                    SP_ITERATION = iteration;
                    insertShortPause(iteration);
                    String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                    String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                    FileUtils.copy(oldMacro, newMacro);

                    // copy of logProbFrame_array in current iteration 
                    logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                    epsilon_array.add(100000000.0);
                    continue;
                }
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 1) {
            if (iteration == (VP_ITERATION + 1)) {
                phoneMlf = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phones3.mlf";
                phoneList = getProp(HTDIR) + File.separator + "etc" + File.separator + "htk.phone3.list";

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_vp + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_vp + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);

                //now we enter in PHASE 2
                PHASE_NUMBER = 2;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                continue;
            }

            // check epsilon_array  
            if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                    || iteration == MAX_VP_ITERATION) {
                VP_ITERATION = iteration;
                insertVirtualPauseThreeStates(iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 2) {
            if (iteration == (FA_ITERATION + 1)) {
                String logfile = "log_hviteMultiplePronunciationAligning_" + iteration + ".txt";
                String labDir = "'*'";//getProp(HTDIR)+File.separator+"lab";
                String alignedMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";
                hviteMultiplePronunciationAligning("hmm" + (iteration - 1), alignedMlf, false, labDir, true,
                        logfile, false);
                phoneMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";

                System.out.println("Copy hmm" + (iteration - 1) + " in " + "hmm" + iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);
                String oldHmmdefs = hmmDir + "hmm" + (iteration - 1) + File.separator + "hmmdefs";
                String newHmmdefs = hmmDir + "hmm" + iteration + File.separator + "hmmdefs";
                FileUtils.copy(oldHmmdefs, newHmmdefs);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                //now we enter in PHASE 3
                PHASE_NUMBER = 3;
                System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);

                continue;
            }

            // check epsilon_array  
            if (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]
                    || iteration == MAX_FA_ITERATION) {
                FA_ITERATION = iteration;

                System.out.println("Copy hmm" + (iteration - 1) + " in " + "hmm" + iteration);
                String oldMacro = hmmDir + "hmm" + (iteration - 1) + File.separator + "macros";
                String newMacro = hmmDir + "hmm" + iteration + File.separator + "macros";
                FileUtils.copy(oldMacro, newMacro);
                String oldHmmdefs = hmmDir + "hmm" + (iteration - 1) + File.separator + "hmmdefs";
                String newHmmdefs = hmmDir + "hmm" + iteration + File.separator + "hmmdefs";
                FileUtils.copy(oldHmmdefs, newHmmdefs);

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 3) {
            // check epsilon_array  
            // the following change_mix_iteration + 2 is used to allow more than one re-estimation after insertion of new mixture
            // Because just after the insertion the delta can be negative 

            if (((iteration != change_mix_iteration + 2)
                    && (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]))
                    || iteration == MAX_MIX_ITERATION) {

                System.out.println("Condition = true: " + "iteration=" + iteration + " change_mix_iteration="
                        + change_mix_iteration + " epsilon_array.get(iteration-2)="
                        + epsilon_array.get(iteration - 2) + " epsilon_PHASE[PHASE_NUMBER]="
                        + epsilon_PHASE[PHASE_NUMBER] + " MAX_MIX_ITERATION" + MAX_MIX_ITERATION);

                change_mix_iteration = iteration;
                MAX_MIX_ITERATION = -1;

                // Creating Increasing mixture config file dynamic iteration
                String hhedconf_mix = getProp(HTDIR) + File.separator + "config" + File.separator + "sil_mix_"
                        + iteration + ".hed";
                File file = new File(hhedconf_mix);
                PrintWriter hhed_conf_pw = new PrintWriter(new FileWriter(file));

                //MU 3 {*.state[2].mix}
                Boolean need_other_updates = false;
                for (int state = 0; state < num_mixtures_for_state.length; state++) {
                    if (current_number_of_mixtures[state] < num_mixtures_for_state[state]) {
                        int wanted_mix = current_number_of_mixtures[state] + 1;
                        int state_to_print = state + 2;
                        hhed_conf_pw.println("MU " + wanted_mix + "{*.state[" + state_to_print + "].mix}");

                        current_number_of_mixtures[state] = wanted_mix;

                        if (current_number_of_mixtures[state] < num_mixtures_for_state[state]) {
                            need_other_updates = true;
                        }
                    }
                }

                if (!need_other_updates) {
                    // copy of logProbFrame_array in current iteration 
                    //logProbFrame_array.add(logProbFrame_array.get(iteration-2));
                    //epsilon_array.add(100000000.0);
                    //now we enter in PHASE 3
                    PHASE_NUMBER = PHASE_NUMBER + 1;
                    System.out.println("Now we enter in PHASE:" + PHASE_NUMBER);
                    String logfile = "log_hviteMultiplePronunciationAligning_" + iteration + ".txt";
                    String labDir = "'*'";//getProp(HTDIR)+File.separator+"lab";
                    String alignedMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";
                    hviteMultiplePronunciationAligning("hmm" + (iteration - 1), alignedMlf, false, labDir, true,
                            logfile, false);
                    phoneMlf = getProp(HTDIR) + File.separator + "aligned_words.mlf";

                    //continue;
                }

                hhed_conf_pw.flush();
                hhed_conf_pw.close();

                System.out.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir
                        + "hmm" + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_mix + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.println("( cd " + getProp(HTDIR) + "; " + hhed + " " + HTK_SO + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm"
                        + (iteration - 1) + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration
                        + " " + hhedconf_mix + " " + phoneList + " >> log_herestTraining_" + iteration + ".txt"
                        + "; exit )\n");
                pw.flush();
                //shut down
                pw.close();
                process.waitFor();
                // check exit value
                if (process.exitValue() != 0) {
                    BufferedReader errorReader = new BufferedReader(
                            new InputStreamReader(process.getErrorStream()));
                    throw new MaryConfigurationException(errorReader.readLine());
                }

                // copy of logProbFrame_array in current iteration 
                logProbFrame_array.add(logProbFrame_array.get(iteration - 2));
                epsilon_array.add(100000000.0);
                continue;
            }
        }

        ///-----------------
        if (PHASE_NUMBER == 4) {
            // check epsilon_array
            if (((iteration != change_mix_iteration + 2)
                    && (epsilon_array.get(iteration - 2) < epsilon_PHASE[PHASE_NUMBER]))
                    || iteration == MAX_ITERATIONS) {
                int last = iteration - 1;
                int previus_last = iteration - 2;

                System.out.println(
                        "Average log prob per frame has not beeen increased too much respect the previus iteration:");
                System.out.println("Average log prob per frame at last HREST iteration (" + last + ")-> "
                        + logProbFrame_array.get(iteration - 2));
                System.out.println("Average log prob per frame at previus HREST iteration (" + previus_last
                        + ")-> " + logProbFrame_array.get(iteration - 3));
                System.out.println("Delta -> " + epsilon_array.get(iteration - 2));
                System.out.println("Suggested Action -> stop the iterations.");

                if (logProbFrame_array.get(iteration - 3) > logProbFrame_array.get(iteration - 2)) {
                    BEST_ITERATION = iteration - 2;
                } else {
                    BEST_ITERATION = iteration - 1;
                }
                break;
            }
        }

        //Normal HEREST:
        System.out.println("( cd " + getProp(HTDIR) + "; " + herest + " " + HTK_SO + " -C " + configFile
                + " -I " + phoneMlf + " -t 250.0 150.0 1000.0" + " -S " + trainList + " -H " + hmmDir + "hmm"
                + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm" + (iteration - 1)
                + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration + " " + phoneList
                + " >> log_herestTraining_" + iteration + ".txt" + "; exit )\n");

        pw.println("( cd " + getProp(HTDIR) + "; " + herest + " " + HTK_SO + " -C " + configFile + " -I "
                + phoneMlf + " -t 250.0 150.0 1000.0" + " -S " + trainList + " -H " + hmmDir + "hmm"
                + (iteration - 1) + File.separator + "macros" + " -H " + hmmDir + "hmm" + (iteration - 1)
                + File.separator + "hmmdefs" + " -M " + hmmDir + "hmm" + iteration + " " + phoneList
                + " >> log_herestTraining_" + iteration + ".txt" + "; exit )\n");
        pw.flush();
        //shut down
        pw.close();
        process.waitFor();
        // check exit value
        if (process.exitValue() != 0) {
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            throw new MaryConfigurationException(errorReader.readLine());
        }

        // update average_log_prob_per_frame and deltas
        check_average_log_prob_per_frame(iteration);

        System.out.println("Delta average log prob per frame to respect previus iteration-> "
                + epsilon_array.get(iteration - 1));
        System.out.println("Current PHASE: " + PHASE_NUMBER);
        System.out.println("Current state and number of mixtures (for each phoneme): "
                + Arrays.toString(current_number_of_mixtures));

        System.out.println("---------------------------------------");
    }

    System.out.println("***********\n");
    System.out.println("BEST ITERATION: " + BEST_ITERATION);
    System.out.println("COPYNING BEST ITERATION FILES IN hmm-final directory");
    System.out.println("logProbFrame_array:" + logProbFrame_array.toString());

    System.out.println("epsilon_array:" + epsilon_array.toString());

    System.out.println("***********\n");

    String oldMacro = hmmDir + "hmm" + BEST_ITERATION + File.separator + "macros";
    String newMacro = hmmDir + "hmm-final" + File.separator + "macros";
    FileUtils.copy(oldMacro, newMacro);

    String oldHmmdefs = hmmDir + "hmm" + BEST_ITERATION + File.separator + "hmmdefs";
    String newHmmdefs = hmmDir + "hmm-final" + File.separator + "hmmdefs";
    FileUtils.copy(oldHmmdefs, newHmmdefs);

}

From source file:org.smartfrog.avalanche.client.sf.apps.ca.CAServiceImpl.java

public String signCert(String certReq) throws CAException {
    String signedCert = null;//from   w  w w  .  ja v  a  2 s  .  co m

    if (null == certReq) {
        log.error("Certificate request is null.");
        return null;
    }

    if (!checkOpenssl()) {
        throw new CAException("Cannot find openssl...");
    }

    TxtFileHelper txt = new TxtFileHelper(confFile);
    Runtime rt = Runtime.getRuntime();

    String reqFileName = "certReq.pem";
    String[] names = reqFileName.split("\\.");
    File reqFile = null;
    try {
        reqFile = File.createTempFile(names[0], "." + names[1]);
        if (!FileUtils.checkFile(reqFile)) {
            log.error("Cannot sign certificate");
            return null;
        }
        reqFile.deleteOnExit();
    } catch (IOException ioe) {
        log.error(ioe);
        throw new CAException(ioe);
    }

    if (!FileUtils.writeString2File(certReq, reqFile)) {
        log.error("Cannot sign certificate");
        return null;
    }

    String reqFilePath = reqFile.getAbsolutePath();
    String outDir = null;
    String dirName = null;
    try {
        outDir = txt.getValue(CAConstants.newCertsDir, CAConstants.separator, CAConstants.comment);
        if (null == outDir) {
            log.error("Value for " + CAConstants.newCertsDir + " is not provided " + "in the config file");
            return null;
        }
        dirName = txt.getValue(CAConstants.dir, CAConstants.separator, CAConstants.comment);
        if (null == dirName) {
            log.error("Value for " + CAConstants.dir + " is not provided " + "in the config file");
            return null;
        }
        log.info("Dir : " + dirName);
        outDir = outDir.replaceAll(CAConstants.variableDecl + CAConstants.dir, dirName);
        log.info("outDir : " + outDir);
        File file = new File(outDir);
        if ((!FileUtils.checkDir(file))) {
            log.error("Cannot sign certificate");
            return null;
        }
    } catch (FileNotFoundException fnfe) {
        log.error(fnfe);
        throw new CAException(fnfe);
    } catch (IOException ioe) {
        log.error(ioe);
        throw new CAException(ioe);
    }

    String signedCertificate = new String(outDir + File.separatorChar + CAConstants.signedCert);
    String cmd = opensslDir + File.separatorChar + "bin" + File.separatorChar + "openssl";

    /*
     * /usr/local/grit/openssl/bin/openssl ca -batch -in ./usercert_request.pem 
     *          -passin pass:sandya -out /usr/local/grit/ca/newcerts/signed.pem 
     * */
    cmd = cmd + " ca -config " + confFile + " -batch -in " + reqFilePath + " -passin pass:" + passPhrase
            + " -out " + signedCertificate;

    Process p;
    BufferedReader cmdError = null;
    int exitVal = 0;
    try {
        p = rt.exec(cmd);
        cmdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        exitVal = p.waitFor();
        if (exitVal != 0) {
            log.error("Error in signing certificate...");
            String line = null;
            String error = null;
            if ((line = cmdError.readLine()) != null) {
                log.error(line);
                error = line;
                while ((line = cmdError.readLine()) != null) {
                    log.error(line);
                    error = error + "\n" + line;
                }
                throw new CAException(error);
            }
        }
    } catch (IOException ioe) {
        log.error(ioe);
        throw new CAException(ioe);
    } catch (InterruptedException ie) {
        log.error(ie);
        throw new CAException(ie);
    }

    File signedCertFile = new File(signedCertificate);
    if ((!signedCertFile.exists()) && (!signedCertFile.isFile())) {
        log.error(signedCertificate + " does not exist or is not a file");
        return null;
    }
    if (!signedCertFile.canRead()) {
        log.error(signedCertificate + " does not have read permissions");
        return null;
    }

    try {
        signedCert = FileUtils.file2String(signedCertFile);
    } catch (FileNotFoundException fnfe) {
        log.error(fnfe);
        throw new CAException(fnfe);
    } catch (IOException ioe) {
        log.error(ioe);
        throw new CAException(ioe);
    }
    return signedCert;
}