2

I'm trying to create a Speech Recognition program, that needs to run on a locked Windows computer, as part of a Home Automation project. But it seems that the SpeechRecognitionEngine stops recognizing when the computer is locked (and continues when the computer is unlocked).

My current test program looks like this:

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 Microsoft.Speech.Recognition;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine sre;

        public Form1()
        {
            InitializeComponent();
            CultureInfo ci = new CultureInfo("en-us");
            sre = new SpeechRecognitionEngine(ci);
            sre.SetInputToDefaultAudioDevice();
            GrammarBuilder gb = new GrammarBuilder("Hello");
            sre.LoadGrammarAsync(new Grammar(gb));
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }

        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            listBox1.Items.Add(DateTime.Now.ToString() + " " + e.Result.Text);
        }
    }
}

I'm wondering if it's possible to change the input of the SpeechRecognitionEngine (perhaps using the SetInputToAudioStream or SetInputToWaveStream methods) to a live audio stream of the microphone input, and that way work around the problem. Because it seems that the microphone is not turned off when the computer (tried with the SoundRecorder).

Unfortunately I have not been able to find a way to get a live stream of the mic input.

Lars Lind Nilsson
  • 1,136
  • 6
  • 14
  • I have tried using the Sound Recorder to record while the computer was locked without problems. And I have also made a small program that uses NAudio to loop the mic to the speaker. And this also works with the computer locked. Unfortunately I could not find a way to get NAudio to provide a stream I could use with the SpeechRecognitionEngine. So it seems that the mic is available when the computer is locked. And regarding using a service, then it seems that a system service have no access to mic and speaker. – Lars Lind Nilsson Jul 05 '16 at 19:16

1 Answers1

4

I have found a workaround using NAudio (http://naudio.codeplex.com/) and the SpeechStreamer class from this StackOverflow answer (https://stackoverflow.com/a/11813276/2950065).

The updated test program, that continues to recognize when the computer is locked, looks like this:

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 Microsoft.Speech.Recognition;
using System.Globalization;
using NAudio.Wave;
using System.IO;
using System.IO.Pipes;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SpeechRecognitionEngine sre;
        WaveIn wi;
        SpeechStreamer ss;

        public Form1()
        {
            InitializeComponent();

            WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();
            wi = new WaveIn(callbackInfo);
            ss = new SpeechStreamer(100000);
            wi.DataAvailable += wi_DataAvailable;
            wi.StartRecording();

            CultureInfo ci = new CultureInfo("en-us");
            sre = new SpeechRecognitionEngine(ci);
            // The default format for WaveIn is 8000 samples/sec, 16 bit, 1 channel
            Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo safi = new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(8000, Microsoft.Speech.AudioFormat.AudioBitsPerSample.Sixteen, Microsoft.Speech.AudioFormat.AudioChannel.Mono);
            sre.SetInputToAudioStream(ss, safi);
            GrammarBuilder gb = new GrammarBuilder("Hello");
            sre.LoadGrammarAsync(new Grammar(gb));
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }

        void wi_DataAvailable(object sender, WaveInEventArgs e)
        {
            ss.Write(e.Buffer, 0, e.BytesRecorded);
        }

        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            listBox1.Items.Add(DateTime.Now.ToString() + " " + e.Result.Text);
        }
    }
}
Community
  • 1
  • 1
Lars Lind Nilsson
  • 1,136
  • 6
  • 14