Winform利用訊飛語音WebAPI實現線上語音合成、語音識別
阿新 • • 發佈:2018-12-18
閒來無事做的一個demo。本來想做成網站的形式,發現不知道怎麼在請求頭裡加資訊,於是就做成了Winform版的。
去官網開通sdk
沒註冊的先要註冊,然後建立應用,應用平臺選擇webAPI
回到控制檯,新增新服務--線上語音合成、語音聽寫
新增IP白名單,把ip加進去(直接百度IP就可以查到自己的IP地址),記住這裡的APIKey,一會要用
還有這裡的APPID一會也有用處
在VS中新建Winform專案,在app.config填入剛才的appid、apikey
接下來就是寫處理邏輯了。其實很簡單,就是檢視官方的sdk,跟著一步一步做就行了。直接附上程式碼:
語音合成
private void btnTTS_Click(object sender, EventArgs e) { String appId = ConfigurationManager.AppSettings["appId"]; String apiKey = ConfigurationManager.AppSettings["apiKeyTTS"]; String realIp = ConfigurationManager.AppSettings["realIp"]; String curTime = Utils.ConvertDateTimeLong(DateTime.Now).ToString(); Dictionary<string, string> xParams = new Dictionary<string, string>(); xParams.Add("auf", ConfigurationManager.AppSettings["aufTTS"]); xParams.Add("aue", ConfigurationManager.AppSettings["aueTTS"]); xParams.Add("voice_name", ConfigurationManager.AppSettings["voice_nameTTS"]); xParams.Add("speed", ConfigurationManager.AppSettings["speedTTS"]); xParams.Add("volume", ConfigurationManager.AppSettings["volumeTTS"]); xParams.Add("pitch", ConfigurationManager.AppSettings["pitchTTS"]); xParams.Add("engine_type", ConfigurationManager.AppSettings["engine_typeTTS"]); xParams.Add("text_type", ConfigurationManager.AppSettings["text_typeTTS"]); string json = JsonConvert.SerializeObject(xParams); String param = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));//"eyJhdWYiOiAiYXVkaW8vTDE2O3JhdGU9MTYwMDAiLCJhdWUiOiAicmF3Iiwidm9pY2VfbmFtZSI6ICJ4aWFveWFuIiwic3BlZWQiOiAiNTAiLCJ2b2x1bWUiOiAiNTAiLCJwaXRjaCI6ICI1MCIsImVuZ2luZV90eXBlIjogImludHA2NSIsInRleHRfdHlwZSI6ICJ0ZXh0In0="; String checkSum = Utils.GetMD5(apiKey + curTime + param); HttpWebRequest req = WebRequest.CreateHttp("http://api.xfyun.cn/v1/service/v1/tts"); req.Method = "POST"; //設定請求頭 req.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; req.Headers.Add("X-Appid", appId); req.Headers.Add("X-CurTime", curTime); req.Headers.Add("X-Param", param); req.Headers.Add("X-Real-Ip", realIp); req.Headers.Add("X-CheckSum", checkSum); //將文字框中文字寫入到post資料 using (Stream stream = req.GetRequestStream()) { string text = System.Web.HttpUtility.HtmlEncode(txtTTS.Text); byte[] buffer = Encoding.UTF8.GetBytes("text=" + text); stream.Write(buffer, 0, buffer.Length); } //獲取響應 using (WebResponse resp = req.GetResponse()) { Stream stream = resp.GetResponseStream(); //如果成功,儲存為音訊檔案 if (resp.ContentType.Contains("audio/mpeg")) { string filename = "temp.wav";//預設wav檔案,檔案型別與配置檔案中aue的值有關 if ("lame" == ConfigurationManager.AppSettings["aueTTS"]) { filename = "temp.mp3"; } using (FileStream fs = new FileStream(filename, FileMode.Create)) { int b = -1; while ((b = stream.ReadByte()) != -1) { fs.WriteByte((byte)b); } //檔案流指標回到開始以便進行播放 fs.Seek(0, SeekOrigin.Begin); SoundPlayer sp = new SoundPlayer(fs); sp.Load(); sp.Play(); } } //否則獲取失敗,輸出錯誤資訊 else { StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")); string retString = streamReader.ReadToEnd(); ErrorCode errCode = JsonConvert.DeserializeObject<ErrorCode>(retString); ErrorMessage errMsg = new ErrorMessage(errCode); txtResult.Text = errMsg.ToString() + Environment.NewLine + errCode.ToString(); } stream.Close(); } }
語音識別
private void btnSTT_Click(object sender, EventArgs e) { //先選擇一個音訊檔案,準備好要Post傳送的資料 string audio = string.Empty; using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Title = "選擇一個音訊檔案進行語音轉文字"; ofd.Filter = "未壓縮的聲音|*.wav;*.pcm"; ofd.ShowDialog(); if (ofd.FileName == "") { return; } //讀取檔案進行base64編碼 using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open)) { byte[] array = new byte[fs.Length]; fs.Read(array, 0, array.Length); string base64Audio = Convert.ToBase64String(array); string text = System.Web.HttpUtility.HtmlEncode(base64Audio); audio = System.Web.HttpUtility.UrlEncode(text); //編碼後長度不超過2兆 if (Encoding.UTF8.GetByteCount(audio) > 2 * 1024 * 1024) { MessageBox.Show("檔案超過了2兆,無法提交!", "檔案太大", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } } //獲取appid、apikey、curTime String appId = ConfigurationManager.AppSettings["appId"]; String apiKey = ConfigurationManager.AppSettings["apiKeySTT"]; String realIp = ConfigurationManager.AppSettings["realIp"]; String curTime = Utils.ConvertDateTimeLong(DateTime.Now).ToString(); //計算校驗和 Dictionary<string, string> xParams = new Dictionary<string, string>(); xParams.Add("engine_type", ConfigurationManager.AppSettings["engine_typeSTT"]); xParams.Add("aue", ConfigurationManager.AppSettings["aueSTT"]); string json = JsonConvert.SerializeObject(xParams); string param = Convert.ToBase64String(Encoding.UTF8.GetBytes(json)); String checkSum = Utils.GetMD5(apiKey + curTime + param); HttpWebRequest req = WebRequest.CreateHttp("http://api.xfyun.cn/v1/service/v1/iat"); req.Method = "POST"; //設定請求頭 req.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; req.Headers.Add("X-Appid", appId); req.Headers.Add("X-CurTime", curTime); req.Headers.Add("X-Param", param); req.Headers.Add("X-Real-Ip", realIp); req.Headers.Add("X-CheckSum", checkSum); //將base64編碼的音訊檔案寫入到post資料 using (Stream stream = req.GetRequestStream()) { byte[] buffer = Encoding.UTF8.GetBytes("audio=" + audio); stream.Write(buffer, 0, buffer.Length); } //獲取響應流 using (Stream stream = req.GetResponse().GetResponseStream()) { //將響應json反序列化為物件 StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")); string retString = streamReader.ReadToEnd(); ErrorCode errCode = JsonConvert.DeserializeObject<ErrorCode>(retString); ErrorMessage errMsg = new ErrorMessage(errCode); //如果獲取失敗,輸出錯誤資訊 txtResult.Text = errMsg.ToString() + Environment.NewLine + errCode.ToString();//retString; //如果成功,顯示轉換後的字串 txtResult.Text = errMsg.ToString(); } }