forked from Picovoice/picovoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicovoiceDemo.cs
241 lines (211 loc) · 7.23 KB
/
PicovoiceDemo.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using Pv.Unity;
public class PicovoiceDemo : MonoBehaviour
{
Text _activityText;
Image[] _locationStates;
PicovoiceManager _picovoiceManager;
private static readonly string _platform;
private static readonly string _keywordPath;
private static readonly string _contextPath;
private readonly Dictionary<string, Color> _colourLookup = new Dictionary<string, Color>()
{
{ "none", new Color(0,0,0,1) },
{ "blue", new Color(0,0,1,1) },
{ "green", new Color(0,1,0,1) },
{ "orange", new Color(0.5f,1,0,1) },
{ "pink", new Color(1,0,0.5f,1) },
{ "purple", new Color(1,0,1,1) },
{ "red", new Color(1,0,0,1) },
{ "white", new Color(1,1,1,1) },
{ "yellow", new Color(1,1,0,1) },
};
static PicovoiceDemo()
{
_platform = GetPlatform();
_keywordPath = GetKeywordPath();
_contextPath = GetContextPath();
}
void Start()
{
_activityText = gameObject.GetComponentInChildren<Text>();
_locationStates = gameObject.GetComponentsInChildren<Image>();
_picovoiceManager = new PicovoiceManager(_keywordPath, OnWakeWordDetected, _contextPath, OnInferenceResult);
}
private void OnWakeWordDetected()
{
_activityText.text = "Listening...";
}
private void OnInferenceResult(Inference inference)
{
if (inference.IsUnderstood)
{
if (inference.Intent == "changeColor")
{
Color newColour = _colourLookup["white"];
if (inference.Slots.ContainsKey("color"))
{
newColour = _colourLookup[inference.Slots["color"]];
}
Image[] locations = _locationStates;
if (inference.Slots.ContainsKey("location"))
{
string locationName = inference.Slots["location"];
locations = _locationStates.Where(g => g.name == locationName).ToArray();
}
ChangeLightColour(locations, newColour);
}
else if (inference.Intent == "changeLightState")
{
bool state = false;
if (inference.Slots.ContainsKey("state"))
{
state = inference.Slots["state"] == "on";
}
Image[] locations = _locationStates;
if (inference.Slots.ContainsKey("location"))
{
string locationName = inference.Slots["location"];
locations = _locationStates.Where(g => g.name == locationName).ToArray();
}
ChangeLightState(locations, state);
}
else if (inference.Intent == "changeLightStateOff")
{
Image[] locations = _locationStates;
if (inference.Slots.ContainsKey("location"))
{
string locationName = inference.Slots["location"];
locations = _locationStates.Where(g => g.name == locationName).ToArray();
}
ChangeLightState(locations, false);
}
}
else
{
Debug.Log("Didn't understand the command.\n");
}
_activityText.text = "Say 'Picovoice'!";
}
private void ChangeLightState(Image[] locations, bool state)
{
float alphaValue = state ? 1 : 0.1f;
for (int i = 0; i < locations.Length; i++)
{
Color c = locations[i].color;
c.a = alphaValue;
locations[i].color = c;
}
}
private void ChangeLightColour(Image[] locations, Color colour)
{
for (int i = 0; i < locations.Length; i++)
{
locations[i].color = colour;
}
}
void Update()
{
if (!_picovoiceManager.IsRecording)
{
if (_picovoiceManager.IsAudioDeviceAvailable())
{
try
{
_picovoiceManager.Start();
}
catch (Exception ex)
{
Debug.LogError(ex.ToString());
}
}
else
Debug.LogError("No audio recording device available!");
}
}
void OnApplicationQuit()
{
if (_picovoiceManager != null)
{
_picovoiceManager.Stop();
}
}
private static string GetPlatform()
{
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
case RuntimePlatform.WindowsPlayer:
return "windows";
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
return "mac";
case RuntimePlatform.LinuxEditor:
case RuntimePlatform.LinuxPlayer:
return "linux";
case RuntimePlatform.IPhonePlayer:
return "ios";
case RuntimePlatform.Android:
return "android";
default:
throw new NotSupportedException(string.Format("Platform '{0}' not supported by Picovoice Unity binding", Application.platform));
}
}
public static string GetKeywordPath()
{
string fileName = string.Format("picovoice_{0}.ppn", _platform);
string srcPath = Path.Combine(Application.streamingAssetsPath, string.Format("keyword_files/{0}/{1}", _platform, fileName));
#if !UNITY_EDITOR && UNITY_ANDROID
string dstPath = Path.Combine(Application.persistentDataPath, string.Format("keyword_files/{0}", _platform));
if (!Directory.Exists(dstPath))
{
Directory.CreateDirectory(dstPath);
}
dstPath = Path.Combine(dstPath, fileName);
return ExtractResource(srcPath, dstPath);
#else
return srcPath;
#endif
}
public static string GetContextPath()
{
string fileName = string.Format("smart_lighting_{0}.rhn", _platform);
string srcPath = Path.Combine(Application.streamingAssetsPath, string.Format("contexts/{0}/{1}", _platform, fileName));
#if !UNITY_EDITOR && UNITY_ANDROID
string dstPath = Path.Combine(Application.persistentDataPath, string.Format("contexts/{0}", _platform));
if (!Directory.Exists(dstPath))
{
Directory.CreateDirectory(dstPath);
}
dstPath = Path.Combine(dstPath, fileName);
return ExtractResource(srcPath, dstPath);
#else
return srcPath;
#endif
}
#if !UNITY_EDITOR && UNITY_ANDROID
public static string ExtractResource(string srcPath, string dstPath)
{
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
}