forked from Picovoice/porcupine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PorcupineDemo.cs
165 lines (144 loc) · 4.47 KB
/
PorcupineDemo.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
//
// 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;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Pv.Unity;
public class PorcupineDemo : MonoBehaviour
{
private const string ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}"; // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
static List<Porcupine.BuiltInKeyword> _keywords = Enum.GetValues(typeof(Porcupine.BuiltInKeyword)).Cast<Porcupine.BuiltInKeyword>().ToList();
public Texture[] _imgs;
Button _startButton;
RawImage _outputImg;
Text _textField;
Color _alphaSubtract = new Color(0, 0, 0, 0.008f);
private bool _isProcessing;
PorcupineManager _porcupineManager;
private bool isError = false;
void Start()
{
_startButton = gameObject.GetComponentInChildren<Button>();
_startButton.onClick.AddListener(ToggleProcessing);
_outputImg = gameObject.GetComponentInChildren<RawImage>();
_textField = gameObject.GetComponentInChildren<Text>();
_keywords.Remove(Porcupine.BuiltInKeyword.HEY_GOOGLE);
FillKeywords();
try
{
_porcupineManager = PorcupineManager.FromBuiltInKeywords(ACCESS_KEY, _keywords, OnWakeWordDetected, processErrorCallback: ErrorCallback);
}
catch (PorcupineInvalidArgumentException ex)
{
SetError($"{ex.Message}\nMake sure your access key '{ACCESS_KEY}' is a valid access key.");
}
catch (PorcupineActivationException)
{
SetError("AccessKey activation error");
}
catch (PorcupineActivationLimitException)
{
SetError("AccessKey reached its device limit");
}
catch (PorcupineActivationRefusedException)
{
SetError("AccessKey refused");
}
catch (PorcupineActivationThrottledException)
{
SetError("AccessKey has been throttled");
}
catch (PorcupineException ex)
{
SetError("PorcupineManager was unable to initialize: " + ex.Message);
}
}
private void ToggleProcessing()
{
if (!_isProcessing)
{
StartProcessing();
}
else
{
StopProcessing();
}
}
private void StartProcessing()
{
(_startButton.targetGraphic as Text).text = "Stop Listening";
_porcupineManager.Start();
_isProcessing = true;
}
private void StopProcessing()
{
if (_porcupineManager == null)
{
return;
}
(_startButton.targetGraphic as Text).text = "Start Listening";
_porcupineManager.Stop();
_isProcessing = false;
}
private void OnWakeWordDetected(int keywordIndex)
{
if (isError)
{
return;
}
if (keywordIndex >= 0)
{
Porcupine.BuiltInKeyword keyword = _keywords[keywordIndex];
_outputImg.color = Color.white;
string a = keyword.ToString().ToLower();
_outputImg.texture = _imgs.First(img => img.name == keyword.ToString().ToLower());
}
}
private void ErrorCallback(Exception e)
{
SetError(e.Message);
}
private void SetError(string message)
{
isError = true;
_textField.text = message;
_textField.color = Color.red;
_startButton.interactable = false;
StopProcessing();
}
void Update()
{
if (isError)
{
return;
}
if (_outputImg.texture != null)
{
_outputImg.color -= _alphaSubtract;
}
}
private void FillKeywords()
{
foreach (Porcupine.BuiltInKeyword keyword in _keywords)
{
_textField.text = $"{_textField.text}\n- '{keyword.ToString().Replace("_", " ").ToLower()}'";
}
}
void OnApplicationQuit()
{
if (_porcupineManager != null)
{
_porcupineManager.Delete();
}
}
}