sherpa-ncnn.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2. using System.Runtime.InteropServices;
  3. using System;
  4. namespace SherpaNcnn {
  5. [StructLayout(LayoutKind.Sequential)]
  6. public struct TransducerModelConfig {
  7. [MarshalAs(UnmanagedType.LPStr)]
  8. public string EncoderParam;
  9. [MarshalAs(UnmanagedType.LPStr)]
  10. public string EncoderBin;
  11. [MarshalAs(UnmanagedType.LPStr)]
  12. public string DecoderParam;
  13. [MarshalAs(UnmanagedType.LPStr)]
  14. public string DecoderBin;
  15. [MarshalAs(UnmanagedType.LPStr)]
  16. public string JoinerParam;
  17. [MarshalAs(UnmanagedType.LPStr)]
  18. public string JoinerBin;
  19. [MarshalAs(UnmanagedType.LPStr)]
  20. public string Tokens;
  21. public int UseVulkanCompute;
  22. public int NumThreads;
  23. }
  24. [StructLayout(LayoutKind.Sequential)]
  25. public struct TransducerDecoderConfig {
  26. [MarshalAs(UnmanagedType.LPStr)]
  27. public string DecodingMethod;
  28. public int NumActivePaths;
  29. }
  30. [StructLayout(LayoutKind.Sequential)]
  31. public struct FeatureConfig {
  32. public float SampleRate;
  33. public int FeatureDim;
  34. }
  35. [StructLayout(LayoutKind.Sequential)]
  36. public struct OnlineRecognizerConfig {
  37. public FeatureConfig FeatConfig;
  38. public TransducerModelConfig ModelConfig;
  39. public TransducerDecoderConfig DecoderConfig;
  40. public int EnableEndpoit;
  41. public float Rule1MinTrailingSilence;
  42. public float Rule2MinTrailingSilence;
  43. public float Rule3MinUtteranceLength;
  44. }
  45. // please see
  46. // https://www.mono-project.com/docs/advanced/pinvoke/#gc-safe-pinvoke-code
  47. // https://www.mono-project.com/docs/advanced/pinvoke/#properly-disposing-of-resources
  48. public class OnlineRecognizer : IDisposable {
  49. public OnlineRecognizer(OnlineRecognizerConfig config) {
  50. IntPtr h = CreateOnlineRecognizer(ref config);
  51. _handle = new HandleRef(this, h);
  52. }
  53. public OnlineStream CreateStream() {
  54. IntPtr p = CreateOnlineStream(_handle.Handle);
  55. return new OnlineStream(p);
  56. }
  57. public bool IsReady(OnlineStream stream) {
  58. return IsReady(_handle.Handle, stream.Handle) != 0;
  59. }
  60. public void Decode(OnlineStream stream) {
  61. Decode(_handle.Handle, stream.Handle);
  62. }
  63. public OnlineRecognizerResult GetResult(OnlineStream stream) {
  64. IntPtr h = GetResult(_handle.Handle, stream.Handle);
  65. OnlineRecognizerResult result = new OnlineRecognizerResult(h);
  66. DestroyResult(h);
  67. return result;
  68. }
  69. public void Dispose() {
  70. Cleanup();
  71. // Prevent the object from being placed on the
  72. // finalization queue
  73. System.GC.SuppressFinalize(this);
  74. }
  75. ~OnlineRecognizer() {
  76. Cleanup();
  77. }
  78. private void Cleanup() {
  79. DestroyOnlineRecognizer(_handle.Handle);
  80. // Don't permit the handle to be used again.
  81. _handle = new HandleRef(this, IntPtr.Zero);
  82. }
  83. private HandleRef _handle;
  84. private const string dllName = "sherpa-ncnn-c-api";
  85. [DllImport(dllName, EntryPoint="CreateRecognizer")]
  86. private static extern IntPtr CreateOnlineRecognizer(ref OnlineRecognizerConfig config);
  87. [DllImport(dllName, EntryPoint="DestroyRecognizer")]
  88. private static extern void DestroyOnlineRecognizer(IntPtr handle);
  89. [DllImport(dllName, EntryPoint="CreateStream")]
  90. private static extern IntPtr CreateOnlineStream(IntPtr handle);
  91. [DllImport(dllName)]
  92. private static extern int IsReady(IntPtr handle, IntPtr stream);
  93. [DllImport(dllName, EntryPoint="Decode")]
  94. private static extern void Decode(IntPtr handle, IntPtr stream);
  95. [DllImport(dllName)]
  96. private static extern IntPtr GetResult(IntPtr handle, IntPtr stream);
  97. [DllImport(dllName)]
  98. private static extern void DestroyResult(IntPtr result);
  99. }
  100. public class OnlineStream : IDisposable {
  101. public OnlineStream(IntPtr p) {
  102. _handle = new HandleRef(this, p);
  103. }
  104. public void AcceptWaveform(float sampleRate, float[] samples) {
  105. AcceptWaveform(Handle, sampleRate, samples, samples.Length);
  106. }
  107. public void InputFinished() {
  108. InputFinished(Handle);
  109. }
  110. ~OnlineStream() {
  111. Cleanup();
  112. }
  113. public void Dispose() {
  114. Cleanup();
  115. // Prevent the object from being placed on the
  116. // finalization queue
  117. System.GC.SuppressFinalize(this);
  118. }
  119. private void Cleanup() {
  120. DestroyOnlineStream(Handle);
  121. // Don't permit the handle to be used again.
  122. _handle = new HandleRef(this, IntPtr.Zero);
  123. }
  124. private HandleRef _handle;
  125. public IntPtr Handle => _handle.Handle;
  126. private const string dllName = "sherpa-ncnn-c-api";
  127. [DllImport(dllName, EntryPoint="DestroyStream")]
  128. private static extern void DestroyOnlineStream(IntPtr handle);
  129. [DllImport(dllName)]
  130. private static extern void AcceptWaveform(IntPtr handle, float sampleRate, float[] samples, int n);
  131. [DllImport(dllName)]
  132. private static extern void InputFinished(IntPtr handle);
  133. }
  134. public class OnlineRecognizerResult {
  135. public OnlineRecognizerResult(IntPtr handle) {
  136. Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
  137. _text = Marshal.PtrToStringUTF8(impl.Text);
  138. }
  139. [StructLayout(LayoutKind.Sequential)]
  140. struct Impl {
  141. public IntPtr Text;
  142. public IntPtr Tokens;
  143. public IntPtr Timestamps;
  144. int Count;
  145. }
  146. private String _text;
  147. public String Text => _text;
  148. }
  149. }