Avalonia跨平台入门第四十七篇之语音唤醒

B站影视 2024-12-06 08:40 2

摘要:public static extern int MSPLogin(string usr, string pwd, string parameters);

前面简单摸索了一下离线语音合成;今天抽空摸索了一下关于语音唤醒的效果,如下图:

public static extern int MSPLogin(string usr, string pwd, string parameters);//开始唤醒功能public static extern IntPtr QIVWSessionBegin(string grammarList, string _params, ref int errorCode);//结束本次语音唤醒public static extern Int QIVWSessionEnd(IntPtr sessionID, string hints);//写入本次唤醒的音频,本接口需要反复调用直到音频写完为止。public static extern int QIVWAudioWrite(IntPtr sessionID, byte audioData, uint audioLen, AudioStatus audioStatus);//注册回调public static extern int QIVWRegisterNotify(IntPtr sessionID, [MarshalAs(UnmanagedType.FunctionPtr)] ivw_ntf_handler msgProcCb, IntPtr userData);//定义回调函数[UnmanagedFunctionPointer(CallingConvention.Cdecl)]public delegate int ivw_ntf_handler(IntPtr sessionID, int msg, int param1, int param2, IntPtr info, IntPtr userData);

2、关于整体使用的流程:

public void StartMSCAwakeListening{ string qivw_session_begin_params = "ivw_threshold=0:1450,sst=wakeup,ivw_res_path =fo|res/ivw/wakeupresource.jet"; /* 首先调用登录接口 在外部调用*/ int errorCode = Login; bool isFirstPart = true; IntPtr sessionID = MscApiHelper.QIVWSessionBegin(, qivw_session_begin_params, ref errorCode); if (errorCode != 0) { Console.WriteLine("初始化语音唤醒失败!错误信息:" + errorCode); //Logout; EventCenter.DoAction(EventCenter.XunfeiAwakeError, errorCode); return; } callback = new ivw_ntf_handler(cb_ivw_msg_proc); int message = MscApiHelper.QIVWRegisterNotify(sessionID, callback, IntPtr.Zero); if (message != 0) { Console.WriteLine("语音唤醒注册回调失败!错误信息:" + errorCode); return; } waveIn = new WaveInEvent; waveIn.WaveFormat = new WaveFormat(sampleRate, bitsPerSample, channels); int avlCount = 0; waveIn.DataAvailable += (sender, e) => { avlCount++; Console.WriteLine($"获取录音{avlCount}"); if (!isFirstPart) { message = MscApiHelper.QIVWAudioWrite(sessionID, e.Buffer, (uint)e.BytesRecorded, AudioStatus.MSP_AUDIO_SAMPLE_FIRST); } else message = MscApiHelper.QIVWAudioWrite(sessionID, e.Buffer, (uint)e.BytesRecorded, AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE); if (message != 0) { Console.WriteLine($"唤醒数据写入失败,错误码:{message}"); waveIn.StopRecording; } }; waveIn.RecordingStopped += (sender, e) => { Console.WriteLine("停止录音"); message = MscApiHelper.QIVWAudioWrite(sessionID, new byte[0], 0, AudioStatus.MSP_AUDIO_SAMPLE_LAST); message = MscApiHelper.QIVWSessionEnd(sessionID, "语音唤醒结束"); if (message != 0) { Console.WriteLine("语音唤醒结束失败!错误信息:" + message); } else { Console.WriteLine("语音唤醒结束"); } Logout; }; waveIn.StartRecording;}

3、语音唤醒的回调函数:

private int cb_ivw_msg_proc(IntPtr sessionID, int msg, int param1, int param2, IntPtr info, IntPtr userData){ if (msg == (int)IvwStatus.MSP_IVW_MSG_ERROR) //唤醒出错消息 { Console.WriteLine("MSP_IVW_MSG_ERROR error code:" + param1); } else if (msg == (int)IvwStatus.MSP_IVW_MSG_WAKEUP)//唤醒成功消息 { isAwaken = true; if (waveIn != ) { waveIn.StopRecording; } Console.WriteLine("唤醒成功,执行回调函数"); string data = Marshal.PtrToStringAnsi(info); EventCenter.DoAction(EventCenter.XunfeiAwake, data); } return 0;}

最终简单的效果先这样吧

;

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

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

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

来源:opendotnet

相关推荐