From 2cec60106c38ad5162badc34a07c499843e7382f Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Thu, 14 Nov 2024 00:23:17 -0500 Subject: [PATCH] com.utilities.audio 2.0.2 - fixed a missing reference exception when cancelling streaming audio only - fixed a bug where streaming audio would stop when max samples are hit, but we don't have to stop since we're not saving the samples to AudioClip --- .../com.utilities.audio/Runtime/ClipData.cs | 3 +- .../com.utilities.audio/Runtime/PCMEncoder.cs | 36 ++++++++++++------- .../Runtime/RecordingManager.cs | 4 ++- .../Packages/com.utilities.audio/package.json | 2 +- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Utilities.Audio/Packages/com.utilities.audio/Runtime/ClipData.cs b/Utilities.Audio/Packages/com.utilities.audio/Runtime/ClipData.cs index 8807d9e..fb7ea04 100644 --- a/Utilities.Audio/Packages/com.utilities.audio/Runtime/ClipData.cs +++ b/Utilities.Audio/Packages/com.utilities.audio/Runtime/ClipData.cs @@ -14,7 +14,6 @@ public ClipData(AudioClip clip, string recordingDevice) Channels = clip.channels; BufferSize = clip.samples; SampleRate = clip.frequency; - MaxSamples = RecordingManager.MaxRecordingLength * SampleRate; } public AudioClip Clip { get; } @@ -29,6 +28,6 @@ public ClipData(AudioClip clip, string recordingDevice) public int SampleRate { get; } - public int MaxSamples { get; } + public int? MaxSamples { get; internal set; } } } diff --git a/Utilities.Audio/Packages/com.utilities.audio/Runtime/PCMEncoder.cs b/Utilities.Audio/Packages/com.utilities.audio/Runtime/PCMEncoder.cs index c04d90d..202431a 100644 --- a/Utilities.Audio/Packages/com.utilities.audio/Runtime/PCMEncoder.cs +++ b/Utilities.Audio/Packages/com.utilities.audio/Runtime/PCMEncoder.cs @@ -231,7 +231,16 @@ public async Task StreamRecordingAsync(ClipData clipData, Func> StreamSaveToDiskAsync(ClipData clipD outStream = new MemoryStream(); } - int totalSampleCount; - var finalSamples = new float[clipData.MaxSamples]; + var totalSampleCount = 0; + var finalSamples = new float[clipData.MaxSamples ?? clipData.SampleRate * RecordingManager.MaxRecordingLength]; var writer = new BinaryWriter(outStream); try @@ -309,10 +318,16 @@ public async Task> StreamSaveToDiskAsync(ClipData clipD } catch (Exception e) { - Debug.LogError($"[{nameof(RecordingManager)}] Failed to record clip!\n{e}"); - RecordingManager.IsRecording = false; - RecordingManager.IsProcessing = false; - return null; + switch (e) + { + case TaskCanceledException: + case OperationCanceledException: + // ignore + break; + default: + Debug.LogException(e); + break; + } } finally { @@ -340,10 +355,6 @@ public async Task> StreamSaveToDiskAsync(ClipData clipD result = new Tuple(outputPath, newClip); callback?.Invoke(result); } - catch (Exception e) - { - Debug.LogException(e); - } finally { RecordingManager.IsProcessing = false; @@ -397,6 +408,7 @@ public async Task> StreamSaveToDiskAsync(ClipData clipD if (samplesToWrite > 0) { + cancellationToken.ThrowIfCancellationRequested(); clipData.Clip.GetData(sampleBuffer, 0); for (var i = 0; i < samplesToWrite; i++) @@ -434,7 +446,7 @@ public async Task> StreamSaveToDiskAsync(ClipData clipD } } - if (sampleCount >= clipData.MaxSamples || cancellationToken.IsCancellationRequested) + if (clipData.MaxSamples.HasValue && sampleCount >= clipData.MaxSamples || cancellationToken.IsCancellationRequested) { if (RecordingManager.EnableDebug) { diff --git a/Utilities.Audio/Packages/com.utilities.audio/Runtime/RecordingManager.cs b/Utilities.Audio/Packages/com.utilities.audio/Runtime/RecordingManager.cs index 07209ff..8312ebf 100644 --- a/Utilities.Audio/Packages/com.utilities.audio/Runtime/RecordingManager.cs +++ b/Utilities.Audio/Packages/com.utilities.audio/Runtime/RecordingManager.cs @@ -265,7 +265,9 @@ public static async Task> StartRecordingAsync encoderCache.TryAdd(typeof(TEncoder), encoder); } - return await encoder.StreamSaveToDiskAsync(InitializeRecording(clip), saveDirectory, OnClipRecorded, cancellationTokenSource.Token); + var clipData = InitializeRecording(clip); + clipData.MaxSamples = MaxRecordingLength * clip.frequency; + return await encoder.StreamSaveToDiskAsync(clipData, saveDirectory, OnClipRecorded, cancellationTokenSource.Token); } catch (Exception e) { diff --git a/Utilities.Audio/Packages/com.utilities.audio/package.json b/Utilities.Audio/Packages/com.utilities.audio/package.json index f4b538d..36c56d6 100644 --- a/Utilities.Audio/Packages/com.utilities.audio/package.json +++ b/Utilities.Audio/Packages/com.utilities.audio/package.json @@ -3,7 +3,7 @@ "displayName": "Utilities.Audio", "description": "A simple package for audio extensions and utilities.", "keywords": [], - "version": "2.0.1", + "version": "2.0.2", "unity": "2021.3", "documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.audio#documentation", "changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.audio/releases",