Repository navigation
Expand file tree
/
Copy pathMicrophoneWidget.cs
More file actions
368 lines (325 loc) · 10.6 KB
/
Copy pathMicrophoneWidget.cs
File metadata and controls
368 lines (325 loc) · 10.6 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 IBM.Watson.DeveloperCloud.DataTypes;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#pragma warning disable 414
namespace IBM.Watson.DeveloperCloud.Widgets
{
/// <summary>
/// This widget records audio from the microphone device.
/// </summary>
public class MicrophoneWidget : Widget
{
#region Inputs
[SerializeField]
private Input m_DisableInput = new Input("Disable", typeof(DisableMicData), "OnDisableInput");
#endregion
#region Outputs
[SerializeField]
private Output m_AudioOutput = new Output(typeof(AudioData));
[SerializeField]
private Output m_LevelOutput = new Output(typeof(LevelData));
[SerializeField]
private Output m_ActivateOutput = new Output(typeof(BooleanData));
#endregion
#region Private Data
private bool m_Active = false;
private bool m_Disabled = false;
private bool m_Failure = false;
private DateTime m_LastFailure = DateTime.Now;
[SerializeField]
private bool m_ActivateOnStart = true;
[SerializeField, Tooltip("Size of recording buffer in seconds.")]
private int m_RecordingBufferSize = 2;
[SerializeField]
private int m_RecordingHZ = 22050; // default recording HZ
[SerializeField, Tooltip("ID of the microphone to use.")]
private string m_MicrophoneID = null; // what microphone to use for recording.
[SerializeField, Tooltip("How often to sample for level output.")]
private float m_LevelOutputInterval = 0.05f;
[SerializeField]
private float m_LevelOutputModifier = 1.0f;
[SerializeField, Tooltip("If true, microphone will playback recorded audio on stop.")]
private bool m_PlaybackRecording = false;
[SerializeField]
private Text m_StatusText = null;
private int m_RecordingRoutine = 0; // ID of our co-routine when recording, 0 if not recording currently.
private AudioClip m_Recording = null;
private List<AudioClip> m_Playback = new List<AudioClip>();
#endregion
#region Constants
// how often to retry the open the microphone on failure
private const int RETRY_INTERVAL = 10000;
#endregion
#region Public Properties
/// <summary>
/// Returns a list of available microphone devices.
/// </summary>
public string[] Microphones { get { return Microphone.devices; } }
/// <summary>
/// True if microphone is active, false if inactive.
/// </summary>
public bool Active
{
get { return m_Active; }
set
{
if (m_Active != value)
{
m_Active = value;
if (m_Active && !m_Disabled)
StartRecording();
else
StopRecording();
}
}
}
/// <summary>
/// True if microphone is disabled, false if enabled.
/// </summary>
public bool Disable
{
get { return m_Disabled; }
set
{
if (m_Disabled != value)
{
m_Disabled = value;
if (m_Active && !m_Disabled)
StartRecording();
else
StopRecording();
}
}
}
/// <summary>
/// This is set to true when the microhphone fails, the update will continue to try to start
/// the microphone so long as it's active.
/// </summary>
public bool Failure
{
get { return m_Failure; }
private set
{
if (m_Failure != value)
{
m_Failure = value;
if (m_Failure)
m_LastFailure = DateTime.Now;
}
}
}
/// <summary>
/// Button handler for toggling the active state.
/// </summary>
public void OnToggleActive()
{
Active = !Active;
}
#endregion
#region Public Functions
/// <summary>
/// Activates the microphone.
/// </summary>
public void ActivateMicrophone()
{
Active = true;
}
/// <summary>
/// Deactivates the microphone.
/// </summary>
public void DeactivateMicrophone()
{
Active = false;
}
#endregion
#region Widget interface
/// <exclude />
protected override string GetName()
{
return "Microphone";
}
#endregion
#region Event Handlers
/// <exclude />
protected override void Start()
{
base.Start();
if (m_ActivateOnStart)
Active = true;
}
private void Update()
{
if (Failure && Active && !Disable
&& (DateTime.Now - m_LastFailure).TotalMilliseconds > RETRY_INTERVAL)
{
// try to restart the recording..
StartRecording();
}
}
private void OnDisableInput(Data data)
{
Disable = ((DisableMicData)data).Boolean;
}
void OnDestroy()
{
Active = false;
Disable = true;
}
#endregion
#region Recording Functions
private void StartRecording()
{
if (m_RecordingRoutine == 0)
{
UnityObjectUtil.StartDestroyQueue();
m_RecordingRoutine = Runnable.Run(RecordingHandler());
m_ActivateOutput.SendData(new BooleanData(true));
if (m_StatusText != null)
m_StatusText.text = "RECORDING";
}
}
private void StopRecording()
{
if (m_RecordingRoutine != 0)
{
Microphone.End(m_MicrophoneID);
Runnable.Stop(m_RecordingRoutine);
m_RecordingRoutine = 0;
m_ActivateOutput.SendData(new BooleanData(false));
if (m_StatusText != null)
m_StatusText.text = "STOPPED";
if (m_PlaybackRecording && m_Playback.Count > 0)
{
AudioClip combined = AudioClipUtil.Combine(m_Playback.ToArray());
if (combined != null)
{
AudioSource source = GetComponentInChildren<AudioSource>();
if (source != null)
{
// destroy any previous audio clip..
if (source.clip != null)
UnityObjectUtil.DestroyUnityObject(source.clip);
source.spatialBlend = 0.0f; // 2D sound
source.loop = false; // do not loop
source.clip = combined; // clip
source.Play();
}
else
Log.Warning("MicrophoneWidget", "Failed to find AudioSource.");
}
foreach (var clip in m_Playback)
UnityObjectUtil.DestroyUnityObject(clip);
m_Playback.Clear();
}
}
}
private IEnumerator RecordingHandler()
{
Failure = false;
#if UNITY_WEBPLAYER
yield return Application.RequestUserAuthorization( UserAuthorization.Microphone );
#endif
m_Recording = Microphone.Start(m_MicrophoneID, true, m_RecordingBufferSize, m_RecordingHZ);
yield return null; // let m_RecordingRoutine get set..
if (m_Recording == null)
{
Failure = true;
StopRecording();
yield break;
}
bool bFirstBlock = true;
int midPoint = m_Recording.samples / 2;
bool bOutputLevelData = m_LevelOutput.IsConnected;
bool bOutputAudio = m_AudioOutput.IsConnected || m_PlaybackRecording;
int lastReadPos = 0;
float[] samples = null;
while (m_RecordingRoutine != 0 && m_Recording != null)
{
int writePos = Microphone.GetPosition(m_MicrophoneID);
if (writePos > m_Recording.samples || !Microphone.IsRecording(m_MicrophoneID))
{
Log.Error("MicrophoneWidget", "Microphone disconnected.");
Failure = true;
StopRecording();
yield break;
}
if (bOutputAudio)
{
if ((bFirstBlock && writePos >= midPoint)
|| (!bFirstBlock && writePos < midPoint))
{
// front block is recorded, make a RecordClip and pass it onto our callback.
samples = new float[midPoint];
m_Recording.GetData(samples, bFirstBlock ? 0 : midPoint);
AudioData record = new AudioData();
record.MaxLevel = Mathf.Max(samples);
record.Clip = AudioClip.Create("Recording", midPoint, m_Recording.channels, m_RecordingHZ, false);
record.Clip.SetData(samples, 0);
if (m_PlaybackRecording)
m_Playback.Add(record.Clip);
if (m_AudioOutput.IsConnected && !m_AudioOutput.SendData(record))
StopRecording(); // automatically stop recording if the callback goes away.
bFirstBlock = !bFirstBlock;
}
else
{
// calculate the number of samples remaining until we ready for a block of audio,
// and wait that amount of time it will take to record.
int remaining = bFirstBlock ? (midPoint - writePos) : (m_Recording.samples - writePos);
float timeRemaining = (float)remaining / (float)m_RecordingHZ;
if (bOutputLevelData && timeRemaining > m_LevelOutputInterval)
timeRemaining = m_LevelOutputInterval;
yield return new WaitForSeconds(timeRemaining);
}
}
else
{
yield return new WaitForSeconds(m_LevelOutputInterval);
}
if (m_Recording != null && bOutputLevelData)
{
float fLevel = 0.0f;
if (writePos < lastReadPos)
{
// write has wrapped, grab the last bit from the buffer..
samples = new float[m_Recording.samples - lastReadPos];
m_Recording.GetData(samples, lastReadPos);
fLevel = Mathf.Max(fLevel, Mathf.Max(samples));
lastReadPos = 0;
}
if (lastReadPos < writePos)
{
samples = new float[writePos - lastReadPos];
m_Recording.GetData(samples, lastReadPos);
fLevel = Mathf.Max(fLevel, Mathf.Max(samples));
lastReadPos = writePos;
}
m_LevelOutput.SendData(new LevelData(fLevel * m_LevelOutputModifier, m_LevelOutputModifier));
}
}
yield break;
}
#endregion
}
}