Source Code Cross Referenced for VoiceCommand.java in  » 6.0-JDK-Modules » java-3d » com » db » media » voice » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » java 3d » com.db.media.voice 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *
003:         * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
004:         *
005:         * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
006:         * royalty free, license to use, modify and redistribute this
007:         * software in source and binary code form,
008:         * provided that i) this copyright notice and license appear on all copies of
009:         * the software; and ii) Licensee does not utilize the software in a manner
010:         * which is disparaging to Silvere Martin-Michiellot.
011:         *
012:         * This software is provided "AS IS," without a warranty of any kind. ALL
013:         * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
014:         * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
015:         * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
016:         * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
017:         * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
018:         * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
019:         * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
020:         * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021:         * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022:         * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023:         * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
024:         * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
025:         *
026:         * This software is not designed or intended for use in on-line control of
027:         * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028:         * the design, construction, operation or maintenance of any nuclear
029:         * facility. Licensee represents and warrants that it will not use or
030:         * redistribute the Software for such purposes.
031:         *
032:         *
033:         */
034:
035:        package com.db.media.voice;
036:
037:        import java.io.*;
038:        import java.util.Locale;
039:        import java.util.ResourceBundle;
040:        import java.util.StringTokenizer;
041:        import javax.speech.*;
042:        import javax.speech.recognition.*;
043:        import javax.speech.synthesis.*;
044:
045:        /*
046:         * The aim of this class is to provide voice command from the user over a microphone and voice synthesis of the interface.
047:         */
048:        public class VoiceCommand {
049:
050:            static RuleGrammar ruleGrammar;
051:            static DictationGrammar dictationGrammar;
052:            static Recognizer recognizer;
053:            static Synthesizer synthesizer;
054:            static ResourceBundle resources;
055:
056:            static Viewer viewer;
057:
058:            public void doVoice(Viewer viewer) {
059:
060:                this .viewer = viewer;
061:
062:                try {
063:
064:                    //System.out.println("Starting JavaSpeech");
065:                    //System.out.println("locale is " + Locale.getDefault());
066:                    resources = ResourceBundle.getBundle("res");
067:
068:                    // create a recognizer matching default locale, add audio listener
069:                    recognizer = Central.createRecognizer(null);
070:                    recognizer.allocate();
071:                    recognizer.getAudioManager()
072:                            .addAudioListener(audioListener);
073:
074:                    // create dictation grammar
075:                    dictationGrammar = recognizer.getDictationGrammar(null);
076:                    dictationGrammar.addResultListener(dictationListener);
077:
078:                    // create a rule grammar, activate it
079:                    String grammarName = resources.getString("grammar");
080:                    Reader reader = new FileReader(grammarName);
081:                    ruleGrammar = recognizer.loadJSGF(reader);
082:                    ruleGrammar.addResultListener(ruleListener);
083:                    ruleGrammar.setEnabled(true);
084:
085:                    // commit new grammars, start recognizer
086:                    recognizer.commitChanges();
087:                    recognizer.requestFocus();
088:                    recognizer.resume();
089:
090:                    SynthesizerModeDesc required = new SynthesizerModeDesc();
091:                    Voice voice = new Voice(null, Voice.GENDER_FEMALE,
092:                            Voice.AGE_TEENAGER, null);
093:                    required.addVoice(voice);
094:
095:                    // create a synthesizer, speak a greeting
096:                    synthesizer = Central.createSynthesizer(required);
097:
098:                    if (synthesizer != null)
099:                        synthesizer.allocate();
100:                    speak(resources.getString("greeting"));
101:                } catch (Exception e) {
102:
103:                    e.printStackTrace();
104:                    System.exit(-1);
105:
106:                }
107:
108:            }
109:
110:            // This is the listener for rule grammar results.  The
111:            // resultAccepted method is called when the user issues a command.
112:            // We then request the tags that we associated with the grammar in
113:            // simplegrammar_en.gram, and take an action based on the tag.  Using tags
114:            // rather than looking directly at what the user said means we can
115:            // change the grammar without having to change our code.
116:            //
117:            static ResultListener ruleListener = new ResultAdapter() {
118:
119:                // accepted result
120:                public void resultAccepted(ResultEvent e) {
121:
122:                    try {
123:
124:                        // get the result
125:                        FinalRuleResult result = (FinalRuleResult) e
126:                                .getSource();
127:                        String tags[] = result.getTags();
128:
129:                        if (tags[0].equals("begin")) {
130:                            speak("listening");
131:                            viewer.setText("\"");
132:                            ruleGrammar.setEnabled(false);
133:                            ruleGrammar.setEnabled("<stop>", true);
134:                            dictationGrammar.setEnabled(true);
135:                            recognizer.commitChanges();
136:
137:                        } else if (tags[0].equals("emote")) {
138:                            speak("listening closely");
139:                            viewer.setText("emote ");
140:                            ruleGrammar.setEnabled(false);
141:                            ruleGrammar.setEnabled("<stop>", true);
142:                            dictationGrammar.setEnabled(true);
143:                            recognizer.commitChanges();
144:
145:                            // the user has said "that's all"
146:                        } else if (tags[0].equals("stop")) {
147:                            dictationGrammar.setEnabled(false);
148:                            ruleGrammar.setEnabled(true);
149:                            recognizer.commitChanges();
150:
151:                        } else if (tags[0].equals("lookleft")) {
152:                            viewer.lookLeft();
153:
154:                        } else if (tags[0].equals("lookright")) {
155:                            viewer.lookRight();
156:
157:                        } else if (tags[0].equals("left")) {
158:                            viewer.turnLeft();
159:
160:                        } else if (tags[0].equals("right")) {
161:                            viewer.turnRight();
162:
163:                        } else if (tags[0].equals("run")) {
164:                            viewer.run();
165:
166:                        } else if (tags[0].equals("forward")) {
167:                            viewer.forward();
168:
169:                        } else if (tags[0].equals("back")) {
170:                            viewer.back();
171:
172:                        } else if (tags[0].equals("halt")) {
173:                            viewer.halt();
174:
175:                        } else if (tags[0].equals("bye")) {
176:                            speak(resources.getString("bye"));
177:                            //    synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
178:                            viewer.bye();
179:                        }
180:                    } catch (Exception ex) {
181:                        ex.printStackTrace();
182:                    }
183:                }
184:
185:                // rejected result - say "eh?" etc.
186:                int i = 0;
187:                String eh[] = null;
188:
189:                public void resultRejected(ResultEvent e) {
190:
191:                    /*   if (eh==null) {
192:                           String s = resources.getString("eh");
193:                           StringTokenizer t = new StringTokenizer(s);
194:                           int n = t.countTokens();
195:                           eh = new String[n];
196:                           for (int i=0; i<n; i++)
197:                               eh[i] = t.nextToken();
198:                       }*/
199:                    //            speak(eh[(i++)%eh.length]);
200:                }
201:
202:            };
203:
204:            //
205:            // This is the listener for dictation results.  The resultUpdated
206:            // method is called for every recognized token.  The
207:            // resultAccepted method is called when the dictation result
208:            // completes, which in this application occurs when the user says
209:            // "that's all".
210:            //
211:            static ResultListener dictationListener = new ResultAdapter() {
212:
213:                int n = 0;
214:
215:                public synchronized void resultUpdated(ResultEvent e) {
216:                    Result result = (Result) e.getSource();
217:                    for (int i = n; i < result.numTokens(); i++) {
218:                        viewer.setText(viewer.getText() + " "
219:                                + result.getBestToken(i).getSpokenText());
220:                    }
221:                    n = result.numTokens();
222:                }
223:
224:                public void resultAccepted(ResultEvent e) {
225:                    speak("Thank you");
226:                }
227:            };
228:
229:            //
230:            // Audio listener prints out audio levels to help diagnose problems.
231:            //
232:            static RecognizerAudioListener audioListener = new RecognizerAudioAdapter() {
233:                public void audioLevel(RecognizerAudioEvent e) {
234:                }
235:            };
236:
237:            //
238:            // Here's a method to say something. If the synthesizer isn't
239:            // available, we just print the message.
240:            //
241:            static void speak(String s) {
242:
243:                if (synthesizer != null) {
244:                    try {
245:                        synthesizer.speak(s, null);
246:                    } catch (Exception e) {
247:                        e.printStackTrace();
248:                    }
249:                } else
250:                    System.out.println(s);
251:
252:            }
253:
254:            public synchronized void stop() {
255:
256:                try {
257:                    synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
258:                } catch (Exception e) {
259:                }
260:
261:            }
262:
263:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.