Avalonia跨平台入门第四十八篇之离线命令词

摘要:public static extern int QISRBuildGrammar([MarshalAs(UnmanagedType.AnsiBStr)] string grammarType, [MarshalAs(UnmanagedType.AnsiBSt

前面简单摸索了一下语音唤醒;今天继续分享关于离线命令词的效果,如下图:

public static extern int QISRBuildGrammar([MarshalAs(UnmanagedType.AnsiBStr)] string grammarType, [MarshalAs(UnmanagedType.AnsiBStr)] string grammarContent, int grammarLength, [MarshalAs(UnmanagedType.AnsiBStr)] string grammarParams, GrammarCallBack callback, out IntPtr userData);public override void SetGrammar(string grammarContent){ IntPtr userDateIntPtr; _grammarId = ParseGrammarId(grammarContent); string param = string.Format("engine_type = local, asr_res_path = {0}, sample_rate={2}, grm_build_path = {1} ", ASR_RES_PATH, GRM_BUILD_PATH, AudioSampleRate); var length = Encoding.Default.GetBytes(grammarContent).Length; var ret = OfflineRecognitionAPI.QISRBuildGrammar("bnf", grammarContent, length, param, BuildGrmCb, out userDateIntPtr); if (ret != 0) { Debug.WriteLine("语法加载失败, 错误码: " + ret); throw new ApplicationException("语法加载失败, 错误码: " + ret); }}

2、识别我们用到的方法:

[DllImport("msc", CallingConvention = CallingConvention.StdCall)]public static extern IntPtr QISRSessionBegin(string grammarList, string _params, ref int errorCode);[DllImport("msc", CallingConvention = CallingConvention.StdCall)]public static extern int QISRAudioWrite(IntPtr sessionID, byte waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);[DllImport("msc", CallingConvention = CallingConvention.StdCall)]public static extern IntPtr QISRGetResult(IntPtr sessionID, ref RecogStatus rsltStatus, int waitTime, ref int errorCode);[DllImport("msc", CallingConvention = CallingConvention.StdCall)]public static extern int QISRSessionEnd(IntPtr sessionID, string hints);

3、开始录音识别:

/// /// 开始录音/// public void StartRecording{ if (waveIn != ) { waveIn.Dispose; waveIn = ; } if (waveIn == ) { waveIn = new WaveIn; waveIn.WaveFormat = new WaveFormat(SampleRate , BitDepth, ChannelCount); waveIn.BufferMilliseconds = BufferTime; waveIn.DataAvailable += (sender, e) => { OnDataReceived (e.Buffer, e.BytesRecorded); }; } waveIn.StartRecording;}

4、识别的流程:

5、分析识别结果:

最终简单的效果先这样吧

;

以后有时间的话,可以再去摸索一下更复杂的效果

;编程不息、Bug不止、无Bug、无生活;

改bug的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!

来源:opendotnet

相关推荐