diff --git a/CavernUnity DLL/Input/InputDeviceBlockReader.cs b/CavernUnity DLL/Input/InputDeviceBlockReader.cs index 304bb540..6aab6998 100644 --- a/CavernUnity DLL/Input/InputDeviceBlockReader.cs +++ b/CavernUnity DLL/Input/InputDeviceBlockReader.cs @@ -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. /// - [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; /// @@ -44,6 +44,14 @@ public class InputDeviceBlockReader : MonoBehaviour { [Tooltip("Amount of audio samples to be delivered per callback.")] public int blockSize = 16384; + /// + /// Number of overlapping samples between frames when calling the . + /// Helps mitigate the spectral distortions of windowing. + /// + [Tooltip("Number of overlapping samples between frames when calling the Callback. " + + "Helps mitigate the spectral distortions of windowing.")] + public int overlap; + /// /// Clip to record to from the device. /// @@ -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; } }