Csharp: Baidu speech recognition and csharp Baidu Speech Recognition
Public string API_id = "8888"; // your ID public string API_record = null; public string API_record_format = "wav"; public string API_record_HZ = "16000 "; public string API_key = "k2"; // your KEY public string API_secret_key = "55519"; // your SECRRET_KEY public string API_language = "zh"; public string API_access_token = null; public string strJSON = ""; // recording [DllImport ("winmm. dll ", EntryPoint =" mciSendString ", CharSet = CharSet. auto)] public static extern int mciSendString (string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback ); /// <summary> ///// </summary> public Form1 () {InitializeComponent ();} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void Form1_Load (object sender, eventArgs e) {API_record = Application. startupPath + "\ mongo.wav"; API_access_token = getStrAccess (API_key, API_secret_key );} /// <summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> private void button#click (object sender, eventArgs e) {this. textBox2.Text = getStrText (API_id, API_access_token, API_language, API_record, API_record_format, API_record_HZ );} /// <summary> ///// </summary> /// <param name = "para_API_key"> your KEY </param> /// <param name = "para_API_secret_key"> your SECRRET_KEY </param> // <returns> </returns> public string getStrAccess (string para_API_key, string para_API_secret_key) {// method parameter description: // para_API_key: API_key (your KEY) // para_API_secret_key (your SECRRET_KEY) // method return value description: // Baidu authentication password, access_token string access_html = null; string access_token = null; string getAccessUrl =" https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials "+" & Client_id = "+ para_API_key +" & client_secret = "+ para_API_secret_key; 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 = 30000; // if the connection fails in 30 seconds, the getAccessRequest is interrupted. 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 (); // get the returned toke return access_token ;} /// <summary> ///// </summary> /// <param name = "para_API_id"> </param> /// <param name = "para_API_access_token "> </param> /// <param name =" para_API_language "> </param> /// <param name =" para_API_record "> </param> /// <param name = "para_format"> </param> // <param name = "para_Hz"> </param> // <returns> </returns> public string getStrText (string para_API_id, string para_API_access_token, string para_API_language, string para_API_record, string para_format, string para_Hz) {// method parameter description: // para_API_id: API_id (your ID) // para_API_access_token (getStrAccess (...) access_token password obtained by the method) // para_API_language (the language you want to recognize, zh, en, ct) // para_API_record (the path of the voice file) // para_format (the format of the voice file) // para_Hz (the sampling rate of the voice file is 16000 or 8000) // return value of this method: // if this method is executed correctly, the returned value is the text of the speech translation. if the error is an error code, you can see the Baidu speech document and check the corresponding error string strText = null; string error = null; FileInfo fi = new FileInfo (para_API_record); FileStream fs = new FileStream (para_API_record, 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= "+ Para_API_language +" & cuid = "+ para_API_id +" & token = "+ para_API_access_token; 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/" + para_format + "; rate =" + para_Hz; 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; // 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); // Parse 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 ;}}
Reference: http://yuyin.baidu.com/docs/asr/57
API request methods
The speech synthesis interface supports POST and GET methods.
Official Address: http://tsn.baidu.com/text2audio