Sei sulla pagina 1di 5

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, IN
CLUDING ANY
014: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PUR
POSE 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 H
AS BEEN
024: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line cont
rol of
027: * aircraft, air traffic, aircraft navigation or aircraft communicat
ions; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use o
r
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 o
ver 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 audi
o 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 comman
d.
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 c
an
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.QU
EUE_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 resultUpdate
d
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 say
s
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 probl
ems.
231: //
232: static RecognizerAudioListener audioListener = new RecognizerAud
ioAdapter() {
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: }

Potrebbero piacerti anche