forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrophoneReceiver.cs
398 lines (340 loc) · 15.4 KB
/
MicrophoneReceiver.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Threading;
using UnityEngine;
using HoloToolkit.Unity;
namespace HoloToolkit.Sharing.VoiceChat
{
/// <summary>
/// Receives and plays voice data transmitted through the session server. This data comes from other clients running the MicrophoneTransmitter behaviour.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class MicrophoneReceiver : MonoBehaviour
{
private readonly BitManipulator versionExtractor = new BitManipulator(0x7, 0); // 3 bits, 0 shift
private readonly BitManipulator audioStreamCountExtractor = new BitManipulator(0x38, 3); // 3 bits, 3 shift
private readonly BitManipulator channelCountExtractor = new BitManipulator(0x1c0, 6); // 3 bits, 6 shift
private readonly BitManipulator sampleRateExtractor = new BitManipulator(0x600, 9); // 2 bits, 9 shift
private readonly BitManipulator sampleTypeExtractor = new BitManipulator(0x1800, 11); // 2 bits, 11 shift
private readonly BitManipulator sampleCountExtractor = new BitManipulator(0x7fe000, 13); // 10 bits, 13 shift
private readonly BitManipulator codecTypeExtractor = new BitManipulator(0x1800000, 23); // 2 bits, 23 shift
private readonly BitManipulator sequenceNumberExtractor = new BitManipulator(0x7C000000, 26); // 6 bits, 26 shift
public Transform GlobalAnchorTransform;
public class ProminentSpeakerInfo
{
public UInt32 SourceId;
public float AverageAmplitude;
public Vector3 HrtfPosition;
}
/// <summary>
/// Maximum number of prominent speakers we should ever encounter
/// </summary>
public const int MaximumProminentSpeakers = 4;
/// <summary>
/// The information for current prominent speakers
/// </summary>
private int prominentSpeakerCount;
/// <summary>
/// The information for current prominent speakers. NOTE: preallocated to avoid any runtime allocations.
/// </summary>
private ProminentSpeakerInfo[] prominentSpeakerList;
private NetworkConnectionAdapter listener;
private readonly Mutex audioDataMutex = new Mutex();
private const float KDropOffMaximum = 5f;
private const float KPanMaximum = 5f;
public float DropOffMaximumMetres = 5.0f;
public float PanMaximumMetres = 5.0f;
public float MinimumDistance = .01f;
private byte[] networkPacketBufferBytes;
private CircularBuffer circularBuffer;
#region DebugVariables
private readonly CircularBuffer testCircularBuffer = new CircularBuffer(48000 * 2 * 4 * 3, true);
private AudioSource testSource;
public AudioClip TestClip;
public bool SaveTestClip;
#endregion
private void Awake()
{
prominentSpeakerList = new ProminentSpeakerInfo[MaximumProminentSpeakers];
for (int prominentSpeaker = 0; prominentSpeaker < MaximumProminentSpeakers; prominentSpeaker++)
{
prominentSpeakerList[prominentSpeaker] = new ProminentSpeakerInfo();
}
networkPacketBufferBytes = new byte[4 * MicrophoneTransmitter.AudioPacketSize];
circularBuffer = new CircularBuffer(48000 * 4);
}
private void TryConnect()
{
try
{
if (listener == null)
{
SharingStage sharingStage = SharingStage.Instance;
if (sharingStage && sharingStage.Manager != null)
{
NetworkConnection connection = SharingStage.Instance.Manager.GetServerConnection();
listener = new NetworkConnectionAdapter();
listener.ConnectedCallback += OnConnected;
listener.DisconnectedCallback += OnDisconnected;
listener.ConnectionFailedCallback += OnConnectedFailed;
listener.MessageReceivedCallback += OnMessageReceived;
connection.AddListener((byte)MessageID.AudioSamples, listener);
Debug.Log("SpeakerController Start called");
}
}
}
catch (Exception ex)
{
Debug.Log("Exception: " + ex);
}
}
private void OnDestroy()
{
if (listener != null)
{
listener.ConnectedCallback -= OnConnected;
listener.DisconnectedCallback -= OnDisconnected;
listener.ConnectionFailedCallback -= OnConnectedFailed;
listener.MessageReceivedCallback -= OnMessageReceived;
}
}
private void OnConnected(NetworkConnection connection)
{
Profile.BeginRange("SpeakerController.OnConnected");
InternalStartSpeaker();
Debug.Log("SpeakerController: Connection to session server succeeded!");
Profile.EndRange();
}
private void OnDisconnected(NetworkConnection connection)
{
InternalStopSpeaker();
prominentSpeakerCount = 0;
Debug.Log("SpeakerController: Session server disconnected!");
}
private void OnConnectedFailed(NetworkConnection connection)
{
InternalStopSpeaker();
Debug.Log("SpeakerController: Connection to session server failed!");
}
/// <summary>
/// Starts playing the audio stream out to the speaker.
/// </summary>
private void InternalStartSpeaker()
{
GetComponent<AudioSource>().Play();
}
/// <summary>
/// Stops playing the audio stream out to the speaker.
/// </summary>
private void InternalStopSpeaker()
{
GetComponent<AudioSource>().Stop();
}
private void Update()
{
TryConnect();
AudioSource audioSource = GetComponent<AudioSource>();
GameObject remoteHead = GameObject.Find("mixamorig:Head");
if (remoteHead)
{
transform.parent = remoteHead.transform;
transform.localPosition = new Vector3();
transform.localRotation = Quaternion.identity;
audioSource.spatialize = true;
audioSource.spatialBlend = 1;
}
else
{
audioSource.spatialize = false;
audioSource.spatialBlend = 0;
}
#region debuginfo
if (SaveTestClip && testCircularBuffer.UsedCapacity == testCircularBuffer.TotalCapacity)
{
float[] testBuffer = new float[testCircularBuffer.UsedCapacity / 4];
testCircularBuffer.Read(testBuffer, 0, testBuffer.Length * 4);
testCircularBuffer.Reset();
TestClip = AudioClip.Create("testclip", testBuffer.Length / 2, 2, 48000, false);
TestClip.SetData(testBuffer, 0);
if (!testSource)
{
GameObject testObj = new GameObject("testclip");
testObj.transform.parent = transform;
testSource = testObj.AddComponent<AudioSource>();
}
testSource.PlayClip(TestClip, true);
SaveTestClip = false;
}
#endregion
}
/// <summary>
/// Now that we've gotten a message, examine it and dissect the audio data.
/// </summary>
/// <param name="connection"></param>
/// <param name="message"></param>
public void OnMessageReceived(NetworkConnection connection, NetworkInMessage message)
{
// Unused byte headerSize
message.ReadByte();
Int32 pack = message.ReadInt32();
// Unused int version
versionExtractor.GetBitsValue(pack);
int audioStreamCount = audioStreamCountExtractor.GetBitsValue(pack);
int channelCount = channelCountExtractor.GetBitsValue(pack);
int sampleRate = sampleRateExtractor.GetBitsValue(pack);
int sampleType = sampleTypeExtractor.GetBitsValue(pack);
int bytesPerSample = sizeof(float);
if (sampleType == 1)
{
bytesPerSample = sizeof(Int16);
}
int sampleCount = sampleCountExtractor.GetBitsValue(pack);
int codecType = codecTypeExtractor.GetBitsValue(pack);
// Unused int sequenceNumber
sequenceNumberExtractor.GetBitsValue(pack);
if (sampleRate == 0)
{
// Unused int extendedSampleRate
message.ReadInt32();
}
try
{
audioDataMutex.WaitOne();
prominentSpeakerCount = 0;
for (int i = 0; i < audioStreamCount; i++)
{
float averageAmplitude = message.ReadFloat();
UInt32 hrtfSourceID = (UInt32)message.ReadInt32();
Vector3 hrtfPosition = new Vector3();
Vector3 hrtfDirection = new Vector3();
if (hrtfSourceID != 0)
{
hrtfPosition.x = message.ReadFloat();
hrtfPosition.y = message.ReadFloat();
hrtfPosition.z = message.ReadFloat();
hrtfDirection.x = message.ReadFloat();
hrtfDirection.y = message.ReadFloat();
hrtfDirection.z = message.ReadFloat();
Vector3 cameraPosRelativeToGlobalAnchor = Vector3.zero;
Vector3 cameraDirectionRelativeToGlobalAnchor = Vector3.zero;
if (GlobalAnchorTransform != null)
{
cameraPosRelativeToGlobalAnchor = MathUtils.TransformPointFromTo(
null,
GlobalAnchorTransform,
CameraCache.Main.transform.position);
cameraDirectionRelativeToGlobalAnchor = MathUtils.TransformDirectionFromTo(
null,
GlobalAnchorTransform,
CameraCache.Main.transform.position);
}
cameraPosRelativeToGlobalAnchor.Normalize();
cameraDirectionRelativeToGlobalAnchor.Normalize();
Vector3 soundVector = hrtfPosition - cameraPosRelativeToGlobalAnchor;
soundVector.Normalize();
// x is forward
float fltx = (KDropOffMaximum / DropOffMaximumMetres) * Vector3.Dot(soundVector, cameraDirectionRelativeToGlobalAnchor);
// y is right
Vector3 myRight = Quaternion.Euler(0, 90, 0) * cameraDirectionRelativeToGlobalAnchor;
float flty = -(KPanMaximum / PanMaximumMetres) * Vector3.Dot(soundVector, myRight);
// z is up
Vector3 myUp = Quaternion.Euler(90, 0, 0) * cameraDirectionRelativeToGlobalAnchor;
float fltz = (KPanMaximum / PanMaximumMetres) * Vector3.Dot(soundVector, myUp);
// Hacky distance check so we don't get too close to source.
Vector3 flt = new Vector3(fltx, flty, fltz);
if (flt.magnitude < (MinimumDistance * KDropOffMaximum))
{
flt = flt.normalized * MinimumDistance * KDropOffMaximum;
fltx = flt.x;
flty = flt.y;
fltz = flt.z;
}
AddProminentSpeaker(hrtfSourceID, averageAmplitude, fltx, flty, fltz);
}
for (int j = 0; j < channelCount; j++)
{
// if uncompressed, size = sampleCount
Int16 size = (Int16)sampleCount;
if (codecType != 0)
{
// if compressed, size is first 2 bytes, sampleCount should be number of bytes after decompression
size = message.ReadInt16();
}
// make this array big enough to hold all of the uncompressed data only if the
// buffer is not the right size, minimize new operations
int totalBytes = size * bytesPerSample;
if (networkPacketBufferBytes.Length != totalBytes)
{
networkPacketBufferBytes = new byte[totalBytes];
}
message.ReadArray(networkPacketBufferBytes, (uint)(totalBytes));
if (codecType != 0)
{
// in place decompression please - should fill out the data buffer
// ...
}
if (hrtfSourceID > 0)
{
// TODO hrtf processing here
}
circularBuffer.Write(networkPacketBufferBytes, 0, networkPacketBufferBytes.Length);
}
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
finally
{
audioDataMutex.ReleaseMutex();
}
}
private void OnAudioFilterRead(float[] data, int numChannels)
{
try
{
audioDataMutex.WaitOne();
int byteCount = data.Length * 4;
circularBuffer.Read(data, 0, byteCount);
if (SaveTestClip)
{
testCircularBuffer.Write(data, 0, byteCount);
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
finally
{
audioDataMutex.ReleaseMutex();
}
}
private void AddProminentSpeaker(UInt32 sourceID, float averageAmplitude, float posX, float posY, float posZ)
{
if (prominentSpeakerCount < MaximumProminentSpeakers)
{
ProminentSpeakerInfo prominentSpeakerInfo = prominentSpeakerList[prominentSpeakerCount++];
prominentSpeakerInfo.SourceId = sourceID;
prominentSpeakerInfo.AverageAmplitude = averageAmplitude;
prominentSpeakerInfo.HrtfPosition.x = posX;
prominentSpeakerInfo.HrtfPosition.y = posY;
prominentSpeakerInfo.HrtfPosition.z = posZ;
}
}
public int GetNumProminentSpeakers()
{
return prominentSpeakerCount;
}
public ProminentSpeakerInfo GetProminentSpeaker(int index)
{
if (index < prominentSpeakerCount)
{
return prominentSpeakerList[index];
}
return null;
}
}
}