This repository has been archived by the owner on May 6, 2022. It is now read-only.
/
SpeechPipelineTest.java
475 lines (404 loc) · 16.2 KB
/
SpeechPipelineTest.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package io.spokestack.spokestack;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.Semaphore;
import java.nio.ByteBuffer;
import androidx.annotation.NonNull;
import io.spokestack.spokestack.android.AudioRecordError;
import io.spokestack.spokestack.util.EventTracer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.*;
import static io.spokestack.spokestack.SpeechTestUtils.FreeInput;
public class SpeechPipelineTest implements OnSpeechEventListener {
private static final List<Class<?>> PROFILES = Arrays.asList(
io.spokestack.spokestack.profile.PushToTalkAndroidASR.class,
io.spokestack.spokestack.profile.PushToTalkAzureASR.class,
io.spokestack.spokestack.profile.PushToTalkGoogleASR.class,
io.spokestack.spokestack.profile.PushToTalkSpokestackASR.class,
io.spokestack.spokestack.profile.TFWakewordAndroidASR.class,
io.spokestack.spokestack.profile.TFWakewordAzureASR.class,
io.spokestack.spokestack.profile.TFWakewordGoogleASR.class,
io.spokestack.spokestack.profile.TFWakewordKeywordASR.class,
io.spokestack.spokestack.profile.TFWakewordSpokestackASR.class,
io.spokestack.spokestack.profile.VADTriggerAndroidASR.class,
io.spokestack.spokestack.profile.VADTriggerAzureASR.class,
io.spokestack.spokestack.profile.VADTriggerGoogleASR.class,
io.spokestack.spokestack.profile.VADTriggerKeywordASR.class,
io.spokestack.spokestack.profile.VADTriggerSpokestackASR.class
);
private List<SpeechContext.Event> events = new ArrayList<>();
@Before
public void before() {
this.events.clear();
}
@After
public void after() {
// shut down any stray speech pipeline background threads
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread.getName().contains("Spokestack")) {
thread.interrupt();
}
}
}
@Test
public void testBuilder() throws Exception {
// default config
try (SpeechPipeline pipeline = new SpeechPipeline.Builder()
.build()) {
assertEquals(
SpeechPipeline.DEFAULT_SAMPLE_RATE,
pipeline.getConfig().getInteger("sample-rate")
);
assertEquals(
SpeechPipeline.DEFAULT_FRAME_WIDTH,
pipeline.getConfig().getInteger("frame-width")
);
assertEquals(
SpeechPipeline.DEFAULT_BUFFER_WIDTH,
pipeline.getConfig().getInteger("buffer-width")
);
}
// invalid pipeline
try (SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("invalid")
.build()) {
assertThrows(Exception.class, new Executable() {
public void execute() throws Exception { pipeline.start(); }
});
}
// attach config
SpeechConfig config = new SpeechConfig();
config.put("sample-rate", 8000);
config.put("frame-width", 10);
config.put("buffer-width", 300);
try (SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechPipelineTest$Input")
.setConfig(config)
.build()) {
pipeline.start();
assertEquals(8000, pipeline.getConfig().getInteger("sample-rate"));
assertEquals(10, pipeline.getConfig().getInteger("frame-width"));
assertEquals(300, pipeline.getConfig().getInteger("buffer-width"));
Input.stop();
}
// attach stages
List<String> stages = new ArrayList<>();
stages.add("io.spokestack.spokestack.SpeechPipelineTest$Stage");
try (SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechPipelineTest$Input")
.setStageClasses(stages)
.build()) {
pipeline.start();
Input.stop();
}
}
@Test
@SuppressWarnings("unchecked")
public void testProfiles() throws Exception {
assertThrows(IllegalArgumentException.class, () ->
new SpeechPipeline.Builder()
.useProfile("io.spokestack.InvalidProfile")
);
SpeechConfig config = new SpeechConfig();
Field stageField = SpeechPipeline.Builder.class
.getDeclaredField("stageClasses");
stageField.setAccessible(true);
// all classes from all profiles should be accessible on the classpath
for (Class<?> profileClass : PROFILES) {
SpeechPipeline.Builder builder =
new SpeechPipeline.Builder()
.useProfile(profileClass.getCanonicalName());
List<String> stages = (List<String>) stageField.get(builder);
for (String stageName : stages) {
try {
SpeechProcessor processor = (SpeechProcessor) Class
.forName(stageName)
.getConstructor(SpeechConfig.class)
.newInstance(new Object[]{config});
} catch (ClassNotFoundException e) {
// we're checking for typos in the fully qualified stage
// names here, so this is the only error we care about
fail("stage " + stageName + " not found for profile " +
profileClass);
} catch (Exception | Error e) {
// various errors are expected because
// this test doesn't provide a proper runtime environment
// or configuration for the stages,
// so do nothing here
}
}
}
// The implicated class requires a config property
SpeechPipeline pipeline = new SpeechPipeline.Builder()
.useProfile(
"io.spokestack.spokestack.SpeechPipelineTest$TestProfile")
.build();
assertThrows(InvocationTargetException.class, pipeline::start);
}
@Test
public void testStartStop() throws Exception {
final SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechPipelineTest$Input")
.addStageClass("io.spokestack.spokestack.SpeechPipelineTest$Stage")
.setProperty("sample-rate", 16000)
.setProperty("frame-width", 20)
.setProperty("buffer-width", 300)
.setProperty("trace-level", EventTracer.Level.DEBUG.value())
.addOnSpeechEventListener(this)
.build();
// startup
pipeline.start();
assertTrue(pipeline.isRunning());
assertEquals(15, pipeline.getContext().getBuffer().size());
assertEquals(0, Input.counter);
assertTrue(Stage.open);
// idempotent restart
pipeline.start();
assertEquals(SpeechContext.Event.TRACE, this.events.get(0));
// first frame
transact(false);
assertEquals(SpeechContext.Event.ACTIVATE, this.events.get(0));
assertTrue(pipeline.getContext().isActive());
// next frame
transact(false);
assertEquals(SpeechContext.Event.DEACTIVATE, this.events.get(0));
assertFalse(pipeline.getContext().isActive());
// third frame reactivates the context
transact(false);
assertEquals(SpeechContext.Event.ACTIVATE, this.events.get(0));
assertTrue(pipeline.getContext().isActive());
// shutdown
Input.stop();
pipeline.close();
assertFalse(pipeline.isRunning());
assertFalse(pipeline.getContext().isActive());
assertEquals(-1, Input.counter);
assertFalse(Stage.open);
}
@Test
public void testPause() throws Exception {
final SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechTestUtils$FreeInput")
.setProperty("sample-rate", 16000)
.setProperty("frame-width", 20)
.setProperty("buffer-width", 300)
.setProperty("trace-level", EventTracer.Level.DEBUG.value())
.build();
// startup
int frames = FreeInput.counter;
assertEquals(frames, 0);
pipeline.start();
assertTrue(pipeline.isRunning());
Thread.sleep(5);
assertTrue(FreeInput.counter > frames);
// we won't get any more frames if we're paused
pipeline.pause();
// wait for the pause to take effect
Thread.sleep(10);
frames = FreeInput.counter;
// wait some more to make sure we don't get any more frames
Thread.sleep(15);
assertEquals(FreeInput.counter, frames);
// after resuming, frames should start increasing almost immediately
pipeline.resume();
Thread.sleep(5);
assertTrue(FreeInput.counter > frames);
}
@Test
public void testInputFailure() throws Exception {
SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechPipelineTest$FailInput")
.addOnSpeechEventListener(this)
.build();
pipeline.start();
// wait for pipeline to shut down due to error
while (pipeline.isRunning()) {
Thread.sleep(1);
}
assertEquals(SpeechContext.Event.ERROR, this.events.get(0));
}
@Test
public void testStageFailure() throws Exception {
SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechPipelineTest$Input")
.addStageClass("io.spokestack.spokestack.SpeechPipelineTest$FailStage")
.addOnSpeechEventListener(this)
.build();
pipeline.start();
transact(false);
assertEquals(SpeechContext.Event.ERROR, this.events.get(0));
this.events.clear();
Input.stop();
pipeline.stop();
assertEquals(SpeechContext.Event.ERROR, this.events.get(0));
}
@Test
public void testContextManagement() throws Exception {
SpeechPipeline pipeline = new SpeechPipeline.Builder()
.setInputClass("io.spokestack.spokestack.SpeechPipelineTest$ManagedInput")
.addStageClass("io.spokestack.spokestack.SpeechPipelineTest$Stage")
.addOnSpeechEventListener(this)
.build();
pipeline.start();
// no activation event because stages don't run
pipeline.getContext().setManaged(true);
transact(true);
assertTrue(this.events.isEmpty());
assertFalse(pipeline.getContext().isActive());
// still no event, and the external activation isn't overridden by the
// stage, which would normally deactivate on the second frame
pipeline.activate();
transact(true);
assertTrue(this.events.isEmpty());
assertTrue(pipeline.getContext().isActive());
// ensure that manual deactivation resets stages
assertFalse(Stage.reset);
pipeline.deactivate();
assertTrue(Stage.reset);
assertFalse(pipeline.getContext().isActive());
assertFalse(pipeline.getContext().isManaged());
// now that external management is off,
// we get the expected activations/events
transact(false);
assertEquals(SpeechContext.Event.ACTIVATE, this.events.get(0));
assertTrue(pipeline.getContext().isActive());
transact(false);
assertEquals(SpeechContext.Event.DEACTIVATE, this.events.get(0));
assertFalse(pipeline.getContext().isActive());
}
private void transact(boolean managed) throws Exception {
this.events.clear();
Input.send();
if (!managed) {
while (this.events.isEmpty()) {
Thread.sleep(1);
}
}
}
public void onEvent(@NonNull SpeechContext.Event event,
@NonNull SpeechContext context) {
this.events.add(event);
}
public static class Input implements SpeechInput {
private static Semaphore semaphore;
private static boolean stopped;
public static int counter;
public Input(SpeechConfig config) {
semaphore = new Semaphore(0);
stopped = false;
counter = 0;
}
public void close() {
counter = -1;
}
public void read(SpeechContext context, ByteBuffer frame)
throws InterruptedException {
if (!stopped) {
semaphore.acquire();
frame.putInt(0, ++counter);
}
}
public static void send() {
semaphore.release();
}
public static void stop() {
stopped = true;
semaphore.release();
}
}
private static class ManagedInput extends Input {
public ManagedInput(SpeechConfig config) {
super(config);
}
@Override
public void read(SpeechContext context, ByteBuffer frame)
throws InterruptedException {
if (!context.isManaged()) {
super.read(context, frame);
}
}
}
public static class Stage implements SpeechProcessor {
public static boolean open;
public static boolean reset = false;
public Stage(SpeechConfig config) {
open = true;
}
public void reset() {
reset = true;
close();
}
public void close() {
open = false;
}
public void process(SpeechContext context, ByteBuffer frame) {
int counter = frame.getInt(0);
boolean active = counter % 2 == 1;
context.setActive(active);
context.dispatch(
active
? SpeechContext.Event.ACTIVATE
: SpeechContext.Event.DEACTIVATE
);
Iterator<ByteBuffer> buffer =
context.getBuffer().descendingIterator();
while (buffer.hasNext() && counter >= 0)
assertEquals(counter--, buffer.next().getInt(0));
}
}
public static class FailInput implements SpeechInput {
public FailInput(SpeechConfig config) {
}
public void close() throws Exception {
throw new Exception("fail");
}
public void read(SpeechContext context, ByteBuffer frame)
throws Exception {
throw new AudioRecordError(-3);
}
}
public static class FailStage implements SpeechProcessor {
public FailStage(SpeechConfig config) {
}
public void reset() throws Exception {
close();
}
public void close() throws Exception {
throw new Exception("fail");
}
public void process(SpeechContext context, ByteBuffer frame)
throws Exception {
throw new Exception("fail");
}
}
public static class ConfigRequiredStage implements SpeechProcessor {
public ConfigRequiredStage(SpeechConfig config) {
config.getString("required-property");
}
public void reset() throws Exception {
close();
}
public void close() throws Exception {
throw new Exception("fail");
}
public void process(SpeechContext context, ByteBuffer frame)
throws Exception {
throw new Exception("fail");
}
}
private static class TestProfile implements PipelineProfile {
public TestProfile() {}
@Override
public SpeechPipeline.Builder apply(SpeechPipeline.Builder builder) {
return builder
.setInputClass(
"io.spokestack.spokestack.SpeechPipelineTest$Input")
.addStageClass(
"io.spokestack.spokestack.SpeechPipelineTest$ConfigRequiredStage");
}
}
}