Page 166 - The Definitive Guide to Building Java Robots
P. 166
Preston_5564C05.fm Page 147 Tuesday, September 20, 2005 5:13 AM
CHAPTER 5 ■ SPEECH 147
private Voice voice;
// creates with name
public FreeTTSVoice(String voiceName) {
voice = VoiceManager.getInstance().getVoice(voiceName);
}
// speaks
public void speak(String msg) {
voice.speak(msg);
}
// deallocates and frees resources
public void close() {
voice.deallocate();
}
// allocates and opens resources
public void open() {
voice.allocate();
}
// sample program
public static void main(String[] args) {
FreeTTSVoice me = new FreeTTSVoice(FreeTTSVoice.VOICE_KEVIN_16);
me.open();
me.speak("Java Robots Are Cool.");
me.close();
}
}
Speech Synthesis Using JNI
Sometimes, whether I like it or not, I need to use some native code to get functionality for my
robots. In the next two examples I’ll create a native text-to-speech class and a native speech
recognition class. The C++ project can be downloaded from www.quadmore.com.
If you recall from the introduction, I must make sure the method name for the JNIEXPORT
matches the fully qualified class name. See Example 5-7.
■Note If you get a java.lang.UnsatisfiedLinkError, check to make sure the method names match.
Example 5-7. QuadmoreTTS.h and QuadTTS.h
JNIEXPORT jboolean JNICALL ➥
Java_com_scottpreston_javarobot_chapter5_QuadmoreTTS_SpeakDarling