We all know that the current voice synthesis TTS can be implemented through Microsoft SAPI. I won’t talk about the benefits, it’s just convenience, because this thing is built into Microsoft’s operating system, and there are two main ways:
1. Using COM component technology, you can play with C++, C#, and Delphi, and the developed things can run in XP and WIN7. (To introduce SpeechLib, it seems that you click the reference on the project and select the system COM. I haven't done it for a long time and I can't remember it)
2. Using the Windows API of WIN7, in fact, SAPI is called in the end, so the things developed can only run on WIN7.
In fact, no matter which one it is, it calls SAPI. Perhaps the latter code is relatively simple, using the installed TTS engine. Now, NeoSpeech is generally used, so this is not explained. It is too powerful. This pronunciation. . .
COM component technology:
public class Speach { private static Speach _Instance = null ; private voice =null; //SAPI5.1 private voice = null;//SAPI 5.4 private Speach() { BuildSpeach() ; } public static Speach instance() { if (_Instance == null) _Instance = new Speach() ; return _Instance ; } private void SetChinaVoice() { = (,).Item(0) ; } private void SetEnglishVoice() { = (,).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<;i++) { char chr = strSpeak[i] ; if (IsChina) { if (chr<=122&&chr>=65) { int iLen = i - iCbeg ; string strValue = (iCbeg,iLen) ; SpeakChina(strValue) ; iEbeg = i ; IsChina = false ; } } else { if (chr>122||chr<65) { int iLen = i - iEbeg ; string strValue = (iEbeg,iLen) ; (strValue) ; iCbeg = i ; IsChina = true ; } } }//end for if (IsChina) { int iLen = - iCbeg ; string strValue = (iCbeg,iLen) ; SpeakChina(strValue) ; } else { int iLen = - iEbeg ; string strValue = (iEbeg,iLen) ; SpeakEnglishi(strValue) ; } } private void BuildSpeach() { if (voice == null) voice = new SpVoiceClass() ; } public int Volume { get { return ; } set { ((ushort)(value)) ; } } public int Rate { get { return ; } set { (value) ; } } private void Speak(string strSpeack) { try { (strSpeack,) ; } catch(Exception err) { throw(new Exception("An error occurred:"+)) ; } } public void Stop() { (,) ; } public void Pause() { () ; } public void Continue() { () ; } }//end class
In private voice =null; here, we define a class for pronunciation, and when the class is called for the first time, it is initialized with the BuildSpeach method.
We also define two properties Volume and Rate, which can set volume and speech speed.
We know that SpVoiceClass has a Speak method, and our pronunciation is mainly to pass it a string, which is responsible for reading the string, as shown below.
private void Speak(string strSpeack) { try { (strSpeack,) ; } catch(Exception err) { throw(new Exception("An error occurred:"+)) ; } }
The second code that uses the .NET class library and system API is as follows:
using System; using ; using ; using ; using ; using ; namespace StudyBeta { public class SRead { public SpeechSynthesizer synth; //Speech synthesis object public SRead() { synth = new SpeechSynthesizer(); } public SRead(int m, int n) { // Use synth to set the reading volume [Range 0 ~ 100] = m; // Use synth to set the reading frequency [range -10 ~ 10] = n; } public void SpeakChina(string ggg) { //SpVoice Voice = new SpVoice(); ("Microsoft Lili"); //(ggg, SpFlags); (ggg); //String speechPeople = ; // Use synth to set the reading volume [Range 0 ~ 100] // = 80; // Use synth to set the reading frequency [range -10 ~ 10] // = 0; // Use synth to synth to synthesize wav audio files: //(string path); } public void SpeakEnglish(string ggg) { //SpVoice Voice = new SpVoice(); ("VW Julie"); (ggg); //ggg is the content to be synthesized } public int m { get { return ; } set { = value; } } public int n { get { return ; } set { = value; } } }