Main Page | Packages | Class List | File List | Class Members

SpeechSynthesizer.java

Go to the documentation of this file.
00001 /*************************************************************************
00002  *                                                                       *
00003  * Voce                                                                  *
00004  * Copyright (C) 2005                                                    *
00005  * Tyler Streeter  tylerstreeter@gmail.com                               *
00006  * All rights reserved.                                                  *
00007  * Web: voce.sourceforge.net                                             *
00008  *                                                                       *
00009  * This library is free software; you can redistribute it and/or         *
00010  * modify it under the terms of EITHER:                                  *
00011  *   (1) The GNU Lesser General Public License as published by the Free  *
00012  *       Software Foundation; either version 2.1 of the License, or (at  *
00013  *       your option) any later version. The text of the GNU Lesser      *
00014  *       General Public License is included with this library in the     *
00015  *       file license-LGPL.txt.                                          *
00016  *   (2) The BSD-style license that is included with this library in     *
00017  *       the file license-BSD.txt.                                       *
00018  *                                                                       *
00019  * This library is distributed in the hope that it will be useful,       *
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00022  * license-LGPL.txt and license-BSD.txt for more details.                *
00023  *                                                                       *
00024  *************************************************************************/ 
00025 
00026 package voce;
00027 
00028 import java.io.File;
00029 import java.util.Locale;
00030 import java.beans.PropertyChangeEvent;
00031 import java.beans.PropertyChangeListener;
00032 
00033 import javax.speech.Central;
00034 import javax.speech.Engine;
00035 import javax.speech.EngineList;
00036 import javax.speech.EngineCreate;
00037 import javax.speech.EngineException;
00038 import javax.speech.EngineModeDesc;
00039 import javax.speech.synthesis.Synthesizer;
00040 import javax.speech.synthesis.SynthesizerModeDesc;
00041 import javax.speech.synthesis.SynthesizerProperties;
00042 import javax.speech.synthesis.Voice;
00043 import javax.speech.synthesis.Speakable;
00044 import javax.speech.synthesis.SpeakableAdapter;
00045 import javax.speech.synthesis.SpeakableEvent;
00046 import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral; 
00047 
00050 public class SpeechSynthesizer
00051 {
00053         private Synthesizer mSynthesizer = null;
00054 
00056         public SpeechSynthesizer(String name)
00057         {
00058                 // Create a default voice.
00059                 Voice theVoice = new Voice(name, 
00060                         Voice.GENDER_DONT_CARE, Voice.AGE_DONT_CARE, null);
00061 
00062                 try
00063                 {               
00064                         // Create the synthesizer with the general domain voice.
00065                         SynthesizerModeDesc generalDesc = new SynthesizerModeDesc(
00066                                 null,                   // engine name
00067                                 "general",              // mode name
00068                                 Locale.US,              // locale
00069                                 null,                   // running
00070                                 null);                  // voice
00071 
00072                         // Avoid using the JSAPI Central class (and the use of the speech.properties 
00073                         // file) by using FreeTTSEngineCentral directly.
00074                         FreeTTSEngineCentral central = new FreeTTSEngineCentral();
00075                         EngineList list = central.createEngineList(generalDesc); 
00076 
00077                         if (list.size() > 0)
00078                         { 
00079                                 EngineCreate creator = (EngineCreate)list.get(0);
00080                                 mSynthesizer = (Synthesizer)creator.createEngine();
00081                         }
00082 
00083                         if (null  == mSynthesizer)
00084                         {
00085                                 Utils.log("ERROR", "Cannot create speech synthesizer");
00086                                 System.exit(1);
00087                         }
00088 
00089                         mSynthesizer.allocate();
00090 
00091                         // Setup the general domain synthesizer.
00092                         mSynthesizer.getSynthesizerProperties().setVoice(theVoice);
00093 
00094                         mSynthesizer.resume();
00095 
00096                         // Force the synthesizer to create its thread now by making 
00097                         // it synthesize something.  Otherwise, the first synthesize 
00098                         // request in a user's app could be delayed.
00099                         synthesize(" ");
00100                 }
00101                 catch (Exception e)
00102                 {
00103                         e.printStackTrace();
00104                 }
00105         }
00106         
00108         public void destroy()
00109         {
00110                 mSynthesizer.cancelAll();
00111                 
00112                 try
00113                 {               
00114                         mSynthesizer.deallocate();
00115                 }
00116                 catch (Exception e)
00117                 {
00118                         e.printStackTrace();
00119                 }
00120         }
00121 
00124         public void synthesize(String message)
00125         {
00126                 //Utils.log("debug", "SpeechSynthesizer.speak: Adding message to speech queue: " + message);
00127         
00128                 // Note that the Synthesize class maintains its own internal queue.
00129                 mSynthesizer.speakPlainText(message, null);
00130         }
00131 
00134         public void stopSynthesizing()
00135         {
00136                 mSynthesizer.cancelAll();
00137         }
00138 }

Generated on Wed Aug 3 16:25:01 2005 for Voce Java API by  doxygen 1.4.1