Csharp: speech to text, text to speech in win

?





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

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

using
System;

using
System.Collections.Generic;

using
System.ComponentModel;

using
System.Data;

using
System.Drawing;

using
System.Linq;

using
System.Text;

using
System.Windows.Forms;

using
System.Threading;

using
SpeechLib;//NET2.0 引用 Speech sdk 5.1 在COM选项卡里面的Microsoft Speech  object  library引用 已经有11.0版本

using
System.Speech;

using
System.Speech.Recognition;

using
System.Speech.Synthesis;

namespace
Speech

{

    /// <summary>

    /// 20140427

    /// 涂聚文

    ///

    /// </summary>

    public
partial class Form1 : Form

    {

        private
enum State

        {

            Idle = 0,

            Accepting = 1,

            Off = 2,

        }

        private
State RecogState = State.Off;

        private
SpeechRecognitionEngine recognizer;

        private
SpeechSynthesizer synthesizer = null;

        private
int Hypothesized = 0;

        private
int Recognized = 0;

        /// <summary>

        ///

        /// </summary>

        public
Form1()

        {

            InitializeComponent();

        }

        /// <summary>

        ///

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private
void Form1_Load(object
sender, EventArgs e)

        {

            // SpVoice voice = new SpVoice();//SAPI 5.4          

            //SpeechLib.ISpeechObjectTokens obj = voice.GetVoices(string.Empty, string.Empty);

            //int count = obj.Count;//获取语音库总数

            //bool result = false;

            //for (int i = 0; i < count; i++)

            //{

            //    string desc = obj.Item(i).GetDescription(i);//.GetDescription(); //遍历语音库

            //    comboBox1.Items.Add(desc);

            //}

            //SpVoiceClass voice = new SpVoiceClass();//SAPI 5.1

            ////voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);

            ////voice.Speak("你要说的话",SpeechVoiceSpeakFlags.SVSFlagsAsync);

            //SpVoice voice1 = new SpVoice();//SAPI 5.4

            //voice1.Volume = 100;//音量

            //voice1.Voice = voice1.GetVoices(string.Empty, string.Empty).Item(0);

            //voice1.Rate = 2;//速度语音朗读速度

            //           voice1.Speak("你要说的话", SpeechVoiceSpeakFlags.SVSFlagsAsync);

             //voice1.Speak("speech sdk 5.1", SpeechVoiceSpeakFlags.SVSFlagsAsync);

            //SpeechSynthesizer syn = new SpeechSynthesizer();

            //syn.SelectVoice("Microsoft Lili");

            //initialize recognizer and synthesizer

            InitializeRecognizerSynthesizer();

            //if input device found then proceed

            if
(SelectInputDevice())

            {

                LoadDictationGrammar();

               

                ReadAloud("中华人民共和国"); //中文方式Speech Engine Ready for Input

            }

        }

        /// <summary>

        /// 中文

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private
void button1_Click(object
sender, EventArgs e)

        {

            SpVoiceClass voice = new
SpVoiceClass();

            voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //0,为系统默认,中文

           

            voice.Speak(this.textBox1.Text.Trim(), SpeechVoiceSpeakFlags.SVSFlagsAsync);

        }

        /// <summary>

        /// 英文

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private
void button2_Click(object
sender, EventArgs e)

        {

            SpVoiceClass voice = new
SpVoiceClass();

            voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);//

            voice.Speak(this.textBox2.Text.Trim(), SpeechVoiceSpeakFlags.SVSFlagsAsync);

        }

        /// <summary>

        /// 输入中文语音输出中文文字

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private
void button3_Click(object
sender, EventArgs e)

        {

            switch
(RecogState)

            {

                case
State.Off:

                    RecogState = State.Accepting;

                    button3.Text = "Stop";

                    recognizer.RecognizeAsync(RecognizeMode.Multiple);

                    break;

                case
State.Accepting:

                    RecogState = State.Off;

                    button3.Text = "Start";

                    recognizer.RecognizeAsyncStop();

                    break;

            }

        }

        /// <summary>

        /// pause recognition and speak the text sent

        /// </summary>

        /// <param name="speakText"></param>

        public
void ReadAloud(string
speakText)

        {

            try

            {

                recognizer.RecognizeAsyncCancel();

                synthesizer.SpeakAsync(speakText);

            }

            catch
{ }

        }

        /// <summary>

        /// initialize recognizer and synthesizer along with their events

        /// /// </summary>

        private
void InitializeRecognizerSynthesizer()

        {

            var
selectedRecognizer = (from
e in
SpeechRecognitionEngine.InstalledRecognizers()

                                      where
e.Culture.Equals(Thread.CurrentThread.CurrentCulture)

                                      select
e).FirstOrDefault();

            recognizer = new
SpeechRecognitionEngine(selectedRecognizer);

            recognizer.AudioStateChanged += new
EventHandler<AudioStateChangedEventArgs>(recognizer_AudioStateChanged);

            recognizer.SpeechHypothesized += new
EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);

            recognizer.SpeechRecognized += new
EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

            synthesizer = new
SpeechSynthesizer();

        }

        #region Recognizer events

        private
