package maryclient.com.craig.tts; import marytts.datatypes.MaryDataType; import marytts.modules.synthesis.Voice; import marytts.server.Mary; import marytts.server.Request; import marytts.util.MaryUtils; import marytts.util.data.audio.MaryAudioUtils; import sun.audio.AudioPlayer; import sun.audio.AudioStream; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import java.applet.AudioClip; import java.io.*; import java.util.Locale; /** * Created by IntelliJ IDEA. * Copywright by MapQuest Dec 29, 2009 * User: cliftoncraig07 * Date: Dec 29, 2009 * Time: 9:35:26 PM */ public class MQMaryClient { public static void main(String[] args) { System.setProperty("mary.base", "/Users/cliftoncraig07/marytmp"); System.setProperty("ignore.mbrola.config", "true"); System.setProperty("ignore.mbrola-us1.config", "true"); System.setProperty("ignore.mbrola-us3.config", "true"); PipedInputStream audioStream = new PipedInputStream(); final PipedOutputStream audioOutputStream; try { audioOutputStream = new PipedOutputStream(audioStream); } catch (IOException e) { e.printStackTrace(); System.exit(-1); return; } playAudio((InputStream)audioStream); try { marytts.server.Mary.startup(); /*marytts.server.Mary.*/ process("Speech synthesis is a wonderful thing, isn't it?", marytts.server.MaryProperties.getProperty("input.type", "TEXT"), marytts.server.MaryProperties.getProperty("output.type", "AUDIO"), marytts.server.MaryProperties.getProperty("locale", "en_US"), marytts.server.MaryProperties.getProperty("audio.type", "WAVE"), marytts.server.MaryProperties.getProperty("voice", null), marytts.server.MaryProperties.getProperty("style", null), marytts.server.MaryProperties.getProperty("effect", null), marytts.server.MaryProperties.getProperty("output.type.params", null), audioOutputStream); audioOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } } private static void playAudio(final InputStream inputStream) { Runnable runnable = new Runnable() { public void run() { try { AudioPlayer.player.start(new AudioStream(inputStream)); } catch (IOException e) { e.printStackTrace(); } } }; new Thread(runnable).start(); } public static void process(String input, String inputTypeName, String outputTypeName, String localeString, String audioTypeName, String voiceName, String style, String effects, String outputTypeParams, OutputStream output) throws Exception { if (Mary.currentState() != Mary.STATE_RUNNING) throw new IllegalStateException("MARY system is not running"); MaryDataType inputType = MaryDataType.get(inputTypeName); MaryDataType outputType = MaryDataType.get(outputTypeName); Locale locale = MaryUtils.string2locale(localeString); Voice voice = null; if (voiceName != null) voice = Voice.getVoice(voiceName); AudioFileFormat audioFileFormat = null; AudioFileFormat.Type audioType = null; if (audioTypeName != null) { audioType = MaryAudioUtils.getAudioFileFormatType(audioTypeName); AudioFormat audioFormat = null; if (audioTypeName.equals("MP3")) { audioFormat = MaryAudioUtils.getMP3AudioFormat(); } else if (audioTypeName.equals("Vorbis")) { audioFormat = MaryAudioUtils.getOggAudioFormat(); } else if (voice != null) { audioFormat = voice.dbAudioFormat(); } else { audioFormat = Voice.AF16000BE; } audioFileFormat = new AudioFileFormat(audioType, audioFormat, AudioSystem.NOT_SPECIFIED); } Request request = new Request(inputType, outputType, locale, voice, effects, style, 1, audioFileFormat, false, outputTypeParams); request.readInputData(new StringReader(input)); request.process(); request.writeOutputData(output); } }