sherpa-ncnn.cs 7.0 KB

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