void recognizer_AudioStateChanged(object
sender, AudioStateChangedEventArgs e)

        {

            switch
(e.AudioState)

            {

                case
AudioState.Speech:

                    LabelStatus.Text = "Listening";

                    break;

                case
AudioState.Silence:

                    LabelStatus.Text = "Idle";

                    break;

                case
AudioState.Stopped:

                    LabelStatus.Text = "Stopped";

                    break;

            }

        }

        /// <summary>

        ///

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private
void recognizer_SpeechHypothesized(object
sender, SpeechHypothesizedEventArgs e)

        {

            Hypothesized++;

            LabelHypothesized.Text = "Hypothesized: "
+ Hypothesized.ToString();

        }

        /// <summary>

        ///

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private
void recognizer_SpeechRecognized(object
sender, SpeechRecognizedEventArgs e)

        {

            Recognized++;

            string
s = "Recognized: "
+ Recognized.ToString();

            if
(RecogState == State.Off)

                return;

            float
accuracy = (float)e.Result.Confidence;

            string
phrase = e.Result.Text;

            {

                if
(phrase == "End Dictate")

                {

                    RecogState = State.Off;

                    recognizer.RecognizeAsyncStop();

                    ReadAloud("Dictation Ended");

                    return;

                }

                textBox1.AppendText(" "
+ e.Result.Text);

            }

        }

        #endregion

        /// <summary>

        /// select input device if available

        /// </summary>

        /// <returns></returns>

        private
bool SelectInputDevice()

        {

            bool
proceedLoading = true;

            //if OS is above XP

            if
(IsOscompatible())

            {

                try

                {

                    recognizer.SetInputToDefaultAudioDevice();

                }

                catch

                {

                    proceedLoading = false; //no audio input device

                }

            }

            //if OS is XP or below

            else

                ThreadPool.QueueUserWorkItem(InitSpeechRecogniser);

            return
proceedLoading;

        }

        /// <summary>

        /// Findout if OS is compatible.

        /// </summary>

        /// <returns>true if greater than XP otherwise false</returns>

        private
bool IsOscompatible()

        {

            OperatingSystem osInfo = Environment.OSVersion;

            if
(osInfo.Version > new
Version("6.0"))

                return
true;

            else

                return
false;

        }

        /// <summary>

        ///

        /// </summary>

        /// <param name="o"></param>

        private
void InitSpeechRecogniser(object
o)

        {

            recognizer.SetInputToDefaultAudioDevice();

        }

        /// <summary>

        /// Load grammars, one for command and other for dictation

        /// </summary>

        private
