Skip to content

Commit 272c006

Browse files
committed
improvements to experimental Duplex-Speech
1 parent cf0752b commit 272c006

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.project
44
.settings
55
/target
6+
/.idea
7+
java-speech-api.iml

java-speech-api.iml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<excludeFolder url="file://$MODULE_DIR$/target" />
9+
</content>
10+
<orderEntry type="inheritedJdk" />
11+
<orderEntry type="sourceFolder" forTests="false" />
12+
<orderEntry type="library" name="Maven: net.sourceforge.javaflacencoder:java-flac-encoder:0.3.7" level="project" />
13+
<orderEntry type="library" name="Maven: org.json:json:20150729" level="project" />
14+
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
15+
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
16+
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
17+
<orderEntry type="library" name="Maven: net.sourceforge.javaflacencoder:java-flac-encoder:0.3.7" level="project" />
18+
<orderEntry type="library" name="Maven: org.json:json:20150729" level="project" />
19+
</component>
20+
</module>

src/main/java/com/darkprograms/speech/recognizer/GSpeechDuplex.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void recognize(byte[] data, int sampleRate){
151151
* @throws IOException
152152
* @throws LineUnavailableException
153153
*/
154-
public void recognize(TargetDataLine tl, AudioFormat af) throws IOException, LineUnavailableException{
154+
public void recognize(TargetDataLine tl, AudioFormat af) throws IOException, LineUnavailableException, InterruptedException {
155155
//Generates a unique ID for the response.
156156
final long PAIR = MIN + (long)(Math.random() * ((MAX - MIN) + 1L));
157157

@@ -164,20 +164,31 @@ public void recognize(TargetDataLine tl, AudioFormat af) throws IOException, Lin
164164
"&key=" + API_KEY + "&continuous=true&interim=true"; //Tells Google to constantly monitor the stream;
165165

166166
//Opens downChannel
167-
this.downChannel(API_DOWN_URL);
167+
Thread downChannel = this.downChannel(API_DOWN_URL);
168168

169169
//Opens upChannel
170-
this.upChannel(API_UP_URL, tl, af);
170+
Thread upChannel = this.upChannel(API_UP_URL, tl, af);
171+
try {
172+
downChannel.join();
173+
upChannel.interrupt();
174+
upChannel.join();
175+
} catch (InterruptedException e) {
176+
downChannel.interrupt();
177+
downChannel.join();
178+
upChannel.interrupt();
179+
upChannel.join();
180+
}
181+
171182
}
172183

173184
/**
174185
* This code opens a new Thread that connects to the downstream URL. Due to threading,
175186
* the best way to handle this is through the use of listeners.
176187
* @param The URL you want to connect to.
177188
*/
178-
private void downChannel(String urlStr) {
189+
private Thread downChannel(String urlStr) {
179190
final String url = urlStr;
180-
new Thread ("Downstream Thread") {
191+
Thread downChannelThread = new Thread ("Downstream Thread") {
181192
public void run() {
182193
// handler for DOWN channel http response stream - httpsUrlConn
183194
// response handler should manage the connection.... ??
@@ -206,7 +217,9 @@ public void run() {
206217
inStream.close();
207218
System.out.println("Finished write on down stream...");
208219
}
209-
}.start();
220+
};
221+
downChannelThread.start();
222+
return downChannelThread;
210223
}
211224

212225

@@ -235,19 +248,21 @@ public void run() {
235248
* @param af The AudioFormat to stream with.
236249
* @throws LineUnavailableException If cannot open or stream the TargetDataLine.
237250
*/
238-
private void upChannel(String urlStr, TargetDataLine tl, AudioFormat af) throws IOException, LineUnavailableException{
251+
private Thread upChannel(String urlStr, TargetDataLine tl, AudioFormat af) throws IOException, LineUnavailableException{
239252
final String murl = urlStr;
240253
final TargetDataLine mtl = tl;
241254
final AudioFormat maf = af;
242255
if(!mtl.isOpen()){
243256
mtl.open(maf);
244257
mtl.start();
245258
}
246-
new Thread ("Upstream Thread") {
259+
Thread upChannelThread = new Thread ("Upstream Thread") {
247260
public void run() {
248261
openHttpsPostConnection(murl, mtl, (int)maf.getSampleRate());
249262
}
250-
}.start();
263+
};
264+
upChannelThread.start();
265+
return upChannelThread;
251266

252267
}
253268

@@ -436,7 +451,11 @@ private void parseResponse(String rawResponse, GoogleResponse gr){
436451
gr.setConfidence(String.valueOf(1));
437452
}
438453
String response = StringUtil.substringBetween(rawResponse, "[{\"transcript\":\"", "\"}],");
454+
if (response == null) {
455+
response = StringUtil.substringBetween(rawResponse, "[{\"transcript\":\"", "\",\"");
456+
}
439457
gr.setResponse(response);
458+
gr.setFinalResponse(rawResponse.contains("\"final\":true"));
440459
String[] currentHypos = rawResponse.split("\\[\\{\"transcript\":\"");
441460
for(int i = 2; i<currentHypos.length; i++){
442461
String cleaned = currentHypos[i].substring(0, currentHypos[i].indexOf("\""));

src/main/java/com/darkprograms/speech/recognizer/GoogleResponse.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class GoogleResponse {
2424
*/
2525
private List<String> otherPossibleResponses = new ArrayList<String>(20);
2626

27+
28+
private boolean finalResponse = true;
2729
/**
2830
* Constructor
2931
*/
@@ -86,4 +88,11 @@ public List<String> getAllPossibleResponses() {
8688
return tmp;
8789
}
8890

91+
public boolean isFinalResponse() {
92+
return finalResponse;
93+
}
94+
95+
public void setFinalResponse(boolean finalResponse) {
96+
this.finalResponse = finalResponse;
97+
}
8998
}

0 commit comments

Comments
 (0)