] > Computer Speech Synthesis

Computer Speech Synthesis

Compile

javac -classpath kjsapi.jar Speaker.java

Execute

java -classpath ".;kjsapi.jar" Speaker [string-to-speak | filename]

Example: java -classpath ".;kjsapi.jar" Speaker "Hello world!"

 import javax.speech.*;
 import javax.speech.synthesis.*;
 import java.util.Locale;
 import java.net.URL;
 import java.io.*;
 
 public class Speaker{
    public static void main(String args[]) {
      try {
         Synthesizer synth = Central.createSynthesizer(
                       new SynthesizerModeDesc(Locale.ENGLISH));
         synth.allocate();
         synth.resume();
 
         try{
            String data = load( args[0] );
            System.err.println("Try JSML file.");
            synth.speak(data, null);                       //<---
         } catch( Exception e1 ){
            try{
               String data = load( args[0] );
               System.err.println("Try XML file.");
               synth.speak(                               //<---
                  "<jsml>"
                  + data.replaceFirst("<[?]xml.*[?]>", "")
                  + "</jsml>", null);
            } catch( Exception e2 ){
               try{
                  String data = load( args[0] );
                  System.err.println("Try text file.");
                  synth.speakPlainText(data, null);       //<---
               } catch( Exception e3 ){
                  System.err.println("Try argument.");
                  synth.speakPlainText(args[0], null);    //<---
         }  }  }
 
         synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
         synth.deallocate();
      } catch( Exception e4 ){
         System.err.println("--- Error --- " );
    } }
    static String load( String file ) throws Exception {
      String data = "", s;
      FileReader fr  = new FileReader( file );
      BufferedReader myIn = new BufferedReader( fr );
      while( (s=myIn.readLine()) != null ){ data += s; }
      return data;
 }  }
-_-_-

Notes.