I am trying to use .NET's System.Speech SpeechRecognitionEngine object to recognize words spoken by a discord user in a voice channel. The raw pcm audio received by the bot is written to a MemoryStream, and I am trying to get the SpeechRecognitionEngine to use this Stream for recognition. Getting this data and writing it works fine, however, using it with the SpeechRecognitionEngine seemingly doesn't work for multiple reasons. For one, the stream is not infinite, and the recognizer reaches the end of the stream and stops before words can even be spoken. Even when data is constantly added to the stream (i.e. the user is continually talking) the recognizer still reaches the end of the stream and refuses to continue. Another issue is that the method to run recognition seemingly cannot be run more than once. I've tried feeding the stream to the recognizer in chunks, however it didn't seem to work. There is an option to set the input to your default audio device, and that works exactly how I want mine to, always running and not stopping even when the user doesn't provide any input. Any help?
private SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
public MemoryStream stream = new MemoryStream();
//called before any other method when the bot joins the voice channel
public void StartRun(){
Choices commands = new Choices();
commands.Add(new string[] { "hello", "hey bot"});
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);
recognizer.LoadGrammar(grammar);
recognizer.SetInputToAudioStream(holdStream, new SpeechAudioFormatInfo(48000, AudioBitsPerSample.Sixteen, AuidoChannel.Mono));
recognizer.SpeechRecognized += async (s, e) => {} //handles
//the eventHandler i have for this event prints something whenever it reaches the end of the stream
recognizer.RecognizeCompleted += RecognizeCompleted;
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
In another program I write to the pcm data to 'stream', if there are any syntax errors it is because of copying the code by hand instead of copying and pasting in order to simplify my code. Thank you!