VB and Voice Recognition

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

I’ve spent some time following up on Voice recognition. This technology is not that new; actually it’s been around for a good 15 years already. So why, you may ask, is it not widely available for you to add to your applications?

Actually, it is. Microsoft has been quietly developing VRS technology and integrating it into Windows. There are many downloads available on the Microsoft Web site that add VRS to your desktop, but very little on how to add this to your applications. This is a short tutorial on how to add voice commands to your application.

Why add voice commands to an application? Well, because it hasn’t been properly documented, many programmers have not worried about using it. Yet, there are a few applications where it comes in handy or even is required. Receptionists who spend 90% of their work time talking on the phone could benefit from the use of VRS. Instead of scrolling through an electronic Rolodex to find a number, they could say ‘Dial Customer.’ The software then would scroll through the Rolodex until a match is found, activate the voice-enabled modem, and dial the requested number.

VRS also can benefit the physically disabled, hence bringing more users to you application. With this method, your application would have to fully implement the voice command system, with a voice prompt for every possible command.

So how do we implement VRS to our application? The first thing you will need is the Speech SDK that is available on the Microsoft Web site. After installing the Speech SDK, you may need to do some voice training so that it can ‘learn’ how you speak and pronounce words.

Several new components are added to the VB runtime, namely Microsoft Voice Commands, Microsoft Voice Dictation, and Microsoft Voice Text. Also, the Microsoft Direct Speech Recognition, which is installed with VB6, now uses this SDK to complete its functionality. In this article, I will cover only the basic ‘Voice commands’ section of the SDK. In future articles, I will cover the other functions and components.

First, to understand what you are doing, look at the ‘Microsoft Voice‘ application that is added with this SDK. This application brings a Vocal command to most Windows functions. It allows you to verbally direct the mouse around the screen, scroll through menus, and has a few direct commands such as ‘Tell me the time‘. Learning to use this can be time consuming; also, the voice commands passed to an application require that you have a menu-driven form, or a visible command button for each function. This can be cumbersome at the best of times.

By adding ‘Voice commands’ directly to your application, you now can bypass the requirement of a menu and include only key command buttons and avoid the possibility of the spoken command activating some other application. You also will add ‘Direct commands’ rather than ‘Indirect commands’.

Indirect commands are available only if currently visible in the menu, and have to be ‘browsed to.’ Direct commands do not even have to be listed on the applications menus; they are essentially Direct. You speak the command and the event, procedure, or method is called or executed.

Microsoft Voice Commands is a collection of commands that are is for the Microsoft Voice Application. You can add your own list of commands to this. This is how you do this.

In your project, add the Microsoft Voice Commands component. Add it to your form. In its properties window, set the Name to ‘VoiceCmd’ and the Visible property to ‘false’. (You don’t need to see this when your application is run.)

Next, you have to Initiate the VRS and tell it which spoken commands have to be passed through to your application.

Private My_Menu As Long
Private Sub Form_Load
VoiceCmd.Initialized = 1
' MenuCreate adds a command list to the collection and returns
' the menu list's ID
My_Menu = VoiceCmd.MenuCreate(App.EXEName, "Commands", 4)
VoiceCmd.Enabled = 1
' Addcommand (Menu list to use , Command ID , Command ,
'             Description , Category , Action)
VoiceCmd.AddCommand My_Menu, 1, "Open", "Open Database", 0, ""
VoiceCmd.AddCommand My_Menu, 2, "Save", "Save Database", 0, ""
VoiceCmd.AddCommand My_Menu, 3, "Exit", "Exit App", 0, ""
VoiceCmd.Activate My_Menu
End Sub

Great. Now all that’s left is to call your ‘Open’ and ‘Save’ Commands. The Microsoft Voice Command has an event called CommandRecognised; this is called when it ‘Finds’ the spoken command in the list you specified. In this event, you can call your relative functions. Now, add this to you code.

Private Sub Voicecmd_CommandRecognize(ByVal ID As Long, _
                                      ByVal CmdName As String, _
                                      ByVal flags As Long, _
                                      ByVal action As String, _
                                      ByVal NumLists As Long, _
                                      ByVal ListValues As String, _
                                      ByVal command As String)
Select Case command
   Case "Open"
      Call Cmd_Open_Click
   Case "Save"
      Call Cmd_Save_Click
   Case "Exit"
      Unload me
End Sub

If you now run your application and speak the word ‘Open‘, the Cmd_Open_Click event would be triggered, thus calling the code that you have written to open a file. The same would happen with the ‘Save‘ command.

What more is required to do Voice commands? Nothing; through development the VRS is as simple as Plug and Play. All the Speech SDK requires from your application is the commands you require. When the SDK detects these commands, it passes them back to your application.

In the accompanying example download, I have also demonstrated how to pick up the audio levels of the microphone through the component, and how to perform commands that have no physical event. I hope you enjoy writing applications that you now can verbally command.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read