/**
   * Converts a given text file into a audio file of WAVE format.
   *
   * @param inputFile The input file path. Cannot be null or empty.
   * @throws Exception
   */
  public void convertText2Audio(String inputFile) throws Exception {
    // Verify the file name
    if (StringUtils.isEmpty(inputFile)) {
      throw new IllegalArgumentException("Invalid file path");
    }

    LOGGER.debug("Starting conversion of file : " + inputFile);

    // Timing the conversion process
    long startTime = System.nanoTime();

    // TODO: This is very memory inefficient.
    String inputText = FileUtils.readFileToString(new File(inputFile));

    // Text to Audio format conversion
    ByteArrayOutputStream audioOutputStream = new ByteArrayOutputStream();
    MaryClient mary = MaryClient.getMaryClient(new Address(maryHostname, maryPort));
    mary.process(inputText, inputType, outputType, locale, audioType, voiceName, audioOutputStream);

    // The byte array constitutes a full wave file, including the header. Write the byte array to
    // file.
    FileOutputStream fileOut = new FileOutputStream(getOutputFile(inputFile));
    audioOutputStream.writeTo(fileOut);
    audioOutputStream.flush();
    fileOut.close();

    // Timing purpose
    long endTime = System.nanoTime();
    long duration = (endTime - startTime);
    LOGGER.info(
        "Conversion time for file : " + inputFile + " took " + duration / 1000000000.0 + " secs");
  }
 /** {@inheritDoc} It creates the MaryClient and starts the synthesisQueue thread. */
 @Override
 public void connect(final ConnectionInformation info) throws IOException {
   processor = MaryClient.getMaryClient();
   synthesisQueue = new SynthesisQueue(this);
   synthesisQueue.addListener(this);
   synthesisQueue.setProcessor(processor);
   synthesisQueue.setRequestParameters(maryRequestParameters);
   synthesisQueue.start();
 }
 public MaryClient getMaryClient() throws IOException {
   if (mary == null) {
     try {
       mary =
           MaryClient.getMaryClient(
               new Address(getProp(MARYSERVERHOST), Integer.parseInt(getProp(MARYSERVERPORT))));
     } catch (IOException e) {
       throw new IOException(
           "Could not connect to Maryserver at "
               + getProp(MARYSERVERHOST)
               + " "
               + getProp(MARYSERVERPORT));
     }
   }
   return mary;
 }
  public void computeFeaturesFor(String basename) throws IOException, Exception {
    String text;
    Locale localVoice;
    localVoice = MaryUtils.string2locale(locale);

    // First, test if there is a corresponding .rawmaryxml file in textdir:
    File rawmaryxmlFile =
        new File(db.getProp(db.MARYXMLDIR) + basename + db.getProp(db.MARYXMLEXT));
    if (rawmaryxmlFile.exists()) {
      text = FileUtils.getFileAsString(rawmaryxmlFile, "UTF-8");
    } else {
      text =
          getMaryXMLHeaderWithInitialBoundary(locale)
              + FileUtils.getFileAsString(
                  new File(db.getProp(db.TEXTDIR) + basename + db.getProp(db.TEXTEXT)), "UTF-8")
              + "</maryxml>";
    }
    File pfeatFile = new File(unitfeatureDir, basename + featsExt);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(pfeatFile));
    MaryClient maryClient = getMaryClient();
    /*Vector voices = maryClient.getVoices(localVoice);
    MaryClient.Voice defaultVoice = (MaryClient.Voice) voices.firstElement();
    String voiceName = defaultVoice.name();*/
    // maryClient.process(text, maryInputType, maryOutputType, null, null, os);

    maryClient.process(text, maryInputType, maryOutputType, locale, null, "slt-arctic", os);
    // maryClient.process(text, maryInputType, maryOutputType, null, "slt-arctic", os, timeout);
    // maryClient.getOutputDataTypes().size()
    // MaryData result = new MaryData(os);

    os.flush();
    os.close();

    // System.out.println(" TO STRING: "+new FileReader(pfeatFile).toString());
    // BufferedReader bfr = new BufferedReader(new FileReader(pfeatFile));
    String line;
    MaryData d = new MaryData(MaryDataType.get("PHONEMISED_EN"), Locale.US);
    // d.readFrom(new ByteArrayInputStream(os.toByteArray()));
    d.readFrom(new FileReader(pfeatFile));

    // MaryData d = new MaryData(pfeatFile);
    Document doc = d.getDocument();
    // Document acoustparams = d.getDocument();

    // NodeIterator it = ((DocumentTraversal)acoustparams).createNodeIterator(acoustparams,
    // NodeFilter.SHOW_ELEMENT,new NameNodeFilter(new String[]{MaryXML.TOKEN,
    // MaryXML.BOUNDARY}),false);
    NodeIterator it =
        ((DocumentTraversal) doc)
            .createNodeIterator(
                doc, NodeFilter.SHOW_ELEMENT, new NameNodeFilter(MaryXML.TOKEN), false);

    Element t = null;
    while ((t = (Element) it.nextNode()) != null) {
      if (t.hasAttribute("g2p_method")) {
        String g2p = t.getAttribute("g2p_method");
        String nodeText = t.getTextContent().trim();
        if (g2p.equals("rules")) { // && nodeText.equals("!")){
          System.out.print(basename + " ----> " + nodeText);
          if (bnl.contains(basename)) bnl.remove(basename);
          System.out.println(" SO removing basename: " + basename);
        }

        // System.out.println("G2P:"+t.getAttribute("g2p_method"));
        // System.out.println("Text:"+t.getTextContent());
      }
    }

    /*while((line =bfr.readLine()) != null){
        //boolean b = m.matches();
        if(Pattern.matches("rules", line))
                System.out.println(basename + " LINE ---> " + line);

    }*/
    // System.out.println(" TO STRING: "+line);

  }