Skip to content

Commit

Permalink
com.utilities.audio 2.0.1
Browse files Browse the repository at this point in the history
- hotfix to include missing changes for 2.0.0 release
  • Loading branch information
StephenHodgson committed Nov 11, 2024
1 parent 6c2656f commit 863d5af
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IEncoder
/// <param name="bufferCallback">The event raised when buffer data is ready to write.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/>.</param>
/// <param name="callingMethodName">Used to determine where this method was called from.</param>
Task StreamRecordingAsync(ClipData microphoneClipData, Action<ReadOnlyMemory<byte>> bufferCallback, CancellationToken cancellationToken, [CallerMemberName] string callingMethodName = null);
Task StreamRecordingAsync(ClipData microphoneClipData, Func<ReadOnlyMemory<byte>, Task> bufferCallback, CancellationToken cancellationToken, [CallerMemberName] string callingMethodName = null);

/// <summary>
/// Streams audio microphone recording input to disk.
Expand Down
24 changes: 19 additions & 5 deletions Utilities.Audio/Packages/com.utilities.audio/Runtime/PCMEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static float[] Resample(float[] samples, int inputSamplingRate, int outpu

/// <inheritdoc />
[Preserve]
public async Task StreamRecordingAsync(ClipData clipData, Action<ReadOnlyMemory<byte>> bufferCallback, CancellationToken cancellationToken, string callingMethodName = null)
public async Task StreamRecordingAsync(ClipData clipData, Func<ReadOnlyMemory<byte>, Task> bufferCallback, CancellationToken cancellationToken, string callingMethodName = null)
{
if (callingMethodName != nameof(RecordingManager.StartRecordingStreamAsync))
{
Expand Down Expand Up @@ -291,7 +291,11 @@ public async Task<Tuple<string, AudioClip>> StreamSaveToDiskAsync(ClipData clipD
{
try
{
(finalSamples, totalSampleCount) = await InternalStreamRecordAsync(clipData, finalSamples, buffer => writer.Write(buffer.Span), cancellationToken);
(finalSamples, totalSampleCount) = await InternalStreamRecordAsync(clipData, finalSamples, async buffer =>
{
writer.Write(buffer.Span);
await Task.Yield();
}, cancellationToken).ConfigureAwait(true);
}
finally
{
Expand Down Expand Up @@ -352,7 +356,7 @@ public async Task<Tuple<string, AudioClip>> StreamSaveToDiskAsync(ClipData clipD
return result;
}

private static async Task<(float[], int)> InternalStreamRecordAsync(ClipData clipData, float[] finalSamples, Action<ReadOnlyMemory<byte>> bufferCallback, CancellationToken cancellationToken)
private static async Task<(float[], int)> InternalStreamRecordAsync(ClipData clipData, float[] finalSamples, Func<ReadOnlyMemory<byte>, Task> bufferCallback, CancellationToken cancellationToken)
{
try
{
Expand Down Expand Up @@ -400,11 +404,21 @@ public async Task<Tuple<string, AudioClip>> StreamSaveToDiskAsync(ClipData clipD
var bufferIndex = (lastMicrophonePosition + i) % clipData.BufferSize; // Wrap around index.
var value = sampleBuffer[bufferIndex];
var sample = (short)(Math.Max(-1f, Math.Min(1f, value)) * short.MaxValue);
bufferCallback.Invoke(new ReadOnlyMemory<byte>(new[]
var sampleData = new ReadOnlyMemory<byte>(new[]
{
(byte)(sample & byte.MaxValue),
(byte)(sample >> 8 & byte.MaxValue)
}));
});

try
{
await bufferCallback.Invoke(sampleData).ConfigureAwait(false);
}
catch (Exception e)
{
Debug.LogException(new Exception($"[{nameof(PCMEncoder)}] error occurred when buffering audio", e));
}

if (finalSamples is { Length: > 0 })
{
finalSamples[sampleCount * clipData.Channels + i] = sampleBuffer[bufferIndex];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ public static async Task<Tuple<string, AudioClip>> StartRecordingAsync<TEncoder>
/// </summary>
/// <param name="bufferCallback">The buffer callback with new sample data.</param>
/// <param name="cancellationToken">Optional, task cancellation token.</param>
public static async void StartRecordingStream<TEncoder>(Action<ReadOnlyMemory<byte>> bufferCallback, CancellationToken cancellationToken = default) where TEncoder : IEncoder
public static async void StartRecordingStream<TEncoder>(Func<ReadOnlyMemory<byte>, Task> bufferCallback, CancellationToken cancellationToken = default) where TEncoder : IEncoder
=> await StartRecordingStreamAsync<TEncoder>(bufferCallback, cancellationToken).ConfigureAwait(true);

/// <summary>
/// Starts the recording process and buffers the samples back as <see cref="ReadOnlyMemory{Tbytes}"/>.
/// </summary>
/// <param name="bufferCallback">The buffer callback with new sample data.</param>
/// <param name="cancellationToken">Optional, task cancellation token.</param>
public static async Task StartRecordingStreamAsync<TEncoder>(Action<ReadOnlyMemory<byte>> bufferCallback, CancellationToken cancellationToken = default) where TEncoder : IEncoder
public static async Task StartRecordingStreamAsync<TEncoder>(Func<ReadOnlyMemory<byte>, Task> bufferCallback, CancellationToken cancellationToken = default) where TEncoder : IEncoder
{
if (IsBusy)
{
Expand Down
2 changes: 1 addition & 1 deletion Utilities.Audio/Packages/com.utilities.audio/package.json
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.0",
"version": "2.0.1",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.audio#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.audio/releases",
Expand Down

0 comments on commit 863d5af

Please sign in to comment.