forked from Picovoice/picovoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicovoiceManager.cs
157 lines (141 loc) · 6.42 KB
/
PicovoiceManager.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
//
// 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.IO;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace Pv.Unity
{
public class PicovoiceManager
{
private VoiceProcessor _voiceProcessor;
private Picovoice _picovoice;
private Action<Exception> _errorCallback;
string _keywordPath;
Action _wakeWordCallback;
string _contextPath;
Action<Inference> _inferenceCallback;
string _porcupineModelPath;
float _porcupineSensitivity = 0.5f;
string _rhinoModelPath;
float _rhinoSensitivity = 0.5f;
/// <summary>
/// PicovoiceManager constructor
/// </summary>
/// <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.
/// </returns>
/// <param name="errorCallback">Callback that triggers is the engine experiences a problem while processing audio.</param>
/// <returns>An instance of PicovoiceManager.</returns>
public PicovoiceManager(string keywordPath, Action wakeWordCallback,
string contextPath, Action<Inference> inferenceCallback,
string porcupineModelPath = null, float porcupineSensitivity = 0.5f,
string rhinoModelPath = null, float rhinoSensitivity = 0.5f,
Action<Exception> errorCallback = null)
{
_keywordPath = keywordPath;
_wakeWordCallback = wakeWordCallback;
_contextPath = contextPath;
_inferenceCallback = inferenceCallback;
_porcupineModelPath = porcupineModelPath;
_porcupineSensitivity = porcupineSensitivity;
_rhinoModelPath = rhinoModelPath;
_rhinoSensitivity = rhinoSensitivity;
_errorCallback = errorCallback;
_voiceProcessor = VoiceProcessor.Instance;
}
/// <summary>
/// Action to catch audio frames as voice processor produces them
/// </summary>
/// <param name="pcm">Frame of pcm audio</param>
private void OnFrameCaptured(short[] pcm)
{
try
{
_picovoice.Process(pcm);
}
catch (Exception ex)
{
if (_errorCallback != null)
_errorCallback(ex);
else
Debug.LogError(ex.ToString());
}
}
/// <summary>
/// Checks to see whether PicovoiceManager is capturing audio or not
/// </summary>
/// <returns>whether PicovoiceManager is capturing audio or not</returns>
public bool IsRecording => _voiceProcessor.IsRecording;
/// <summary>
/// Checks to see whether there are any audio capture devices available
/// </summary>
/// <returns>whether there are any audio capture devices available</returns>
public bool IsAudioDeviceAvailable()
{
_voiceProcessor.UpdateDevices();
return _voiceProcessor.CurrentDeviceIndex >= 0;
}
/// <summary>
/// Starts audio capture and Picovoice processing
/// </summary>
public void Start()
{
if (_picovoice != null)
return;
_picovoice = Picovoice.Create(_keywordPath, _wakeWordCallback,
_contextPath, _inferenceCallback,
_porcupineModelPath, _porcupineSensitivity,
_rhinoModelPath, _rhinoSensitivity);
_voiceProcessor.OnFrameCaptured += OnFrameCaptured;
_voiceProcessor.StartRecording(_picovoice.SampleRate, _picovoice.FrameLength);
}
/// <summary>
/// Stops audio capture and Picovoice processing
/// </summary>
public void Stop()
{
if (_voiceProcessor.IsRecording)
{
_voiceProcessor.StopRecording();
}
_voiceProcessor.OnFrameCaptured -= OnFrameCaptured;
_picovoice?.Dispose();
_picovoice = null;
}
}
}