sherpa-ncnn.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 EnableEndpoit;
  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 void Decode(OnlineStream stream)
  71. {
  72. Decode(_handle.Handle, stream.Handle);
  73. }
  74. public OnlineRecognizerResult GetResult(OnlineStream stream)
  75. {
  76. IntPtr h = GetResult(_handle.Handle, stream.Handle);
  77. OnlineRecognizerResult result = new OnlineRecognizerResult(h);
  78. DestroyResult(h);
  79. return result;
  80. }
  81. public void Dispose()
  82. {
  83. Cleanup();
  84. // Prevent the object from being placed on the
  85. // finalization queue
  86. System.GC.SuppressFinalize(this);
  87. }
  88. ~OnlineRecognizer()
  89. {
  90. Cleanup();
  91. }
  92. private void Cleanup()
  93. {
  94. DestroyOnlineRecognizer(_handle.Handle);
  95. // Don't permit the handle to be used again.
  96. _handle = new HandleRef(this, IntPtr.Zero);
  97. }
  98. private HandleRef _handle;
  99. private const string dllName = "sherpa-ncnn-c-api";
  100. [DllImport(dllName, EntryPoint = "CreateRecognizer")]
  101. private static extern IntPtr CreateOnlineRecognizer(ref OnlineRecognizerConfig config);
  102. [DllImport(dllName, EntryPoint = "DestroyRecognizer")]
  103. private static extern void DestroyOnlineRecognizer(IntPtr handle);
  104. [DllImport(dllName, EntryPoint = "CreateStream")]
  105. private static extern IntPtr CreateOnlineStream(IntPtr handle);
  106. [DllImport(dllName)]
  107. private static extern int IsReady(IntPtr handle, IntPtr stream);
  108. [DllImport(dllName, EntryPoint = "Decode")]
  109. private static extern void Decode(IntPtr handle, IntPtr stream);
  110. [DllImport(dllName)]
  111. private static extern IntPtr GetResult(IntPtr handle, IntPtr stream);
  112. [DllImport(dllName)]
  113. private static extern void DestroyResult(IntPtr result);
  114. }
  115. public class OnlineStream : IDisposable
  116. {
  117. public OnlineStream(IntPtr p)
  118. {
  119. _handle = new HandleRef(this, p);
  120. }
  121. public void AcceptWaveform(float sampleRate, float[] samples)
  122. {
  123. AcceptWaveform(Handle, sampleRate, samples, samples.Length);
  124. }
  125. public void InputFinished()
  126. {
  127. InputFinished(Handle);
  128. }
  129. ~OnlineStream()
  130. {
  131. Cleanup();
  132. }
  133. public void Dispose()
  134. {
  135. Cleanup();
  136. // Prevent the object from being placed on the
  137. // finalization queue
  138. System.GC.SuppressFinalize(this);
  139. }
  140. private void Cleanup()
  141. {
  142. DestroyOnlineStream(Handle);
  143. // Don't permit the handle to be used again.
  144. _handle = new HandleRef(this, IntPtr.Zero);
  145. }
  146. private HandleRef _handle;
  147. public IntPtr Handle => _handle.Handle;
  148. private const string dllName = "sherpa-ncnn-c-api";
  149. [DllImport(dllName, EntryPoint = "DestroyStream")]
  150. private static extern void DestroyOnlineStream(IntPtr handle);
  151. [DllImport(dllName)]
  152. private static extern void AcceptWaveform(IntPtr handle, float sampleRate, float[] samples, int n);
  153. [DllImport(dllName)]
  154. private static extern void InputFinished(IntPtr handle);
  155. }
  156. public class OnlineRecognizerResult
  157. {
  158. public OnlineRecognizerResult(IntPtr handle)
  159. {
  160. Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl));
  161. // PtrToStringUTF8() requires .net standard 2.1
  162. // _text = Marshal.PtrToStringUTF8(impl.Text);
  163. int length = 0;
  164. unsafe
  165. {
  166. byte* buffer = (byte*)impl.Text;
  167. while (*buffer != 0)
  168. {
  169. ++buffer;
  170. }
  171. length = (int)(buffer - (byte*)impl.Text);
  172. }
  173. byte[] stringBuffer = new byte[length];
  174. Marshal.Copy(impl.Text, stringBuffer, 0, length);
  175. _text = Encoding.UTF8.GetString(stringBuffer);
  176. }
  177. [StructLayout(LayoutKind.Sequential)]
  178. struct Impl
  179. {
  180. public IntPtr Text;
  181. public IntPtr Tokens;
  182. public IntPtr Timestamps;
  183. int Count;
  184. }
  185. private String _text;
  186. public String Text => _text;
  187. }
  188. }