This repository has been archived on 2025-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
Files
enginex_bi_series-sherpa-onnx/scripts/dotnet/OfflineStream.cs
东风破 00de2bd00b Refactor .Net example project (#1049)
Co-authored-by: 东风破 <birdfishs@163.com>
2024-06-24 10:10:13 +08:00

70 lines
1.9 KiB
C#

/// Copyright (c) 2024.5 by 东风破
using System;
using System.Runtime.InteropServices;
namespace SherpaOnnx
{
public class OfflineStream : IDisposable
{
public OfflineStream(IntPtr p)
{
_handle = new HandleRef(this, p);
}
public void AcceptWaveform(int sampleRate, float[] samples)
{
AcceptWaveform(Handle, sampleRate, samples, samples.Length);
}
public OfflineRecognizerResult Result
{
get
{
IntPtr h = GetResult(_handle.Handle);
OfflineRecognizerResult result = new OfflineRecognizerResult(h);
DestroyResult(h);
return result;
}
}
~OfflineStream()
{
Cleanup();
}
public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}
private void Cleanup()
{
DestroyOfflineStream(Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}
private HandleRef _handle;
public IntPtr Handle => _handle.Handle;
[DllImport(Dll.Filename)]
private static extern void DestroyOfflineStream(IntPtr handle);
[DllImport(Dll.Filename, EntryPoint = "AcceptWaveformOffline")]
private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);
[DllImport(Dll.Filename, EntryPoint = "GetOfflineStreamResult")]
private static extern IntPtr GetResult(IntPtr handle);
[DllImport(Dll.Filename, EntryPoint = "DestroyOfflineRecognizerResult")]
private static extern void DestroyResult(IntPtr handle);
}
}