DotNetSpeech.dll的使用
语音是人类最自然的交互方式,也是现阶段软件用户界面发展的最高目标。微软公司一直积极推动语音技术的发展,并且公布了语音开发平台Speech SDK帮助开发人员实现语音应用。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DotNetSpeech;
namespace SpeechApp
{
聽///
聽/// Form1 的摘要说明。
聽///
聽public class Form1 : System.Windows.Forms.Form
聽{
聽聽private System.Windows.Forms.GroupBox groupBox1;
聽聽private System.Windows.Forms.TextBox textBox1;
聽聽private System.Windows.Forms.Button ButtonSynthesis;
聽聽private System.Windows.Forms.Button ButtonTTStoWave;
聽聽///
聽聽/// 必需的设计器变量。
聽聽///
聽聽private System.ComponentModel.Container components = null;
聽聽public Form1()
聽聽{
聽聽聽//
聽聽聽// Windows 窗体设计器支持所必需的
聽聽聽//
聽聽聽InitializeComponent();
聽聽聽//
聽聽聽// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
聽聽聽//
聽聽}
聽聽///
聽聽/// 清理所有正在使用的资源。
聽聽///
聽聽protected override void Dispose( bool disposing )
聽聽{
聽聽聽if( disposing )
聽聽聽{
聽聽聽聽if (components != null)聽
聽聽聽聽{
聽聽聽聽聽components.Dispose();
聽聽聽聽}
聽聽聽}
聽聽聽base.Dispose( disposing );
聽聽}
聽聽#region Windows 窗体设计器生成的代码
聽聽///
聽聽/// 设计器支持所需的方法 - 不要使用代码编辑器修改
聽聽/// 此方法的内容。
聽聽///
聽聽private void InitializeComponent()
聽聽{
聽聽聽this.groupBox1 = new System.Windows.Forms.GroupBox();
聽聽聽this.textBox1 = new System.Windows.Forms.TextBox();
聽聽聽this.ButtonSynthesis = new System.Windows.Forms.Button();
聽聽聽this.ButtonTTStoWave = new System.Windows.Forms.Button();
聽聽聽this.groupBox1.SuspendLayout();
聽聽聽this.SuspendLayout();
聽聽聽//聽
聽聽聽// groupBox1
聽聽聽//聽
聽聽聽this.groupBox1.Controls.Add(this.textBox1);
聽聽聽this.groupBox1.Location = new System.Drawing.Point(8, 8);
聽聽聽this.groupBox1.Name = "groupBox1";
聽聽聽this.groupBox1.Size = new System.Drawing.Size(272, 144);
聽聽聽this.groupBox1.TabIndex = 0;
聽聽聽this.groupBox1.TabStop = false;
聽聽聽this.groupBox1.Text = "请输入要合成的文本";
聽聽聽//聽
聽聽聽// textBox1
聽聽聽//聽
聽聽聽this.textBox1.Location = new System.Drawing.Point(8, 24);
聽聽聽this.textBox1.Multiline = true;
聽聽聽this.textBox1.Name = "textBox1";
聽聽聽this.textBox1.Size = new System.Drawing.Size(256, 112);
聽聽聽this.textBox1.TabIndex = 0;
聽聽聽this.textBox1.Text = "欢迎光临Lion互动网络";
聽聽聽//聽
聽聽聽// ButtonSynthesis
聽聽聽//聽
聽聽聽this.ButtonSynthesis.CausesValidation = false;
聽聽聽this.ButtonSynthesis.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
聽聽聽this.ButtonSynthesis.Location = new System.Drawing.Point(24, 160);
聽聽聽this.ButtonSynthesis.Name = "ButtonSynthesis";
聽聽聽this.ButtonSynthesis.TabIndex = 1;
聽聽聽this.ButtonSynthesis.Text = "朗 读";
聽聽聽this.ButtonSynthesis.Click += new System.EventHandler(this.ButtonSynthesis_Click);
聽聽聽//聽
聽聽聽// ButtonTTStoWave
聽聽聽//聽
聽聽聽this.ButtonTTStoWave.CausesValidation = false;
聽聽聽this.ButtonTTStoWave.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
聽聽聽this.ButtonTTStoWave.Location = new System.Drawing.Point(128, 160);
聽聽聽this.ButtonTTStoWave.Name = "ButtonTTStoWave";
聽聽聽this.ButtonTTStoWave.Size = new System.Drawing.Size(136, 23);
聽聽聽this.ButtonTTStoWave.TabIndex = 2;
聽聽聽this.ButtonTTStoWave.Text = "生成声音文件(WAV)";
聽聽聽this.ButtonTTStoWave.Click += new System.EventHandler(this.ButtonTTStoWave_Click);
聽聽聽//聽
聽聽聽// Form1
聽聽聽//聽
聽聽聽this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
聽聽聽this.ClientSize = new System.Drawing.Size(292, 191);
聽聽聽this.Controls.Add(this.ButtonTTStoWave);
聽聽聽this.Controls.Add(this.ButtonSynthesis);
聽聽聽this.Controls.Add(this.groupBox1);
聽聽聽this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
聽聽聽this.Name = "Form1";
聽聽聽this.Text = "欢迎光临Lion互动网络";
聽聽聽this.groupBox1.ResumeLayout(false);
聽聽聽this.ResumeLayout(false);
聽聽}
聽聽#endregion
聽聽///
聽聽/// 应用程序的主入口点。
聽聽///
聽聽[STAThread]
聽聽static void Main()聽
聽聽{
聽聽聽Application.Run(new Form1());
聽聽}
聽聽///
聽聽/// 朗读
聽聽///
聽聽///
聽聽///
聽聽private void ButtonSynthesis_Click(object sender, System.EventArgs e)
聽聽{
聽聽聽try
聽聽聽{
聽聽聽聽DotNetSpeech.SpeechVoiceSpeakFlags SSF = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync;
聽聽聽聽DotNetSpeech.SpVoice vo = new SpVoiceClass();
聽聽聽聽vo.Speak(this.textBox1.Text,SSF);
聽聽聽}
聽聽聽catch(System.Exception ec)
聽聽聽{
聽聽聽聽MessageBox.Show(ec.ToString(),"SpeechApp",MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
聽聽聽}
聽聽}
聽聽///
聽聽/// 生成声音文件
聽聽///
聽聽///
聽聽///
聽聽private void ButtonTTStoWave_Click(object sender, System.EventArgs e)
聽聽{
聽聽聽try
聽聽聽{
聽聽聽聽DotNetSpeech.SpeechVoiceSpeakFlags SSF = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync;
聽聽聽聽DotNetSpeech.SpVoice vo = new SpVoiceClass();
聽聽聽聽System.Windows.Forms.SaveFileDialog SFD = new System.Windows.Forms.SaveFileDialog();
聽聽聽聽SFD.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
聽聽聽聽SFD.Title = "Save to a wav file";
聽聽聽聽SFD.FilterIndex = 2;
聽聽聽聽SFD.RestoreDirectory = true;
聽聽聽聽if(SFD.ShowDialog()==System.Windows.Forms.DialogResult.OK)
聽聽聽聽{
聽聽聽聽聽DotNetSpeech.SpeechStreamFileMode SSFM = DotNetSpeech.SpeechStreamFileMode.SSFMCreateForWrite;
聽聽聽聽聽DotNetSpeech.SpFileStream SFS = new DotNetSpeech.SpFileStreamClass();
聽聽聽聽聽SFS.Open(SFD.FileName,SSFM,false);
聽聽聽聽聽vo.AudioOutputStream = SFS;
聽聽聽聽聽vo.Speak(this.textBox1.Text,SSF);
聽聽聽聽聽vo.WaitUntilDone(System.Threading.Timeout.Infinite);
聽聽聽聽聽SFS.Close();
聽聽聽聽}
聽聽聽}
聽聽聽catch(System.Exception ec)
聽聽聽{
聽聽聽聽MessageBox.Show(ec.ToString(),"SpeechApp",MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
聽聽聽}
聽聽}
聽}
}
下载仅供下载体验和测试学习,不得商用和正当使用。
下载体验
