View Javadoc

1   package org.wcb.plugins.speech;
2   
3   import java.util.Vector;
4   import java.util.Locale;
5   
6   import org.wcb.plugins.speech.Talker;
7   import org.wcb.plugins.speech.Translator;
8   import com.sun.speech.freetts.util.Utilities;
9   import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral;
10  
11  import javax.speech.synthesis.SynthesizerModeDesc;
12  import javax.speech.synthesis.Synthesizer;
13  import javax.speech.EngineList;
14  import javax.speech.EngineCreate;
15  
16  
17  
18  /***
19   *The Speak plugin sort of determines that it is available to the system
20   *and if that is the case it will go ahead and allow speach to take place.
21   *When the program sends a command to the x10 module through the x10 interface
22   *certain words in the text will be anounceated such as ON A 1.  This is more
23   *or less a verbal que.
24   *
25   *  @author wbogaardt
26   *  @version $version
27   */
28  public class SpeakPlugin {
29  
30      private Talker talker;
31      private Translator converter;
32      private Synthesizer synthesizer;
33  
34      public SpeakPlugin(){
35          createSynthesizer();
36      }
37  
38      /***
39       * Creates a FreeTTS synthesizer.
40       */
41      public void createSynthesizers()
42      {
43          try {
44              String synthesizerName = Utilities.getProperty("synthesizerName","Unlimited domain FreeTTS Speech Synthesizer from Sun Labs");
45              // Create a new SynthesizerModeDesc that will match the FreeTTS
46              // Synthesizer.
47              SynthesizerModeDesc desc = new SynthesizerModeDesc(synthesizerName, null, Locale.US,
48                      Boolean.FALSE,         // running?
49                      null);                 // voice
50  
51              AliceSynthesizerModeDesc myModeDesc = new AliceSynthesizerModeDesc(desc);
52              synthesizer = myModeDesc.getSynthesizer();
53              if (synthesizer == null)
54              {
55                  synthesizer = myModeDesc.createSynthesizer();
56              }
57              if (!myModeDesc.isSynthesizerLoaded())
58              {
59                  myModeDesc.loadSynthesizer();
60              }
61          }
62          catch (Exception e)
63          {
64              System.err.println("Unable to create synthesizer");
65          }
66      }
67  
68      /***
69       * Creates a freeTTS synthesizer speech engine without the use
70       * of properties files.
71       */
72      public void createSynthesizer() {
73          try {
74  // Create a new SynthesizerModeDesc that will match the FreeTTS
75  // Synthesizer.
76              SynthesizerModeDesc desc = new SynthesizerModeDesc
77                      ("Unlimited domain FreeTTS Speech Synthesizer from Sun Labs",
78                              null,
79                              Locale.US,
80                              Boolean.FALSE, // running?
81                              null); // voice
82  
83              FreeTTSEngineCentral central = new FreeTTSEngineCentral();
84              synthesizer = null;
85  
86              EngineList list = central.createEngineList(desc);
87  
88              if (list.size() > 0) {
89                  EngineCreate creator = (EngineCreate) list.get(0);
90                  synthesizer = (Synthesizer) creator.createEngine();
91              }
92  
93              if (synthesizer == null) {
94                  System.err.println("Can't find synthesizer");
95                  System.exit(1);
96              }
97  
98  // get it ready to speak
99              synthesizer.allocate();
100             synthesizer.resume();
101         }
102         catch (Exception e) {
103             e.printStackTrace();
104         }
105     }
106 
107     /***
108      * Performs text-to-speech on the given text.
109      *
110      * @param text the text to perform TTS
111      */
112     public void play(String text)
113     {
114         if(synthesizer !=null)
115         {
116             synthesizer.speakPlainText(text, null);
117         }
118         else
119         {
120             System.err.println("Speech did not initialize.");
121         }
122 
123     }
124 
125     /****** used in older home grown implementation **********/
126 
127     /***
128      *This loads the classes for the speach plugin.
129      *Actually this is a simple part in that it just
130      *says what commands it has run.  Take a look at
131      *Javaworlds article on <a href="http://www.javaworld.com/javaworld/jw-08-2001/jw-0817-javatalk_p.html">http://www.javaworld.com/javaworld/jw-08-2001/jw-0817-javatalk_p.html</a>
132      *this subject.
133      * @deprecated
134      */
135     public boolean load(){
136         try{
137             converter = new Translator();           
138             return true;
139         }catch(NoClassDefFoundError ncdfe){
140             return false;
141         }
142     }
143 
144 
145     /***
146      *This simple method simply has the words converted
147      *to an allaphone and then they are spoken.  This is a plugin
148      *and so won't be executed unless the user has the neccessary jar
149      *file.
150      *The string to this file is
151      * @deprecated
152      */
153     private void speakMessage(String message){
154         Vector words = converter.getWords(message);
155         String wordtosay;
156         for(int i=0; i<words.size();i++){
157             wordtosay = (String)words.elementAt(i);
158             talker.sayPhoneWord(converter.getPhoneWord(wordtosay));
159 
160         }
161     }
162 
163     /***
164      *This simple method simply has the words converted
165      *to an allaphone and then they are spoken.  This is a plugin
166      *and so won't be executed unless the user has the neccessary jar
167      *file.
168      *The string to this file is
169      * @deprecated
170      */
171     private void speakCommands(String message){
172         Vector words = converter.getWords(message);
173         String wordtosay;
174         for(int i=0; i<words.size();i++){
175             wordtosay = (String)words.elementAt(i);
176             talker.sayPhoneWord(wordtosay);
177         }
178     }
179 }