Voice recognition using Microsoft Speech SDK 5.1

 

Before we even start coding, some project options need to be set up. Go to Project, References…

Once you’re there, check “Microsoft Speech Object Library.” Okay, we’re done with that part—on to the code.

 

 

The first thing to do is declare these variables somewhere (probably at the top of the main form):

 

'Voice recognition variables

Dim WithEvents RC As SpSharedRecoContext

Dim Grammar As IspeechRecoGrammar

 

 

The following subroutine should be called at some time before you want voice recognition to begin (be sure the above variables are visible to this subroutine—in other words, put them in the same form as each other):

 

 

'

' Initialize the speech recognition portion of the program

'

Public Sub InitSR()

   

    If (RC Is Nothing) Then

        Set RC = New SpSharedRecoContext

        Set Grammar = RC.CreateGrammar(1)

        Grammar.CmdLoadFromFile Experiment.directory & "voicecommands.xml"

        Grammar.DictationSetState SGDSInactive

        Grammar.CmdSetRuleIdState 1, SGDSActive

    End If

   

End Sub

 

 

 

The speech recognition engine will call the next subroutine when something the user says is recognized as a word. As you can probably guess, the word is stored in Result.PhraseInfo.GetText.

 

 

'

' Called whenever a word is recognized

'

Public Sub RC_Recognition( _

ByVal StreamNumber As Long, _

ByVal StreamPosition As Variant, _

ByVal RecognitionType As SpeechRecognitionType, _

ByVal Result As ISpeechRecoResult)

 

    HandleVoice Result.PhraseInfo.GetText

 

End Sub

 

 

 

This last subroutine is useful for timing. The speech recognition engine will call it as soon as the sound card detects a sound. So basically, start a timer beforehand, and read it at this point to record RT’s.

 

 

'

' Called whenever the user speaks into the microphone

'

Private Sub RC_SoundStart(ByVal StreamNumber As Long,_

ByVal StreamPosition As Variant)

    If testing Then

        RT = ReadTime_Query

    End If

End Sub