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
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 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;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  /// <summary>
  /// 朗读
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  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);
   }
  }

  /// <summary>
  /// 生成声音文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  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);
   }
  }
 }
}

posted @ 2006-11-16 22:23  Leepy  阅读(10730)  评论(1编辑  收藏  举报