Skip to content

Commit

Permalink
fix output sample rate
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Jan 19, 2025
1 parent cd72a8e commit 226b8cf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public enum SampleRates
#endif

private readonly ConcurrentQueue<float> sampleQueue = new();
private FileStream fileStream;

private void OnValidate()
{
Expand Down Expand Up @@ -91,7 +90,7 @@ private void Update()
{
if (RecordingManager.IsRecording)
{
EndRecording();
RecordingManager.EndRecording();
}
else
{
Expand All @@ -106,44 +105,29 @@ private void Update()
UnityEngine.Debug.Log($"playback sample rate: {playbackSampleRate}");
}

// write to a file
fileStream = new FileStream($"{Application.dataPath}/{DateTime.UtcNow:yy-MM-dd-ss}-recording.raw", FileMode.Create, FileAccess.Write, FileShare.Read);

// ReSharper disable once MethodHasAsyncOverload
RecordingManager.StartRecordingStream<PCMEncoder>(BufferCallback, recordingSampleRate, destroyCancellationToken);

async Task BufferCallback(ReadOnlyMemory<byte> bufferCallback)
{
await fileStream.WriteAsync(bufferCallback, destroyCancellationToken);

var samples = PCMEncoder.Decode(bufferCallback.ToArray(), inputSampleRate: recordingSampleRate, outputSampleRate: playbackSampleRate);

foreach (var sample in samples)
{
sampleQueue.Enqueue(sample);
}

await Task.Yield();
}
}
}
}
}

private void EndRecording()
{
if (RecordingManager.IsRecording)
{
RecordingManager.EndRecording();
}

fileStream?.Dispose();
fileStream = null;
}

#if !UNITY_2022_1_OR_NEWER
private void OnDestroy()
{
lifetimeCancellationTokenSource.Cancel();
EndRecording();
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ async Task BufferCallback(ReadOnlyMemory<byte> buffer)
var lastMicrophonePosition = 0;
var inputBufferSize = clipData.InputBufferSize;
var sampleBuffer = new float[inputBufferSize];
var sampleBufferLength = sampleBuffer.Length;
var outputSamples = new float[inputBufferSize];

do
Expand Down Expand Up @@ -494,7 +495,7 @@ async Task BufferCallback(ReadOnlyMemory<byte> buffer)
{
sampleProvider.GetData(clipData.Clip, sampleBuffer);

for (var i = 0; i < sampleBuffer.Length; i++)
for (var i = 0; i < sampleBufferLength; i++)
{
if (i < samplesToWrite)
{
Expand All @@ -511,7 +512,7 @@ async Task BufferCallback(ReadOnlyMemory<byte> buffer)

try
{
await bufferCallback(Encode(Resample(outputSamples, null, clipData.InputSampleRate, clipData.OutputSampleRate), null, 0, samplesToWrite)).ConfigureAwait(false);
await bufferCallback(Encode(outputSamples, null, 0, samplesToWrite)).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,8 @@ public static async Task<Tuple<string, AudioClip>> StartRecordingAsync<TEncoder>
Debug.Log($"[{nameof(RecordingManager)}] Recording device(s): {deviceName} | minFreq: {minFreq} | maxFreq {maxFreq}");
}

var inputSampleRate = outputSampleRate;

if (inputSampleRate <= minFreq)
{
inputSampleRate = minFreq;
}

if (inputSampleRate >= maxFreq)
{
inputSampleRate = maxFreq;
}

if (EnableDebug && inputSampleRate != outputSampleRate)
{
Debug.LogWarning($"[{nameof(RecordingManager)}] device frequency range is outside of the requested output sample range. Clip will be resampled: {inputSampleRate}->{outputSampleRate}");
}

// create dummy clip for recording purposes with a 1-second buffer.
var clip = Microphone.Start(DefaultRecordingDevice, loop: true, length: 1, inputSampleRate);
var clip = Microphone.Start(DefaultRecordingDevice, loop: true, length: 1, outputSampleRate);

if (clip == null)
{
Expand Down Expand Up @@ -361,25 +344,8 @@ public static async Task StartRecordingStreamAsync<TEncoder>(Func<ReadOnlyMemory
Debug.Log($"[{nameof(RecordingManager)}] Recording device(s): {deviceName} | minFreq: {minFreq} | maxFreq {maxFreq}");
}

var inputSampleRate = outputSampleRate;

if (inputSampleRate <= minFreq)
{
inputSampleRate = minFreq;
}

if (inputSampleRate >= maxFreq)
{
inputSampleRate = maxFreq;
}

if (EnableDebug && inputSampleRate != outputSampleRate)
{
Debug.LogWarning($"[{nameof(RecordingManager)}] device frequency range is outside of the requested output sample range. Clip will be resampled: {inputSampleRate}->{outputSampleRate}");
}

// create dummy clip for recording purposes with a 1-second buffer.
var clip = Microphone.Start(DefaultRecordingDevice, loop: true, length: 1, inputSampleRate);
var clip = Microphone.Start(DefaultRecordingDevice, loop: true, length: 1, outputSampleRate);

if (clip == null)
{
Expand Down

0 comments on commit 226b8cf

Please sign in to comment.