-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecorder.java
More file actions
129 lines (101 loc) · 4.08 KB
/
Recorder.java
File metadata and controls
129 lines (101 loc) · 4.08 KB
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
129
package cn.sribd.si.threads;
import cn.sribd.si.Utils;
import com.iflytek.cloud.speech.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import static cn.sribd.si.Utils.inputBuffer;
public class Recorder implements Runnable {
private SpeechRecognizer recognizer;
private RecognizerListener recognizerListener;
private long lastTime;
private StringBuilder stringBuffer;
public Recorder() {
this.recognizerListener = new RecognizerListener() {
@Override
public void onBeginOfSpeech() {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onResult(RecognizerResult results, boolean islast) {
// System.out.println(islast);
if (!results.getResultString().isEmpty()) {
String result = parseIatResult(results.getResultString());
stringBuffer.append(result);
inputBuffer.append(cleanWords(result));
// System.out.println(result);
if (System.currentTimeMillis() - lastTime > 13000 || stringBuffer.length() > 5 * 30) {
// System.out.println(System.currentTimeMillis() - lastTime);
// System.out.println(stringBuffer.length());
//
// System.out.println(stringBuffer);
synchronized (Utils.sentenceBuffer) {
Utils.sentenceBuffer.add(stringBuffer.toString());
Utils.sentenceBuffer.notify();
}
stringBuffer = new StringBuilder();
lastTime = System.currentTimeMillis();
}
}
if (islast) {
if (Utils.isStopped || Utils.stopRecorder) {
if (stringBuffer.length() > 0) {
synchronized (Utils.sentenceBuffer) {
Utils.sentenceBuffer.add(stringBuffer.toString());
Utils.sentenceBuffer.notify();
}
}
} else {
recognizer.startListening(recognizerListener);
}
}
}
@Override
public void onVolumeChanged(int volume) {
}
@Override
public void onError(SpeechError error) {
error.printStackTrace();
recognizer.startListening(recognizerListener);
}
@Override
public void onEvent(int eventType, int arg1, int agr2, String msg) {
}
};
this.recognizer = SpeechRecognizer.createRecognizer();
stringBuffer = new StringBuilder();
recognizer.setParameter(SpeechConstant.DOMAIN, "iat");
recognizer.setParameter(SpeechConstant.LANGUAGE, "en_us");
recognizer.setParameter(SpeechConstant.VAD_EOS, "800");
}
private String cleanWords(String result) {
if(result == null) return null;
result = result.replaceAll("\\.", ". ");
result = result.replaceAll("fuck","f**k");
return result;
}
@Override
public void run() {
System.out.println("Start speaking ");
lastTime = System.currentTimeMillis();
recognizer.startListening(recognizerListener);
}
private String parseIatResult(String json) {
StringBuilder ret = new StringBuilder();
try {
JSONObject joResult = new JSONObject(json);
JSONArray words = joResult.getJSONArray("ws");
for (int i = 0; i < words.length(); i++) {
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
ret.append(obj.getString("w"));
}
} catch (JSONException e) {
e.printStackTrace();
return "";
}
return ret.toString();
}
}