The application of Chinese and English speech synthesis and speech recognition in C # (I.)

Source: Internet
Author: User
Tags chr empty pack sapi
Chinese in. NET, the English voice has better support, but the Chinese voice support has not yet joined in, we want to achieve Chinese pronunciation or Chinese speech recognition, must first install the Microsoft Speech Application SDK (SASDK), its latest version is SAPI 5.1 He can identify three languages in Chinese, Japanese and English, you can download here: http://www.microsoft.com/speech/download/sdk51/, you need to install these two files speech SDK 5.1 and 5.1 Language Pack, Where the 5.1 Language pack can choose to install the supported languages.

Once installed, we can start to develop the voice program, of course, before we need to add SAPI.dll to the reference in the following figure

Below we design a class that can read the mixed language in Chinese and English:

We'll implement the class in a single example, and the code for the class is as follows, and we'll explain in more 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&LT;65)

{

int ilen = I-iebeg;

string strvalue = Strspeak.substring (Iebeg,ilen);

This. Speakenglishi (strvalue);

Icbeg = i;

Ischina = true;

}

}



}//end for

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 (The 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 the first time the class is invoked.

We have also defined two properties volume and rate to set the volume and speed.

We know that Spvoiceclass has a speak method, and we pronounce it mainly by passing a string to him, which reads the string as follows.

private void Speak (string strspeack)

{

Try

{

Voice. Speak (Strspeack,speechvoicespeakflags.svsflagsasync);

}

catch (Exception err)

{

Throw (The New Exception ("An error occurred:" +err.) message));

}

}

Where Speechvoicespeakflags.svsflagsasync represents an asynchronous pronunciation.




Related Article

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.