(Transfer) Application of Chinese speech recognition technology in C # (1) Source: 9CBS

xiaoxiao2021-03-06  36

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, 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 5.1 Language Pack selects the language 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 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; Iebeg = 0; bool ischina = true; for (int i = 0; i = 65) { INT Ilen = i - icbeg; string strign = strspeak.substring (icbeg, Ilen); speakchina; 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 strValue = strSpeak.Substring (iEbeg, iLen); SpeakEnglishi (strValue);}} private void BuildSpeach () {if (voice == null) voice = new SpVoiceClass (); PUBLIC INT VOLUME.VOLUME;} set {voice.setVolume;}}} public int Rate;} set {voice.cedrate; }}} Private void speak (string strspeack) {Try {Voice.SPEAK (STRSPEACK, Speechvoicespeakflags.svsflagsa SYNC);} 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 void Speak (string strSpeack) {try {voice.Speak (strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);} catch (Exception err) {throw (new Exception ( "An error occurred:" err.Message));}}

Where SpeechVoicespeakflags.svsflagSasync represents asynchronous pronunciation.

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

New Post(0)