MicrophoneInput.cs
1.79 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
//This file is deprecated. Use the high level voip system instead:
// https://developer.oculus.com/documentation/unity/ps-voip/
//
// NOTE for android developers: The existence of UnityEngine.Microphone causes Unity to insert the
// android.permission.RECORD_AUDIO permission into the AndroidManifest.xml generated at build time
#if OVR_PLATFORM_USE_MICROPHONE
using UnityEngine;
using System.Collections.Generic;
namespace Oculus.Platform
{
public class MicrophoneInput : IMicrophone
{
AudioClip microphoneClip;
int lastMicrophoneSample;
int micBufferSizeSamples;
private Dictionary<int, float[]> micSampleBuffers;
public MicrophoneInput()
{
int bufferLenSeconds = 1; //minimum size unity allows
int inputFreq = 48000; //this frequency is fixed throughout the voip system atm
microphoneClip = Microphone.Start(null, true, bufferLenSeconds, inputFreq);
micBufferSizeSamples = bufferLenSeconds * inputFreq;
micSampleBuffers = new Dictionary<int, float[]>();
}
public void Start()
{
}
public void Stop()
{
}
public float[] Update()
{
int pos = Microphone.GetPosition(null);
int copySize = 0;
if (pos < lastMicrophoneSample)
{
int endOfBufferSize = micBufferSizeSamples - lastMicrophoneSample;
copySize = endOfBufferSize + pos;
}
else
{
copySize = pos - lastMicrophoneSample;
}
if (copySize == 0) {
return null;
}
float[] samples;
if (!micSampleBuffers.TryGetValue(copySize, out samples))
{
samples = new float[copySize];
micSampleBuffers[copySize] = samples;
}
microphoneClip.GetData(samples, lastMicrophoneSample);
lastMicrophoneSample = pos;
return samples;
}
}
}
#endif