Alpha Webdesign

From the Blog

VB Voice Recognition Code

'https://a-webdesign.com/VoiceRecognition.zip
'Please visit: https://www.youtube.com/channel/UCGBJFrVjfsTRu-C_Eh2BgVQ?view_as=subscriber
'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"
        MyVoice.SetInputToDefaultAudioDevice()

        '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
        MyWordsRule.Add(MyWords)

        'Add the MyWordRule onto the system
        MyGrammar.Rules.Add(MyWordsRule)

        '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
        MyVoice.RecognizeAsync()

    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

        MyVoice.RecognizeAsync()

    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
                SetColor(Color.Blue)
            'if the user speaks "Blue"
            Case "green"
                'The Background of the Form will change to Green
                SetColor(Color.Green)

        End Select

    End Sub

End Class

Have your say

sixteen − twelve =