Application of Chinese and English Voice Synthesis and Chinese Voice Recognition Technology in C # (1) (from Tashanzhishi)

xiaoxiao2021-03-06  105

In .NET, there is better support for English voice, but the support of Chinese voice has not been added yet, we want to implement Chinese pronunciation or Chinese speech recognition, you must install Microsoft's Speech Application SDK (Sasdk), it The latest version is SAPI 5.1 he can identify, day, and English, you can download: http://www.microsoft.com/speech/download/SDK51/, you need to install these two files SPK 5.1 and 5.1 Language Pack, where 5.1 Language Pack selects the language that is installed.

After installation, we can start the development of voice programs, of course, before this, we need to add SAPI.dll to the referenced as shown below.

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

We will use a single case model to implement this class, the code of the class is as follows, we will explain it in detail:

Public Class Speach

{

Private static sperach _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 vid SpeakChina (String Strspeak)

{

SetChinaVoice ();

SPEAK (STRSPEAK);

}

Private vid Speakenglishi (String Strspeak)

{

SetENGLISHVOICE ();

SPEAK (STRSPEAK);

}

Public void Analysis (String STRSPEAK)

{

INT ICBEG = 0;

INT IEBEG = 0;

Bool ischina = True;

For (int i = 0; i

{

Char chr = strspeak [i];

IF (ischina)

{

IF (chr <= 122 && chr> = 65)

{

INT ILEN = I - ICBEG;

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

SpeakChina (Strvalue);

IEBEG = I;

Ischina = false;

}

}

Else

{

IF (chr> 122 || chr <65)

{

INT ILEN = i - IEBEG;

String strign = strspeak.substring (IEBEG, ILEN);

THIS.SPEAKENGLISHI (STRVALUE);

ICBEG = i;

Ischina = true;

}

}

} // end for

IF (ischina)

{

INT Ilen = strspeak.length - icbeg;

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

SpeakChina (Strvalue);

}

Else

{

INT Ilen = strspeak.length - IEBEG;

String strign = 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 vid Speak (String Strspeck)

{

Try

{

Voice.SPEAK (STRSPEACK, Speechvoicespeakflags.svsflagsasync);

}

Catch (Exception Err)

{

Throw (New Exception ":" 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 the class is called for the first time.

We also define two attributes Volume and Rate, which can set the volume and speed.

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

Private vid Speak (String Strspeck)

{

Try

{

Voice.SPEAK (STRSPEACK, Speechvoicespeakflags.svsflagsasync);

}

Catch (Exception Err)

{

Throw (New Exception ":" Err.Message));

}

The speechvoicespeakflags.svsflagsasync represents asynchronous pronunciation.

转载请注明原文地址:https://www.9cbs.com/read-104401.html

New Post(0)