Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.utilities.audio 2.0.2 #35

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -29,6 +28,6 @@ public ClipData(AudioClip clip, string recordingDevice)

public int SampleRate { get; }

public int MaxSamples { get; }
public int? MaxSamples { get; internal set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,16 @@ public async Task StreamRecordingAsync(ClipData clipData, Func<ReadOnlyMemory<by
}
catch (Exception e)
{
Debug.LogException(e);
switch (e)
{
case TaskCanceledException:
case OperationCanceledException:
// ignore
break;
default:
Debug.LogException(e);
break;
}
}
finally
{
Expand Down Expand Up @@ -283,8 +292,8 @@ public async Task<Tuple<string, AudioClip>> 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
Expand All @@ -309,10 +318,16 @@ public async Task<Tuple<string, AudioClip>> 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
{
Expand Down Expand Up @@ -340,10 +355,6 @@ public async Task<Tuple<string, AudioClip>> StreamSaveToDiskAsync(ClipData clipD
result = new Tuple<string, AudioClip>(outputPath, newClip);
callback?.Invoke(result);
}
catch (Exception e)
{
Debug.LogException(e);
}
finally
{
RecordingManager.IsProcessing = false;
Expand Down Expand Up @@ -397,6 +408,7 @@ public async Task<Tuple<string, AudioClip>> StreamSaveToDiskAsync(ClipData clipD

if (samplesToWrite > 0)
{
cancellationToken.ThrowIfCancellationRequested();
clipData.Clip.GetData(sampleBuffer, 0);

for (var i = 0; i < samplesToWrite; i++)
Expand Down Expand Up @@ -434,7 +446,7 @@ public async Task<Tuple<string, AudioClip>> StreamSaveToDiskAsync(ClipData clipD
}
}

if (sampleCount >= clipData.MaxSamples || cancellationToken.IsCancellationRequested)
if (clipData.MaxSamples.HasValue && sampleCount >= clipData.MaxSamples || cancellationToken.IsCancellationRequested)
{
if (RecordingManager.EnableDebug)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ public static async Task<Tuple<string, AudioClip>> StartRecordingAsync<TEncoder>
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down