Speech Synthesizer in Java

What is the speech Synthesizer?

Now a days the need and the use of the speech synthesizer is increasing everyday and there are lots and lots of new innovations are going on in this field of synthesizer.

The synthesizer is the one which will convert your text input to the speech and for that we’ll be using the Speech Engine that will help us to fulfill our need.

1:

To use the functionality of this you first require to use the Freetts which will provide the functionality of Text to Speech

FreeTTS is a speech synthesis engine written entirely in the Java programming language. FreeTTS was written by the Sun Microsystems Laboratories Speech Team and is based on CMU’s Flite engine. FreeTTS also includes a partial JSAPI 1.0You can download this with the single click here.

Once you have downloaded that .zip then all you need to do is to extract it and have a look inside the folder and move in side the lib. folder “\freetts-1.2.2-bin\freetts-1.2\lib” here you will see collection of .jar files and also the setup for the jsapi.jar file double click on that and that will generate the jsapi.jar file.

Once you have generated the jsapi.jar file now you need to set all this .jar files to the CLASSPATH so that you can import it without any kinda errors.

Else if you are using the Eclipse IDE for your Java Coding than it will be lot easier for you to add all the .jar files to you program by just following step:-

1:- Right click on the new/existing project.

2:-Go to the  properties and java Build path — Libraries — add external Jars–select and ok.

else download the .doc that i have used to display the actual way to import it. click here -> Example to import

3:- after importing write the Code

This following code that i have developed through which you can create your speech synthesizer in java.

import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.speech.Central;
import javax.speech.EngineList;
import javax.speech.EngineCreate;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.Voice;
import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral;
import java.util.Locale;
//import java.awt.Event;
@SuppressWarnings(“serial”)
public class voisedemo extends Applet implements ActionListener
{
public Synthesizer synth;
private static Voice kevinHQ;
TextField t1;
public void init()
{
Button b1 = new Button(“press me”);
add(b1);
b1.addActionListener(this);
t1 = new TextField(50);
add(t1);
}
public void start()
{
}
public void actionPerformed(ActionEvent e)
{
// synthesizer.speakPlainText(“Hello, world!”, null);
try {
// create SynthesizerModeDesc that will match the FreeTTS Synthesizer
// System.out.print( ”  Loading voice…” );
setKevinHQ(new Voice(“Hitesh”,
Voice.AGE_NEUTRAL,
Voice.GENDER_MALE,
null ));
System.setProperty(“freetts.voices”, “com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory”);
SynthesizerModeDesc modeDesc = new SynthesizerModeDesc(
null,
“general”, /* use “time” or “general” */
Locale.US,
Boolean.FALSE,
null);
//try{}catch(Exception e){}
FreeTTSEngineCentral central = new FreeTTSEngineCentral();
Synthesizer synthesizer = null;
synthesizer = Central.createSynthesizer( modeDesc );
// synthesizer.getSynthesizerProperties().setPitchRange(0.0f);
EngineList list = central.createEngineList(modeDesc);
if (list.size() > 0) {
EngineCreate creator = (EngineCreate) list.get(0);
synthesizer = (Synthesizer) creator.createEngine();
}
if (synthesizer == null) {
System.err.println(“Cannot create synthesizer”);
System.exit(1);
}
//get ready to speak
synthesizer.allocate();
synthesizer.resume();
// say hello world
String s1 = t1.getText();
synthesizer.speakPlainText(s1, null);
// synthesizer.speakPlainText(“Hello, world!”, null);
// wait until speaking is done and clean up
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.deallocate();
} catch (Exception eq) {
eq.printStackTrace();
}
}
/**
* @param kevinHQ the kevinHQ to set
*/
public static void setKevinHQ(Voice kevinHQ) {
voisedemo.kevinHQ = kevinHQ;
}
/**
* @return the kevinHQ
*/
public static Voice getKevinHQ() {
return kevinHQ;
}
public void paint(Graphics g)
{
//g.drawString(“Hello world”,50,50);
}
}
/*
<applet code=”voisedemo” width=1000 height=1000>
</applet>
*/
The output will b as follows:-
output
output
Click on press me to let the code speak what you have written..
Thank you and if you have nay doubt then feel free to ask me.. Thanks..!
Stay Hungry Stay Foolish..@};–
Advertisements

