1. 程式人生 > >Csharp: speech to text, text to speech in win

Csharp: speech to text, text to speech in win

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

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;

An introduction to parsing text in Haskell with Parsec

util eof try xib reporting where its ner short Parsec makes parsing text very easy in Haskell. I write this as much for myself as for any

How to read text file in client side via HTML5

In HTML4, As we known, if you want to read a local text file with javascript, you may need to use ActiveX object, there is a blog descri

微信企業號報agent only allow to send text

微信 企業號 報錯 {"errcode":60011,"errmsg":"no privilege to access\/modify contact\/party\/agent "} ,主要是沒有權限訪問或修改人員的信息等,研究後,其實只要將紅圈部分的通訊錄權限開通好,就可以了。{"errc

Structural Features for Predicting the Linguistic Quality of Text: Applications to Machine Translation, Automatic Summarization and Human-Authored Tex

abstract句子結構是文字語言質量的關鍵,我們記錄了以下實驗結果:句法短語統計和其他結構特徵對文字方面的預測能力。手工評估的句子fluency流利度用於機器翻譯評估和文字摘要質量的評估是黃金準則。我們發現和短語長度相關的結構特徵是弱特徵,但是與fluency強相關,基於整個結構特徵的分類器可以在句子flu

深度學習論文翻譯解析(二):An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition

論文標題:An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition 論文作者: Baoguang Shi, Xiang B

4.2 LeetCode字串類題目選做(1) —— Roman to Integer & Text Justification

字串的題目,首先是一些簡單的字串處理的問題,不涉及到什麼演算法。關鍵點是找規律,思考全面所有的情況。 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Using machine learning to index text from billions of images

In our previous blog posts, we talked about how we updated the Dropbox search engine to add intelligence into our users’ workflow, and how we built our

How to add a text filter to Django Admin

Support: Yes, for now…The problem with search fieldsDjango Admin search fields are great — throw a bunch of fields in search_fields and Django will handle

Oracle啟動時報錯 error while trying to retrieve text for error ORA-12543

今天 安裝oracle資料庫,使用 pl啟東市總是提示 error while trying to retrieve text for error ORA-12543,網上查了半天,有的說 tnsna

Towards End-to-end Text Spotting with Convolutional Recurrent Neural Networks閱讀筆記

1.摘要 論文提出一種統一的網路結構模型,這種模型可以直接通過一次前向計算就可以同時實現對影象中文字定位和識別的任務。這種網路結構可以直接以end-to-end的方式訓練,訓練的時候只需要輸入影象,影象中文字的bbox,以及文字對應的標籤資訊。這種end-to-end訓練的

How to Clean Text for Machine Learning with Python

Tweet Share Share Google Plus You cannot go straight from raw text to fitting a machine learning

plsql開啟錯誤 error while trying to retrieve text for error ORA-12541

1,確認自己輸入的服務名是不是正確,服務名不區分大小寫。 2,看看系統的環境變數中ORACLE_HOME的值是不是正確, ORACLE_HOME的值要設定到client目錄。 原因:沒設定ORACLE_HOME 解決: 後來加上就可以了 在pat

Error while trying to retrieve text error CRA-12154 資料庫連線錯誤

Error while trying to retrieve text error CRA-12154 資料庫連線錯誤 Pl/SQL   連線資料庫  看ip地址 在PL/SQL中選擇Help-->Support Info... ->檢視DNS File C:\

終於解決了Linux下執行OCCI程式一直報Error while trying to retrieve text for error ORA-01804錯誤

Linux 下執行OCCI程式,一直報如下錯誤: Exception: Code - 1804, Message - Error while trying to retrieve text for error ORA-01804 之前也遇到了該錯誤,當時解決辦法是配置 O

2000_narrowband to wideband conversion of speech using GMM based transformation

論文地址:基於GMM的語音窄帶到寬頻轉換 部落格作者:凌逆戰 部落格地址:https://www.cnblogs.com/LXP-Never/p/12151027.html 摘要   在不改變現有通訊網路的情況下,利用窄帶語音重建寬頻語音是一個很有吸引力的問題。本文提出了一種從窄帶語音中恢復寬頻語

[Python] How to unpack and pack collection in Python?

ide ont add off art video lec ref show It is a pity that i can not add the video here. As a result, i offer the link as below: How to

How To View the HTML Source in Google Chrome

inner eve spi together member mes mnt line split Whether you are new to the web industry or a seasoned veteran, viewing the HTML source o

How to convert matrix to RDD[Vector] in spark

toarray kcon tex logs def supports iterator ati true The matrix is generated from SVD, and I am using the results from SVD to do clusteri

[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

export begin async delet tin remove todo ras alt You‘ll begin to notice as you build out your actions in Vuex, many of them will look qui