Skip to content

Commit

Permalink
Handle "any" sample rate in InputDeviceBlockReader
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Dec 4, 2023
1 parent e45707b commit 2a69ba7
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions CavernUnity DLL/Input/InputDeviceBlockReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class InputDeviceBlockReader : MonoBehaviour {
/// Returns the most recent samples recorded, whatever the inner state of recording is.
/// </summary>
public float[] ForceRead() {
int pos = MultiplatformMicrophone.GetPosition(deviceName);
int pos = MultiplatformMicrophone.GetPosition(activeDevice);
if (buffer != null) {
buffer.GetData(frame, pos > blockSize ? pos - blockSize : (sampleRate + pos - blockSize));
} else {
Expand All @@ -85,24 +85,26 @@ void OnEnable() {

activeDevice = deviceName;
if (Application.platform != RuntimePlatform.Android) {
MultiplatformMicrophone.GetDeviceCaps(deviceName, out int minFreq, out int maxFreq);
sampleRate = Math.Clamp(sampleRate, minFreq, maxFreq);
buffer = MultiplatformMicrophone.Start(deviceName, true, 1, sampleRate);
MultiplatformMicrophone.GetDeviceCaps(activeDevice, out int minFreq, out int maxFreq);
if (minFreq != 0 && maxFreq != 0) {
sampleRate = Math.Clamp(sampleRate, minFreq, maxFreq);
}
buffer = MultiplatformMicrophone.Start(activeDevice, true, 1, sampleRate);
} else { // Fix for a Unity bug that's a feature: on Android, GetDeviceCaps always reports 16 kHz
buffer = MultiplatformMicrophone.Start(deviceName, true, 1, sampleRate);
buffer = MultiplatformMicrophone.Start(activeDevice, true, 1, sampleRate);
if (buffer == null) {
int[] androidSampleRates = new[] { 8000, 16000, 22050, 32000, 44100, 48000, 88200, 96000, 192000 };
int index = Array.BinarySearch(androidSampleRates, sampleRate);
if (index < 0) {
index = ~index;
}
for (int i = index; i < androidSampleRates.Length; i++) {
if (buffer = MultiplatformMicrophone.Start(deviceName, true, 1, sampleRate = androidSampleRates[i])) {
if (buffer = MultiplatformMicrophone.Start(activeDevice, true, 1, sampleRate = androidSampleRates[i])) {
return;
}
}
for (int i = 0; i < index; i++) {
if (buffer = MultiplatformMicrophone.Start(deviceName, true, 1, sampleRate = androidSampleRates[i])) {
if (buffer = MultiplatformMicrophone.Start(activeDevice, true, 1, sampleRate = androidSampleRates[i])) {
return;
}
}
Expand All @@ -124,7 +126,7 @@ void Update() {
return;
}

int pos = MultiplatformMicrophone.GetPosition(deviceName);
int pos = MultiplatformMicrophone.GetPosition(activeDevice);
if (lastPosition > pos) {
lastPosition -= sampleRate;
}
Expand All @@ -135,11 +137,11 @@ void Update() {
}
}

[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "Unity lifecycle")]
void OnDisable() {
if (MultiplatformMicrophone.IsRecording(deviceName)) {
MultiplatformMicrophone.End(deviceName);
if (MultiplatformMicrophone.IsRecording(activeDevice)) {
MultiplatformMicrophone.End(activeDevice);
Destroy(buffer);
buffer = null;
}
}
}
Expand Down

0 comments on commit 2a69ba7

Please sign in to comment.