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