/// <summary> /// 语音识别。如果是标准音频格式:pcm/16k/16位/单通道,则调用此方法。如果是其他格式的音频,请调用另一个方法并传入音频格式 WaveFormat 参数。 /// </summary> /// <param name="audioData">小于 60s 的音频数据(总长度不超过1920k),音频格式要求:pcm/16k/16位/单通道 。</param> /// <param name="languageType">音频语种类型</param> /// <param name="recogResult">识别成功返回识别结果,识别失败返回错误消息</param> /// <returns>识别成功或失败,true-成功;false-失败</returns> public bool AudioRecog(byte[] audioData, LanguageType languageType, out string recogResult) { if (_asr == null) { recogResult = "客户端尚未初始化"; return(false); } return(_asr.AudioRecog(audioData, languageType, out recogResult)); }
private void btnRecog_Click(object sender, EventArgs e) { string fileName = txtAudioFile.Text.Trim(); if (string.IsNullOrEmpty(fileName)) { return; } if (!File.Exists(fileName)) { return; } btnRecog.Enabled = false; string text = cmbLanguage.Text; ThreadPool.QueueUserWorkItem(o => { WriteLine("Start ..."); string result = string.Empty; byte[] data = File.ReadAllBytes(fileName); LanguageType type = GetLanguageType(text); Stopwatch sw = new Stopwatch(); sw.Start(); if (cbPCM.Checked) { // 3)语音识别 _asr.AudioRecog(data, type, out result); } else { WaveFormat waveFormat = WaveFormat.PCM_MONO; waveFormat.nSamplesPerSec = 22050; // 默认值就是这样的 waveFormat.nAvgBytesPerSec = 22050 * 2; _asr.AudioRecog(data, type, out result, waveFormat); } sw.Stop(); WriteLine(result); WriteLine("Finish! 耗时:" + sw.ElapsedMilliseconds + " 毫秒。\n"); EnableBtn(true); }); }
private void Asr(List <string> wavList) { for (int i = 0; i < wavList.Count; i++) { string wavFileName = wavList[i]; string pcmFileName = wavFileName.Replace(".wav", ".pcm"); if (!File.Exists(pcmFileName)) { continue; } byte[] data = File.ReadAllBytes(pcmFileName); LanguageType languageType = LanguageType.Mandarin; string result = string.Empty; bool ret = _asr.AudioRecog(data, languageType, out result); if (ret) { AddNewData(wavFileName, result); } UpdateProcessBar(i + 1); } UpdateProcessBar(_progressBar1.Maximum); MessageBox.Show("音频文件识别完成!"); _isBusy = false; }