/// <summary> /// 合成测试(不保存,合成完立即播放) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { if (!SDKRealize.GetInitialize().IsLogin) { SDKRealize.GetInitialize().MSPLogin(); } int eCode = -1; //错误码 var sessionId = SDKRealize.GetInitialize().TTSSessionBegin(ttsSessionParam, ref eCode); if (eCode == 0) { string text = this.textBox4.Text; uint textLen = (uint)Encoding.Default.GetBytes(text).Length; bool isSuccess1 = SDKRealize.GetInitialize().TTSTextPut(sessionId, text, textLen, null); if (isSuccess1) { uint audioLen = 0; //返回的音频字节长度 int synthStatus = 0; //返回的合成状态 int eCode1 = -1; //返回的合成音频错误码 byte[] bytes = SDKRealize.GetInitialize().TTSAudioGet(sessionId, ref audioLen, ref synthStatus, ref eCode1); if (eCode1 == 0) { Naudio.GetInstance().PlayAsBytes(bytes); } } } SDKRealize.GetInitialize().TTSSessionEnd(sessionId, null); }
/// <summary> /// 自定义播放结束事件 /// 自带的播放结束事件经常不会触发,自己写个事件 /// </summary> private void MainForm_PlayEnd() { if (autoRecord) { Naudio.GetInstance().StartRecord(); } }
/// <summary> /// 处理识别结果 /// </summary> /// <param name="txt"></param> public void DealIdentifyResult(string txt) { string[] strsIndex = { "返回首页", "回到首页", "进入首页" }; //首页命令 string[] strsPrevious = { "上一步", "后退", "返回上一步" }; //上一步命令 if (((IList)strsIndex).Contains(txt)) { this.butIndex.PerformClick(); } else if (((IList)strsPrevious).Contains(txt)) { this.butSuper.PerformClick(); } else { var qpoint = SelectSimilarityObj(txt, listQnode.Last().Qpoints); if (qpoint != null) { var qpList = SearchSells(qpoint); listQnode.Add(new Qnode { TSY = qpoint.TSY, Qpoints = qpList }); ShowCells(); } else { autoRecord = false; Naudio.GetInstance().PlayText("没找到'" + txt + "',请尝试点击屏幕中的选项!"); } } }
/// <summary> /// 合成+播放 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void but_Synthetic_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.text_Txt.Text)) { var bytes = XFwebApi.GetInstance().XunFeiTTS(this.text_Txt.Text); Naudio.GetInstance().PlayAsBytes(bytes); } }
} //链表记录当前层级节点关系路径 public MainForm() { InitializeComponent(); IndexStepInit(); Naudio.GetInstance().waveIn.RecordingStopped += WaveIn_RecordingStopped; Naudio.GetInstance().waveOut.PlaybackStopped += WaveOut_PlaybackStopped; Naudio.GetInstance().PlayEnd += MainForm_PlayEnd; }
/// <summary> /// 录音结束事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WaveIn_RecordingStopped(object sender, NAudio.Wave.StoppedEventArgs e) { Naudio.GetInstance().wfw.Dispose(); Naudio.GetInstance().wfw = null; var bytes = File.ReadAllBytes(Naudio.GetInstance().fileName); var txt = XFwebApi.GetInstance().XunFeiIAT(bytes).Data; DealIdentifyResult(txt); }
/// <summary> /// 自动录音+自动识别+自动播放 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void but_AutoAudio_Click(object sender, EventArgs e) { Naudio.GetInstance().StartRecord(); Thread.Sleep(10 * 1000); Naudio.GetInstance().StopRecord(); FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\T02.wav", FileMode.Open); var bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); this.text_AutoResult.Text = XFwebApi.GetInstance().XunFeiIAT(bytes).Data; Naudio.GetInstance().PlayAsBytes(bytes); }
/// <summary> /// 识别+播放 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void but_Identify_Click(object sender, EventArgs e) { var filePath = @"C:\Users\Administrator\Desktop\T02.wav"; using (FileStream fs = new FileStream(filePath, FileMode.Open)) { var bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); this.text_Result.Text = XFwebApi.GetInstance().XunFeiIAT(bytes).Data; Naudio.GetInstance().PlayAsBytes(bytes); } }
/// <summary> /// 展示队列中最后一个节点 /// </summary> public void ShowCells() { var qnode = listQnode.Last(); this.panel1.Controls.Clear();//清除面板 if (listQnode.Count > 1) { this.butIndex.Enabled = true; this.butSuper.Enabled = true; } else { this.butIndex.Enabled = false; this.butSuper.Enabled = false; } Label label = new Label(); label.Location = new Point(100, 10); label.Text = qnode.TSY; this.panel1.Controls.Add(label); if (qnode.Qpoints != null && qnode.Qpoints.Count > 0) { int xLocation = 100; //x轴位置 int yLocation = 50; //y轴位置 int butNum = 0; foreach (Qpoint qpoint in qnode.Qpoints) { QButton button = new QButton(); button.Location = new Point(xLocation, yLocation); button.Text = qpoint.Text; button.Click += ClickEvent; button.QPoint = qpoint; this.panel1.Controls.Add(button); yLocation += 30; butNum++; } } autoRecord = true; Naudio.GetInstance().PlayText(qnode.TSY); }
/// <summary> /// 停止录音 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void but_StopRecord_Click(object sender, EventArgs e) { Naudio.GetInstance().StopRecord(); }