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

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



                                                                                  CHAPTER 5  ■  SPEECH   157



                            public SphinxSR(URL url) throws Exception {
                                //loads configuration data from XML-based configuration file
                                ConfigurationManager cm = new ConfigurationManager(url);
                                // gets component by name
                                recognizer = (Recognizer) cm.lookup("recognizer");
                                microphone = (Microphone) cm.lookup("microphone");
                                // get grammar file
                                JSGFGrammar gram = (JSGFGrammar) cm.lookup("jsgfGrammar");
                                // create the grammar
                                gram.allocate();
                                // get rules
                                ruleGrammar = gram.getRuleGrammar();
                                // get rule names
                                String[] rules = ruleGrammar.listRuleNames();
                                // display to console so you know what to speak.
                                for (int i=0; i < rules.length;i++) {
                                    System.out.println("rule name = " + rules[i]);
                                    System.out.println("rule = " +ruleGrammar.getRule(rules[i]).➥
                        toString());;
                                }
                                // separator
                                System.out.println("----");
                            }


                            // allocates resources
                            public void open() {
                                try {
                                    recognizer.allocate();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    System.exit(1);
                                }
                            }
                            // deallocates resources
                            public void close() {
                                recognizer.deallocate();
                            }

                            // start recording
                            public void start() {
                                // begins capturing audio data
                                if (microphone.startRecording() == false) {
                                    recognizer.deallocate();
                                    System.exit(1);
                                }
                            }




                   97022d2480fe4a63cfdfa123a6e70098
   171   172   173   174   175   176   177   178   179   180   181