-
Notifications
You must be signed in to change notification settings - Fork 85
/
RhinoDemo.cs
239 lines (210 loc) · 7.21 KB
/
RhinoDemo.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
//
// 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 System.IO;
using UnityEngine;
using UnityEngine.UI;
using Pv.Unity;
public class RhinoDemo : MonoBehaviour
{
private const string ACCESS_KEY = "${YOUR_ACCESS_KEY_HERE}"; // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
Button _startButton;
Image[] _locationStates;
Text _errorMessage;
private bool _isProcessing;
RhinoManager _rhinoManager;
private static readonly string _contextPath;
private static readonly string _platform;
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 RhinoDemo()
{
_platform = GetPlatform();
_contextPath = GetContextPath();
}
void Start()
{
_startButton = gameObject.GetComponentInChildren<Button>();
_startButton.onClick.AddListener(ToggleProcessing);
_locationStates = gameObject.GetComponentsInChildren<Image>().Where(i => i.name != "ToggleListeningButton").ToArray();
_errorMessage = gameObject.transform.Find("ErrorMessage").GetComponent<Text>();
try
{
_rhinoManager = RhinoManager.Create(ACCESS_KEY, _contextPath, OnInferenceResult, processErrorCallback: ErrorCallback);
}
catch (RhinoInvalidArgumentException ex)
{
SetError(ex.Message);
}
catch (RhinoActivationException)
{
SetError("AccessKey activation error");
}
catch (RhinoActivationLimitException)
{
SetError("AccessKey reached its device limit");
}
catch (RhinoActivationRefusedException)
{
SetError("AccessKey refused");
}
catch (RhinoActivationThrottledException)
{
SetError("AccessKey has been throttled");
}
catch (RhinoException ex)
{
SetError("RhinoManager was unable to initialize: " + ex.Message);
}
}
private void ToggleProcessing()
{
if (!_isProcessing)
{
StartProcessing();
}
}
private void StartProcessing()
{
(_startButton.targetGraphic as Text).text = "...";
_startButton.enabled = false;
_rhinoManager.Process();
_isProcessing = true;
}
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");
}
(_startButton.targetGraphic as Text).text = "Start Listening";
_startButton.enabled = true;
_isProcessing = false;
}
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;
}
}
private void ErrorCallback(RhinoException e)
{
SetError(e.Message);
}
private void SetError(string message)
{
_errorMessage.text = message;
_startButton.interactable = false;
}
void Update()
{
}
void OnApplicationQuit()
{
if (_rhinoManager != null)
{
_rhinoManager.Delete();
}
}
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 Rhino Unity binding", Application.platform));
}
}
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));
return srcPath;
}
}