Refactor C API to prefix each API with SherpaOnnx. (#1171)

This commit is contained in:
Fangjun Kuang
2024-07-26 18:47:02 +08:00
committed by GitHub
parent 994c3e7c96
commit 4e6aeff07e
47 changed files with 667 additions and 618 deletions

View File

@@ -13,20 +13,20 @@ namespace SherpaOnnx
{
public KeywordSpotter(KeywordSpotterConfig config)
{
IntPtr h = CreateKeywordSpotter(ref config);
IntPtr h = SherpaOnnxCreateKeywordSpotter(ref config);
_handle = new HandleRef(this, h);
}
public OnlineStream CreateStream()
{
IntPtr p = CreateKeywordStream(_handle.Handle);
IntPtr p = SherpaOnnxCreateKeywordStream(_handle.Handle);
return new OnlineStream(p);
}
public OnlineStream CreateStream(string keywords)
{
byte[] utf8Bytes = Encoding.UTF8.GetBytes(keywords);
IntPtr p = CreateKeywordStreamWithKeywords(_handle.Handle, utf8Bytes);
IntPtr p = SherpaOnnxCreateKeywordStreamWithKeywords(_handle.Handle, utf8Bytes);
return new OnlineStream(p);
}
@@ -81,7 +81,7 @@ namespace SherpaOnnx
private void Cleanup()
{
DestroyKeywordSpotter(_handle.Handle);
SherpaOnnxDestroyKeywordSpotter(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
@@ -90,30 +90,30 @@ namespace SherpaOnnx
private HandleRef _handle;
[DllImport(Dll.Filename)]
private static extern IntPtr CreateKeywordSpotter(ref KeywordSpotterConfig config);
private static extern IntPtr SherpaOnnxCreateKeywordSpotter(ref KeywordSpotterConfig config);
[DllImport(Dll.Filename)]
private static extern void DestroyKeywordSpotter(IntPtr handle);
private static extern void SherpaOnnxDestroyKeywordSpotter(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr CreateKeywordStream(IntPtr handle);
private static extern IntPtr SherpaOnnxCreateKeywordStream(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr CreateKeywordStreamWithKeywords(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Keywords);
private static extern IntPtr SherpaOnnxCreateKeywordStreamWithKeywords(IntPtr handle, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] byte[] utf8Keywords);
[DllImport(Dll.Filename, EntryPoint = "IsKeywordStreamReady")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxIsKeywordStreamReady")]
private static extern int IsReady(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DecodeKeywordStream")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDecodeKeywordStream")]
private static extern void Decode(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DecodeMultipleKeywordStreams")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDecodeMultipleKeywordStreams")]
private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
[DllImport(Dll.Filename, EntryPoint = "GetKeywordResult")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxGetKeywordResult")]
private static extern IntPtr GetResult(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DestroyKeywordResult")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDestroyKeywordResult")]
private static extern void DestroyResult(IntPtr result);
}
}

View File

@@ -10,13 +10,13 @@ namespace SherpaOnnx
{
public OfflineRecognizer(OfflineRecognizerConfig config)
{
IntPtr h = CreateOfflineRecognizer(ref config);
IntPtr h = SherpaOnnxCreateOfflineRecognizer(ref config);
_handle = new HandleRef(this, h);
}
public OfflineStream CreateStream()
{
IntPtr p = CreateOfflineStream(_handle.Handle);
IntPtr p = SherpaOnnxCreateOfflineStream(_handle.Handle);
return new OfflineStream(p);
}
@@ -54,7 +54,7 @@ namespace SherpaOnnx
private void Cleanup()
{
DestroyOfflineRecognizer(_handle.Handle);
SherpaOnnxDestroyOfflineRecognizer(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
@@ -63,18 +63,18 @@ namespace SherpaOnnx
private HandleRef _handle;
[DllImport(Dll.Filename)]
private static extern IntPtr CreateOfflineRecognizer(ref OfflineRecognizerConfig config);
private static extern IntPtr SherpaOnnxCreateOfflineRecognizer(ref OfflineRecognizerConfig config);
[DllImport(Dll.Filename)]
private static extern void DestroyOfflineRecognizer(IntPtr handle);
private static extern void SherpaOnnxDestroyOfflineRecognizer(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr CreateOfflineStream(IntPtr handle);
private static extern IntPtr SherpaOnnxCreateOfflineStream(IntPtr handle);
[DllImport(Dll.Filename, EntryPoint = "DecodeOfflineStream")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDecodeOfflineStream")]
private static extern void Decode(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOfflineStreams")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDecodeMultipleOfflineStreams")]
private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
}

View File

@@ -44,7 +44,7 @@ namespace SherpaOnnx
private void Cleanup()
{
DestroyOfflineStream(Handle);
SherpaOnnxDestroyOfflineStream(Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
@@ -54,15 +54,15 @@ namespace SherpaOnnx
public IntPtr Handle => _handle.Handle;
[DllImport(Dll.Filename)]
private static extern void DestroyOfflineStream(IntPtr handle);
private static extern void SherpaOnnxDestroyOfflineStream(IntPtr handle);
[DllImport(Dll.Filename, EntryPoint = "AcceptWaveformOffline")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxAcceptWaveformOffline")]
private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);
[DllImport(Dll.Filename, EntryPoint = "GetOfflineStreamResult")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxGetOfflineStreamResult")]
private static extern IntPtr GetResult(IntPtr handle);
[DllImport(Dll.Filename, EntryPoint = "DestroyOfflineRecognizerResult")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDestroyOfflineRecognizerResult")]
private static extern void DestroyResult(IntPtr handle);
}

View File

@@ -14,13 +14,13 @@ namespace SherpaOnnx
{
public OnlineRecognizer(OnlineRecognizerConfig config)
{
IntPtr h = CreateOnlineRecognizer(ref config);
IntPtr h = SherpaOnnxCreateOnlineRecognizer(ref config);
_handle = new HandleRef(this, h);
}
public OnlineStream CreateStream()
{
IntPtr p = CreateOnlineStream(_handle.Handle);
IntPtr p = SherpaOnnxCreateOnlineStream(_handle.Handle);
return new OnlineStream(p);
}
@@ -35,7 +35,7 @@ namespace SherpaOnnx
/// true.
public bool IsEndpoint(OnlineStream stream)
{
return IsEndpoint(_handle.Handle, stream.Handle) != 0;
return SherpaOnnxOnlineStreamIsEndpoint(_handle.Handle, stream.Handle) != 0;
}
/// You have to ensure that IsReady(stream) returns true before
@@ -71,7 +71,7 @@ namespace SherpaOnnx
/// When this method returns, IsEndpoint(stream) will return false.
public void Reset(OnlineStream stream)
{
Reset(_handle.Handle, stream.Handle);
SherpaOnnxOnlineStreamReset(_handle.Handle, stream.Handle);
}
public void Dispose()
@@ -89,7 +89,7 @@ namespace SherpaOnnx
private void Cleanup()
{
DestroyOnlineRecognizer(_handle.Handle);
SherpaOnnxDestroyOnlineRecognizer(_handle.Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
@@ -98,33 +98,33 @@ namespace SherpaOnnx
private HandleRef _handle;
[DllImport(Dll.Filename)]
private static extern IntPtr CreateOnlineRecognizer(ref OnlineRecognizerConfig config);
private static extern IntPtr SherpaOnnxCreateOnlineRecognizer(ref OnlineRecognizerConfig config);
[DllImport(Dll.Filename)]
private static extern void DestroyOnlineRecognizer(IntPtr handle);
private static extern void SherpaOnnxDestroyOnlineRecognizer(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern IntPtr CreateOnlineStream(IntPtr handle);
private static extern IntPtr SherpaOnnxCreateOnlineStream(IntPtr handle);
[DllImport(Dll.Filename, EntryPoint = "IsOnlineStreamReady")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxIsOnlineStreamReady")]
private static extern int IsReady(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DecodeOnlineStream")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDecodeOnlineStream")]
private static extern void Decode(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOnlineStreams")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDecodeMultipleOnlineStreams")]
private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
[DllImport(Dll.Filename, EntryPoint = "GetOnlineStreamResult")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxGetOnlineStreamResult")]
private static extern IntPtr GetResult(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename, EntryPoint = "DestroyOnlineRecognizerResult")]
[DllImport(Dll.Filename, EntryPoint = "SherpaOnnxDestroyOnlineRecognizerResult")]
private static extern void DestroyResult(IntPtr result);
[DllImport(Dll.Filename)]
private static extern void Reset(IntPtr handle, IntPtr stream);
private static extern void SherpaOnnxOnlineStreamReset(IntPtr handle, IntPtr stream);
[DllImport(Dll.Filename)]
private static extern int IsEndpoint(IntPtr handle, IntPtr stream);
private static extern int SherpaOnnxOnlineStreamIsEndpoint(IntPtr handle, IntPtr stream);
}
}

View File

@@ -16,12 +16,12 @@ namespace SherpaOnnx
public void AcceptWaveform(int sampleRate, float[] samples)
{
AcceptWaveform(Handle, sampleRate, samples, samples.Length);
SherpaOnnxOnlineStreamAcceptWaveform(Handle, sampleRate, samples, samples.Length);
}
public void InputFinished()
{
InputFinished(Handle);
SherpaOnnxOnlineStreamInputFinished(Handle);
}
~OnlineStream()
@@ -39,7 +39,7 @@ namespace SherpaOnnx
private void Cleanup()
{
DestroyOnlineStream(Handle);
SherpaOnnxDestroyOnlineStream(Handle);
// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
@@ -49,13 +49,13 @@ namespace SherpaOnnx
public IntPtr Handle => _handle.Handle;
[DllImport(Dll.Filename)]
private static extern void DestroyOnlineStream(IntPtr handle);
private static extern void SherpaOnnxDestroyOnlineStream(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void AcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);
private static extern void SherpaOnnxOnlineStreamAcceptWaveform(IntPtr handle, int sampleRate, float[] samples, int n);
[DllImport(Dll.Filename)]
private static extern void InputFinished(IntPtr handle);
private static extern void SherpaOnnxOnlineStreamInputFinished(IntPtr handle);
}
}