如何将计算机语音加载到语音识别器中?

时间:2013-05-28 17:32:29

标签: c# .net windows speech-recognition voice-recognition

请查看以下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Windows.Forms;

namespace US_Speech_Recognizer
{
    public class RecognizeSpeech
    {
        private SpeechRecognitionEngine sEngine; //Speech recognition engine
        private SpeechSynthesizer sSpeak; //Speech synthesizer

        public RecognizeSpeech()
        {
            //Make the recognizer ready
            sEngine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));


            //Load grammar
            Choices sentences = new Choices();
            sentences.Add(new string[] { "I am hungry" });

            GrammarBuilder gBuilder = new GrammarBuilder(sentences);

            Grammar g = new Grammar(gBuilder);

            sEngine.LoadGrammar(g);

            //Add a handler
            sEngine.SpeechRecognized +=new EventHandler<SpeechRecognizedEventArgs>(sEngine_SpeechRecognized);

            //Ask the to speak
            sSpeak = new SpeechSynthesizer();
            sSpeak.Rate = -2;

            sSpeak.Speak("Please speak the sentence above");


            //Configure the recognizer to Mike
            sEngine.SetInputToDefaultAudioDevice();

            sEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        //Start the speech recognition task
        private void sEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            MessageBox.Show(e.Result.Confidence.ToString());
        }
    }
}

在这里,我正在加载Mike的输入。但是,我需要从sSpeak.Speak("Please speak the sentence above");内的计算机内部加载输入,而不是那样。如何加载此计算机语音作为语音识别器的输入?请帮忙。

1 个答案:

答案 0 :(得分:0)

我找到了方法

Stream stream = new MemoryStream();
            sSpeak.SetOutputToWaveStream(stream);


            sSpeak.Speak("I was hungry");
            stream.Position = 0;
            sSpeak.SetOutputToNull();


            //Configure the recognizer to stream
            sEngine.SetInputToWaveStream(stream);

sSpeakSpeechSynthesizer的实例。 sEngineSpeechRecognitionEngine

的实例