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

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



                 162    CHAPTER 5  ■  SPEECH



                            Example 5-18 is useful when you want to hear what the Microsoft speech recognition
                        engine “hears.” You can use whatever voice you want. Here I’m using the MicrosoftVoice to
                        repeat what it hears.


                        Example 5-18. EchoTalk.java
                        package com.scottpreston.javarobot.chapter5;

                        public class EchoTalk {

                            private MicrosoftVoice voice;
                            private MicrosoftSR ear;

                            public EchoTalk() throws Exception {
                                // generic constructor gets instance of two worker classes
                                voice = MicrosoftVoice.getInstance();
                                ear = MicrosoftSR.getInstance();
                                // give user instructions
                                voice.speak("I will repeat what you say. Say exit, to end program.");
                            }

                            public void start() throws Exception {
                                String words = "";
                                // tell user to begin talking.
                                voice.speak("listening");
                                // this will loop until it hears 'exit'
                                while (words.equalsIgnoreCase("exit") == false) {
                                    // gets words heard.
                                    words = ear.listen();
                                    //prints this to system out (good for debugging)
                                    System.out.println("I heard --> " + words);
                                    // say the words
                                    voice.speak(words);
                                }
                                // last words spoken.
                                voice.speak("goodbye");
                            }

                            public static void main(String[] args) {

                                try {
                                    EchoTalk echo = new EchoTalk();
                                    echo.start();
   176   177   178   179   180   181   182   183   184   185   186