2 ways to implement speech synthesis by calling SAPI in C #

Source: Internet
Author: User
Tags chr sapi

This article mainly introduced in C # SAPI to achieve speech synthesis of 2 methods, this article directly to show example code, the need for friends can refer to the

We all know that TTS is now available through Microsoft's SAPI, the benefits of which I will not say, convenient, because in the operating system of Microsoft's own this thing, the main way there are two kinds:

1, using COM component technology, whether it is c++,c#,delphi can play the turn, developed things in XP and WIN7 can run. (to introduce Speechlib, as if on the project point reference, and then selected to the System COM bar, long time No, don't remember clearly)

2, the use of WIN7 Windows API, in fact, eventually called the SAPI, so developed things can only run on the WIN7.

In fact, regardless of which is called SAPI, it is possible that the latter kind of code is simpler, using the TTS engine already installed, now generally used NeoSpeech, this does not explain, too powerful this pronunciation ...

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 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 5, 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 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 The 143 144 public class Speach {private static speach _instance = null; private Speechlib.spvoiceclass voice =null;//sapi5.1 Priv Ate 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; f or (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); 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); The catch (Exception err) {throw ("An error occurred:" +err, New Exception. 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 the first time the class is invoked.

We have also defined two properties volume and rate to set the volume and speed.

We know that Spvoiceclass has a speak method, and we pronounce it mainly by passing a string to him, which reads the string as follows.

?

1 2 3 4 5 6 7 8 9 10 11 private void Speak (string strspeack) {try {Voice. Speak (Strspeack,speechvoicespeakflags.svsflagsasync); The catch (Exception err) {throw ("An error occurred:" +err, New Exception. message)); } }

The second type of use. The code for the Net class library and system APIs are 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 The 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 Sp Eechsynthesizer (); public sread (int m, int n) {//Synth set aloud volume [range 0 ~] Synth. Volume = m; Use synth to set the reading frequency [range -10 ~] Synth. Rate = n; The 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 reading volume [range 0 ~]//Synth. Volume = 80; Use synth to set the reading frequency [range -10 ~]//Synth. RaTe = 0; Use synth to synthesize WAV audio files://synth. Setoutputtowavefile (string path); The public void Speakenglish (string ggg) {//spvoice Voice = new SpVoice (), synth. Selectvoice ("VW Julie"); Synth. Speak (GGG); GGG for content to be synthesized} public int M {getting {return synth. Volume; } set {synth. Volume = value; } public int N {get {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.