Application of speech synthesis and recognition technology in C #

Source: Internet
Author: User
Tags chr pack sapi

In. NET, there is better support for English speech, but the support of Chinese voice has not yet joined, we want to achieve Chinese pronunciation or Chinese speech recognition, we 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

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.