forked from Picovoice/picovoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicovoice.cs
322 lines (295 loc) · 13.2 KB
/
Picovoice.cs
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
//
// Copyright 2021-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.
//
using System;
using System.Collections.Generic;
namespace Pv.Unity
{
public class Picovoice : IDisposable
{
private Porcupine _porcupine;
private Rhino _rhino;
private Action _wakeWordCallback;
private Action<Inference> _inferenceCallback;
private bool _isWakeWordDetected;
/// <summary>
/// Gets the required number of audio samples per frame.
/// </summary>
/// <returns>Required frame length.</returns>
public int FrameLength { get; private set; }
/// <summary>
/// Get the audio sample rate required by Picovoice
/// </summary>
/// <returns>Required sample rate.</returns>
public int SampleRate { get; private set; }
/// <summary>
/// Gets the version number of the Picovoice platform
/// </summary>
/// <returns>Version of Picovoice</returns>
public string Version => "3.0.0";
/// <summary>
/// Get the version of the Porcupine library
/// </summary>
/// <returns>Porcupine library version</returns>
public string PorcupineVersion { get; private set; }
/// <summary>
/// Get the version of the Rhino library
/// </summary>
/// <returns>Rhino library version</returns>
public string RhinoVersion { get; private set; }
/// <summary>
/// Gets the current Rhino context information.
/// </summary>
/// <returns>Context information</returns>
public string ContextInfo { get; private set; }
/// <summary>
/// Picovoice constructor
/// </summary>
/// <param name="accessKey">AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).</param>
/// <param name="keywordPath">Absolute path to Porcupine's keyword model file.</param>
/// <param name="wakeWordCallback">
/// User-defined callback invoked upon detection of the wake phrase.
/// The callback accepts no input arguments.
/// </param>
/// <param name="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.
/// </param>
/// <param name="inferenceCallback">
/// User-defined callback invoked upon completion of intent inference. The callback
/// accepts a single input argument of type `Map<String, dynamic>` that is populated with the following items:
/// (1) IsUnderstood: whether Rhino understood what it heard based on the context
/// (2) Intent: if isUnderstood, name of intent that were inferred
/// (3) Slots: if isUnderstood, dictionary of slot keys and values that were inferred
/// </param>
/// <param name="porcupineModelPath">Absolute path to the file containing Porcupine's model parameters.</param>
/// <param name="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.
/// </param>
/// <param name="rhinoModelPath">Absolute path to the file containing Rhino's model parameters.</param>
/// <param name="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.
/// <param name="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.
/// </param>
/// <param name="requireEndpoint">
/// If set to `true`, Rhino requires an endpoint (a chunk of silence) after the spoken command.
/// If set to `false`, Rhino tries to detect silence, but if it cannot, it still will provide inference regardless. Set
/// to `false` only if operating in an environment with overlapping speech (e.g. people talking in the background).
/// </param>
/// </returns>
public static Picovoice Create(
string accessKey,
string keywordPath,
Action wakeWordCallback,
string contextPath,
Action<Inference> inferenceCallback,
string porcupineModelPath = null,
float porcupineSensitivity = 0.5f,
string rhinoModelPath = null,
float rhinoSensitivity = 0.5f,
float endpointDurationSec = 1.0f,
bool requireEndpoint = true)
{
try
{
Porcupine porcupine = Porcupine.FromKeywordPaths(accessKey,
keywordPaths: new List<string> { keywordPath },
modelPath: porcupineModelPath,
sensitivities: new List<float> { porcupineSensitivity });
Rhino rhino = Rhino.Create(accessKey,
contextPath: contextPath,
modelPath: rhinoModelPath,
sensitivity: rhinoSensitivity,
endpointDurationSec: endpointDurationSec,
requireEndpoint: requireEndpoint);
if (wakeWordCallback == null)
{
throw new PicovoiceInvalidArgumentException("'wakeWordCallback' must be set.");
}
if (inferenceCallback == null)
{
throw new PicovoiceInvalidArgumentException("'inferenceCallback' must be set.");
}
if (porcupine.FrameLength != rhino.FrameLength)
{
throw new PicovoiceInvalidArgumentException($"Porcupine frame length ({porcupine.FrameLength}) and Rhino frame length ({rhino.FrameLength}) are different");
}
if (porcupine.SampleRate != rhino.SampleRate)
{
throw new PicovoiceInvalidArgumentException($"Porcupine sample rate ({porcupine.SampleRate}) and Rhino sample rate ({rhino.SampleRate}) are different");
}
return new Picovoice(porcupine, wakeWordCallback, rhino, inferenceCallback);
}
catch (Exception ex)
{
throw MapToPicovoiceException(ex);
}
}
// private constructor
private Picovoice(Porcupine porcupine, Action wakeWordCallback, Rhino rhino, Action<Inference> inferenceCallback)
{
_porcupine = porcupine;
_wakeWordCallback = wakeWordCallback;
_rhino = rhino;
_inferenceCallback = inferenceCallback;
FrameLength = porcupine.FrameLength;
SampleRate = porcupine.SampleRate;
PorcupineVersion = porcupine.Version;
RhinoVersion = rhino.Version;
ContextInfo = rhino.ContextInfo;
}
/// <summary>
/// Processes a frame of the incoming audio stream. Upon detection of wake word and completion of follow-on command
/// inference invokes user-defined callbacks.
/// </summary>
/// <param name="pcm">
/// A frame of audio samples. The number of samples per frame can be found by calling `.FrameLength`.
/// The incoming audio needs to have a sample rate equal to `.SampleRate` and be 16-bit linearly-encoded.
/// Picovoice operates on single-channel audio.
/// </param>
public void Process(short[] pcm)
{
if (pcm.Length != FrameLength)
{
throw new PicovoiceInvalidArgumentException(string.Format("Input audio frame size ({0}) was not the size specified by Picovoice engine ({1}). ", pcm.Length, FrameLength) +
"Use picovoice.FrameLength to get the correct size.");
}
if (_porcupine == null || _rhino == null)
{
throw new PicovoiceInvalidStateException("Cannot process frame - resources have been released.");
}
try
{
if (!_isWakeWordDetected)
{
int keywordIndex = _porcupine.Process(pcm);
if (keywordIndex >= 0)
{
_isWakeWordDetected = true;
_wakeWordCallback.Invoke();
}
}
else
{
bool isFinalized = _rhino.Process(pcm);
if (isFinalized)
{
_isWakeWordDetected = false;
_inferenceCallback.Invoke(_rhino.GetInference());
}
}
}
catch (Exception ex)
{
throw MapToPicovoiceException(ex);
}
}
/// <summary>
/// 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.
/// </summary>
public void Reset()
{
if (_porcupine == null || _rhino == null)
{
throw new PicovoiceInvalidStateException("Cannot reset - resources have been released.");
}
try
{
_isWakeWordDetected = false;
_rhino.Reset();
}
catch (Exception ex)
{
throw MapToPicovoiceException(ex);
}
}
/// <summary>
/// Frees memory that was allocated for Picovoice
/// </summary>
public void Dispose()
{
if (_porcupine != null)
{
_porcupine.Dispose();
_porcupine = null;
}
if (_rhino != null)
{
_rhino.Dispose();
_rhino = null;
}
}
/// <summary>
/// Maps Porcupine/Rhino Exception to Picovoice Exception
/// </summary>
private static PicovoiceException MapToPicovoiceException(Exception ex)
{
if (ex is PorcupineActivationException || ex is RhinoActivationException)
{
return new PicovoiceActivationException(ex.Message, ex);
}
else if (ex is PorcupineActivationLimitException || ex is RhinoActivationLimitException)
{
return new PicovoiceActivationLimitException(ex.Message, ex);
}
else if (ex is PorcupineActivationRefusedException || ex is RhinoActivationRefusedException)
{
return new PicovoiceActivationRefusedException(ex.Message, ex);
}
else if (ex is PorcupineActivationThrottledException || ex is RhinoActivationThrottledException)
{
return new PicovoiceActivationThrottledException(ex.Message, ex);
}
else if (ex is PorcupineInvalidArgumentException || ex is RhinoInvalidArgumentException)
{
return new PicovoiceInvalidArgumentException(ex.Message, ex);
}
else if (ex is PorcupineInvalidStateException || ex is RhinoInvalidStateException)
{
return new PicovoiceInvalidStateException(ex.Message, ex);
}
else if (ex is PorcupineIOException || ex is RhinoIOException)
{
return new PicovoiceIOException(ex.Message, ex);
}
else if (ex is PorcupineKeyException || ex is RhinoKeyException)
{
return new PicovoiceKeyException(ex.Message, ex);
}
else if (ex is PorcupineMemoryException || ex is RhinoMemoryException)
{
return new PicovoiceMemoryException(ex.Message, ex);
}
else if (ex is PorcupineRuntimeException || ex is RhinoRuntimeException)
{
return new PicovoiceRuntimeException(ex.Message, ex);
}
else if (ex is PorcupineStopIterationException || ex is RhinoStopIterationException)
{
return new PicovoiceStopIterationException(ex.Message, ex);
}
else
{
return new PicovoiceException(ex.Message, ex);
}
}
~Picovoice()
{
Dispose();
}
}
}