Page 165 - The Definitive Guide to Building Java Robots
P. 165
Preston_5564C05.fm Page 146 Tuesday, September 20, 2005 5:13 AM
146 CHAPTER 5 ■ SPEECH
// waits until queue is empty
synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
}
// sample program
public static void main(String[] args) {
try {
JavaVoice voice = new JavaVoice();
voice.open();
voice.speak("Java Robots Are Cool!");
voice.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("done");
}
}
Code Objective
The code objective here is to create a speech synthesis implementation using FreeTTS.
Code Discussion
First, I’ve created three static fields with the names alan, kevin, and kevin16. Alan sounds the
best, but his domain (things he can speak) is limited to date and time sounds. Kevin is an 8-bit
voice of unlimited domain (any word) and sounds very close to the JavaVoice class created in
Example 5-5. Kevin16 is a 16-bit voice, is medium quality, and has unlimited domain. The
remaining field is com.sun.speech.freetts.Voice called voice.
I construct voice in the class constructor via the getInstance().getVoice()method from
the VoiceManager. The remaining methods, open(), speak(), and close(), are self-explanatory.
See Example 5-6.
Example 5-6. FreeTTSVoice.java
package com.scottpreston.javarobot.chapter5;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class FreeTTSVoice implements JVoice {
// create these for use in constructor
public static final String VOICE_ALAN = "alan";
public static final String VOICE_KEVIN = "kevin";
public static final String VOICE_KEVIN_16 = "kevin16";