forked from Picovoice/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRhino.cs
375 lines (319 loc) · 14.4 KB
/
Rhino.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
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
//
// Copyright 2021 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;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
namespace Pv.Unity
{
/// <summary>
/// Status codes returned by Rhino library
/// </summary>
public enum RhinoStatus
{
SUCCESS = 0,
OUT_OF_MEMORY = 1,
IO_ERROR = 2,
INVALID_ARGUMENT = 3,
STOP_ITERATION = 4,
KEY_ERROR = 5,
INVALID_STATE = 6
}
/// <summary>
/// Class for holding Rhino inference result
/// </summary>
public class Inference
{
public Inference(bool isUnderstood, string intent, Dictionary<string, string> slots)
{
IsUnderstood = isUnderstood;
Intent = intent;
Slots = slots;
}
public bool IsUnderstood { get; }
public string Intent { get; }
public Dictionary<string, string> Slots { get; }
}
public class Rhino : IDisposable
{
#if !UNITY_EDITOR && UNITY_IOS
private const string LIBRARY_PATH = "__Internal";
#else
private const string LIBRARY_PATH = "pv_rhino";
#endif
private IntPtr _libraryPointer = IntPtr.Zero;
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_init(string modelPath, string contextPath, float sensitivity, out IntPtr handle);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int pv_sample_rate();
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern void pv_rhino_delete(IntPtr handle);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_process(IntPtr handle, short[] pcm, out bool isFinalized);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_is_understood(IntPtr handle, out bool isUnderstood);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_get_intent(IntPtr handle, out IntPtr intent, out int numSlots, out IntPtr slots, out IntPtr values);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_free_slots_and_values(IntPtr handle, IntPtr slots, IntPtr values);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_reset(IntPtr handle);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern RhinoStatus pv_rhino_context_info(IntPtr handle, out IntPtr contextInfo);
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr pv_rhino_version();
[DllImport(LIBRARY_PATH, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int pv_rhino_frame_length();
public static readonly string DEFAULT_MODEL_PATH;
static Rhino()
{
DEFAULT_MODEL_PATH = GetDefaultModelPath();
}
private bool _isFinalized;
/// <summary>
/// Factory method for Rhino Speech-to-Intent engine.
/// </summary>
/// <param name="contextPath">
/// Absolute path to file containing context model (file with `.rhn` extension. A context represents the set of
/// expressions(spoken commands), intents, and intent arguments(slots) within a domain of interest.
/// </param>
/// <param name="modelPath">
/// Absolute path to the file containing model parameters. If not set it will be set to the
/// default location.
/// </param>
/// <param name="sensitivity">
/// Inference sensitivity expressed as floating point value within [0,1]. A higher sensitivity value results in fewer misses
/// at the cost of (potentially) increasing the erroneous inference rate.
/// </param>
/// <returns>An instance of Rhino Speech-to-Intent engine.</returns>
public static Rhino Create(string contextPath, string modelPath = null, float sensitivity = 0.5f)
{
return new Rhino(modelPath ?? DEFAULT_MODEL_PATH, contextPath, sensitivity);
}
/// <summary>
/// Creates an instance of the Rhino wake word engine.
/// </summary>
/// <param name="modelPath">Absolute path to file containing model parameters.
/// <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="sensitivity">
/// 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>
private Rhino(string modelPath, string contextPath, float sensitivity = 0.5f)
{
if (!File.Exists(modelPath))
{
throw new IOException("Couldn't find model file at " + modelPath);
}
if (!File.Exists(contextPath))
{
throw new IOException("Couldn't find context file at " + contextPath);
}
if (sensitivity < 0 || sensitivity > 1)
{
throw new ArgumentException("Sensitivity value should be within [0, 1].");
}
RhinoStatus status = pv_rhino_init(modelPath, contextPath, sensitivity, out _libraryPointer);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
IntPtr contextInfoPtr;
status = pv_rhino_context_info(_libraryPointer, out contextInfoPtr);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
ContextInfo = Marshal.PtrToStringAnsi(contextInfoPtr);
Version = Marshal.PtrToStringAnsi(pv_rhino_version());
SampleRate = pv_sample_rate();
FrameLength = pv_rhino_frame_length();
}
/// <summary>
/// Processes a frame of audio and emits a flag indicating if the inference is finalized. When finalized,
/// `pv_rhino_is_understood()` should be called to check if the spoken command is considered valid.
/// </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.
/// Rhino operates on single-channel audio.
/// </param>
/// <returns>
/// Flag indicating if the inference is finalized.
/// </returns>
public bool Process(short[] pcm)
{
if (pcm.Length != FrameLength)
{
throw new ArgumentException(string.Format("Input audio frame size ({0}) was not the size specified by Rhino engine ({1}). ", pcm.Length, FrameLength) +
"Use rhino.FrameLength to get the correct size.");
}
RhinoStatus status = pv_rhino_process(_libraryPointer, pcm, out _isFinalized);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
return _isFinalized;
}
/// <summary>
/// Gets inference results from Rhino. If the spoken command was understood, it includes the specific intent name
/// that was inferred, and (if applicable) slot keys and specific slot values. Should only be called after the
/// process function returns true, otherwise Rhino has not yet reached an inference conclusion.
/// </summary>
/// <returns>
/// An immutable Inference object with `.IsUnderstood`, '.Intent` , and `.Slots` getters.
/// </returns>
public Inference GetInference()
{
if (!_isFinalized)
{
throw RhinoStatusToException(RhinoStatus.INVALID_STATE);
}
bool isUnderstood;
string intent;
Dictionary<string, string> slots;
RhinoStatus status = pv_rhino_is_understood(_libraryPointer, out isUnderstood);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
if (isUnderstood)
{
IntPtr intentPtr, slotKeysPtr, slotValuesPtr;
int numSlots;
status = pv_rhino_get_intent(_libraryPointer, out intentPtr, out numSlots, out slotKeysPtr, out slotValuesPtr);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
intent = Marshal.PtrToStringAnsi(intentPtr);
int elementSize = Marshal.SizeOf(typeof(IntPtr));
slots = new Dictionary<string, string>();
for (int i = 0; i < numSlots; i++)
{
string slotKey = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(slotKeysPtr, i * elementSize));
string slotValue = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(slotValuesPtr, i * elementSize));
slots[slotKey] = slotValue;
}
status = pv_rhino_free_slots_and_values(_libraryPointer, slotKeysPtr, slotValuesPtr);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
}
else
{
intent = null;
slots = new Dictionary<string, string>();
}
status = pv_rhino_reset(_libraryPointer);
if (status != RhinoStatus.SUCCESS)
{
throw RhinoStatusToException(status);
}
return new Inference(isUnderstood, intent, slots);
}
/// <summary>
/// Gets the current context information.
/// </summary>
/// <returns>Context information</returns>
public string ContextInfo { get; private set; }
/// <summary>
/// Gets the version number of the Rhino library.
/// </summary>
/// <returns>Version of Rhino</returns>
public string Version { get; private set; }
/// <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 Rhino
/// </summary>
/// <returns>Required sample rate.</returns>
public int SampleRate { get; private set; }
/// <summary>
/// Coverts status codes to relavent .NET exceptions
/// </summary>
/// <param name="status">Picovoice library status code.</param>
/// <returns>.NET exception</returns>
private static Exception RhinoStatusToException(RhinoStatus status)
{
switch (status)
{
case RhinoStatus.OUT_OF_MEMORY:
return new OutOfMemoryException();
case RhinoStatus.IO_ERROR:
return new IOException();
case RhinoStatus.INVALID_ARGUMENT:
return new ArgumentException();
case RhinoStatus.INVALID_STATE:
return new Exception("Rhino reported an invalid state.");
default:
return new Exception("Unmapped error code returned from Rhino.");
}
}
/// <summary>
/// Frees memory that was allocated for Rhino
/// </summary>
public void Dispose()
{
if (_libraryPointer != IntPtr.Zero)
{
pv_rhino_delete(_libraryPointer);
_libraryPointer = IntPtr.Zero;
// ensures finalizer doesn't trigger if already manually disposed
GC.SuppressFinalize(this);
}
}
~Rhino()
{
Dispose();
}
private static string GetDefaultModelPath()
{
#if !UNITY_EDITOR && UNITY_ANDROID
return ExtractResource("rhino_params.pv");
#else
return Path.Combine(Application.streamingAssetsPath, "rhino_params.pv");
#endif
}
#if !UNITY_EDITOR && UNITY_ANDROID
public static string ExtractResource(string filePath)
{
string srcPath = Path.Combine(Application.streamingAssetsPath, filePath);
string dstPath = Path.Combine(Application.persistentDataPath, filePath);
var loadingRequest = UnityWebRequest.Get(srcPath);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
{
break;
}
}
if (!(loadingRequest.isNetworkError || loadingRequest.isHttpError))
{
File.WriteAllBytes(dstPath, loadingRequest.downloadHandler.data);
}
return dstPath;
}
#endif
}
}