Recognition and Pause Console

3

Good morning. I have a console application made in C # for speech recognition with the class System.Speech.Recognition that recognizes the audio and writes it in a document called tub.txt. Here the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;
using System.IO;

namespace VoiceRecognitionForIvernAppConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechRecognitionEngine listen = new SpeechRecognitionEngine();
            listen.SetInputToDefaultAudioDevice();
            listen.LoadGrammar(new DictationGrammar());
            listen.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(reader);
            listen.RecognizeAsync(RecognizeMode.Multiple); 
        void reader(object sender, SpeechRecognizedEventArgs e)
        {
            TextWriter file;
            file = new StreamWriter("tub.txt");
            foreach (RecognizedWordUnit word in e.Result.Words)
            {
                file.WriteLine(word.Text + " ");
                Console.Write(word.Text + " ");
            }
            file.Close();
        }
        Console.ReadKey();
        }
    }
}

My problem is that when I finish speaking I must press a key obviously the command Console.ReadKey (); Otherwise, the application opens and closes immediately. I just want the app not to close until I hear something, a word or a sentence, then if it can be closed, without having to press a key. If not wait, listen, write in the document and close later. Nor would I put something with an if (word.Text.Equals ("quit")) because I just want to say maybe "Hello world" and then close. I read the documentation but I did not see a utility like that. Or I do not know if I'll be overlooking it. If someone has an idea, I thank them in advance.

    
asked by limg21 03.09.2017 в 18:08
source

1 answer

1

As far as I can understand, reading a little the doc, this line:

listen.RecognizeAsync(RecognizeMode.Multiple);

causes recognition to occur continuously and asynchronously ( link ).

The application should not close, but listen continuously. You could implement an initial command, like "ok google" or "hey siri", from which to start talking to the application.

The problem is that (as I read in the doc) a continuous dictation session is closed when the user stops talking. You should implement a grammar for the hands off ("hey siri"), which would remain listening permanently until match the configured text, and then start the DictationGrammar (). When the dictationGramar finishes, switch back to the hands-off grammar switch.

The application closes because being asynchronous is separated from Main. You could implement a while (true){Console.ReadLine();} at the end of Main, which would make it listen continuously without closing the application. To close it you can use some event type SpeechContinuousRecognitionSession, which will indicate if the session has been closed.

    
answered by 03.09.2017 в 18:27