void LoadDictationGrammar()

        {

            GrammarBuilder grammarBuilder = new
GrammarBuilder();

            grammarBuilder.Append(new
Choices("End Dictate"));

            Grammar commandGrammar = new
Grammar(grammarBuilder);

            commandGrammar.Name = "main command grammar";

            recognizer.LoadGrammar(commandGrammar);

            DictationGrammar dictationGrammar = new
DictationGrammar();

            dictationGrammar.Name = "dictation";

            recognizer.LoadGrammar(dictationGrammar);

        }

    }

}

Csharp: speech to text, text to speech in win,布布扣,bubuko.com

时间: 2024-12-22 11:24:14

Csharp: speech to text, text to speech in win的相关文章

csharp:Google TTS API text to speech

? 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

Text Converted into Speech in Pi

Step 1: Convert any text into uint8 type in matlab : Step 2: Add models in matlab : copy the uint8 numbers and put them in Repeating Sequence Stair model: Step 3: Connect your Headphone or other Audio System to the audio output jack on Pi, and Run! T

【转帖】VOICEBOX: Speech Processing Toolbox for MATLAB

VOICEBOX: Speech Processing Toolbox for MATLAB Introduction VOICEBOX is a speech processing toolbox consists of MATLAB routines that are  maintained by and mostly written by Mike Brookes, Department of Electrical & Electronic  Engineering, Imperial C

A curated list of speech and natural language processing resources

A curated list of speech and natural language processing resources At Josh.ai, we’re often asked for developer resources relating to natural language processing, machine learning, and artificial intelligence. Paul Dixon, a researcher living in Kyoto

C#中的System.Speech命名空间初探

本程序是口算两位数乘法,随机生成两个两位数,用语音读出来.然后开启语音识别,接受用户输入,知道答案正确关闭语音识别.用户说答案时,可以说“再说一遍”重复题目. 关键是GrammarBuilder和Choices的用法. 首先来看看如何获得已安装的语音识别引擎 void showInstalled() { Console.WriteLine("installed recognizers"); foreach (var i in SpeechRecognitionEngine.Instal

C#WPF 语音开发教程 源代码下载 csdn tts(text to sound) 一步一步 教你制作语音软件 附图和源代码

C#WPF  语音开发教程  一步一步 教你制作语音软件 附图和源代码 效果展示 一 项目准备 1.vs2012开发平台 2.微软的语音软件库 下载:http://download.csdn.net/detail/wyx100/8431269 (含实例项目源代码) 二.开发目标 制作一个语音软件,可以朗读文字: 多个语音库:男音和女音.支持英文和中文朗读: 支持选择播放设备 支持朗读语速选择 支持音量选择 三 开发过程 1.新建WpfSpeechDemo工程 文件(vs开发平台左上角)----新

Speak Text, Speak SSML

TTS(Text To Speech) Speak Text Speak SSML 示例1.演示如何通过 TTS 朗读一段文本,以及如何将其保存为音频文件SpeakText.xaml <Page x:Class="Windows81.TTS.SpeakText" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.micro

重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML

[源码下载] 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之 TTS(Text To Speech) Speak Text Speak SSML 示例1.演示如何通过 TTS 朗读一段文本,以及如何将其保存为音频文件SpeakText.xaml <Page x:Class="Windows81.TTS.SpeakText" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre

智能电话机器人,使用Microsoft语音识别技术(Speech sdk)(下)

接上文 现在,硬件上的准备工作做完了. 下一步,先打开Modem的电源,用SecureCRT连接串口,敲入些AT命令,看看Modem能否执行. 这里简单说一下AT命令: AT命令有两种解释一种是调制解调器命令语言,简单来说就是一些固定格式的字符串,我们通过串口向Modem发送AT命令的字符串,Modem就会按照 命令去执行不同的操作.AT命令的百度百科请看这里,具体的命令格式和使用方法请自行百度. 我们先来试试拨号 向串口发送命令 : atdt10086;\r 不出意外的话Modem就会摘机,并