/** * Copyright 2003 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */ import java.io.*; import java.util.Locale; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.speech.Central; import javax.speech.Engine; import javax.speech.EngineException; import javax.speech.synthesis.Synthesizer; import javax.speech.synthesis.SynthesizerModeDesc; import javax.speech.synthesis.SynthesizerProperties; import javax.speech.synthesis.Voice; import javax.speech.synthesis.Speakable; import javax.speech.synthesis.SpeakableAdapter; import javax.speech.synthesis.SpeakableEvent; /** * Uses multiple voices to read a script, optionally leaving out one part * by lowering the volume so one has time to practice one's lines */ public class TheaterPractice { public static Synthesizer synthesizer = null; public static String name = null; public static String practicer = null; public static float savedVolume, savedPitch; public static String[][] performers = { {"NORMAN", "kevin16", "100"}, {"MARTHA", "kevin16", "200"}, {"JUNE", "mbrola_us1", "100"}, }; private final static void usage() { System.out.println("TheaterPractice"); } /** * Returns a "no synthesizer" message, and asks * the user to check if the "speech.properties" file is * at user.home or java.home/lib. * * @return a no synthesizer message */ static private String noSynthesizerMessage(String synthesizer) { String message = "Cannot find " + synthesizer + ".\n" + "This may be the result of any number of problems. It's\n" + "typically due to a missing \"speech.properties\" file that\n" + "should be at either of these locations: \n\n"; message += "user.home : " + System.getProperty("user.home") + "\n"; message += "java.home/lib: " + System.getProperty("java.home") + File.separator + "lib\n\n" + "Another cause of this problem might be corrupt or missing\n" + "voice jar files in the freetts lib directory. This problem\n" + "also sometimes arises when the freetts.jar file is corrupt\n" + "or missing. Sorry about that. Please check for these\n" + "various conditions and then try again.\n"; return message; } public static void speak(String name, String lines) throws Exception { int i, colon; float pitch, volume; Voice voice; if (savedVolume == 0) { // save default pitch and volume savedVolume = synthesizer.getSynthesizerProperties().getVolume(); savedPitch = synthesizer.getSynthesizerProperties().getPitch(); } pitch = savedPitch; volume = savedVolume; for (i = 0; i < performers.length; i++) { if (performers[i][0].equals(name)) { voice = new Voice(performers[i][1], Voice.GENDER_DONT_CARE, Voice.AGE_DONT_CARE, null); pitch = Integer.parseInt(performers[i][2]); if (name.equals(practicer)) volume = 0.5f; synthesizer.getSynthesizerProperties().setVoice(voice); synthesizer.getSynthesizerProperties().setPitch(pitch); synthesizer.getSynthesizerProperties().setVolume(volume); } } if (name != null && lines.length() > 0) { synthesizer.waitEngineState(Synthesizer.ALLOCATED); synthesizer.resume(); synthesizer.speakPlainText(lines, null); synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY); synthesizer.getSynthesizerProperties().setPitch(savedPitch); synthesizer.getSynthesizerProperties().setVolume(savedVolume); } } public static void main(String[] argv) throws Exception { String line, lines; int i, colon; Voice voice; System.out.println(" ** Theater Practice **"); try { SynthesizerModeDesc generalDesc = new SynthesizerModeDesc( null, // engine name "general", // mode name Locale.US, // locale null, // running null); // voice synthesizer = Central.createSynthesizer(generalDesc); if (synthesizer == null) { System.err.println( noSynthesizerMessage("general domain synthesizer")); System.exit(1); } System.out.print(" Allocating synthesizers..."); synthesizer.allocate(); /* get general domain synthesizer ready to speak */ System.out.println("And here we go!"); synthesizer.resume(); BufferedReader script = new BufferedReader( new FileReader(argv[0])); if (argv.length > 1) { practicer = argv[1]; } lines = ""; while ((line = script.readLine()) != null) { // make sure a blank line follows last line of script colon = line.indexOf(":"); if (colon > 0 && colon < line.indexOf(" ")) { name = line.substring(0, colon); System.err.print(name + ": "); lines = line.substring(colon + 1).trim() + " "; } else if (line.equals("")) { System.out.println(lines); speak(name, lines); name = null; lines = ""; } else { // otherwise, just add to current dialogue lines += line.trim() + " "; } } // clean up synthesizer.deallocate(); } catch (Exception e) { e.printStackTrace(); } System.exit(0); } }