34 thoughts on “Speech Synthesizer in Java

    1. in the netBeans , it ll work as the same
      1>Create a new java project
      2>create a file and copy that synthesizer code in to that file.
      3>it ll be showing u some errors, so that now go to the project properties and then add the jar files, so that all errors will be cleared.
      4>Execute this project as the form of applet, because i have used the applet code so that,
      5>that’s all , still if u find any problem let me know,
      Bye.

  1. Good Work buddy…
    but i have one prob.
    i am executing it using cmd (appletviewer)
    It’s executing but on clicking a button i can’t hear anything.what will be the problem?

      1. Thanks for reply…
        I have downloaded freetts files & copied to jdk folder \jre\lib\ext
        Can u plz let me know the further steps required to make the project run prop.
        Also if u can give me some code to save the speech text i.e voice in some format.so that it can be used later for hearing.

  2. u hv done a gr8 job hitesh……..i m working on speech recognition……bt nt getting any way 2 proceed……so can u please help me by providing me it’s code……..i w’ll be highly obliged!!!pls. help me .peply soon.thanks

  3. u hv done a gr8 job hitesh……..i m working on speech recognition……bt nt getting any way 2 proceed……so can u please help me by providing me it’s code……..i w’ll be highly obliged!!!pls. help me .peply soon.thanks

      1. thanks hitesh for replying me……actually i want the code of hindi speech recognition in java…..so pls help me out & pls. provide me code!!!!By the way i m very impressed by ur work….ur blog is outstanding & very useful..nice…….waiting for ur reply…..thanks!!!

  4. thanks yaar for your initiative ……..ok den can u pls. provide me the code of speech recognition to open any application using speech….as u told us in ur video to open any site,excel,paint etc.. using speech…….pls. help me out!!!!thanks.

  5. Hi hitesh, thanks for the tut. I am getting a number of errors saying the same thing
    at some points it says “Syntax error, delete these tokens”
    please help me

  6. hi vinam….hitesh is genious yar….i hv also done his speech synthesizer …it’s just superb…d error u r getting is only becoz of some jar files missing…hats off to Mr. Hitesh for such outstanding info given on his blog…thnks a lottttt

  7. HI Hitesh,
    i have seen your videos .They are just amazing.
    really good stuff.
    Actully i am working on voice browser and in need of dictation grammar for websites,
    i am getting nullpointer for recognizer,can you please send me code for browser or dictation grammer…. for recognition

  8. i have a following example code….

    import javax.speech.*;
    import java.util.*;
    import javax.speech.synthesis.*;

    public class demojsapi
    {
    String speaktext;
    public void dospeak(String speak,String
    voicename)
    {
    speaktext=speak;
    String voiceName =voicename;
    try
    {
    SynthesizerModeDesc desc = new SynthesizerModeDesc(null,”general”,Locale.US,null,null);
    Synthesizer synthesizer = Central.createSynthesizer(desc);
    synthesizer.allocate();
    synthesizer.resume();
    desc = (SynthesizerModeDesc)synthesizer.getEngineModeDesc();
    Voice[] voices = desc.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);
    synthesizer.speakPlainText(speaktext, null); synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
    synthesizer.deallocate();
    }catch (Exception e)
    { String message = " missing speech.properties in " + System.getProperty("user.home") + "\n";
    System.out.println(""+e);
    System.out.println(message);}
    }
    public static void main(String[] args)
    {
    demojsapi obj=new demojsapi();
    obj.dospeak("Welcome Aabinash","kevin16");
    }

    }

    ##Now i want to make a jar file from this in eclipse.What should i do? and what about speech.properties..should i include it somewhere while making jar file??
    ##And what speech.properties is actually for??

    please help me …

      1. can you please provide me more specifically becoz when i try to make a jar file from above source code, it actually makes one but i am not able to run it. It gives me a dialog box with title”Java Virtual Machine Launcher” that says “A java exception has occurred”. so please help me with more specific steps

      2. i have not faced any such kind of problem ever, that will be great if you can upload any snapshot of that error, so we can try solving that out.

  9. can you please provide me more specifically becoz when i try to make a jar file from above source code, it actually makes one but i am not able to run it. It gives me a dialog box with title”Java Virtual Machine Launcher” that says “A java exception has occurred”. so please help me with more specific steps

  10. hii.. ur blog is awsumm.. its really very helpful..
    actually m making project on text to speech converter in java.. but dont knw how to proceed.. i tried to run ur code in netbeans ide(7.1.1) but it was showing some errors.. can u plzz tell me how to add jar files as you mentioned above..
    plzz reply asap..

    1. adding jar files is very easy, in my speech recognition video i have show that procedure, so you can use that to learn adding files in project.

      let me know if you face any problem.

      -hitesh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s