Using speech recognition and speech synthesis technology in. net

Source: Internet
Author: User

Using speech recognition and speech synthesis technology in. net
To use Speech recognition and Speech synthesis technology in. net, you need to use Microsoft's Speech SDK. To use it in Web applications, you need the Speech Application SDK. The Speech SDK can be downloaded at the two files include the Speech SDK 5.1 and 5.1 Language Pack. The former is an development kit, but only supports English. The latter is a Chinese and Japanese Language Pack, after installation, Chinese characters are supported.

SDK Composition


1. Open vs2005, create a windows application, add a label, A richtextbox (used to input the text to be read), and a button to the design form. And set the Text attributes of the label and button respectively. For example

2. Add necessary references. Select "project"> "add reference"> "COM" and click "Microsoft Speech Object Library" to exit.

3. Double-click the button to add events to it. Add a namespace at the top of the code page. The Code is as follows:

Using SpeechLib;


4. The event handler code of the button is as follows.

Private void button#click (object sender, EventArgs e)
SpVoiceClass voice = new SpVoiceClass ();
Voice. Voice = voice. GetVoices (string. Empty, string. Empty). Item (3); // where 3 is Chinese and 024 is English
Voice. Speak (richTextBox1.Text, SpeechVoiceSpeakFlags. SVSFDefault );


5. Press F5 to run the task. Enter the text in the blank area and click "read aloud". Try the result.


Gets or sets the pause line.
Set whether to allow the sound to automatically adjust to the appropriate status to adapt to its audio output.
. Gets or sets the audio output object used by the current sound.
Gets or sets the audio output stream object used by the current sound.
Gets or sets the event type returned by the current sound.
Gets or sets the voice priority.
Get or set the reading speed.
Returns an ISpeechVoiceStatus object used to display the status of the current read and event.
Gets or sets a time interval to identify how long a synchronization Speak and SpeakStream will terminate after an output device is not obtained, in milliseconds.
Obtains or sets the pronunciation object.
Obtains or sets the sound size.


Whether to display detailed settings in the control panel.
Returns an available audio output tag.
Returns an available pronunciation object.
Determines whether the control can be achieved by controlling the audio settings of the Cotton Board.
Pause reading ..
Resume pause and Resume playback.
In the current text stream, jump forward or backward for a certain distance and then play back.
Read a string.
Get a time handle for reading.
Read a text stream or sound file.
Blocks the process until the sound has been played or timed out.
This article introduces some basic knowledge about speech synthesis, that is, first j creates a SpVoiceClass class object, and then calls the GetVoices method of the object to obtain a pronunciation object, however, by setting the parameters of this method, only objects with Chinese or English pronunciation can be created, but there is no way to mix Chinese and English texts. To solve this problem, you can determine the ASC code of each character in the string to determine whether the input string is Chinese or English. The following is the judgment code.
1 public bool Analyse (string strSpeak)
2 {
3 int iCbeg = 0;
4 int iEbeg = 0;
5 bool IsChina = true;
6 for (int I = 0; I <strSpeak. Length; I ++)
7 {
8 char chr = strSpeak [I];
9 if (IsChina)
10 {
11 if (chr <= 122 & chr> = 65)
12 {
13 int iLen = I-iCbeg;
14 string strValue =
15strSpeak. Substring (iCbeg, iLen );
16 SpeakChina (strValue );
17 iEbeg = I;
18 IsChina = false;
21 else
22 {
23 if (chr> 122 | chr <65)
24 {
25 int iLen = I-iEbeg;
26 string strValue =
27strSpeak. Substring (iEbeg, iLen );
28 this. SpeakEnglishi (strValue );
29 iCbeg = I;
30 IsChina = true;
34 return IsChina;
For the parameter of the Speak method, the first is a string type, and the second is an enumeration of the SpeechVoiceSpeakFlags type. When SVSFDefault is set, the first parameter is the text to be read. If SVSFIsFilename is set, the first parameter is the file name of the text to be read, instead of the content to be read.

The following describes the SpeakStream method of this class. This method has two parameters, the first is SpeechBaseStream, and the second is an enumeration of the SpeechVoiceSpeakFlags type like Speak. SpeechBaseStream is an interface that inherits three objects. These three objects are similar. First, we will introduce SpFileStream. SpFileStream has three common methods: Read, Seek, and Write. The Read method can create a *. wav file. The following Code demonstrates how to create a file:

SpFileStreamClass fs1 = new SpFileStreamClass ();
SpVoiceClass v = new SpVoiceClass ();
Fs1.Open (textBox1.Text, SpeechStreamFileMode. SSFMCreateForWrite, false );
// TextBox1.text is the path of the file to be created.
V. AudioOutputStream = fs1;
String [] ss = new string [4] {"this", "is", "a", "demo "};
Foreach (string s in ss)
V. Speak (s, SpeechVoiceSpeakFlags. SVSFlagsAsync );
Fs1.Close ();
The following code is used to show the Speak and SpeakStream:

SpFileStreamClass fs1 = new SpFileStreamClass ();
SpFileStreamClass fs2 = new SpFileStreamClass ();
SpVoiceClass v = new SpVoiceClass ();
Fs1.Open (textBox1.Text, SpeechStreamFileMode. SSFMOpenForRead, false );
Fs2.Open (textBox2.Text, SpeechStreamFileMode. SSFMOpenForRead, false );
V. Speak ("This is the first sound file", SpeechVoiceSpeakFlags. SVSFlagsAsync );
V. SpeakStream (fs1, SpeechVoiceSpeakFlags. SVSFlagsAsync );
V. Speak ("This is the second sound file", SpeechVoiceSpeakFlags. SVSFlagsAsync );
V. SpeakStream (fs2, SpeechVoiceSpeakFlags. SVSFlagsAsync );
Fs1.Close ();
Fs2.Close ();

This article from the CSDN blog, reproduced please indicate the source:

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: 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.