DefaultSapiTTS.java :  » XML » alttext » nl_dutchear_speechgen » external » win » Java Open Source

Java Open Source » XML » alttext 
alttext » nl_dutchear_speechgen » external » win » DefaultSapiTTS.java
/*
 * DMFC - The DAISY Multi Format Converter
 * Copyright (C) 2005  Daisy Consortium
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package nl_dutchear_speechgen.external.win;

import java.io.File;
import java.io.IOException;
import java.util.Map;


import nl_dutchear_speechgen.external.win.sapi5.ClassFactory;
import nl_dutchear_speechgen.external.win.sapi5.ISpeechFileStream;

import nl_dutchear_speechgen.external.win.sapi5.ISpeechVoice;
import nl_dutchear_speechgen.external.win.sapi5.SpeechStreamFileMode;
import nl_dutchear_speechgen.external.win.sapi5.SpeechVoiceSpeakFlags;
import nl_dutchear_speechgen.tts.TTSException;
import nl_dutchear_speechgen.tts.adapters.AbstractTTSAdapter; 
import nl_dutchear_speechgen.tts.util.TTSUtils;

/**
 * @author Romain Deltour
 * 
 */
public class DefaultSapiTTS extends AbstractTTSAdapter {

  ISpeechVoice voice;
  String sapiVoiceSelection;

  public DefaultSapiTTS(TTSUtils ttsUtils, Map<String, String> params) {
    super(ttsUtils, params);
    voice = ClassFactory.createSpVoice();
    sapiVoiceSelection = mParams.get("sapiVoiceSelection");
  }

  @Override
  public void read(String line, File destination) throws IOException, TTSException {
    ISpeechFileStream stream = null;
    try {
      stream = ClassFactory.createSpFileStream();
      stream.open(destination.getAbsolutePath(),
          SpeechStreamFileMode.SSFMCreateForWrite, false);
      voice.audioOutputStream(stream);
      if (sapiVoiceSelection != null) {
        line = "<voice optional=\"" + sapiVoiceSelection + "\">" + line
            + "</voice>";
      }
      voice.speak(line, SpeechVoiceSpeakFlags.SVSFIsXML);
      voice.waitUntilDone(-1);
    } catch (Exception e) {
      throw new TTSException(e.getMessage(), e);
    } finally {
      if (stream != null) {
        stream.close();
        stream.dispose();
      }
    }
  }

  @Override
  public void close() throws IOException, TTSException {
    if (voice != null) {
      voice.dispose();
    }
  }
  

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.