Share this page 

Use the Java Speech API (JSPAPI)Tag(s): IO


The Java Speech API (JSAPI) is not part of the JDK and Sun does not ship an implementation of JSAPI. Sun porivides the specification and third-parties provide the implementations.

The most popular implementation is the open-source TTS (Text-To-Speech) package, http://freetts.sourceforge.net/

You download the required jars here : http://sourceforge.net/projects/freetts/files/

The configuration is done in 2 steps.

  • speech.properties contains the package to provide the JSAPI implementation. Typically, the file is located in the JRE\lib directory. In this example, I register the TTS package directly.
  • You specify the available voices. You have many different ways to do so, see the javadoc at http://freetts.sourceforge.net/javadoc/com/sun/speech/freetts/VoiceManager.html#getVoices(). In this example, the available voice is setted with the system property "freetts.voices".

    import java.beans.PropertyVetoException;
    import java.util.Locale;
    
    import javax.speech.AudioException;
    import javax.speech.Central;
    import javax.speech.EngineException;
    import javax.speech.EngineStateError;
    import javax.speech.synthesis.Synthesizer;
    import javax.speech.synthesis.SynthesizerModeDesc;
    import javax.speech.synthesis.Voice;
    
    public class SpeechUtils {
      
      SynthesizerModeDesc desc;
      Synthesizer synthesizer;
      Voice voice;
      
    
      public void init(String voiceName) 
        throws EngineException, AudioException, EngineStateError, 
               PropertyVetoException 
      {
        if (desc == null) {
          
          System.setProperty("freetts.voices", 
            "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
          
          desc = new SynthesizerModeDesc(Locale.US);
          Central.registerEngineCentral
            ("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
          synthesizer = Central.createSynthesizer(desc);
          synthesizer.allocate();
          synthesizer.resume();
          SynthesizerModeDesc smd = 
            (SynthesizerModeDesc)synthesizer.getEngineModeDesc();
          Voice[] voices = smd.getVoices();
          Voice voice = null;
          for(int i = 0; i < voices.length; i++) {
            if(voices[i].getName().equals(voiceName)) {
              voice = voices[i];
              break;
            }
          }
          synthesizer.getSynthesizerProperties().setVoice(voice);
        }
        
      }
    
      public void terminate() throws EngineException, EngineStateError {
        synthesizer.deallocate();
      }
      
      public void doSpeak(String speakText) 
        throws EngineException, AudioException, IllegalArgumentException, 
               InterruptedException 
      {
          synthesizer.speakPlainText(speakText, null);
          synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
    
      }
      
      
      public static void main (String[]args) throws Exception{
        SpeechUtils su = new SpeechUtils();
        
        su.init("kevin16");
        // high quality
        su.doSpeak("Hello world from Real's How To");
        su.terminate();
      }
    } 
    
    Related links :
  • http://www.ryan-h.com/uncategorized/java-speech-jsapi-freetts/
  • http://java.sun.com/products/java-media/speech/forDevelopers/jsapi-doc/javax/speech/package-summary.html
  • http://today.java.net/pub/a/today/2006/04/13/vocal-java.html