Alpha Webdesign

From the Blog

VB Voice Recognition Code

'Please visit:
'for instructions on how to complete this Visual Basic project
'Created by Ioannis Ntizoglou 05/03/2019
'Import the Speech recognition on the form
Imports System.Speech

Public Class Form1

    'Create the first global variable MyVoice to recognise the new voice each time a person speaks
    Dim WithEvents MyVoice As New Recognition.SpeechRecognitionEngine

    'The first Private Sub - SetColor (it will allow you to action the command and set the background colour to the colour the user has said.
    Private Sub SetColor(ByVal color As System.Drawing.Color)
        Me.BackColor = color
    End Sub

    'Form load - if you have renamed your form, you will need to change the "Form1_Load" to the "NameOfYourForm_Loud
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'activate the default audio device "Mic"

        'Create a var MyGrammar
        Dim MyGrammar As New Recognition.SrgsGrammar.SrgsDocument

        'Create a var MyWordsRule
        Dim MyWordsRule As New Recognition.SrgsGrammar.SrgsRule("words")

        'Create a var MyWordsRule
        Dim MyWords As New Recognition.SrgsGrammar.SrgsOneOf("green", "blue")

        'Add the words I speak onto the system

        'Add the MyWordRule onto the system

        'The location to MyWordRule
        MyGrammar.Root = MyWordsRule

        'When you hear my voice, LoadGrammar
        MyVoice.LoadGrammar(New Recognition.Grammar(MyGrammar))

        'recognise my voice on form load

    End Sub

    'recognise my voice every time I speak
    Private Sub reco_RecognizeCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles MyVoice.RecognizeCompleted


    End Sub

    'recognise my voice and if the case exists, execute the procedure
    Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles MyVoice.SpeechRecognized

        'Select Case
        Select Case e.Result.Text
            'if the user speaks "Blue"
            Case "blue"
                'The Background of the Form will change to Blue
            'if the user speaks "Blue"
            Case "green"
                'The Background of the Form will change to Green

        End Select

    End Sub

End Class

Have your say

19 − 15 =