How to Make Voice Command Programs in Visual Basicby Kevin Lee
Twenty-first century computer technology allows users to talk to their computers. Speech recognition software converts human speech into digital data that computers can recognize. Several commercial products, such as Dragon NaturallySpeaking and MacSpeech, allow users to perform a variety of speech recognition tasks. Using software built into Microsoft's Visual Studio development environment, you can create your own voice command programs using the latest version of Visual Basic.
Launch Microsoft Visual Studio and click "New Project" to open the "New Project" window. Click "Visual Basic" to highlight it, and then double-click "Windows Forms Application" to create a new Windows forms project. Code files will appear on the right side of the user interface in the Solution Navigator panel. An empty form named "Form1" will also appear on the user interface.
Click "Project," and then click "Add Reference" to display the "Add Reference" pop-up window. Type "Speech" (without the quotes) in the text box and press "Enter." The name "System.Speech" will appear in the search results. Click "Add" to add it to your project, and then click "Close" to close the "Add Reference" window.
Double-click the title bar of the empty form named "Form1." This causes Visual Studio to open a code window and display the following code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
This method, named "Form1_Load," executes when the application starts and loads the form.
Add the following code before the "End Sub" statement shown in the previous step:
Dim engine As New Speech.Recognition.SpeechRecognitionEngine
Dim dictionGrammar As New Speech.Recognition.DictationGrammar
Dim recognitionResult As Speech.Recognition.RecognitionResult
Dim timeSpan As New TimeSpan(0, 0, 10)
recognitionResult = engine.Recognize(timeSpan)
For Each word As RecognizedWordUnit In recognitionResult.Words
The first four lines initialize the Microsoft speech recognition engine. Line five sets the engine's audio input parameters. Line six tells Visual Basic to use the standard speech recognition dictionary that Windows uses. The line that begins with "recognitionResult" starts the speech recognition engine. The final three lines of code display the words that the program recognizes when someone speaks into a microphone.
Connect a microphone to your computer, and press the "F5" key to launch the application.
Speak several words into the microphone. The "engine.Recognize(timeSpan)" code will capture your words and store them in the variable named "recognitionResult." Visual Basic will then display your words in a pop-up window.
- check Note the block of code that begins with, "For Each word as RecognizedWordUnit In recognitionResults.Words." There you see the "MessageBox.Show(word.Text)" statement. That statement is inside a loop. The loop executes for each word in your sentence. If you say three words, the loop will run three times and display the contents of the variable "word.Text."
- check When you build your application, your VB code can examine that variable and decide how to proceed. For example, if a user says "Notepad," your application might launch the Notepad application. If the user says "Red," you could display a red image. The possibilities are infinite when it comes to interacting with users and tailoring your application's response based on what they say.
- check Visual Studio has additional helpful classes, methods and properties that you can use to create more powerful voice recognition applications (see Resources).
- photo_camera brunette woman with headset on white background image by Valentin Mosichev from Fotolia.com
Click here to provide feedback on this article