View Javadoc

1   package org.wcb.plugins.speech;
2   
3   import javax.speech.synthesis.SynthesizerModeDesc;
4   import javax.speech.synthesis.Synthesizer;
5   import javax.speech.Engine;
6   import javax.speech.Central;
7   
8   /***
9   * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
22  *
23  *   Project:   Home Automation Interface
24  *  Filename:  $Id: AliceSynthesizerModeDesc.java,v 1.5 2004/02/27 01:29:53 wbogaardt Exp $
25  *  Abstract:  Web bean for x10 jsp.
26  *
27  * $Log: AliceSynthesizerModeDesc.java,v $
28  * Revision 1.5  2004/02/27 01:29:53  wbogaardt
29  * modified classes so they conform to Checkstyle format in readability
30  *
31  * Revision 1.4  2003/12/18 17:35:53  wbogaardt
32  * fixed speech engine issue so that the application does not lock up but will notify user of the need to
33   * install speech.properties file in their home directory.
34  *
35  * Revision 1.3  2003/12/13 05:36:52  wbogaardt
36  * fixed javadoc comments.
37  *
38  * Revision 1.2  2003/12/11 23:10:12  wbogaardt
39  * cleaned up exception handeling and logging of system.out messages
40  *
41  * Revision 1.1  2003/12/11 21:58:42  wbogaardt
42  * added speach synthesizer using java speech api
43  *
44   *
45   * A SynthesizerModeDesc that implements the <code>toString()</code>
46   * method so that it returns the name of the synthesizer.
47   */
48  public class AliceSynthesizerModeDesc extends SynthesizerModeDesc {
49  
50      private Synthesizer synthesizer = null;
51      private boolean synthesizerLoaded = false;
52  
53  
54      /***
55       * Constructs a MySynthesizerModeDesc with the attributes from
56       * the given SynthesizerModeDesc.
57       *
58       * @param modeDesc the SynthesizerModeDesc to get attributes from
59       */
60      public AliceSynthesizerModeDesc(SynthesizerModeDesc modeDesc)
61      {
62          super(modeDesc.getEngineName(), modeDesc.getModeName(),
63                  modeDesc.getLocale(), modeDesc.getRunning(),
64                  modeDesc.getVoices());
65      }
66  
67  
68      /***
69       * Returns true if the synthesizer is already loaded.
70       *
71       * @return true if the synthesizer is already loaded
72       */
73      public synchronized boolean isSynthesizerLoaded()
74      {
75          if (synthesizer == null)
76          {
77              return false;
78          }
79          return ((synthesizer.getEngineState() & Engine.ALLOCATED) != 0);
80      }
81  
82  
83      /***
84       * Returns a Synthesizer that fits the description of this
85       * MySynthesizerModeDesc. If the synthesize was never loaded,
86       * it is loaded in a separate thread.
87       *
88       * @return a Synthesizer
89       */
90      public synchronized Synthesizer getSynthesizer()
91      {
92          debugPrint("Found Speech engine: " + getEngineName());
93          return synthesizer;
94      }
95  
96  
97      /***
98       * Creates the Synthesizer and its Monitor.
99       *
100      * @return the created Synthesizer
101      */
102     public Synthesizer createSynthesizer()
103     {
104         try
105         {
106             debugPrint("Creating " + getEngineName() + "...");
107             synthesizer = Central.createSynthesizer(this);
108 
109             if (synthesizer == null)
110             {
111                 debugPrint("Central created null synthesizer");
112                 debugPrint("Check that speech.properties is installed at " + System.getProperty("user.home"));
113             }
114             else
115             {
116                 synthesizer.allocate();
117                 synthesizer.resume();
118                 debugPrint("...created monitor");
119             }
120         }
121         catch (Exception e)
122         {
123             e.printStackTrace();
124         }
125         finally
126         {
127             return synthesizer;
128         }
129     }
130 
131 
132 
133     /***
134      * Allocates the synthesizer if it has never been allocated. This
135      * method should be called after method <code>createSynthesizer()</code>.
136      * It spawns a new thread to allocate the synthesizer.
137      * @return the Synthesizer to allow speaking.
138      */
139     public Synthesizer loadSynthesizer()
140     {
141         try
142         {
143             if (!synthesizerLoaded)
144             {
145                 debugPrint("Loading " + getEngineName() + "...");
146                 synthesizerLoaded = true;
147                 SynthesizerLoader loader = new SynthesizerLoader(synthesizer);
148                 loader.start();
149             }
150         }
151         catch (Exception e)
152         {
153             e.printStackTrace();
154         }
155         finally
156         {
157             return synthesizer;
158         }
159     }
160 
161     /***
162      * Returns the name of the Synthesizer.
163      *
164      * @return the name of the Synthesizer
165      */
166     public String toString()
167     {
168         return getEngineName();
169     }
170 
171 
172     /***
173      * Prints debug statements.
174      *
175      * @param statement debug statements
176      */
177     private void debugPrint(String statement)
178     {
179         System.out.println(statement);
180     }
181 }
182