Speech.Recognizer :>> Cheching if User is done inputting voice command

Lokesh R 61 Reputation points
2021-04-15T06:06:38.357+00:00

Hi

Hope you all are doing fine.

I have been working on a sound recorder and a speech recognition programme in visual basic 2010. It works fine it recods the sound and plays back. I want to check if I can know the time when a user have completed a voice recording or a voice command so that I can hit "enter key" from the code automatically to start another sub procedure.

Below is my code

Public Class Form1

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, _

                                                                                   ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, _

                                                                                   ByVal hwndCallback As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' record from microphone

        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)

        mciSendString("record recsound", "", 0, 0)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ' stop  and save

        mciSendString("save recsound c:\record.wav", "", 0, 0)

        mciSendString("close recsound", "", 0, 0)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        ' play audio

        My.Computer.Audio.Play("c:\record.wav", AudioPlayMode.Background)

    End Sub

End Class

Belwo is for speech recognizer

Imports System.Speech.Recognition 
System.Speech
Imports System.Speech.Recognition.SrgsGrammar 

Public Class Form1

    Dim WithEvents recognizer As SpeechRecognitionEngine = New SpeechRecognitionEngine()

    Dim VoxCommands As New List(Of String)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        recognizer.SetInputToDefaultAudioDevice()

        VoxCommands.Add("Hello Mr. rawat")
        VoxCommands.Add("Go Away")


        For Each VoxCmd In VoxCommands
            Dim NewGrammar As New Grammar(New Choices(New String(VoxCmd)))
            recognizer.LoadGrammarAsync(NewGrammar)
        Next

        recognizer.RecognizeAsync(RecognizeMode.Multiple)

    End Sub

    Private Sub recognizer_LoadGrammarCompleted(sender As Object, e As LoadGrammarCompletedEventArgs) Handles recognizer.LoadGrammarCompleted

        Dim grammarName As String = e.Grammar.Name
        Dim grammarLoaded As Boolean = e.Grammar.Loaded

        If e.[Error] IsNot Nothing Then

            Label1.Text = "LoadGrammar for " & grammarName & " failed with a " & e.[Error].[GetType]().Name & "."
        End If

        Label1.Text = ("Grammar " & grammarName & " " & If((grammarLoaded), "Is", "Is Not") & " loaded.")

    End Sub

    Private Sub recognizer_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs) Handles recognizer.SpeechRecognized

        Label2.Text = "Grammar " & e.Result.Grammar.Name & " " & e.Result.Text

        Dim Neuro = CreateObject("sapi.spvoice")

        Select Case e.Result.Text.ToUpper
            Case Is = "HELLO MR. RAWAT"
                recognizer.RecognizeAsyncStop()
                Neuro.Speak(" Hello Mr. rawat")
                Process.Start("Notepad")
                recognizer.RecognizeAsync(RecognizeMode.Multiple)
            Case Is = "GO AWAY"
                recognizer.RecognizeAsyncStop()
                Neuro.Speak("Go Away")
                recognizer.RecognizeAsync(RecognizeMode.Multiple)
                Me.Close()
                End
        End Select

    End Sub

End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,848 questions
{count} votes