C # Baidu Speech synthesis

Source: Internet
Author: User

Speech synthesis and TTS, we try to use the voice synthesis technology of Baidu but I found

One drawback is that there's no offline package that makes me uncomfortable, probably on the online version.

cause Microsoft speech recognition technology in Windows 2000 is the default integration in system components

Maybe we don't have to look for a new speech synthesis alternative, but once the online version is offline

Only game Over,hah But one advantage is that it is easy to use for GF development

The audio stream after the No. 200-degree speech synthesis, the sister's voice sounds more comfortable if you can

in in the "Kawaii" sister, will certainly be better, but should be in the resolution of the sound

hard. The feeling may be better, though speech synthesis technology has developed so many years at least presently I

not yet . have seen what services can provide a natural sense of speaking with people, and there will always be some machine

Mechanical Dry But when it comes to "Baidu speech recognition" and "optical recognition/OCR," I don't mean it .

thought discussion but support the domestic heart is the same, but Baidu's documents are only so concise

I can conclude that it must be Baidu's developers, Hah we are not Japanese and

India people do not write well what notes and documents, others that is writing compositions can we build?

First you need to have a Baidu account, and then to the new version of the official Baidu Voice homepage

Http://yuyin.baidu.com/fc.html


Then you will see the two different technologies above, and we choose speech synthesis technology


You can try a quick experience, which is actually implemented using the rest API, and don't tangle

We click on the use immediately, if it is the first time to use the technical services of Baidu then you need

Register The developer information once the rotten fill best lest Baidu put your information where to sell, Hah.


If you have registered your information, you will not be able to use Baidu services without creating an app.

You must create a project to "Create a new app" in the upper-right corner as shown


then you will see the following page, feel free to fill out a valid name to confirm the application of the class Don't

is also arbitrary choice, make so clear to Baidu what to do just use service is not sell information


When the app is created you will see the following page, then you need to click the Open service


If you do not see the service tag button, you can return to the Baidu Voice support home page

Select "Baidu Speech Synthesis" and click "Use Now"


It will prompt you to select your app and choose a sucks, then click Next

If the following page indicates success, you can also click "Create App" on the right.

Hyperlink Quick Launch Service


Well, if there is a need for Baidu help document can go to "Application Management" page click on you need

To the technical button, the following shortcut menu will pop up to select "Download SDK", or

From Baidu Voice Support home page Select "Baidu Speech synthesis"


After clicking "Related Downloads" in the toolbar, we select the "REST API" tab, please refer to

Click Download document, but Baidu's documentation you may have seen that you might think you have not seen Hah.


Now we need to go to "get access Token" is a new thing Baidu got out

We need to go to the "app management" page and click on the "Baidu Voice recognition" technology

App, click on the "View Key" graphic button and it will appear as shown in the


We now need to extract two important parameters and API key, Secret key session

To say why it's called "secret key." I reckoned a thing that's like a cipher.

We stitch a connection to a document to query our own "Access Token".

Https://openapi.baidu.com/oauth/2.0/token?

grant_type=client_credentials&

client_id=hinqtsu6g8c6xqwfvfbwt7tm&

Client_secret=8d5dbdb1b92bf945dca42f75c9eb1a69

Explain:

Grant_type//Authorization type

client_id//API Key

Client_secret//Secret Key


Accessing the above connection in the Browse will see the text JSON code like

What we just need "access_token/access tokens" in something else straight

There is nothing too much to delete and delete.

The following began to write in C # using the Baidu Speech Synthesis Service code, first we

Need to know, Baidu speech synthesis after the output of the audio stream is MP3 not wav

with my To play the audio stream, you need to ask for media that supports MP3 audio format

Library, but we're simply playing with its audio instead of having to make a music broadcast

The use of MCI is the most convenient and memory-saving approach

        Private Const string LAN = "zh";        Private Const string per = "0";         Private Const string CTP = "1";        Private Const string SPD = "3";        Private Const string pit = "6";             Private Const String vol = "9";        Private Const string cuid = "00-12-7b-16-74-8d";                                   Private Const String Tex = "far away from the mountains and rivers of silence, love the scenery as ever" + "Cang he chopped down the love, life and death light pay off" +        "Ramming a vein acacia into mud, mu Sihai sad Wind no Trace" + "to the raw Sea lakeshore again, Miao Hua Mirror blizzard total Calendar";        Private Const string Tok = "24.fffd842a3fe829c873fe1c4cd0cb9c4e.2592000.1439044224.282335-6396988"; Private Const string rest = "tex={0}&lan={1}&per={2}&ctp={3}&cuid={4}&tok={5}&spd={6}&        Pit={7}&vol={8} ";        Private Const int NULL = 0, error_success = null; [DllImport ("WinMm.dll", CharSet = CharSet.Auto, callingconvention = callingconvention.stdcall)] private static Exte RN int mciSendString (string lpstrcommand, STring lpstrreturnstring, int ureturnlength, int hwndcallback); 
From the code above you may be wondering why or there are so many constants then you see

You will know that some friends who watch the document do not


Are you finding the same as the above constants now? It is "Baidu speech synthesis" needs

Some of the "necessary or optional" parameters, the way we use post here is mainly

Is that I'm a little too dick to use get way, although the document is get way

        protected override void OnLoad (EventArgs e) {string strupdatedata = string.            Format (rest, Tex, LAN, per, CTP, cuid, Tok, SPD, Pit, vol);            HttpWebRequest req = webrequest.create ("Http://tsn.baidu.com/text2audio") as HttpWebRequest; Req.            Method = "POST"; Req.            ContentType = "application/x-www-form-urlencoded"; Req.            ContentLength = Encoding.UTF8.GetByteCount (Strupdatedata); using (StreamWriter SW = new StreamWriter (req). GetRequestStream ())) SW.            Write (Strupdatedata); HttpWebResponse res = req.            GetResponse () as HttpWebResponse; using (Stream stream = Res.)                GetResponseStream ()) {string strfullfilename = Application.startuppath + "/app.mp3"; using (FileStream fs = new FileStream (Strfullfilename, Filemode.truncate | FileMode.OpenOrCreate, FileAccess.ReadWrite, fileshare.readwrite)) stream.                CopyTo (FS); if (MCISENDSTRing (String. Format ("open \" {0}\ "Alias app", Strfullfilename), NULL, NULL, NULL) = = ERROR_SUCCESS) mcisendstring ("p            Lay app ", NULL, NULL, NULL); }        }
. NET post is not the way it is, generally the most commonly used or

. NET upper layer provides HttpWebRequest, but Baidu speech recognition

Required to use UTF-8 string post, be sure to pay attention to the details here

Otherwise you will not be able to use the service provided by Baidu and return the error code to you

Presumably these details as a developer should not be committed

We write the HTTP stream returned after post to the file, but above

I said Baidu is the return of the MP3 stream, then you need to mp3 the side

Write to a file, and if you use a bass, it does not have to play in-memory

Media streaming, but the MCI way is not, if you don't understand the code

and need to use can copy my code but suggest you can read the best

Namespaces that depend on:

Using system.net;using system.io;using system.text;using System.Runtime.InteropServices;
To the end of the basic, the use of Baidu speech synthesis in C # is not

It's so hard for you to imagine, you don't have to tangle with how it's used.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Baidu Speech synthesis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.