C # Two Methods of speech synthesis using SAPI

Source: Internet
Author: User
Tags sapi

C # Two Methods of speech synthesis using SAPI

This article mainly introduces two methods to call SAPI in C # For speech synthesis. This article provides the sample code directly. For more information, see

We all know that the current speech synthesis TTS can be implemented through Microsoft's SAPI. I will not talk about the advantages, but it is convenient, because it comes with this feature in Microsoft's operating system, there are two main methods:

1. Using COM component technology, both C ++, C #, and Delphi can play a role, and developed things can run on XP and WIN7. (To introduce SpeechLib, it seems to be a reference on the project, and then select the system COM. I haven't figured it out for a long time, but I can't remember it clearly)

2. Using windows api of windows 7, in fact, the SAPI is still called, so the developed things can only run on windows 7.

In fact, no matter which one is, SAPI is called. The latter code may be relatively simple. The installed TTS engine is used, and NeoSpeech is generally used. This is not explained, it's so powerful...

COM Component Technology:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

Public class Speach

{

Private static Speach _ Instance = null;

Private SpeechLib. SpVoiceClass voice = null; // SAPI5.1

Private SpeechLib. SpVoice voice = null; // SAPI 5.4

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 <65)

{

Int iLen = I-iEbeg;

String strValue = strSpeak. Substring (iEbeg, iLen );

This. SpeakEnglishi (strValue );

ICbeg = I;

IsChina = true;

}

}

} // End

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 (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 when calling this class for the first time.

We also define two attributes: Volume and Rate, which can be used to set the Volume and speed.

We know that SpVoiceClass has a Speak method. Our pronunciation is mainly to pass a string to it, which is responsible for reading the string, as shown below.

?

1

2

3

4

5

6

7

8

9

10

11

Private void Speak (string strSpeack)

{

Try

{

Voice. Speak (strSpeack, SpeechVoiceSpeakFlags. SVSFlagsAsync );

}

Catch (Exception err)

{

Throw (new Exception ("an error occurred:" + err. Message ));

}

}

The second method uses the. NET class library and system API Code as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Speech. Synthesis;

Using System. Speech;

 

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 read volume. The value ranges from 0 ~ 100]

Synth. Volume = m;

// Use synth to set the read frequency. The value range is-10 ~ 10]

Synth. Rate = n;

}

Public void SpeakChina (string ggg)

{

// SpVoice Voice = new SpVoice ();

Synth. SelectVoice ("Microsoft Lili ");

// Voice. Speak (ggg, SpFlags );

Synth. SpeakAsync (ggg );

// String speechPeople = synth. Voice;

// Use synth to set the read volume. The value ranges from 0 ~ 100]

// Synth. Volume = 80;

// Use synth to set the read frequency. The value range is-10 ~ 10]

// Synth. Rate = 0;

// Use synth to synthesize wav audio files:

// Synth. SetOutputToWaveFile (string path );

}

Public void SpeakEnglish (string ggg)

{

// SpVoice Voice = new SpVoice ();

Synth. SelectVoice ("VW Julie ");

Synth. Speak (ggg); // ggg indicates the content to be merged.

}

Public int m

{

Get

{

Return synth. Volume;

}

Set

{

Synth. Volume = value;

}

}

Public int n

{

Get

{

Return synth. Rate;

}

Set

{

Synth. Rate = value;

}

}

}

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.