语音识别&文本转换语音(TTS) C#代码

  前一段时间做过语音识别,因为时间比较紧,所以就在网上找了一些代码用上了,发现些的很复杂,现在想要把语音识别应用到Unity项目中来,所以又梳理了一下发现其实微软已经给我们封装了很好类库。下面是采用的微软的Speech SDK5.1 ,数据库采用的MySQL数据库(MySQL数据库昨天刚刚接触,不过感觉很好使比SQL Server 要好用,速度也不较快,觉得一般的项目的话MySQL就能应付了)

界面可以根据自己的爱好自己设计了 ^_^

主要添加引用

下面附上代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//添加数据库引用
using MySQLDriverCS;
using System.Data.Odbc;
//语音转换引用
using DotNetSpeech;
//using SpeechLib;
//添加语音识别引用
using System.Speech.Recognition;
using System.Threading;
 

namespace SpeechDemo
{
    public partial class Form1 : Form
    {
        //定义语音识别的类
        SpeechRecognizer spr;
        List<string> li;
        public Form1()
        {
            InitializeComponent();
        }
       //private SpeechRecognitionEngine spre;
        private SpVoice voice;
        SpeechVoiceSpeakFlags svsf = SpeechVoiceSpeakFlags.SVSFlagsAsync;
       
        //load加载
        private void Form1_Load(object sender, EventArgs e)
        {
            voice = new DotNetSpeech.SpVoice(); //实例化
            this.txt_text.Text = "";
            this.cmb_type.Text = "中文";
            this.btn_pause.Text = "暂停";
            this.voice.Rate = 0;
            this.voice.Volume = 50;
            li = this.MySQConnectionMother();
            this.txt_recive.Focus();
        }
        //speech
        private void btn_speech_Click(object sender, EventArgs e)
        {
            voice.Speak(this.txt_text.Text, svsf);
            //定义一个线程 并给线程绑定方法
        }
        public void btnclick()
        {
            voice.Speak(this.txt_text.Text, svsf);
        }
        //清空
        private void btn_clear_Click(object sender, EventArgs e)
        {
            this.txt_text.Text = "";
            this.btn_pause.Text = "暂停";
        }
        //音量值发生改变后触发的事件
        private void tk_voice_ValueChanged(object sender, EventArgs e)
        {
            this.voice.Volume = this.tk_voice.Value;
        }
        //语音速度值发生改变触发的事件
        private void tk_speed_ValueChanged(object sender, EventArgs e)
        {
            this.voice.Rate = this.tk_speed.Value;
        }
        //暂停&开始
        private void btn_pause_Click(object sender, EventArgs e)
        {
            btnClick(this.btn_pause.Text);
        }
        private void btnClick(string BtnText)
        {
            switch (BtnText)
            {
                case "暂停":
                 this.voice.Pause();
                 this.btn_pause.Text = "继续";
                break;
                case"继续":
                this.voice.Resume();
                this.btn_pause.Text = "暂停";
                    break;
                default:
                    break;
            }
        }
        //生成语音文件
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
                sfd.Title = "Save to a wave file";
                sfd.FilterIndex = 2;
                sfd.RestoreDirectory = true;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
                    SpFileStream SpFileStream = new SpFileStream();
                    SpFileStream.Open(sfd.FileName, SpFileMode, false);
                    voice.AudioOutputStream = SpFileStream;
                    voice.Speak(this.txt_text.Text, SpFlags);
                    voice.WaitUntilDone(Timeout.Infinite);
                    SpFileStream.Close();
                }
            }
            catch(Exception )
            {
            }
        }
        //开起对话
        private void button2_Click(object sender, EventArgs e)
        {
            SpeechRegestion();
        }
        /// <summary>
        /// 语音识别
        /// </summary>
        private void SpeechRegestion()
        {
            spr = new SpeechRecognizer();//提供访问和管理进程内语音识别的引擎
             GrammarBuilder gramm = new GrammarBuilder(); //设置GrammarBuilder
             string[] strs = null;
             strs= MakeListTostring( li );
             Choices choice = new Choices(strs); //定义语法
              gramm.Append(choice);  //把添加到gramm上
              Grammar gra = new Grammar(gramm);//
            spr.LoadGrammarAsync(gra);//异步加载语法
            spr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(spr_SpeechRecognized);  //当语音识别器接收到与其语音识别向匹配的时候发生
        }
        //当语音识别器接收到与其语音识别向匹配的时候发生
        void spr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            string reciveMessage = e.Result.Text.ToString();
            this.txt_recive.Text = reciveMessage;      //识别器经过语法处理后得到的文本
        }

        //连接MySQL数据库
        public List<string> MySQConnectionMother()
        {
          List<string> lis = new List<string>(); ;
           MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "speechdb", "root", "admin").AsString);
           try
           {
             conn.Open();//打开数据库
            // MessageBox.Show("数据库已经打开!");
             MySQLCommand comm = new MySQLCommand("select * from sptable",conn);
             MySQLDataReader dr = comm.ExecuteReaderEx();
             
             while (dr.Read())
             {
                 string mm = dr[2].ToString();
                // MessageBox.Show(mm);
                 lis.Add(mm);
             }
           }
           catch (Exception er)
           {
               MessageBox.Show(er.ToString());
           }
           conn.Close();
           conn.Dispose();
           return lis;
        }
        //添加语法到string[] 当中
        public string[]  MakeListTostring(List<string> liststring)
        {
            string[] str = new string[liststring.Count];
            for (int i = 0; i < liststring.Count(); i++)
            {
                str[i] = liststring[i].ToString();
            }
            return str;
        }

        //通过问题得到答案
        public string GetAnswer(string question)
        {
            string answer = null;
           MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "speechdb", "root", "admin").AsString);
           try
           {
               conn.Open();
              // MessageBox.Show("数据库又被打开了!");
               MySQLCommand comm = new MySQLCommand();
               comm.CommandText = "select answer from sptable where question='" + question + "'";
               comm.Connection = conn;
               //MessageBox.Show(comm.CommandText);
               if (comm.ExecuteScalar()==null)
               {
                   return null;
               }
               else
               {
                   answer = comm.ExecuteScalar().ToString();
               }
           }
           catch (Exception er)
           {
               MessageBox.Show(er.ToString());              
           }
           conn.Close();
           conn.Dispose();
           return answer;
        }

        private void txt_text_TextChanged(object sender, EventArgs e)
        {
            voice.Speak(this.txt_text.Text, svsf);
        }
        private void txt_recive_TextChanged(object sender, EventArgs e)
        {
            if (this.txt_recive.Text.Trim()!=null)
            {
               string answer = GetAnswer(this.txt_recive.Text);
               if (answer != null)
              {
                this.txt_text.Text = answer;
              }
             else
             {
                this.txt_text.Text = "你说的什么?我没听清楚,请再清楚的说一遍!";
              }
            }
        }
    }
}

转载于:https://www.cnblogs.com/Nickzhou/archive/2012/12/21/2827470.html

猜你喜欢

转载自blog.csdn.net/weixin_34297300/article/details/92850759