I am attempting to use a different input device with Speech Recognition Engine. I have attempted to use NAudio to achieve this, using WaveIn and the DataAvailable event, however I can't quite work out how to convert the buffer from the event into a stream usable with speech recognition engine's SetInputToAudioStream(). My current code looks like:
using System.Speech.Recognition;
using NAudio.Wave;
private SpeechRecognition sre;
private WaveInEvent wi;
private Stream st;
static void main(string[] args) {
Choices words = new Choices(new string[] { "word", "test" });
Grammar g = new Grammar(words);
wi = new WaveInEvent();
wi.DeviceNumber = 0; // Default device
wi.DataAvailable += Wi_DataAvailable;
wi.StartRecording();
st = new MemoryStream();
sre = new SpeechRecognitionEngine();
sre.LoadGrammar(g);
//sre.SetInputToDefaultAudioDevice();
sre.SetInputToAudioStream(st, /* SpeechAudioFormatInfo */);
sre.SpeechRecognized += Sre_SpeechRecognized;
sre.RecognizeAsync(RecognizeMode.Multiple);
}
private void Wi_DataAvailable(object sender, WaveInEventArgs e) {
// Convert e.Buffer to Stream st
}
private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) {
Console.WriteLine(e.Result.Text);
}
Could anybody help me with this? Thanks.