Monday, January 24, 2011

Text To Speech Using Java API



Text to Speech
This is the first post in java speech API series intended to give a brief idea about Java Speech APIs.


   A brief Idea

The Java Speech API
The Java Speech API is a specification that describes a standard set of classes and interfaces for integrating speech technologies into Java software. It is important to keep in mind that Java Speech is only a specification -no implementation is included. Thus third-parties provide the implementations. 

Java Speech supports two kinds of speech technologies: speech recognition and speech synthesis. Speech recognition converts speech to text. Special input devices called recognizers make speech recognition possible. In contrast, speech synthesis converts text to speech. Special output devices called synthesizers make speech synthesis possible.

The Java Speech API outlines standards and guidelines as to how speech applications can be built to inter-operate with each other and on all Java compatible platforms. As such, it provides the API and functionality to build the base for speech applications.

Features

  • Converts speech to text
  • Converts text and delivers them in various formats of speech
  • Supports events based on the Java event queue
  • Easy to implement API interoperates with multiple Java-based applications like applets and Swing applications
  • Interacts seamlessly with the AWT event queue
  • Supports annotations using JSML to improve pronunciation and naturalness in speech
  • Supports grammar definitions using JSGF
  • Ability to adapt to the language of the speaker
The javax.speech package defines the common functionality of recognizers, synthesizers, and other speech engines. The package javax.speech.recognition extends this basic functionality for recognizers. Similarly, javax.speech.synthesis extends this basic functionality for synthesizers.



We are going to use an open source implementation of java speech synthesis called  FreeTTSFreeTTS was built by the Speech Integration Group of Sun Microsystems Laboratories.




we are going to explore about the steps to configure FreeTTS in local windows machine.

Step 1 : Download freeTTS - Click here
Step 2 : Unzip the freeTTS binary package and check inside the \lib directory for jsapi.exe .
Step 3 : Run jsapi.exe. It will create a jar file namely jsapi.jar
Step 4 : Place all the jar files available inside lib directory to your classpath.
            Or copy all jar files to your  Java\jdk1.x.x.x\jre\lib\ext directory.


package com.sarf.tts;

import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;

public class TextSpeaker {
 public static void main(String[] args){
 try
 {
   System.setProperty("freetts.voices",
    "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
   
   Central.registerEngineCentral
    ("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
   Synthesizer  synthesizer = 
    Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
   synthesizer.allocate();
   synthesizer.resume();
   synthesizer.speakPlainText("Can you hear me now?", null);
   synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
   synthesizer.deallocate();
  }
   catch(Exception e)
   {
     e.printStackTrace();
   }
 }
}






Compile and run this java class. You can hear a voice which says .... GuEss What?


Ooops - Don't forget to connect speaker with your machine.