This repository has been archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildRecSysRecognizer.java
128 lines (101 loc) · 3.07 KB
/
BuildRecSysRecognizer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package edu.cmu.sphinx.demo.buildrecsyst10team4;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.recognizer.Recognizer.State;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
import edu.cmu.sphinx.util.props.PropertyException;
/**
* responsible for speech recognition for the project
*
* @author eslam
*/
public class BuildRecSysRecognizer implements Runnable {
private Microphone microphone;
private Recognizer recognizer;
private List<RecListener> recListeners = new ArrayList<RecListener>();
private char theCharacter;
/**
* create the recognizer
*
* @throws IOException
*/
public BuildRecSysRecognizer() throws IOException {
try {
URL url = this.getClass().getResource("buildrecsys.config.xml");
if (url == null) {
throw new IOException("Can't find buildrecsys.config.xml");
}
ConfigurationManager cm = new ConfigurationManager(url);
recognizer = (Recognizer) cm.lookup("recognizer");
microphone = (Microphone) cm.lookup("microphone");
} catch (PropertyException e) {
throw new IOException("Problem configuring BuildRecSysRecognizer " + e);
}
}
/** Turns on the microphone and starts recognition */
public boolean microphoneOn() {
if (microphone.getAudioFormat() == null) {
return false;
} else {
new Thread(this).start();
return true;
}
}
/** Turns off the microphone, ending the current recognition in progress */
public void microphoneOff() {
microphone.stopRecording();
}
/** Allocates resources necessary for recognition. */
public void startup() throws IOException {
recognizer.allocate();
}
/** Releases recognition resources */
public void shutdown() {
microphoneOff();
if (recognizer.getState() == State.ALLOCATED) {
recognizer.deallocate();
}
}
@Override
public void run() {
microphone.clear();
microphone.startRecording();
Result result = recognizer.recognize();
microphone.stopRecording();
if(result != null) {
String resultText = result.getBestFinalResultNoFiller();
setTheCharacter(resultText.toCharArray()[0]);
if (!resultText.isEmpty()) {
fireListeners(resultText);
} else {
fireListeners(null);
}
}
System.out.println("$$$$$$$$$$$$$$$$$$$$");
}
public void setTheCharacter(char c) {
this.theCharacter = c;
}
public char getTheCharacter() {
return this.theCharacter;
}
public synchronized void addRecListener(RecListener rs) {
recListeners.add(rs);
}
public synchronized void removeRecListener(RecListener rs) {
recListeners.remove(rs);
}
private synchronized void fireListeners(String word) {
for(RecListener rl : recListeners) {
rl.notify(word);
}
}
}
interface RecListener {
void notify(String word);
}