Speech Recognition

At last, some code!

In the Load function of the form input the following code:

Private Sub Form_Load()
  Dim totaldata As String
  'define configuration
  '[Grammer], set language to english (1033)
  '[<Start>], define the words we are looking for
  totaldata = "[Grammar]" & vbCrLf & _
  "langid = 1033" & vbCrLf & _
  "type=cfg" & vbCrLf & _
  "[<Start>]" & vbCrLf & _
  "<start>=Notepad" & vbCrLf & _
  "<start>=Volume" & vbCrLf & _
  "<start>=Media Player" & vbCrLf

  DirectSR1.GrammarFromString (totaldata)
  DirectSR1.Activate

So this is how the string looks. For each line that contains a word, a tag "<start>=" is used to signify a word that follows for the DirectSR to recognize. In the sample code, three words are used, they are Notepad, Volume and Media Player. Guess what? Right, we will use these words for the voice command to load these programs. In addition, the DirectSR1.Activate is used to start the engine to begin recognizing voice commands. DirectSR1.Deactivate is used to stop the engine from recognizing voice commands.

We are almost set. All we have to do is return to the PhraseFinish procedure to parse the command.
From here on is pretty simple, remember that the Phrase parameter is all we need to be concern about. Just put the following code in the PhraseFinish procedure:

Dim sFile As String
Dim noth As Long

Select Case Phrase
Case "Notepad"
    sFile = "\system32\notepad.exe"
    noth = ShellExecute(0, "OPEN", Environ("SystemRoot") & sFile, "", "", 1)
Case "Volume"
    sFile = "\system32\sndvol32.exe"
    noth = ShellExecute(0, "OPEN", Environ("SystemRoot") & sFile, "", "", 1)

Case "Media Player:
    sFile = "C:\Program Files\Windows Media Player\mplayer2.exe"

    noth = ShellExecute(0, "OPEN", sFile, "", "", 1)

End Select

Here is the low down or lodown :). When the DirectSR recognizes a sound, it will process the sound into a word that may closely match the one you provided. If it matches, the Phrase variable will contain the matched word. And lo and behold, the code we use is a Select Case just for the Phrase variable for the words we fed in the engine in the load function. When a match occurs we will use an API function ShellExecute() to load the program.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky