添加的DLL库
大致流程
1.调用傲瑞公司的声卡、混音录制样例,以MP3格式保存在本地。
2.个人花时间相对多的部分:将MP3格式的录音文件转化为WAV格式,参数是:16000(8000可能会出现3301错误:语音模糊)因为百度云语音识别只接受WAV、Pcm格式的录音文件。
3.调用百度云语音识别:获取密钥,转化为文本。
代码部分
using ESBasic;
using NAudio.Wave;
using Newtonsoft.Json.Linq;
using Oraycn.MCapture;
using Oraycn.MFile;
using System;
using System.IO;
using System.Net;
using System.Speech.Synthesis;
using System.Text;
using System.Windows.Forms;
using WaveFormat = NAudio.Wave.WaveFormat;
namespace 测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private int audioCount = 0;
private string strJSON;
private ISoundcardCapturer soundcardCapturer;
private IMicrophoneCapturer microphoneCapturer;
private IAudioMixter audioMixter;
private AudioFileMaker audioFileMaker;
private SpeechSynthesizer speechSyn;
private volatile bool isRecording = false;
//开始声卡采集、录制
void capturer_CaptureError(Exception obj)
{
}
void audioMixter_AudioMixed(byte[] audioData)
{
if (this.isRecording)
{
this.audioFileMaker.AddAudioFrame(audioData);
++this.audioCount;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.audioCount = 0;
//声卡采集器 【目前声卡采集仅支持vista以及以上系统】
this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer();
//麦克风采集器
this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0);
//混音器
this.audioMixter = CapturerFactory.CreateAudioMixter(this.microphoneCapturer, this.soundcardCapturer, SoundcardMode4Mix.DoubleChannel, true);
this.audioMixter.AudioMixed += new CbGeneric<byte[]>(audioMixter_AudioMixed);
this.microphoneCapturer.CaptureError += new CbGeneric<Exception>(capturer_CaptureError);
this.soundcardCapturer.CaptureError += new CbGeneric<Exception>(capturer_CaptureError);
//开始采集
this.microphoneCapturer.Start();
this.soundcardCapturer.Start();
//录制组件
this.audioFileMaker = new AudioFileMaker();
this.audioFileMaker.Initialize("test.mp3", AudioCodecType.MP3, this.audioMixter.SampleRate, this.audioMixter.ChannelCount);
this.isRecording = true;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
//停止声卡采集、停止录制
private void button2_Click(object sender, EventArgs e)
{
try
{
this.Cursor = Cursors.WaitCursor;
CbGeneric cb = new CbGeneric(this.StopRecordAsyn);
cb.BeginInvoke(null, null);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void StopRecordAsyn()
{
this.isRecording = false;
this.microphoneCapturer.Stop();
this.microphoneCapturer.Dispose();
this.soundcardCapturer.Stop();
this.soundcardCapturer.Dispose(); //释放声卡采集器
this.audioMixter.Dispose();
this.audioFileMaker.Close(true);
this.audioFileMaker.Dispose();
this.audioFileMaker = null;
this.AfterStopRecord();
}
private void AfterStopRecord()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbGeneric(this.AfterStopRecord));
}
else
{
this.Cursor = Cursors.Default;
MessageBox.Show(Get());
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.isRecording)
{
MessageBox.Show("正在录制视频,请先停止!");
e.Cancel = true;
return;
}
}
public static string Convert2Wav()
{
string filePath = AppDomain.CurrentDomain.BaseDirectory + @"\test.Mp3";
string directoryName = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
string tempDir = directoryName + "\\temp";//+ DateTime.Now.ToString("yyyyMMddHHmmss") + "\\";
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
}
if (filePath.EndsWith(".wav", StringComparison.CurrentCultureIgnoreCase))
{
using (var reader = new WaveFileReader(filePath))
{
var newFormat = new WaveFormat(8000, 8, 1); // 8kHz, 8bit
using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
{
WaveFileWriter.CreateWaveFile(tempDir + fileName, conversionStream);
}
}
}
else if (filePath.EndsWith(".mp3", StringComparison.CurrentCultureIgnoreCase))
{
using (Mp3FileReader reader = new Mp3FileReader(filePath))
{
var newFormat = new WaveFormat(16000, 16, 1); // 8kHz, 8bit
using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
{
WaveFileWriter.CreateWaveFile(tempDir + "yyy.WAV", conversionStream);
}
}
}
return tempDir + "yyy.WAV";
}
public string GetStrText(string ch, string pal, string pai, string fbl, string gs)
{
string strText = null;
string error = null;
string fff = Convert2Wav();
FileInfo fi = new FileInfo(fff);
FileStream fs = new FileStream(fff, FileMode.Open);
byte[] voice = new byte[fs.Length];
fs.Read(voice, 0, voice.Length);
fs.Close();
string getTextUrl = "http://vop.baidu.com/server_api?lan=" + "&cuid=" + "" + pai + "" + "&token=" + "" + ch + "";
HttpWebRequest getTextRequst = WebRequest.Create(getTextUrl) as HttpWebRequest;
getTextRequst.Proxy = null;
getTextRequst.ServicePoint.Expect100Continue = false;
getTextRequst.ServicePoint.UseNagleAlgorithm = false;
getTextRequst.ServicePoint.ConnectionLimit = 65500;
getTextRequst.AllowWriteStreamBuffering = false;
getTextRequst.ContentType = "audio /" + "" + gs + "" + ";rate=" + "" + fbl + "";
getTextRequst.ContentLength = fi.Length;
getTextRequst.Method = "post";
getTextRequst.Accept = "*/*";
getTextRequst.KeepAlive = true;
getTextRequst.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
getTextRequst.Timeout = 30000;//30秒连接不成功就中断
using (Stream writeStream = getTextRequst.GetRequestStream())
{
writeStream.Write(voice, 0, voice.Length);
}
HttpWebResponse getTextResponse = getTextRequst.GetResponse() as HttpWebResponse;
using (StreamReader strHttpText = new StreamReader(getTextResponse.GetResponseStream(), Encoding.UTF8))
{
strJSON = strHttpText.ReadToEnd();
}
JObject jsons = JObject.Parse(strJSON);//解析JSON
if (jsons["err_msg"].Value<string>() == "success.")
{
strText = jsons["result"][0].ToString();
return strText;
}
else
{
error = jsons["err_no"].Value<string>() + jsons["err_msg"].Value<string>();
return error;
}
}
public string Get()
{
string access_html = null;
string access_token = null;
string bbb = "15208407";
string aaa = "1537";
//参数16000 8000可能会语音模糊
string ccc = "16000";
string ddd = "wav";
string getAccessUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" +
"&client_id=" + "TUNRWWLgvYGVTq2Hw1BGQ0Fe" + "&client_secret=" + "G9HNiYVPHsj6bblL6OQUg4LNxMXPMYnb";
try
{
HttpWebRequest getAccessRequest = WebRequest.Create(getAccessUrl) as HttpWebRequest;
//getAccessRequest.Proxy = null;
getAccessRequest.ContentType = "multipart/form-data";
// getAccessRequest.Accept = "*/*";
getAccessRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
getAccessRequest.Timeout = 100000;//100秒连接不成功就中断
getAccessRequest.Method = "post";
HttpWebResponse response = getAccessRequest.GetResponse() as HttpWebResponse;
using (StreamReader strHttpComback = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
access_html = strHttpComback.ReadToEnd();
}
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString());
}
JObject jo = JObject.Parse(access_html);
access_token = jo["access_token"].ToString();//得到返回的toke
textBox1.Text = GetStrText(access_token, aaa, bbb, ccc, ddd);
return access_token;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}