Page 167 - The Definitive Guide to Building Java Robots
P. 167

Preston_5564C05.fm  Page 148  Tuesday, September 20, 2005  5:13 AM



                 148    CHAPTER 5  ■  SPEECH


                        Code Objective

                        The objective here is to use the JNI native class to synthesize speech.

                        Code Discussion

                        This class has a static block that calls the QuadTTS.dll. This DLL must be in the path; I put it in
                        the c:\windows\system32 directory. The constructor is just a default QuadmoreTTS() and is
                        excluded from the source. I have the three methods from the native code at my disposal—
                        SpeakDarling(), setVoice(), and getVoiceToken()—but currently, I’m only using SpeakDarling().
                        See Example 5-8.

                        Example 5-8. QuadmoreTTS.java
                        package com.scottpreston.javarobot.chapter5;

                        public class QuadmoreTTS {

                            // this is a DLL in system path QuadTTS.dll
                            static {
                                System.loadLibrary("QuadTTS");
                            }

                            // native method
                            public native boolean SpeakDarling(String strInput);
                            // native method
                            public native boolean setVoiceToken(String s);
                            // native method
                            public native String getVoiceToken();

                            // sample program
                            public static void main(String args[]) {
                                QuadmoreTTS v = new QuadmoreTTS();
                                boolean result = v.SpeakDarling("Java Robots Are Cool!");
                                System.out.println("done!");
                            }
                        }

                            I could use the QuadmoreTTS class in my programs, but I decided to create a wrapper
                        class that implements the JVoice interface of my other text-to-speech classes.
                            There are two fields—one is the QuadmoreTTS class I created in the previous example,
                        and the other is a static instance of the MicrosoftVoice—because I only want one program at a
                        time accessing the voice synthesis engine.
                            Next, I implement the following methods from JVoice: open(), close(), and speak(). While
                        open() and close() do nothing, speak() calls the native class SpeakDarling() method. If there’s
                        an error from this class, I’ve thrown an exception.
                            The sample program, main(), says the same phrase done earlier in the last two speech
                        implementation classes. See Example 5-9.
   162   163   164   165   166   167   168   169   170   171   172