BlueGit

blue-star ggj-2020

Files

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

VoipAudioSourceHiLevel.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
VoipAudioSourceHiLevel.cs 3.9 KiB Download Raw
1namespace Oculus.Platform
2{
3 using UnityEngine;
4 using System;
5 using System.Collections.Generic;
6
7 public class VoipAudioSourceHiLevel : MonoBehaviour
8 {
9 // This is a delegate that exists as a surface for OnAudioFilterRead
10 // It will be callled on unity's audio thread
11 public class FilterReadDelegate : MonoBehaviour
12 {
13 public VoipAudioSourceHiLevel parent;
14 float[] scratchBuffer;
15
16 void Awake()
17 {
18 int bufferSizeElements = (int)CAPI.ovr_Voip_GetOutputBufferMaxSize();
19 scratchBuffer = new float[bufferSizeElements];
20 }
21
22 void OnAudioFilterRead(float[] data, int channels)
23 {
24 int sizeToFetch = data.Length / channels;
25 int sourceBufferSize = sizeToFetch;
26 if (sourceBufferSize > scratchBuffer.Length)
27 {
28 Array.Clear(data, 0, data.Length);
29 throw new Exception(string.Format("Audio system tried to pull {0} bytes, max voip internal ring buffer size {1}", sizeToFetch, scratchBuffer.Length));
30 }
31
32 int available = parent.pcmSource.PeekSizeElements();
33 if (available < sourceBufferSize)
34 {
35 if (verboseLogging)
36 {
37 Debug.LogFormat(
38 "Voip starved! Want {0}, but only have {1} available",
39 sourceBufferSize,
40 available);
41 }
42 return;
43 }
44
45 int copied = parent.pcmSource.GetPCM(scratchBuffer, sourceBufferSize);
46 if (copied < sourceBufferSize)
47 {
48 Debug.LogWarningFormat(
49 "GetPCM() returned {0} samples, expected {1}",
50 copied,
51 sourceBufferSize);
52
53 return;
54 }
55
56 int dest = 0;
57 float tmpPeakAmp = -1;
58 for (int i = 0; i < sizeToFetch; i++)
59 {
60 float val = scratchBuffer[i];
61
62 for (int j = 0; j < channels; j++)
63 {
64 data[dest++] = val;
65 if (val > tmpPeakAmp)
66 {
67 tmpPeakAmp = val;
68 }
69 }
70 }
71 parent.peakAmplitude = tmpPeakAmp;
72 }
73 }
74
75
76 int initialPlaybackDelayMS;
77 public UInt64 senderID
78 {
79 set
80 {
81 pcmSource.SetSenderID(value);
82 }
83 }
84
85 public AudioSource audioSource;
86 public float peakAmplitude;
87
88 protected IVoipPCMSource pcmSource;
89
90 static int audioSystemPlaybackFrequency;
91 static bool verboseLogging = false;
92
93 protected void Stop() {}
94
95 VoipSampleRate SampleRateToEnum(int rate) {
96 switch(rate) {
97 case 48000:
98 return VoipSampleRate.HZ48000;
99 case 44100:
100 return VoipSampleRate.HZ44100;
101 case 24000:
102 return VoipSampleRate.HZ24000;
103 default:
104 return VoipSampleRate.Unknown;
105 }
106 }
107
108 protected void Awake()
109 {
110 CreatePCMSource();
111 if(audioSource == null) {
112 audioSource = gameObject.AddComponent<AudioSource>();
113 }
114 audioSource.gameObject.AddComponent<FilterReadDelegate>();
115 var filterDelegate = audioSource.gameObject.GetComponent<FilterReadDelegate>();
116 filterDelegate.parent = this;
117
118 initialPlaybackDelayMS = 40;
119
120 audioSystemPlaybackFrequency = AudioSettings.outputSampleRate;
121 CAPI.ovr_Voip_SetOutputSampleRate(SampleRateToEnum(audioSystemPlaybackFrequency));
122 if(verboseLogging) {
123 Debug.LogFormat("freq {0}", audioSystemPlaybackFrequency);
124 }
125 }
126
127 void Start() {
128 audioSource.Stop();
129 }
130
131 protected virtual void CreatePCMSource()
132 {
133 pcmSource = new VoipPCMSourceNative();
134 }
135
136 protected static int MSToElements(int ms) {
137 return ms * audioSystemPlaybackFrequency / 1000;
138 }
139
140 void Update()
141 {
142 pcmSource.Update();
143
144 if (!audioSource.isPlaying && pcmSource.PeekSizeElements() >= MSToElements(initialPlaybackDelayMS)) {
145 if(verboseLogging) {
146 Debug.LogFormat("buffered {0} elements, starting playback", pcmSource.PeekSizeElements());
147 }
148 audioSource.Play();
149 }
150 }
151 }
152}