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

Add Memory<float> for ProcessAsync #62

Merged
merged 1 commit into from
Jun 4, 2023
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
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