C # Baidu speech synthesis,

Source: Internet
Author: User

C # Baidu speech synthesis,

Speech Synthesis and TTS, we tried to use Baidu's speech synthesis technology, but I found

One drawback is that the absence of an offline package makes me a little uncomfortable. It may be an online version.

The reason is that Microsoft Speech Recognition technology is integrated in system components by default in Windows 2000.

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

Only Game Over, Hah, however, has the advantage that it is easy to use and suitable for guangfa development.

Audio streams after 200th-degree speech synthesis. The girl's voice sounds more comfortable.

It will certainly be better for the cute sister in "kawaii", but the sound should be fixed.

Although the speech synthesis technology has been developing for so many years, at least I

We haven't seen any services that can provide a natural sense of talking to people, and there will always be some machines.

I am not happy to mention "Baidu Speech Recognition" and "optical recognition/OCR"

Thinking about the discussion, but the support for domestic products remains unchanged, but Baidu's documentation is only so concise

I can conclude that it must have been done by Baidu's developers. Hah, we are not Japanese and

It is difficult for Indians to write comments and documents. Can we make them?

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

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


Then you will see the two different technologies above. We choose speech synthesis technology.


You can try to experience it quickly. In fact, it is implemented by using the rest api.

Click "use now". If this is the first time you use Baidu's Technical Services, you need

It is best to fill in the registration developer information once, lest Baidu sell your information, Hah.


If you have already registered the information and have not created an application, you cannot use Baidu's service.

You must create a project and click "create application" in the upper-right corner, as shown in"


Then you will see the following page, enter a valid name at will to confirm the Application category

It is also a casual choice, so clearly what to do for Baidu is just to use services, not to sell information


After the application is created, you will see the following page.


If you do not see the Service Activation tag button, you can go back to the Baidu speech support homepage.

Select "Baidu speech synthesis" and click "use now"


It will prompt you to select your application and select a sufficient number, and then click Next.

If the following page is displayed, you can click "create application" on the right side"

Hyperlink quick activation Service


Well, if you need Baidu help documentation, go to the "Application Management" Page and click what you need

You can select "Download SDK" from the shortcut menu below, or

Select "Baidu speech synthesis" from the Baidu speech support Homepage"


Click "download related" in the toolbar and select the "rest api" tab. Refer

Click to download the document. However, you may think you have not read the document of Baidu.


Now we need to go to "get Access Token", which is a new product developed by Baidu.

We need to go to the "Application Management" Page and click

Application. Click the "view Key" graphic button, as shown in.


Now we need to extract two important parameters and the API Key and Secret Key words

Why is it a "secret Key"? I guess it's similar to a password.

We concatenate a connection based on the 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

Explanation:

Grant_type // authorization type

Client_id // API Key

Client_secret // Secret Key


After accessing the above connection during browsing, the text Json code shown in figure is displayed.

We only need something in the "access_token/access token ".

It does not play a major role in connection, deletion, and deletion.

The following describes how to use C # To compile the code for using the Baidu speech synthesis service.

You need to know that the audio stream output after Baidu speech synthesis is mp3, not wav.

To play the audio stream, you need to seek help to support the mp3 audio format media.

Library, but we simply play its audio instead of making music.

The use of mci is the most convenient and memory-saving method.

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 mountains and rivers quiet, those who have fallen in love with the scenery like Xi "+" Cang he has fallen into love fans, while those who have fallen in love with life and death have moved away from each other "+, mu Sihai sorrow and Sorrow "+" to the sea smoke wave again, wonderful China Mirror Snow co-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 extern int mciSendString (string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback );
From the above Code, you may wonder why there are so many constants.

You will know that some friends who watch documents do not need


Is it found that it is the same as the above constant? It is required by "Baidu speech synthesis"

Some of the "necessary or optional" parameters, here we use the POST method mainly

It seems that I am a little embarrassed to use the GET method, although the GET method is used in the document.

        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("play app", null, NULL, NULL);            }        }
The. net post method is generally the most common

The. NET upper layer provides HttpWebRequest, but Baidu Speech Recognition

Requires the use of the UTF-8 string POST, be sure to pay attention to the details here

Otherwise, you will not be able to use the services provided by Baidu, and the error code will be returned to you.

Presumably, these details should not be committed as a developer.

We write the HTTP stream returned after POST to the file,

I said that Baidu is the returned mp3 stream, so you need

Write to a file. If BASS is used, it is not required to play back the memory.

However, it is not possible to use the mci method. If you do not understand the code

I am eager to use it to copy my code, but it is recommended that you understand the best.

Dependent namespace:

using System.Net;using System.IO;using System.Text;using System.Runtime.InteropServices;
This is basically the end. The use of Baidu speech synthesis in C # is not

As hard as you think, you don't have to worry about how to use it.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.