Thursday, February 15, 2007

C# starter code for SAPI 5.1 speech recognition from audio file

Here's how you would change the code in the previous post to recognize speech input from an audio file instead of the microphone.

First, create a wav file with some utterance using an audio recording software. Most formats should be supported, but a good setting would be 44,100Hz 16-bit mono wav file.

Simply replace the part of the code between /****** BEGIN: set up recognition context *****/ and /****** END: set up recognition context *****/ with the following snippet:

Caveat: Recognizing from audio file only works with in-proc recognizer.
            /****** BEGIN: set up recognition context *****/
result_textBox.AppendText("File mode\n");

// create an audio file stream
ISpeechFileStream sfs = new SpFileStreamClass();
sfs.Open(@"recording.wav", SpeechStreamFileMode.SSFMOpenForRead, false);

// create the recognition context
recoContext = new SpeechLib.SpInProcRecoContext();
recoContext.Recognizer.AudioInputStream = sfs;
((SpInProcRecoContext)recoContext).Recognition +=
new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);
/****** END: set up recognition context *****/

No comments: