Question

I am working on the following code:

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());
        }
    }
}

Here, I am loading input from Mike. But, instead of that, I need to load input from what the computer spoke inside sSpeak.Speak("Please speak the sentence above");. How can I load this computer speech as the input to the speech recognizer?

Was it helpful?

Solution

I found the way

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);

sSpeak is the instance of SpeechSynthesizer. sEngine is the instance of SpeechRecognitionEngine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top