Thursday, January 3, 2013

Text to Speech using Java API Voice(file reader)

Hello Friends,

Please read my previous post before using it.

If you run previous program then you find output is:
1. alan
2. kevin
3. kevin16

These output name is the voice file name which is using in FreeTTS Java API.

FileReader :

In this case you write any paragraph or word in a file and read it using this program. This program read that file and play them.

Source code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tts;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import java.io.IOException;

/**
 *
 * @author sandy
 */
public class FileSpeaker {
    public static void main(String[] arg) throws IOException,Exception
    {
        String voicename    =   "kevin";
        VoiceManager voicemgr   =   VoiceManager.getInstance();
        Voice voice   =   voicemgr.getVoice(voicename);
        voice.setPitch((float)6.00);
        voice.setPitchShift((float) .002);
        voice.setPitchRange((float) 0.05);
       
        voice.setStyle("business");
        //allocate the resouce for the voice
        voice.allocate();
       
        // create input stream for the file
        InputStream in = new FileInputStream(new File("D:/sample.txt"));
        voice.speak(in);
        voice.deallocate();
       
    }
   
}

Sample.txt

Hello friends my name is Sandeep.

Text to speech using java api voice part 1

Hello Friends,

Please read my previous post before using it
We know that there are a number of objects that work together java to perform speech synthesis. One of them is voice.


The Voice is the central processing point for FreeTTS. The Voice takes as input a FreeTTSSpeakable, translates the text associated with the FreeTTSSpeakable into speech and generates audio output corresponding to that speech. Read more.....


The VoiceManager class is the central repository of voices available to FreeTTS. You can use this sample code to get available voice list.
source code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tts;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

/**
 *
 * @author sandy
 */
public class voicelist {
    public static void main(String arg[])
    {
        VoiceManager voicemgr   =   VoiceManager.getInstance();// get all availiable voice
        Voice[] voice   =   voicemgr.getVoices();
        for(int i=0; i<voice.length; i++)
        {
            System.out.println(voice[i].getName());
           
        }
       
    }
   
}



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.