Computational Methods for Research in Speech Science

How to use Speech Recognition within a Visual Basic .NET application

This page provides a tutorial for the use of the Windows speech recognition engine from within a Visual Basic application.

An excellent free version of Visual Basic .NET called Visual Basic 2008 Express Edition can be downloaded from the MSDN web site.

Full documentation for the .NET System.Speech.Recognition component.

1. A "Yes-No-Maybe" application

Create a Windows Forms application. Give the application three labels: LabelYes, LabelNo and LabelMaybe. Initialise these to a large font and a light-gray colour:

2. Reference the System.Speech component

Use Project|Add Reference to add a reference to the System.Speech component:

3. Recognition code

Add this code to the form:

    Imports System.Speech.Recognition
    Imports System.Threading
    Imports System.Globalization
    Public Class Form1
        ' recogniser & grammar
        Dim recog As New SpeechRecognizer
        Dim gram As Grammar
        ' events
        Public Event SpeechRecognized As _
        	EventHandler(Of SpeechRecognizedEventArgs)
        Public Event SpeechRecognitionRejected As _
        	EventHandler(Of SpeechRecognitionRejectedEventArgs)
        ' word list
        Dim wordlist As String() = New String() {"Yes", "No", "Maybe"}
        ' word recognised event
        Public Sub recevent(ByVal sender As System.Object, _
        		ByVal e As RecognitionEventArgs)
            LabelYes.ForeColor = Color.LightGray
            LabelNo.ForeColor = Color.LightGray
            LabelMaybe.ForeColor = Color.LightGray
            If (e.Result.Text = "Yes") Then
                LabelYes.ForeColor = Color.Blue
            ElseIf (e.Result.Text = "No") Then
                LabelNo.ForeColor = Color.Blue
            ElseIf (e.Result.Text = "Maybe") Then
                LabelMaybe.ForeColor = Color.Blue
            End If
        End Sub
        ' recognition failed event
        Public Sub recfailevent(ByVal sender As System.Object, _
        		ByVal e As RecognitionEventArgs)
            LabelYes.ForeColor = Color.LightGray
            LabelNo.ForeColor = Color.LightGray
            LabelMaybe.ForeColor = Color.LightGray
        End Sub
        ' form initialisation
        Private Sub Form1_Load(ByVal sender As System.Object, _
        		ByVal e As System.EventArgs) Handles MyBase.Load
            ' need these to get British English rather than default US
            Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
            Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-GB")
            ' convert the word list into a grammar
            Dim words As New Choices(wordlist)
            gram = New Grammar(New GrammarBuilder(words))
            recog.LoadGrammar(gram)
            ' add handlers for the recognition events
            AddHandler recog.SpeechRecognized, AddressOf Me.recevent
            AddHandler recog.SpeechRecognitionRejected, AddressOf Me.recfailevent
            ' enable the recogniser
            recog.Enabled = True
        End Sub
    End Class

4. Run the application

When the application starts the Windows speech recognition system will be loaded. You will need to say "Start Listening" or to click on the microphone icon to start recognition.

Then when you say "yes", "no" or "maybe", the appropriate label will light up. If you say anything else, the labels should turn back to grey.

To improve recognition, you should set up the speech recognition system for your microphone, environment and your voice using the Speech Recognition applet in the Control Panel.

 

Also see:

 

Back to Computational Methods Home