BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Platform / Scripts / MicrophoneInput.cs

MicrophoneInput.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
MicrophoneInput.cs 1.8 KiB Download Raw
1//This file is deprecated. Use the high level voip system instead:
2// https://developer.oculus.com/documentation/platform/latest/concepts/dg-cc-voip/
3//
4// NOTE for android developers: The existence of UnityEngine.Microphone causes Unity to insert the
5// android.permission.RECORD_AUDIO permission into the AndroidManifest.xml generated at build time
6
7#if OVR_PLATFORM_USE_MICROPHONE
8using UnityEngine;
9using System.Collections.Generic;
10
11namespace Oculus.Platform
12{
13
14 public class MicrophoneInput : IMicrophone
15 {
16 AudioClip microphoneClip;
17 int lastMicrophoneSample;
18 int micBufferSizeSamples;
19
20 private Dictionary<int, float[]> micSampleBuffers;
21
22 public MicrophoneInput()
23 {
24 int bufferLenSeconds = 1; //minimum size unity allows
25 int inputFreq = 48000; //this frequency is fixed throughout the voip system atm
26 microphoneClip = Microphone.Start(null, true, bufferLenSeconds, inputFreq);
27 micBufferSizeSamples = bufferLenSeconds * inputFreq;
28
29 micSampleBuffers = new Dictionary<int, float[]>();
30 }
31
32 public void Start()
33 {
34
35 }
36
37 public void Stop()
38 {
39 }
40
41 public float[] Update()
42 {
43 int pos = Microphone.GetPosition(null);
44 int copySize = 0;
45 if (pos < lastMicrophoneSample)
46 {
47 int endOfBufferSize = micBufferSizeSamples - lastMicrophoneSample;
48 copySize = endOfBufferSize + pos;
49 }
50 else
51 {
52 copySize = pos - lastMicrophoneSample;
53 }
54
55 if (copySize == 0) {
56 return null;
57 }
58
59 float[] samples;
60 if (!micSampleBuffers.TryGetValue(copySize, out samples))
61 {
62 samples = new float[copySize];
63 micSampleBuffers[copySize] = samples;
64 }
65
66 microphoneClip.GetData(samples, lastMicrophoneSample);
67 lastMicrophoneSample = pos;
68 return samples;
69
70 }
71 }
72}
73#endif