This post originated from an RSS feed registered with Java Buzz
by Sam Dalton.
Original Post: Java speaks back
Feed Title: import java.*;
Feed URL: http://www.samjdalton.com/pebble/rss.xml
Feed Description: Random, Infrequent Bloggings of a Techie
While perusing my friend Gerard's blog today I noticed he has been playing around with FreeTTS which is a speech synthesis system written entirely in Java.
One day I will find a good use for this (when I ever have an assignment that allows me to write client side code!), but for now, I put together a small demo. All this does is speaks back anything that you type. You too can have endless (almost!) fun findong out how it pronounces various swear words (I am such a child somethimes ;)) Anyhow, the code is as follows:
package com.samjdalton.speakie;
import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral;
import javax.speech.EngineCreate;
import javax.speech.EngineList;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
public class JavaSpeak implements Runnable {
private Synthesizer synthesizer;
public JavaSpeak() {
initialise();
}
private void initialise() {
try {
SynthesizerModeDesc desc =
new SynthesizerModeDesc(null,
"general",
Locale.US,
Boolean.FALSE,
null);
FreeTTSEngineCentral central = new FreeTTSEngineCentral();
EngineList list = central.createEngineList(desc);
if (list.size() > 0) {
EngineCreate creator = (EngineCreate) list.get(0);
this.synthesizer = (Synthesizer) creator.createEngine();
}
if (this.synthesizer == null) {
System.exit(1);
}
this.synthesizer.allocate();
this.synthesizer.resume();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String line = reader.readLine();
while (!line.equals("")) {
synthesizer.speakPlainText(line, null);
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.exit(0);
}
public static void main(String[] args) {
Runnable runnable = new JavaSpeak();
Thread t = new Thread(runnable);
t.start();
}
}
Simply put freetts.jar and jsapi.jar from the distribution into your classpath, and type
java com.samjdalton.speakie.JavaSpeak
to run the demo (an empty line followed by a carriage return will terminate the program)