Skip to content

Commit

Permalink
Add Memory<float> for ProcessAsync (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdluzen authored Jun 4, 2023
1 parent 09ae26a commit 22576a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions Whisper.net/Whisper.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageReference Include="System.Memory" Version="4.5.1" />
</ItemGroup>

</Project>
11 changes: 8 additions & 3 deletions Whisper.net/WhisperProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async IAsyncEnumerable<SegmentData> ProcessAsync(Stream waveStream, [Enum
}
}

public async IAsyncEnumerable<SegmentData> ProcessAsync(float[] samples, [EnumeratorCancellation] CancellationToken cancellationToken = default)
public async IAsyncEnumerable<SegmentData> ProcessAsync(Memory<float> samples, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var resetEvent = new AsyncAutoResetEvent();
var buffer = new ConcurrentQueue<SegmentData>();
Expand Down Expand Up @@ -192,6 +192,11 @@ await Task.WhenAny(whisperTask, resetEvent.WaitAsync())
}
}

#pragma warning disable IDE0022 // Use block body for method
public IAsyncEnumerable<SegmentData> ProcessAsync(float[] samples, [EnumeratorCancellation] CancellationToken cancellationToken = default) =>
ProcessAsync(new Memory<float>(samples), cancellationToken);
#pragma warning restore IDE0022 // Use block body for method

public void Dispose()
{
if (processingSemaphore.CurrentCount == 0)
Expand All @@ -216,15 +221,15 @@ public void Dispose()
isDisposed = true;
}

private unsafe Task ProcessInternalAsync(float[] samples, CancellationToken cancellationToken)
private unsafe Task ProcessInternalAsync(Memory<float> samples, CancellationToken cancellationToken)
{
if (isDisposed)
{
throw new ObjectDisposedException("This processor has already been disposed.");
}
return Task.Factory.StartNew(() =>
{
fixed (float* pData = samples)
fixed (float* pData = samples.Span)
{
processingSemaphore.Wait();
segmentIndex = 0;
Expand Down

0 comments on commit 22576a6

Please sign in to comment.