/// <summary> /// 开始录音回调函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void waveSource_DataAvailable(object sender, WaveInEventArgs e) { if (FlowManage.canDistinguish) { totalBufferLength += e.Buffer.Length; secondsRecorded = (float)(totalBufferLength / 32000); VoiceData data = new VoiceData(); for (int i = 0; i < 3200; i++) { data.data[i] = e.Buffer[i]; } VoiceBuffer.Add(data); if (lastPeak < 20) { Ends = Ends - 1; } else { Ends = 5; } if (Ends == 0) { if (VoiceBuffer.Count() > 5) { VoiceManage.SpeechRecognition(VoiceBuffer);//调用语音识别 } VoiceBuffer.Clear(); Ends = 5; } } }
public bool FlowStart = false; //流程开始标识 // Use this for initialization void Start() { if (VoiceManage.MSCLogin() != (int)ErrorCode.MSP_SUCCESS) { Debug.Log("登陆失败!" + ret); MSC.MSPLogout(); return; } mc = new M1101Ctrl(); mc.mResultAction = FlowStartAction; u = Camera.main.GetComponent <UIObject>(); BeforeAskList = Answer.LoadQuestions(2, 1); AfterAskList = Answer.LoadQuestions(2, 2); AnswerList = Answer.LoadQuestions(1, 1); init(); }
/// <summary> /// 程序退出 /// </summary> void OnApplicationQuit() { VoiceManage.MSCLogout(); if (nar.waveSource != null) { nar.StopRec(); } if (singleNar.waveSource != null) { singleNar.StopRec(); } if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Stop(); } if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } }
// Update is called once per frame void Update() { /*if (FlowStart) //监听流程是否开始 * { * FlowStart = false; * curMode = 1; * FlowManage.PlayTransitVoice(1, "下面进入沙勿略问我环节。"); * }*/ if (canPlay) //语音合成完毕并生成音频后播放 { canPlay = false; FlowManage.waveOutDevice = new WaveOutEvent(); //waveOutDevice.PlaybackStopped += waveOutDevice_PlaybackStopped; FlowManage.audioFileReader = new AudioFileReader(voice_path + "/" + FlowManage.voicename + ".wav"); FlowManage.waveOutDevice.Init(FlowManage.audioFileReader); FlowManage.waveOutDevice.Play(); FlowManage.PlayModeAnimation(); if (curMode == 1 && UserStartAnswer) { FlowManage.ShowQuestionInfo(); } } if (FlowManage.waveOutDevice != null) //音频播放完毕后开始答题 { if (UserStartAnswer) //用户开始回答问题 { if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped) { FlowManage.animName = AnimationControl.GetAnimationClipName(CharacterAction.Looking); FlowManage.PlayModeAnimation(); UserStartAnswer = false; Debug.Log("开始答题"); isAnswer = true; if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } if (FlowManage.audioFileReader != null) { FlowManage.audioFileReader.Close(); FlowManage.audioFileReader = null; } singleNar.StartRec(); } } if (AnswerAnalysis) //沙勿略问我模式答题后给出答案解析 { if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped) { if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } if (FlowManage.audioFileReader != null) { FlowManage.audioFileReader.Close(); FlowManage.audioFileReader = null; } Debug.Log("完成答案解析,进入下一题"); AnswerAnalysis = false; int questionNo = FlowManage.curNo; questionNo++; if (questionNo <= 3) { FlowManage.M2PMode(questionNo); } else { curMode = 2; FlowManage.PlayTransitVoice(2, "下面进入我问沙勿略环节。"); } } } if (isTransit) //流程过渡播放声音 { if (FlowManage.waveOutDevice != null) { if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped) { FlowManage.animName = AnimationControl.GetAnimationClipName(CharacterAction.Looking); FlowManage.PlayModeAnimation(); if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } if (FlowManage.audioFileReader != null) { FlowManage.audioFileReader.Close(); FlowManage.audioFileReader = null; } isTransit = false; Debug.Log("完成过渡"); flow_change = true; } } } if (FinishedAnswer) //我问沙勿略环节回答完毕 { if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped) { if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } if (FlowManage.audioFileReader != null) { FlowManage.audioFileReader.Close(); FlowManage.audioFileReader = null; } FinishedAnswer = false; FlowManage.canDistinguish = true; } } if (isFinished) //我问沙勿略环节结束 { if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped) { if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } if (FlowManage.audioFileReader != null) { FlowManage.audioFileReader.Close(); FlowManage.audioFileReader = null; } isFinished = false; FlowManage.canDistinguish = true; FlowStart = false; init(); } } } if (Input.GetMouseButtonDown(1)) { if (!FlowStart) { FlowStart = true; curMode = 1; FlowManage.PlayTransitVoice(1, "下面进入沙勿略问我环节。"); } } if (isAnswer) //答题计时 { answer_time += Time.deltaTime; if (answer_time >= 10) { isAnswer = false; answer_time = 0f; singleNar.StopRec(); } } if (curMode == 1 && successDistinguish) { FlowManage.StopAnswer(); successDistinguish = false; } if (flow_change) //切换流程 { flow_change = false; if (curMode == 1) { FlowManage.M2PMode(1); } else if (curMode == 2) { //AskMode = true; u.HideM2PAnswerPanel(); u.ShowP2MAskPanel(); FlowManage.canDistinguish = true; VoiceManage vm = new VoiceManage(); vm.VoiceDistinguish(); //nar.StartRec(); } } }
/// <summary> /// 我问沙勿略模式 /// </summary> public static void P2MMode(NAudioRecorder n) { canDistinguish = false; if (FlowManage.waveOutDevice != null) { FlowManage.waveOutDevice.Dispose(); FlowManage.waveOutDevice = null; } if (FlowManage.audioFileReader != null) { FlowManage.audioFileReader.Close(); FlowManage.audioFileReader = null; } if (VoiceManage.ask_rec_result == string.Empty || VoiceManage.ask_rec_result == null) { u.P2M_Ask_Panel.transform.GetChild(0).gameObject.transform.GetChild(1).gameObject.SetActive(true); u.P2M_Ask_Panel.transform.GetChild(0).gameObject.transform.GetChild(1).gameObject.GetComponent <UILabel>().text = "对不起,我没有听清您说的话!可以再说一次吗?"; content = "对不起,我没有听清您说的话!可以再说一次吗?"; voicename = "answer"; mt.FinishedAnswer = true; //mt.isFinished = true; } else { u.P2M_Ask_Panel.transform.GetChild(0).gameObject.transform.GetChild(1).gameObject.SetActive(true); u.P2M_Ask_Panel.transform.GetChild(0).gameObject.transform.GetChild(1).gameObject.GetComponent <UILabel>().text = VoiceManage.ask_rec_result; Debug.Log("小沙正在思考中..."); string answer_result = KeywordMatch.GetAnswerByKeywordMatch(VoiceManage.ask_rec_result, mt.BeforeAskList);//AIUI.HttpPost(AIUI.TEXT_SEMANTIC_API, "{\"userid\":\"test001\",\"scene\":\"main\"}", "text=" + Utils.Encode(VoiceManage.ask_rec_result)); if (answer_result.Equals("抱歉,这个问题我还不知道,问答结束!")) { answer_result = KeywordMatch.GetAnswerByKeywordMatch(VoiceManage.ask_rec_result, mt.AfterAskList); } u.P2M_Ask_Panel.transform.GetChild(0).gameObject.transform.GetChild(2).gameObject.SetActive(true); u.P2M_Ask_Panel.transform.GetChild(0).gameObject.transform.GetChild(2).gameObject.GetComponent <UILabel>().text = answer_result; content = answer_result; voicename = "answer"; mt.FinishedAnswer = true; if (answer_result.Equals("抱歉,这个问题我还不知道,问答结束!")) { XmlDocument xml = new XmlDocument(); XmlDeclaration decl = xml.CreateXmlDeclaration("1.0", "utf-8", null); xml.AppendChild(decl); XmlElement rootEle = xml.CreateElement("root"); xml.AppendChild(rootEle); xml.Save(mt.record_path + "/record.xml"); DateTime dateTime = new DateTime(); dateTime = DateTime.Now; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Application.dataPath + "/Resources/Question/record.xml"); XmlNode root = xmlDoc.SelectSingleNode("root"); XmlElement xe1 = xmlDoc.CreateElement("问题"); xe1.SetAttribute("内容", VoiceManage.ask_rec_result); xe1.SetAttribute("时间", dateTime.ToString()); root.AppendChild(xe1); xmlDoc.Save(Application.dataPath + "/Resources/Question/record.xml"); mt.FinishedAnswer = false; VoiceManage.StopSpeech(); } } Thread thread_answer = new Thread(new ThreadStart(playVoice)); thread_answer.IsBackground = true; thread_answer.Start(); //结束界面 //进入唤醒状态 }