VB and Voice Recognition

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.

VB and Voice Recognition

Okay, so now you have a few simple commands that you can pass to your application. What more can you do with it? You can add Sub Command lists to your collection of commands. In this tutorial, you are going to create the first part of a Voice commanded text editor.

The only code I'm going to display here is code to add and remove Command menus in real time from the Microsoft Voice Commands. The rest of the text editor is in the download.

First, how do you add a second command menu? Almost the same way as you created your original command menu. Create a new menu with a different name. Use a naming convention so that you can easily pick it out.

Public Sub Add_Sub_Commands()
Sub_menu = VoiceCmd.MenuCreate("Sub Commands", "commands State", 4)
VoiceCmd.AddCommand Sub_menu, 1, "close", "Close File", _
                              "listen list", 0, ""
VoiceCmd.AddCommand Sub_menu, 1, "save", "Save File", _
                              "listen list", 0, ""
VoiceCmd.AddCommand Sub_menu, 1, "select all", "Select all text", _
                              "listen list", 0, ""
VoiceCmd.AddCommand Sub_menu, 1, "select to end", _
                              "Select text to end", _
                              "listen list", 0, ""
VoiceCmd.AddCommand Sub_menu, 1, "select to begin", _
                              "select text to begin", _
                              "listen list", 0, ""
End Sub

You will notice that I have used a variable named 'Sub_Menu' to identify the second command list. I passed all the new commands I want to this sub menu. But these commands still are not working; you have to activate them.

Public Sub Activate_Sub_Commands()
VoiceCmd.Activate Sub_menu
End Sub

Then, to deactivate them, you use:

Public Sub Deactivate_Sub_Commands()
VoiceCmd.Deactivate Sub_menu
End Sub

Because you only want your extended commands to be active while you have a document open, you have to keep them in the deactivated state until the document opens. After you have opened the first document, you can call Activate Sub Commands, and when you close the last document, you can call Deactivate Sub Commands. And, of course. you cannot forget to release the Sub commands when you end the application.

TCount = VoiceCmd.CountCommands(Sub_menu)
For Loop_1 = TCount To 1 Step -1
   VoiceCmd.Remove Sub_menu, Loop_1
Next Loop_1
VoiceCmd.ReleaseMenu Sub_menu

Next time, I will cover the Voice diction Commands and add some text to speech to your application.

About the Author

Richard Newcombe

Richard Newcombe has been involved in computers since the time of the Commodore 64. Today, he has excelled in programming, and designs. Richard is in his mid 30's and, if or when you looking for him look no further than his computer. Always willing to help and give advice where he can in regard to computer related subjects. At present he is working as a .NET 2008 Software Developer for Syncrony Web Services, South Africa.



  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds