Page 169 - The Definitive Guide to Building Java Robots
P. 169
Preston_5564C05.fm Page 150 Tuesday, September 20, 2005 5:13 AM
150 CHAPTER 5 ■ SPEECH
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("done!");
}
}
Now that I’ve created three separate implementations of the voice, it’s time to see what
they all sound like. Try it and see which one you like.
In TTSCompare, I’m showing a technique called factory method in getVoice(). In a nutshell,
this means that I can get whatever voice I want by sending a parameter—in this case, an int
enumerating one of the voices. Because all of the voices share the same interface, I can send
this back from the method and use them the same way I do the sample program main(). See
Example 5-10.
Example 5-10. TTSCompare.java
package com.scottpreston.javarobot.chapter5;
public class TTSCompare {
public static final int JAVA_VOICE = 0;
public static final int FREETTS_VOICE = 1;
public static final int MICROSOFT_VOICE = 2;
public JVoice getVoice(int voiceID) throws Exception {
JVoice voice;
if (voiceID == FREETTS_VOICE) {
voice = new FreeTTSVoice(FreeTTSVoice.VOICE_KEVIN_16);
} else if (voiceID == MICROSOFT_VOICE) {
voice = MicrosoftVoice.getInstance();
} else {
voice = new JavaVoice();
}
return voice;
}
// simple program to test all classes and compare quality
public static void main(String[] args) {
try {
97022d2480fe4a63cfdfa123a6e70098