Skip to content

Commit

Permalink
Optional overlap for InputDeviceBlockReader
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Dec 6, 2023
1 parent f176ea3 commit 69e296c
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions CavernUnity DLL/Input/InputDeviceBlockReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class InputDeviceBlockReader : MonoBehaviour {
/// Target device sample rate. Will be overridden if the device doesn't support it.
/// Only updated when the component is enabled.
/// </summary>
[Tooltip("Target device sample rate. Will be overridden if the device doesn't support it." +
" Only updated when the component is enabled.")]
[Tooltip("Target device sample rate. Will be overridden if the device doesn't support it. " +
"Only updated when the component is enabled.")]
public int sampleRate = 48000;

/// <summary>
Expand All @@ -44,6 +44,14 @@ public class InputDeviceBlockReader : MonoBehaviour {
[Tooltip("Amount of audio samples to be delivered per callback.")]
public int blockSize = 16384;

/// <summary>
/// Number of overlapping samples between frames when calling the <see cref="Callback"/>.
/// Helps mitigate the spectral distortions of windowing.
/// </summary>
[Tooltip("Number of overlapping samples between frames when calling the Callback. " +
"Helps mitigate the spectral distortions of windowing.")]
public int overlap;

/// <summary>
/// Clip to record to from the device.
/// </summary>
Expand Down Expand Up @@ -126,14 +134,19 @@ void Update() {
return;
}

int pos = MultiplatformMicrophone.GetPosition(activeDevice);
int pos = MultiplatformMicrophone.GetPosition(activeDevice),
interval = blockSize - overlap;
if (interval < 0) {
throw new ArgumentOutOfRangeException(nameof(overlap), "The overlap can't be larger than the block size.");
}

if (lastPosition > pos) {
lastPosition -= sampleRate;
}
while (lastPosition + blockSize < pos) {
buffer.GetData(frame, lastPosition < 0 ? lastPosition + buffer.samples : lastPosition);
while (lastPosition + interval < pos) {
buffer.GetData(frame, lastPosition < 0 ? lastPosition + sampleRate : lastPosition);
Callback?.Invoke(frame);
lastPosition += blockSize;
lastPosition += interval;
}
}

Expand Down

0 comments on commit 69e296c

Please sign in to comment.