using System; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: AssemblyVersion("0.0.0.0")] namespace Vosk; public class Model : IDisposable { private HandleRef handle; internal Model(IntPtr cPtr) { handle = new HandleRef(this, cPtr); } public Model(string model_path) : this(VoskPINVOKE.new_Model(model_path)) { } internal static HandleRef getCPtr(Model obj) { return obj?.handle ?? new HandleRef(null, IntPtr.Zero); } ~Model() { Dispose(disposing: false); } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { lock (this) { if (handle.Handle != IntPtr.Zero) { VoskPINVOKE.delete_Model(handle); handle = new HandleRef(null, IntPtr.Zero); } } } public int vosk_model_find_word(string word) { return VoskPINVOKE.Model_vosk_model_find_word(handle, word); } } public class SpkModel : IDisposable { private HandleRef handle; internal SpkModel(IntPtr cPtr) { handle = new HandleRef(this, cPtr); } public SpkModel(string model_path) : this(VoskPINVOKE.new_SpkModel(model_path)) { } internal static HandleRef getCPtr(SpkModel obj) { return obj?.handle ?? new HandleRef(null, IntPtr.Zero); } ~SpkModel() { Dispose(disposing: false); } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { lock (this) { if (handle.Handle != IntPtr.Zero) { VoskPINVOKE.delete_SpkModel(handle); handle = new HandleRef(null, IntPtr.Zero); } } } } public class Vosk { public static void SetLogLevel(int level) { VoskPINVOKE.SetLogLevel(level); } public static void GpuInit() { VoskPINVOKE.GpuInit(); } public static void GpuThreadInit() { VoskPINVOKE.GpuThreadInit(); } } internal class VoskPINVOKE { static VoskPINVOKE() { } [DllImport("libvosk", EntryPoint = "vosk_model_new")] public static extern IntPtr new_Model(string jarg1); [DllImport("libvosk", EntryPoint = "vosk_model_free")] public static extern void delete_Model(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_model_find_word")] public static extern int Model_vosk_model_find_word(HandleRef jarg1, string jarg2); [DllImport("libvosk", EntryPoint = "vosk_spk_model_new")] public static extern IntPtr new_SpkModel(string jarg1); [DllImport("libvosk", EntryPoint = "vosk_spk_model_free")] public static extern void delete_SpkModel(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_recognizer_new")] public static extern IntPtr new_VoskRecognizer(HandleRef jarg1, float jarg2); [DllImport("libvosk", EntryPoint = "vosk_recognizer_new_spk")] public static extern IntPtr new_VoskRecognizerSpk(HandleRef jarg1, float jarg2, HandleRef jarg3); [DllImport("libvosk", EntryPoint = "vosk_recognizer_new_grm")] public static extern IntPtr new_VoskRecognizerGrm(HandleRef jarg1, float jarg2, string jarg3); [DllImport("libvosk", EntryPoint = "vosk_recognizer_free")] public static extern void delete_VoskRecognizer(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_recognizer_set_max_alternatives")] public static extern void VoskRecognizer_SetMaxAlternatives(HandleRef jarg1, int jarg2); [DllImport("libvosk", EntryPoint = "vosk_recognizer_set_words")] public static extern void VoskRecognizer_SetWords(HandleRef jarg1, int jarg2); [DllImport("libvosk", EntryPoint = "vosk_recognizer_set_spk_model")] public static extern void VoskRecognizer_SetSpkModel(HandleRef jarg1, HandleRef jarg2); [DllImport("libvosk", EntryPoint = "vosk_recognizer_accept_waveform")] public static extern bool VoskRecognizer_AcceptWaveform(HandleRef jarg1, [In][MarshalAs(UnmanagedType.LPArray)] byte[] jarg2, int jarg3); [DllImport("libvosk", EntryPoint = "vosk_recognizer_accept_waveform_s")] public static extern bool VoskRecognizer_AcceptWaveformShort(HandleRef jarg1, [In][MarshalAs(UnmanagedType.LPArray)] short[] jarg2, int jarg3); [DllImport("libvosk", EntryPoint = "vosk_recognizer_accept_waveform_f")] public static extern bool VoskRecognizer_AcceptWaveformFloat(HandleRef jarg1, [In][MarshalAs(UnmanagedType.LPArray)] float[] jarg2, int jarg3); [DllImport("libvosk", EntryPoint = "vosk_recognizer_result")] public static extern IntPtr VoskRecognizer_Result(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_recognizer_partial_result")] public static extern IntPtr VoskRecognizer_PartialResult(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_recognizer_final_result")] public static extern IntPtr VoskRecognizer_FinalResult(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_recognizer_reset")] public static extern void VoskRecognizer_Reset(HandleRef jarg1); [DllImport("libvosk", EntryPoint = "vosk_set_log_level")] public static extern void SetLogLevel(int jarg1); [DllImport("libvosk", EntryPoint = "vosk_gpu_init")] public static extern void GpuInit(); [DllImport("libvosk", EntryPoint = "vosk_gpu_thread_init")] public static extern void GpuThreadInit(); } public class VoskRecognizer : IDisposable { private HandleRef handle; internal VoskRecognizer(IntPtr cPtr) { handle = new HandleRef(this, cPtr); } public VoskRecognizer(Model model, float sample_rate) : this(VoskPINVOKE.new_VoskRecognizer(Model.getCPtr(model), sample_rate)) { } public VoskRecognizer(Model model, float sample_rate, SpkModel spk_model) : this(VoskPINVOKE.new_VoskRecognizerSpk(Model.getCPtr(model), sample_rate, SpkModel.getCPtr(spk_model))) { } public VoskRecognizer(Model model, float sample_rate, string grammar) : this(VoskPINVOKE.new_VoskRecognizerGrm(Model.getCPtr(model), sample_rate, grammar)) { } internal static HandleRef getCPtr(VoskRecognizer obj) { return obj?.handle ?? new HandleRef(null, IntPtr.Zero); } ~VoskRecognizer() { Dispose(disposing: false); } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { lock (this) { if (handle.Handle != IntPtr.Zero) { VoskPINVOKE.delete_VoskRecognizer(handle); handle = new HandleRef(null, IntPtr.Zero); } } } public void SetMaxAlternatives(int max_alternatives) { VoskPINVOKE.VoskRecognizer_SetMaxAlternatives(handle, max_alternatives); } public void SetWords(bool words) { VoskPINVOKE.VoskRecognizer_SetWords(handle, words ? 1 : 0); } public void SetSpkModel(SpkModel spk_model) { VoskPINVOKE.VoskRecognizer_SetSpkModel(handle, SpkModel.getCPtr(spk_model)); } public bool AcceptWaveform(byte[] data, int len) { return VoskPINVOKE.VoskRecognizer_AcceptWaveform(handle, data, len); } public bool AcceptWaveform(short[] sdata, int len) { return VoskPINVOKE.VoskRecognizer_AcceptWaveformShort(handle, sdata, len); } public bool AcceptWaveform(float[] fdata, int len) { return VoskPINVOKE.VoskRecognizer_AcceptWaveformFloat(handle, fdata, len); } private static string PtrToStringUTF8(IntPtr ptr) { int i; for (i = 0; Marshal.ReadByte(ptr, i) != 0; i++) { } byte[] array = new byte[i]; Marshal.Copy(ptr, array, 0, i); return Encoding.UTF8.GetString(array); } public string Result() { return PtrToStringUTF8(VoskPINVOKE.VoskRecognizer_Result(handle)); } public string PartialResult() { return PtrToStringUTF8(VoskPINVOKE.VoskRecognizer_PartialResult(handle)); } public string FinalResult() { return PtrToStringUTF8(VoskPINVOKE.VoskRecognizer_FinalResult(handle)); } public void Reset() { VoskPINVOKE.VoskRecognizer_Reset(handle); } }