Monday, January 16, 2012

How To Listen the contents of the Text file(.txt) using the Java 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(“Dipayan”,
  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>
*/

No comments: