forked from oculus-samples/voicesdk-samples-whisperer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppVoiceExperience.cs
executable file
·347 lines (299 loc) · 12.2 KB
/
AppVoiceExperience.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus SDK
* 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 Meta.WitAi;
using Meta.WitAi.Configuration;
using Meta.WitAi.Data;
using Meta.WitAi.Data.Configuration;
using Meta.WitAi.Interfaces;
using Meta.WitAi.Json;
using Oculus.Voice.Bindings.Android;
using Oculus.Voice.Core.Bindings.Android.PlatformLogger;
using Oculus.Voice.Core.Bindings.Interfaces;
using Oculus.Voice.Interfaces;
using Oculus.VoiceSDK.Utilities;
using UnityEngine;
namespace Oculus.Voice
{
[HelpURL("https://developer.oculus.com/experimental/voice-sdk/tutorial-overview/")]
public class AppVoiceExperience : VoiceService, IWitRuntimeConfigProvider
{
[SerializeField] private WitRuntimeConfiguration witRuntimeConfiguration;
[Tooltip("Uses platform services to access wit.ai instead of accessing wit directly from within the application.")]
[SerializeField] private bool usePlatformServices;
[Tooltip("Enables logs related to the interaction to be displayed on console")]
[SerializeField] private bool enableConsoleLogging;
public WitRuntimeConfiguration RuntimeConfiguration
{
get => witRuntimeConfiguration;
set => witRuntimeConfiguration = value;
}
private IPlatformVoiceService platformService;
private IVoiceService voiceServiceImpl;
private IVoiceSDKLogger voiceSDKLoggerImpl;
#if UNITY_ANDROID && !UNITY_EDITOR
// This version is auto-updated for a release build
private readonly string PACKAGE_VERSION = "50.0.0.94.257";
#endif
private bool Initialized => null != voiceServiceImpl;
public event Action OnInitialized;
#region Voice Service Properties
public override bool Active => null != voiceServiceImpl && voiceServiceImpl.Active;
public override bool IsRequestActive => null != voiceServiceImpl && voiceServiceImpl.IsRequestActive;
public override ITranscriptionProvider TranscriptionProvider
{
get => voiceServiceImpl.TranscriptionProvider;
set => voiceServiceImpl.TranscriptionProvider = value;
}
public override bool MicActive => null != voiceServiceImpl && voiceServiceImpl.MicActive;
protected override bool ShouldSendMicData => witRuntimeConfiguration.sendAudioToWit ||
null == TranscriptionProvider;
#endregion
#if UNITY_ANDROID && !UNITY_EDITOR
public bool HasPlatformIntegrations => usePlatformServices && voiceServiceImpl is VoiceSDKImpl;
#else
public bool HasPlatformIntegrations => false;
#endif
public bool EnableConsoleLogging => enableConsoleLogging;
public bool UsePlatformIntegrations
{
get => usePlatformServices;
set
{
// If we're trying to turn on platform services and they're not currently active we
// will forcibly reinit and try to set the state.
if (usePlatformServices != value || HasPlatformIntegrations != value)
{
usePlatformServices = value;
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log($"{(usePlatformServices ? "Enabling" : "Disabling")} platform integration.");
InitVoiceSDK();
#endif
}
}
}
#region Voice Service Methods
public override void Activate(string text, WitRequestOptions options)
{
if (IsNetworkPresent())
{
voiceSDKLoggerImpl.LogInteractionStart(options.requestID, "message");
voiceServiceImpl.Activate(text, options);
}
}
public override void Activate(WitRequestOptions options)
{
if (IsMicPresent() && IsNetworkPresent())
{
voiceSDKLoggerImpl.LogInteractionStart(options.requestID, "speech");
voiceServiceImpl.Activate(options);
}
}
public override void ActivateImmediately(WitRequestOptions options)
{
if (IsMicPresent() && IsNetworkPresent())
{
voiceSDKLoggerImpl.LogInteractionStart(options.requestID, "speech");
voiceServiceImpl.ActivateImmediately(options);
}
}
private bool IsMicPresent()
{
if (!HasPlatformIntegrations && !AudioBuffer.Instance.IsInputAvailable)
{
VoiceEvents.OnError?.Invoke("No Microphone present",
"No Microphone(s)/recording devices found. You will be unable to capture audio on this device.");
return false;
}
return true;
}
private bool IsNetworkPresent()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
VoiceEvents.OnError?.Invoke("Network unreachable", "Unable to reach the internet. Check your connection.");
return false;
}
return true;
}
public override void Deactivate()
{
voiceServiceImpl.Deactivate();
}
public override void DeactivateAndAbortRequest()
{
voiceServiceImpl.DeactivateAndAbortRequest();
}
#endregion
private void InitVoiceSDK()
{
// Clean up if we're switching to native C# wit impl
if (!UsePlatformIntegrations && voiceServiceImpl is VoiceSDKImpl)
{
((VoiceSDKImpl) voiceServiceImpl).Disconnect();
}
#if UNITY_ANDROID && !UNITY_EDITOR
// Do not re-init logging if we've already initialized logger
if (voiceSDKLoggerImpl == null)
{
var loggerImpl = new VoiceSDKPlatformLoggerImpl();
loggerImpl.Connect(PACKAGE_VERSION);
voiceSDKLoggerImpl = loggerImpl;
}
if (UsePlatformIntegrations)
{
Debug.Log("Checking platform capabilities...");
var platformImpl = new VoiceSDKImpl(this);
platformImpl.OnServiceNotAvailableEvent += () => RevertToWitUnity();
platformImpl.Connect(PACKAGE_VERSION);
platformImpl.SetRuntimeConfiguration(RuntimeConfiguration);
if (platformImpl.PlatformSupportsWit)
{
voiceServiceImpl = platformImpl;
if (voiceServiceImpl is Wit wit)
{
wit.RuntimeConfiguration = witRuntimeConfiguration;
}
voiceServiceImpl.VoiceEvents = VoiceEvents;
voiceSDKLoggerImpl.IsUsingPlatformIntegration = true;
}
else
{
Debug.Log("Platform registration indicated platform support is not currently available.");
RevertToWitUnity();
}
}
else
{
RevertToWitUnity();
}
#else
voiceSDKLoggerImpl = new VoiceSDKConsoleLoggerImpl();
RevertToWitUnity();
#endif
voiceSDKLoggerImpl.WitApplication = RuntimeConfiguration?.witConfiguration?.GetLoggerAppId();
voiceSDKLoggerImpl.ShouldLogToConsole = EnableConsoleLogging;
OnInitialized?.Invoke();
}
private void RevertToWitUnity()
{
Wit w = GetComponent<Wit>();
if (null == w)
{
w = gameObject.AddComponent<Wit>();
w.hideFlags = HideFlags.HideInInspector;
}
voiceServiceImpl = w;
if (voiceServiceImpl is Wit wit)
{
wit.RuntimeConfiguration = witRuntimeConfiguration;
}
voiceServiceImpl.VoiceEvents = VoiceEvents;
voiceSDKLoggerImpl.IsUsingPlatformIntegration = false;
}
protected override void OnEnable()
{
base.OnEnable();
if (MicPermissionsManager.HasMicPermission())
{
InitVoiceSDK();
}
else
{
MicPermissionsManager.RequestMicPermission();
}
#if UNITY_ANDROID && !UNITY_EDITOR
platformService?.SetRuntimeConfiguration(witRuntimeConfiguration);
#endif
// Logging
VoiceEvents.OnResponse?.AddListener(OnWitResponseListener);
VoiceEvents.OnAborted?.AddListener(OnAborted);
VoiceEvents.OnError?.AddListener(OnError);
VoiceEvents.OnStartListening?.AddListener(OnStartedListening);
VoiceEvents.OnStoppedListening?.AddListener(OnStoppedListening);
VoiceEvents.OnMicDataSent?.AddListener(OnMicDataSent);
}
protected override void OnDisable()
{
base.OnDisable();
#if UNITY_ANDROID
if (voiceServiceImpl is VoiceSDKImpl platformImpl)
{
platformImpl.Disconnect();
}
if (voiceSDKLoggerImpl is VoiceSDKPlatformLoggerImpl loggerImpl)
{
loggerImpl.Disconnect();
}
#endif
voiceServiceImpl = null;
// Logging
VoiceEvents.OnResponse?.RemoveListener(OnWitResponseListener);
VoiceEvents.OnAborted?.RemoveListener(OnAborted);
VoiceEvents.OnError?.RemoveListener(OnError);
VoiceEvents.OnStartListening?.RemoveListener(OnStartedListening);
VoiceEvents.OnStoppedListening?.RemoveListener(OnStoppedListening);
VoiceEvents.OnMicDataSent?.RemoveListener(OnMicDataSent);
}
private void OnApplicationFocus(bool hasFocus)
{
if (enabled && hasFocus && !Initialized)
{
if (MicPermissionsManager.HasMicPermission())
{
InitVoiceSDK();
}
}
}
#region Event listeners for logging
void OnWitResponseListener(WitResponseNode witResponseNode)
{
var tokens = witResponseNode?["speech"]?["tokens"];
if (tokens != null)
{
int speechTokensLength = tokens.Count;
string speechLength = witResponseNode["speech"]["tokens"][speechTokensLength - 1]?["end"]?.Value;
voiceSDKLoggerImpl.LogAnnotation("audioLength", speechLength);
}
voiceSDKLoggerImpl.LogInteractionEndSuccess();
}
void OnAborted()
{
voiceSDKLoggerImpl.LogInteractionEndFailure("aborted");
}
void OnError(string errorType, string errorMessage)
{
voiceSDKLoggerImpl.LogInteractionEndFailure($"{errorType}:{errorMessage}");
}
void OnStartedListening()
{
voiceSDKLoggerImpl.LogInteractionPoint("startedListening");
}
void OnStoppedListening()
{
voiceSDKLoggerImpl.LogInteractionPoint("stoppedListening");
}
void OnMicDataSent()
{
voiceSDKLoggerImpl.LogInteractionPoint("micDataSent");
}
#endregion
}
}