iT邦幫忙

DAY 8
3

c#應用系列 第 8

C#應用(8)語音應用

  • 分享至 

  • xImage
  •  

語音應用分為語音合成語音辨視這2大運用

相關技術:
*Microsoft text-to-speech voices
*Microsoft Speech API
*System.Speech Namespaces

回目錄
*原始版本出處:
**Speech recognition, speech to text, text to speech, and speech synthesis in C#
**Text to Speech Converter Sample - C# - Visual Studio 2010

1.先新增一個C#的Windows Form應用程式的專案, 名稱為 Speak
2.加入參考 System.Speech.dll
3.在Form1上用"工具箱"分別拉進
1個TextBox
1個ComboBox
6個Button
1個OpenFileDialog
1個SaveFileDialog

*檔案 Form1.Designer.cs

namespace Speak
{
    partial class Form1
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器
        /// 修改這個方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.button6 = new System.Windows.Forms.Button();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textBox1.Size = new System.Drawing.Size(624, 290);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(214, 305);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "發音";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(539, 305);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "從檔案載入";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(295, 305);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 3;
            this.button3.Text = "停止";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(377, 305);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 4;
            this.button4.Text = "暫停";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(458, 305);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 5;
            this.button5.Text = "繼續";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(14, 307);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(194, 20);
            this.comboBox1.TabIndex = 6;
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(214, 335);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(75, 23);
            this.button6.TabIndex = 7;
            this.button6.Text = "錄音到檔案";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(624, 361);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
    }
}

*檔案 Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
//using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;

namespace Speak
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            reader = new SpeechSynthesizer();
            foreach (InstalledVoice voice in reader.GetInstalledVoices())
            {
                comboBox1.Items.Add(voice.VoiceInfo.Name);
            }
            comboBox1.SelectedIndex = 0;

            this.button1.Enabled = true;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
            this.button6.Enabled = true;
        }

        SpeechSynthesizer reader = null;
        //SpeechRecognitionEngine _recognizer = null;

        private void button1_Click(object sender, EventArgs e)
        {
            string text = this.textBox1.Text.Trim();
            if (text.Length > 0)
            {
                if (reader != null)
                    reader.Dispose();

                reader = new SpeechSynthesizer();
                reader.SelectVoice(this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString());
                reader.SpeakAsync(text);

                comboBox1.Enabled = false;
                this.button1.Enabled = false;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = false;
                this.button6.Enabled = false;

                reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted);
            }
        }

        void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            this.comboBox1.Enabled = true;
            this.button1.Enabled = true;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
            this.button6.Enabled = true;
        }
 

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Text files (*.txt)|*.txt";
            openFileDialog1.FilterIndex = 1;
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                this.textBox1.Text = File.ReadAllText(this.openFileDialog1.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            this.comboBox1.Enabled = true;
            this.button1.Enabled = true;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Speaking)
                {
                    reader.Pause();
                    this.button1.Enabled = false;
                    this.button3.Enabled = true;
                    this.button4.Enabled = false;
                    this.button5.Enabled = true;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Paused)
                {
                    reader.Resume();
                }
                this.button1.Enabled = false;
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = false;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string text = this.textBox1.Text.Trim();
            if (text.Length > 0)
            {
                saveFileDialog1.Filter = "Wave files (*.wav)|*.wav";
                saveFileDialog1.FilterIndex = 1;
                DialogResult result = this.saveFileDialog1.ShowDialog();
                if (result == DialogResult.OK) // Test result.
                {
                    if (reader != null)
                        reader.Dispose();

                    reader = new SpeechSynthesizer();
                    reader.SelectVoice(this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString());
                    reader.SetOutputToWaveFile(this.saveFileDialog1.FileName);
                    reader.SpeakAsync(text);

                    comboBox1.Enabled = false;
                    this.button1.Enabled = false;
                    this.button3.Enabled = false;
                    this.button4.Enabled = false;
                    this.button5.Enabled = false;
                    this.button6.Enabled = false;

                    reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted2);
                }
            }
        }

        void reader_SpeakCompleted2(object sender, SpeakCompletedEventArgs e)
        {
            MessageBox.Show("錄音完成");

            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            this.comboBox1.Enabled = true;
            this.button1.Enabled = true;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
            this.button6.Enabled = true;
        }
    }
}

4.最後, 建置 Speak, 就這樣了
本來想加上接麥克風做發音練習的
可是語音辨視的功能要先把字串拆出單字產生一堆Grammar的設定, 因此這部份的功能先跳過囉


上一篇
C#應用(7)使用DirectShow.NET做轉檔工具
下一篇
C#應用(9)自訂HtmlHelperExtensions
系列文
c#應用13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言