Application of speech synthesis and recognition technology in C #

Source: Internet
Author: User

To implement Chinese pronunciation or Chinese speech recognition, you must first install Microsoft's speech application SDK (sasdk). The latest version is SAPI 5.1, which can recognize Chinese, Japanese, and English languages, you can download: http://www.microsoft.com/speech/download/sdk51/, and install the two files speech SDK 5.1 and 5.1 Language Pack, of which 5.1 Language Pack can select the language to install. After the installation, we can start the development of the speech program.

Below we design a class that can read both Chinese and English languages:

We will implement this class in singleton mode. The code of the class is as follows, which will be explained in detail:

Public class speach

{

Private Static speach _ instance = NULL;

Private speechlib. spvoiceclass voice = NULL;

Private speach ()

{

Buildspeach ();

}

Public static speach instance ()

{

If (_ instance = NULL)

_ Instance = new speach ();

Return _ instance;

}

Private void setchinavoice ()

{

Voice. Voice = voice. getvoices (string. Empty, String. Empty). Item (0 );

}

Private void setenglishvoice ()

{

Voice. Voice = voice. getvoices (string. Empty, String. Empty). Item (1 );

}

Private void speakchina (string strspeak)

{

Setchinavoice ();

Speak (strspeak );

}

Private void speakenglishi (string strspeak)

{

Setenglishvoice ();

Speak (strspeak );

}

Public void analysespeak (string strspeak)

{

Int icbeg = 0;

Int iebeg = 0;

Bool ischina = true;

For (INT I = 0; I <strspeak. length; I ++)

{

Char CHR = strspeak [I];

If (ischina)

{

If (CHR <= 122 & CHR> = 65)

{

Int ilen = I-icbeg;

String strvalue = strspeak. substring (icbeg, ilen );

Speakchina (strvalue );

Iebeg = I;

Ischina = false;

}

}

Else

{

If (CHR> 122 | CHR <65)

{

Int ilen = I-iebeg;

String strvalue = strspeak. substring (iebeg, ilen );

This. speakenglishi (strvalue );

Icbeg = I;

Ischina = true;

}

}

} // End

If (ischina)

{

Int ilen = strspeak. Length-icbeg;

String strvalue = strspeak. substring (icbeg, ilen );

Speakchina (strvalue );

}

Else

{

Int ilen = strspeak. Length-iebeg;

String strvalue = strspeak. substring (iebeg, ilen );

Speakenglishi (strvalue );

}

}

Private void buildspeach ()

{

If (voice = NULL)

Voice = new spvoiceclass ();

}

Public int volume

{

Get

{

Return voice. volume;

}

Set

{

Voice. setvolume (ushort) (value ));

}

}

Public int Rate

{

Get

{

Return voice. rate;

}

Set

{

Voice. setrate (value );

}

}

Private void speak (string strspeack)

{

Try

{

Voice. Speak (strspeack, speechvoicespeakflags. svsflagsasync );

}

Catch (exception ERR)

{

Throw (new exception ("an error occurred:" + err. Message ));

}

}

Public void stop ()

{

Voice. Speak (string. Empty, speechlib. speechvoicespeakflags. svsfpurgebeforespeak );

}

Public void pause ()

{

Voice. Pause ();

}

Public void continue ()

{

Voice. Resume ();

}

} // End class

 

In private speechlib. spvoiceclass voice = NULL; here, we define a class for pronunciation and initialize it with the buildspeach method when calling this class for the first time.

We also define two attributes: volume and rate, which can be used to set the volume and speed.

We know that spvoiceclass has a speak method. Our pronunciation is mainly to pass a string to it, which is responsible for reading the string, as shown below.

Private void speak (string strspeack)

{

Try

{

Voice. Speak (strspeack, speechvoicespeakflags. svsflagsasync );

}

Catch (exception ERR)

{

Throw (new exception ("an error occurred:" + err. Message ));

}

}

 

Speechvoicespeakflags. svsflagsasync indicates asynchronous pronunciation.

However, this method does not know the language of the string you are giving,
So we need to read this string in what language. The voice attribute of the spvoiceclass class is used to set the language,
We can use the getvoices method of spvoiceclass to obtain a list of all languages, and then select the corresponding language based on the parameter,
For example, set the language to Chinese as follows:

Private void setchinavoice ()

{

Voice. Voice = voice. getvoices (string. Empty, String. Empty). Item (0 );

}

0 indicates Chinese, 1234 indicates English, and the accent is different.

In this way, we set the language. If we combine the pronunciation method, we can design a method for sending only Chinese speech.

Private void speakchina (string strspeak)

{

Setchinavoice ();

Speak (strspeak );

}

The method for sending only English speech is similar, as shown in the above program.

For a mix of Chinese and English languages, the method for the program to read the Mixed Speech is: programming separates the English and Chinese of the language, calls the speakchina Method for Chinese, and calls the speakengishi method in English; as for how to determine whether a character is English or Chinese, I use the ASC code judgment method. The specific class method is implemented through analysespeak.

In this way, we only need to pass a piece of mixed Chinese and English text to analysespeak as a parameter, and he can complete the mixed Chinese and English pronunciation.

Of course, for the tentative, continue, stop and other operations of pronunciation, the above also provides a simple method call, it is easy to understand.

The following describes the methods for Chinese speech recognition:

First paste the source code of the Speech Recognition class below, and then explain:

Public class sprecognition

{

Private Static sprecognition _ instance = NULL;

Private speechlib. ispeechrecogrammar isrg;

Private speechlib. spsharedrecocontextclass ssrcontex = NULL;

Private system. Windows. Forms. Control cdisplay;

Private sprecognition ()

{

Ssrcontex = new spsharedrecocontextclass ();

Isrg = ssrcontex. creategrammar (1 );

Speechlib. _ ispeechrecoconcontextevents_recognitioneventhandler rechandle = new _ ispeechrecocontextevents_recognitioneventhandler (contexrecognition );

Ssrcontex. Recognition + = rechandle;

}

Public void beginrec (control tbresult)

{

Isrg. dictationsetstate (speechrulestate. sgdsactive );

Cdisplay = tbresult;

}

Public static sprecognition instance ()

{

If (_ instance = NULL)

_ Instance = new sprecognition ();

Return _ instance;

}

Public void closerec ()

{

Isrg. dictationsetstate (speechrulestate. sgdsinactive );

}

Private void contexrecognition (INT iindex, object OBJ, speechlib. speechrecognitiontype, speechlib. ispeechrecoresult result)

{

Cdisplay. Text + = result. phraseinfo. gettext (0,-1, true );

}

}


We have defined ssrcontex and isrg as the context and syntax for speech recognition. By setting the dictationsetstate method of isrg, we can start or end the recognition. In the above program, the beginrec and closerec methods are used. Cdisplay is the place for us to output recognition results. In order to display results on most controls, I use a control class to define it. Of course, after each speech recognition, the ispeechrecocontextevents_recognitioneventhandler event is triggered. We define a contexrecognition method to respond to events and output the recognition result in this method.
 

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.