This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
forked from Kaljurand/net-speech-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
WsDuplexRecognitionSession.java
171 lines (136 loc) · 4.44 KB
/
WsDuplexRecognitionSession.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ee.ioc.phon.netspeechapi.duplex;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
public class WsDuplexRecognitionSession implements DuplexRecognitionSession {
class MyWsClient extends WebSocketClient {
private JSONParser parser=new JSONParser();
public MyWsClient(URI serverURI) {
super(serverURI);
}
@Override
public void onClose(int code, String reason, boolean remote) {
}
@Override
public void onCloseInitiated(int code, String reason) {
for (RecognitionEventListener listener : recognitionEventListeners) {
listener.onClose();
}
}
@Override
public void onError(Exception arg0) {
}
@Override
public void onMessage(String msg) {
RecognitionEvent event = parseRecognitionEventJson(msg);
if (event != null) {
for (RecognitionEventListener listener : recognitionEventListeners) {
listener.onRecognitionEvent(event);
}
}
}
@Override
public void onOpen(ServerHandshake arg0) {
}
}
private String serverUrl;
private MyWsClient wsClient;
private List<RecognitionEventListener> recognitionEventListeners = new LinkedList<RecognitionEventListener>();
private Map<String, String> parameters;
public WsDuplexRecognitionSession(String serverUrl) throws IOException, URISyntaxException {
this.serverUrl = serverUrl;
this.parameters = new HashMap<String, String>();
this.parameters.put("content-type", "audio/x-raw, layout=(string)interleaved, rate=(int)16000, format=(string)S16LE, channels=(int)1");
}
public void connect() throws IOException {
String parameterString = "";
if (parameters != null) {
for (Map.Entry<String, String> entry : parameters.entrySet()) {
if (parameterString.isEmpty()) {
parameterString = "?";
} else {
parameterString += "&";
}
parameterString += URLEncoder.encode(entry.getKey() + "=" + entry.getValue(), "UTF-8");
}
}
URI serverURI;
try {
serverURI = new URI(this.serverUrl + parameterString);
wsClient = new MyWsClient(serverURI);
wsClient.connectBlocking();
} catch (URISyntaxException e) {
throw new IOException(e);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
public void addRecognitionEventListener(
RecognitionEventListener recognitionEventListener) {
recognitionEventListeners.add(recognitionEventListener);
}
public void sendChunk(byte[] bytes, boolean isLast) throws IOException {
// FIXME: check if connected?
wsClient.send(bytes);
if (isLast) {
wsClient.send("EOS");
}
}
public static RecognitionEvent parseRecognitionEventJson(String json) {
Object obj = JSONValue.parse(json);
if (obj == null) {
return null;
}
JSONObject jsonObj = (JSONObject) obj;
long status = (Long)jsonObj.get("status");
RecognitionEvent.Result result = null;
if (jsonObj.containsKey("result")) {
JSONObject jo1 = (JSONObject) jsonObj.get("result");
boolean isFinal = (Boolean)jo1.get("final");
List<RecognitionEvent.Hypothesis> hyps = new ArrayList<RecognitionEvent.Hypothesis>();
for (Object o1 : (JSONArray) jo1.get("hypotheses")) {
JSONObject jo2 = (JSONObject) o1;
String transcript = (String)jo2.get("transcript");
double confidence = 1.0;
if (jo2.containsKey("confidence")) {
confidence = (Double) jo2.get("confidence");
}
hyps.add(new RecognitionEvent.Hypothesis(transcript, (float) confidence));
}
result = new RecognitionEvent.Result(isFinal, hyps);
}
return new RecognitionEvent((int)status, result);
}
public void setContentType(String contentType) {
this.parameters.put("content-type", contentType);
}
/**
* Set user who is using the service. Sent to the server.
* @param userId
*/
public void setUserId(String userId) {
this.parameters.put("user-id", userId);
}
/**
* Set the content ID that is being dictated. For special purposes.
*
* @param contentId
*/
public void setContentId(String contentId) {
this.parameters.put("content-id", contentId);
}
}