Thursday, January 3, 2013

How Convert Text to Speech using Java API

Hello Friends,
Today I tell you how to develop a Text to Speech Recognition   Programme  using Java.

Text to Speech

This is my 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.
Source Code Of Text to Speech Program 

Here I take a class name TextSpeech under package name is tts.


package tts;

import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
public class TextSpeech {
 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 .... Can you hear me now?
You write any text in the place of (can you hear me now ) and that text play as a speech.

24 comments:

  1. how to use this jar i don't understand

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. itz best.....i hv got more use 4m it..... thnkzx

    ReplyDelete
  4. I appreciate you efforts.. It would be good if you create video for this task..

    ReplyDelete
  5. Very nicely explained Complete procedure with running code , see here -

    Text To Speech in Java Using Freetts

    ReplyDelete
  6. F:\abc\Print\projwork>java TextSpeech
    Exception in thread "main" java.lang.NoClassDefFoundError: TextSpeech (wrong nam
    e: tts/TextSpeech)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
    4)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    I am getting this error
    Plz explain

    ReplyDelete
  7. wonderfull,really very helpful

    ReplyDelete
  8. How could I change the voice properties of this code.

    ReplyDelete
  9. javax.speech.EngineException: javax.speech.Central: no class found for com.sun.speech.freetts.jsapi.FreeTTSEngineCentral
    at javax.speech.Central.registerEngineCentral(Central.java:706)
    at employeee.ejaza.jButton2ActionPerformed(ejaza.java:369)
    at employeee.ejaza.access$300(ejaza.java:36)
    at employeee.ejaza$4.actionPerformed(ejaza.java:118)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

    ReplyDelete
  10. Thanks to all who like this post ...........

    ReplyDelete
  11. thank its working and how to conver speach to text please share it..

    ReplyDelete
  12. i am having error: cannot find symbol
    synthesizer.speakPlainText("Can you hear me now?", null);
    symbol: method speakPlainText(String,)
    location: variable synthesizer of type Synthesizer

    ReplyDelete
  13. when am try to RUN the Code that time, it show run time ERROR bellow ,

    Exception in thread "main" java.lang.Error: Unable to load voice directory. java.lang.ClassNotFoundException: com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory
    at com.sun.speech.freetts.VoiceManager.getVoiceDirectories(VoiceManager.java:208)
    at com.sun.speech.freetts.VoiceManager.getVoices(VoiceManager.java:109)
    at com.sun.speech.freetts.jsapi.FreeTTSEngineCentral.createEngineList(FreeTTSEngineCentral.java:94)
    at javax.speech.Central.availableSynthesizers(Central.java:660)
    at javax.speech.Central.createSynthesizer(Central.java:553)
    at com.tts.SpeechUtils.init(SpeechUtils.java:33)
    at com.tts.SpeechUtils.main(SpeechUtils.java:68)

    ReplyDelete
  14. I got this error :
    Exception in thread "Thread-1" java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 16000.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian is supported.
    at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:476)
    at com.sun.speech.freetts.audio.JavaStreamingAudioPlayer.openLine(JavaStreamingAudioPlayer.java:194)
    at com.sun.speech.freetts.audio.JavaStreamingAudioPlayer.begin(JavaStreamingAudioPlayer.java:410)
    at com.sun.speech.freetts.relp.LPCResult.playWaveSamples(LPCResult.java:505)
    at com.sun.speech.freetts.relp.LPCResult.playWave(LPCResult.java:403)
    at com.sun.speech.freetts.relp.AudioOutput.processUtterance(AudioOutput.java:65)
    at com.sun.speech.freetts.Voice.runProcessor(Voice.java:595)
    at com.sun.speech.freetts.Voice.outputUtterance(Voice.java:536)
    at com.sun.speech.freetts.Voice.access$200(Voice.java:81)
    at com.sun.speech.freetts.Voice$1.run(Voice.java:496)

    Let me know what went wrong...

    ReplyDelete
  15. package javax does not exist..... ?????
    reply

    ReplyDelete
  16. how to store genarated voice in one wav file

    ReplyDelete
  17. Even though this text to speech library it's Free, it's pretty outdated and does not provide a Natural Voice.
    I would recommend going for a third-party library, they provide much much better Speech Synthesize and some of them even give free access for "Hackers".

    I have tried many services out there, but one of the best was the API from iSpeech, it is very well documented and they provide excellent support.
    And yes they have Java SDK, it will make your life a lot easier.
    API here: http://www.ispeech.org/api

    I hope I could help you.

    ReplyDelete
  18. orey howley is anybody put valid program what is this do u eat food only

    ReplyDelete
  19. Its not working for web application

    ReplyDelete
  20. It's working ,your explanation is excellent

    ReplyDelete
  21. How I can get converted speech in .wav format?

    ReplyDelete
  22. Great useful article. Most of the information is really helpful to me. Thanks for sharing this article.
    Speech to Text


    ReplyDelete