import com.mapquest.util.io.StreamReader; 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 javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import java.io.*; import java.net.URL; import java.util.Enumeration; import java.util.Locale; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; /** * Created by IntelliJ IDEA. * Copywright by MapQuest Dec 29, 2009 * User: cliftoncraig07 * Date: Dec 29, 2009 * Time: 9:35:26 PM */ public class MyMaryClient { static { URL markerFileURL = MQMaryClient.class.getResource("/marydefaultspkg"); if(null== markerFileURL) throw new RuntimeException("MaryDefaults not found. Please set mary.base or include a valid marydefaults environment."); File defaultPkgJarFile = new File(markerFileURL.getFile().substring(markerFileURL.getFile().indexOf(':') + 1, markerFileURL.getFile().indexOf('!'))); File maryBaseDir = new File(System.getProperty("java.io.tmpdir"), "marydefaults"); expandZipTo(defaultPkgJarFile, maryBaseDir.toString()); System.out.println("Setting mary.base to " + maryBaseDir); System.setProperty("mary.base", maryBaseDir.getAbsolutePath()); System.setProperty("ignore.mbrola.config", "true"); System.setProperty("ignore.mbrola-us1.config", "true"); System.setProperty("ignore.mbrola-us3.config", "true"); try { Mary.startup(); } catch (Exception e) { throw new RuntimeException(e); } } public static String description() { return "MaryTTS Version 4.0 client"; } private static void expandZipTo(File zipFile, String path) { File dir = new File(path); if(! dir.exists() && ! dir.mkdirs()) throw new RuntimeException(new IOException("Could not create destination path " + path)); try { ZipFile zip = new ZipFile(zipFile); for(Enumerationentries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); File deflated = new File(dir, entry.getName()); if(entry.isDirectory()) { System.out.println("Creating folder " + deflated); deflated.mkdirs(); continue; } System.out.println("Deflating " + deflated); InputStream entryData = zip.getInputStream(entry); StreamReader reader = new StreamReader(1024); FileOutputStream destination = new FileOutputStream(deflated); reader.transferStream(entryData, destination); destination.close(); entryData.close(); } } catch (IOException e) { throw new RuntimeException(e); } } 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); } }