/
Picovoice.java
439 lines (400 loc) · 17.9 KB
/
Picovoice.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
/*
Copyright 2020-2023 Picovoice Inc.
You may not use this file except in compliance with the license. A copy of the license is
located in the "LICENSE" file accompanying this source.
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/
package ai.picovoice.picovoice;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import ai.picovoice.porcupine.*;
import ai.picovoice.rhino.*;
/**
* Android binding for Picovoice end-to-end platform. Picovoice enables building voice experiences
* similar to Alexa but processes audio entirely on-device (offline).
*
* <p>Picovoice detects utterances of a customizable wake word (phrase) within an incoming stream of
* audio in real-time. After detection of wake word, it begins to infer the user's intent from the
* follow-on spoken command. Upon detection of wake word and completion of voice command, it invokes
* user-provided callbacks to signal these events.
*
* <p>Picovoice processes incoming audio in consecutive frames. The number of samples per frame is
* ${@link #getFrameLength()}. The incoming audio needs to have a sample rate equal to
* ${@link #getSampleRate()} and be 16-bit linearly-encoded. Picovoice operates on single-channel
* audio. It uses Porcupine wake word engine for wake word detection and Rhino Speech-to-Intent
* engine for intent inference.
*/
public class Picovoice {
private final PicovoiceWakeWordCallback wakeWordCallback;
private final PicovoiceInferenceCallback inferenceCallback;
private Porcupine porcupine;
private Rhino rhino;
private boolean isWakeWordDetected = false;
private final Handler callbackHandler = new Handler(Looper.getMainLooper());
/**
* Private Constructor.
*
* @param porcupine An instance of Porcupine wake word engine
* @param wakeWordCallback User-defined callback invoked upon detection of the wake phrase.
* ${@link PicovoiceWakeWordCallback} defines the interface of the
* callback.
* @param rhino An instance of Rhino Speech-to-Intent engine
* @param inferenceCallback User-defined callback invoked upon completion of intent inference.
* #{@link PicovoiceInferenceCallback} defines the interface of the
* callback.
*/
private Picovoice(
Porcupine porcupine,
PicovoiceWakeWordCallback wakeWordCallback,
Rhino rhino,
PicovoiceInferenceCallback inferenceCallback) {
this.porcupine = porcupine;
this.wakeWordCallback = wakeWordCallback;
this.rhino = rhino;
this.inferenceCallback = inferenceCallback;
}
/**
* Releases resources acquired by Picovoice.
*/
public void delete() {
if (porcupine != null) {
porcupine.delete();
porcupine = null;
}
if (rhino != null) {
rhino.delete();
rhino = null;
}
}
/**
* Processes a frame of the incoming audio stream. Upon detection of wake word and completion
* of follow-on command inference invokes user-defined callbacks.
*
* @param pcm A frame of audio samples. The number of samples per frame can be attained by calling
* ${@link #getFrameLength()}. The incoming audio needs to have a sample rate equal
* to ${@link #getSampleRate()} and be 16-bit linearly-encoded. Picovoice operates on
* single-channel audio.
* @throws PicovoiceException if there is an error while processing the audio frame.
*/
public void process(short[] pcm) throws PicovoiceException {
if (porcupine == null || rhino == null) {
throw new PicovoiceInvalidStateException("Cannot process frame - resources have been released");
}
if (pcm == null) {
throw new PicovoiceInvalidArgumentException("Passed null frame to Picovoice process.");
}
if (pcm.length != getFrameLength()) {
throw new PicovoiceInvalidArgumentException(
String.format("Picovoice process requires frames of length %d. " +
"Received frame of size %d.", getFrameLength(), pcm.length));
}
try {
if (!isWakeWordDetected) {
isWakeWordDetected = (porcupine.process(pcm) == 0);
if (isWakeWordDetected && wakeWordCallback != null) {
callbackHandler.post(new Runnable() {
@Override
public void run() {
wakeWordCallback.invoke();
}
});
}
} else {
if (rhino.process(pcm)) {
if (inferenceCallback != null) {
final RhinoInference inference = rhino.getInference();
callbackHandler.post(new Runnable() {
@Override
public void run() {
inferenceCallback.invoke(inference);
}
});
}
isWakeWordDetected = false;
}
}
} catch (PorcupineException | RhinoException e) {
throw mapToPicovoiceException(e);
}
}
/**
* Resets the internal state of Picovoice. It should be called before processing a new stream of audio
* or when process was stopped while processing a stream of audio.
*
* @throws PicovoiceException if reset fails.
*/
public void reset() throws PicovoiceException {
try {
this.isWakeWordDetected = false;
this.rhino.reset();
} catch (RhinoException e) {
throw mapToPicovoiceException(e);
}
}
/**
* Getter for version.
*
* @return Version.
*/
public String getVersion() {
return "3.0.0";
}
/**
* Getter for number of audio samples per frame.
*
* @return Number of audio samples per frame.
*/
public int getFrameLength() {
return porcupine != null ? porcupine.getFrameLength() : 0;
}
/**
* Getter for audio sample rate accepted by Picovoice.
*
* @return Audio sample rate accepted by Picovoice.
*/
public int getSampleRate() {
return porcupine != null ? porcupine.getSampleRate() : 0;
}
/**
* Getter for the Rhino context.
*
* @return Rhino context
*/
public String getContextInformation() throws PicovoiceException {
try {
return rhino != null ? rhino.getContextInformation() : "";
} catch (RhinoException e) {
throw mapToPicovoiceException(e);
}
}
/**
* Getter for the version of Porcupine.
*
* @return Porcupine version
*/
public String getPorcupineVersion() {
return porcupine != null ? porcupine.getVersion() : "";
}
/**
* Getter for the version of Rhino.
*
* @return Rhino version
*/
public String getRhinoVersion() {
return rhino != null ? rhino.getVersion() : "";
}
/**
* Maps Porcupine/Rhino Exception to Picovoice Exception.
*/
private static PicovoiceException mapToPicovoiceException(Exception e) {
if (e instanceof PorcupineActivationException || e instanceof RhinoActivationException) {
return new PicovoiceActivationException(e.getMessage(), e);
} else if (e instanceof PorcupineActivationLimitException || e instanceof RhinoActivationLimitException) {
return new PicovoiceActivationLimitException(e.getMessage(), e);
} else if (e instanceof PorcupineActivationRefusedException || e instanceof RhinoActivationRefusedException) {
return new PicovoiceActivationRefusedException(e.getMessage(), e);
} else if (e instanceof PorcupineActivationThrottledException ||
e instanceof RhinoActivationThrottledException) {
return new PicovoiceActivationThrottledException(e.getMessage(), e);
} else if (e instanceof PorcupineInvalidArgumentException || e instanceof RhinoInvalidArgumentException) {
return new PicovoiceInvalidArgumentException(e.getMessage(), e);
} else if (e instanceof PorcupineInvalidStateException || e instanceof RhinoInvalidStateException) {
return new PicovoiceInvalidStateException(e.getMessage(), e);
} else if (e instanceof PorcupineIOException || e instanceof RhinoIOException) {
return new PicovoiceIOException(e.getMessage(), e);
} else if (e instanceof PorcupineKeyException || e instanceof RhinoKeyException) {
return new PicovoiceKeyException(e.getMessage(), e);
} else if (e instanceof PorcupineMemoryException || e instanceof RhinoMemoryException) {
return new PicovoiceMemoryException(e.getMessage(), e);
} else if (e instanceof PorcupineRuntimeException || e instanceof RhinoRuntimeException) {
return new PicovoiceRuntimeException(e.getMessage(), e);
} else if (e instanceof PorcupineStopIterationException || e instanceof RhinoStopIterationException) {
return new PicovoiceStopIterationException(e.getMessage(), e);
} else if (e instanceof PorcupineException || e instanceof RhinoException) {
return new PicovoiceException(e.getMessage(), e);
} else {
return new PicovoiceException(
String.format("Unknown exception: '%s', message: '%s'",
e.getClass().getSimpleName(),
e.getMessage()), e);
}
}
/**
* Builder for creating an instance of Picovoice with a mixture of default arguments.
*/
public static class Builder {
private String accessKey = null;
private String porcupineModelPath = null;
private String keywordPath = null;
private float porcupineSensitivity = 0.5f;
private PicovoiceWakeWordCallback wakeWordCallback = null;
private String rhinoModelPath = null;
private String contextPath = null;
private float rhinoSensitivity = 0.5f;
private float endpointDurationSec = 1.0f;
private boolean requireEndpoint = true;
private PicovoiceInferenceCallback inferenceCallback = null;
/**
* Setter for AccessKey.
*
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
*/
public Picovoice.Builder setAccessKey(String accessKey) {
this.accessKey = accessKey;
return this;
}
/**
* Setter for path to Porcupine model file.
*
* @param porcupineModelPath Absolute path to the file containing Porcupine's model parameters.
*/
public Picovoice.Builder setPorcupineModelPath(String porcupineModelPath) {
this.porcupineModelPath = porcupineModelPath;
return this;
}
/**
* Setter for path to Porcupine keyword file.
*
* @param keywordPath Absolute path to Porcupine's keyword model file.
*/
public Picovoice.Builder setKeywordPath(String keywordPath) {
this.keywordPath = keywordPath;
return this;
}
/**
* Setter for wake word engine sensitivity.
*
* @param porcupineSensitivity Wake word detection sensitivity. It should be a number within
* [0, 1]. A higher sensitivity results in fewer misses at the cost
* of increasing the false alarm rate.
*/
public Picovoice.Builder setPorcupineSensitivity(float porcupineSensitivity) {
this.porcupineSensitivity = porcupineSensitivity;
return this;
}
/**
* Setter for wake word detection callback.
*
* @param wakeWordCallback User-defined callback invoked upon detection of the wake phrase.
* ${@link PicovoiceWakeWordCallback} defines the interface of the
* callback.
*/
public Picovoice.Builder setWakeWordCallback(PicovoiceWakeWordCallback wakeWordCallback) {
this.wakeWordCallback = wakeWordCallback;
return this;
}
/**
* Setter for path to Rhino model file.
*
* @param rhinoModelPath Absolute path to the file containing Rhino's model parameters.
*/
public Picovoice.Builder setRhinoModelPath(String rhinoModelPath) {
this.rhinoModelPath = rhinoModelPath;
return this;
}
/**
* Setter for path to Rhino context file.
*
* @param contextPath Absolute path to file containing context parameters. A context
* represents the set of expressions (spoken commands), intents, and
* intent arguments (slots) within a domain of interest.
*/
public Picovoice.Builder setContextPath(String contextPath) {
this.contextPath = contextPath;
return this;
}
/**
* Setter for inference engine sensitivity.
*
* @param rhinoSensitivity Inference sensitivity. It should be a number within [0, 1]. A
* higher sensitivity value results in fewer misses at the cost of
* (potentially) increasing the erroneous inference rate.
*/
public Picovoice.Builder setRhinoSensitivity(float rhinoSensitivity) {
this.rhinoSensitivity = rhinoSensitivity;
return this;
}
/**
* Setter for endpointDurationSec.
*
* @param endpointDurationSec Endpoint duration in seconds. An endpoint is a chunk of silence at the end of an
* utterance that marks the end of spoken command. It should be a positive number
* within [0.5, 5]. A lower endpoint duration reduces delay and improves
* responsiveness. A higher endpoint duration assures Rhino doesn't return inference
* pre-emptively in case the user pauses before finishing the request.
*/
public Picovoice.Builder setEndpointDurationSec(float endpointDurationSec) {
this.endpointDurationSec = endpointDurationSec;
return this;
}
/**
* Setter for requireEndpoint.
*
* @param requireEndpoint Boolean variable to indicate if Rhino should wait for a chunk of
* silence before finishing inference.
*/
public Picovoice.Builder setRequireEndpoint(boolean requireEndpoint) {
this.requireEndpoint = requireEndpoint;
return this;
}
/**
* Setter for intent inference callback.
*
* @param inferenceCallback User-defined callback invoked upon completion of intent inference.
* #{@link PicovoiceInferenceCallback} defines the interface of the
* callback.
*/
public Picovoice.Builder setInferenceCallback(PicovoiceInferenceCallback inferenceCallback) {
this.inferenceCallback = inferenceCallback;
return this;
}
/**
* Validates properties and creates an instance of the Porcupine wake word engine.
*
* @param appContext Android app context (for extracting Porcupine resources)
* @return An instance of Porcupine wake word engine
* @throws PicovoiceException if there is an error while initializing Porcupine.
*/
public Picovoice build(Context appContext) throws PicovoiceException {
try {
Porcupine porcupine = new Porcupine.Builder()
.setAccessKey(accessKey)
.setModelPath(porcupineModelPath)
.setKeywordPath(keywordPath)
.setSensitivity(porcupineSensitivity)
.build(appContext);
if (!porcupine.getVersion().startsWith("3.0.")) {
final String message = String.format(
"Expected Porcupine library with version '3.0.x' but received %s",
porcupine.getVersion());
throw new PicovoiceRuntimeException(message);
}
Rhino rhino = new Rhino.Builder()
.setAccessKey(accessKey)
.setModelPath(rhinoModelPath)
.setContextPath(contextPath)
.setSensitivity(rhinoSensitivity)
.setEndpointDurationSec(endpointDurationSec)
.setRequireEndpoint(requireEndpoint)
.build(appContext);
if (!rhino.getVersion().startsWith("3.0.")) {
final String message = String.format(
"Expected Rhino library with version '3.0.x' but received %s",
rhino.getVersion());
throw new PicovoiceRuntimeException(message);
}
return new Picovoice(
porcupine,
wakeWordCallback,
rhino,
inferenceCallback);
} catch (PorcupineException | RhinoException e) {
throw mapToPicovoiceException(e);
}
}
}
}