2

I'm trying to create a program that can recognise what a user is saying, and make a decision on what to do based on that, but i can't seem to find any code on the internet for it in vb.net and a console application. Is it even possible to do it in a non-object orientated language and there not being any events in a console application?

I've tried converting c# console code to vb.net console code and it doesn't word due to the issues of converting from an object orientated programming langue to a non object-orientated one.

I've also tried to just get it to run in a form, and that doesn't work due to it locating the grammar file, but failing to recognise it as one

This is the code i've tried: 1. My VB form code

Imports System.Speech
Imports System.Speech.Recognition

Public Class Form1
    Dim WithEvents recog As New Recognition.SpeechRecognitionEngine
    Private Sub setcolo(ByVal colour As System.Drawing.Color)
        Dim synth As New Synthesis.SpeechSynthesizer
        Me.BackColor = colour
    End Sub

2. My translated C# code

Imports System, System.Speech.Recognition, System.Speech.Synthesis, System.Globalization
Module Module1
    Dim ss As New System.Speech.Synthesis.SpeechSynthesizer
    Dim sre As Speech.Recognition.SpeechRecognitionEngine
    Dim done As Boolean = False
    Dim speechOn As Boolean = True
    Sub Main(args As String)
        Try
            ss.SetOutputToDefaultAudioDevice()
            Console.WriteLine("\n(Speaking: I am awake)")
            ss.Speak("I am awake")
            Dim ci = New CultureInfo("en-us")
            sre = New Speech.Recognition.SpeechRecognitionEngine(ci)
            sre.SetInputToDefaultAudioDevice()
            'sre.SpeechRecognized = sre.SpeechRecognized + sre_SpeechRecognized()
            Dim ch_StartStopCommands As Speech.Recognition.Choices = New Speech.Recognition.Choices
            ch_StartStopCommands.Add("speech on")
            ch_StartStopCommands.Add("speech off")
            ch_StartStopCommands.Add("klatu barada nikto")
            Dim gb_StartStop As Speech.Recognition.GrammarBuilder = New Speech.Recognition.GrammarBuilder()
            gb_StartStop.Append(ch_StartStopCommands)
            Dim g_StartStop As Speech.Recognition.Grammar = New Speech.Recognition.Grammar(gb_StartStop)
            Dim ch_Numbers As Speech.Recognition.Choices = New Speech.Recognition.Choices()
            ch_Numbers.Add("1")
            ch_Numbers.Add("2")
            ch_Numbers.Add("3")
            ch_Numbers.Add("4")
            Dim gb_WhatIsXplusY As Speech.Recognition.GrammarBuilder = New Speech.Recognition.GrammarBuilder()
            gb_WhatIsXplusY.Append("What is")
            gb_WhatIsXplusY.Append(ch_Numbers)
            gb_WhatIsXplusY.Append("plus")
            gb_WhatIsXplusY.Append(ch_Numbers)
            Dim g_WhatIsXplusY As Speech.Recognition.Grammar = New Speech.Recognition.Grammar(gb_WhatIsXplusY)
            sre.LoadGrammarAsync(g_StartStop)
            sre.LoadGrammarAsync(g_WhatIsXplusY)
            sre.RecognizeAsync(Speech.Recognition.RecognizeMode.Multiple)
            While (done = False)
            End While
            Console.WriteLine("\nHit <enter> to close shell\n")
            Console.ReadLine()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub
    Function sre_SpeechRecognized(sender As Object, e As Speech.Recognition.SpeechRecognizedEventArgs)

        Dim txt As String = e.Result.Text
        Dim confidence As Double = e.Result.Confidence
        Console.WriteLine("\nRecognized: " & txt)
        If (confidence < 0.6) Then Return ""
        If (txt.IndexOf("speech on") >= 0) Then

            Console.WriteLine("Speech is now ON")
            speechOn = True
        End If
        If (txt.IndexOf("speech off") >= 0) Then

            Console.WriteLine("Speech is now OFF")
            speechOn = False
        End If
        If (speechOn = False) Then Return ""
        If (txt.IndexOf("klatu") >= 0 And txt.IndexOf("barada") >= 0) Then

            '((SpeechRecognitionEngine)sender).RecognizeAsyncCancel()


            done = True
            Console.WriteLine("(Speaking: Farewell)")
            ss.Speak("Farewell")
        End If
        If (txt.IndexOf("What") >= 0 And txt.IndexOf("plus") >= 0) Then

            Dim words() As String = txt.Split(" ")
            Dim num1 As Integer = Int(words(2))
            Dim num2 As Integer = Int(words(4))
            Dim sum As Integer = num1 + num2
            Console.WriteLine("(Speaking: " & words(2) & " plus " & words(4) & " equals " & sum & ")")
            ss.SpeakAsync(words(2) & " plus " & words(4) & " equals " & sum)
        End If
        Return ""
    End Function
End Module
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        recog.SetInputToDefaultAudioDevice()
        Dim gram As New Recognition.SrgsGrammar.SrgsDocument("D:\Voiceregogform\Voiceregogform\bin\Debug\Voiceregogform.xml")
        Dim colourr As Recognition.SrgsGrammar.SrgsRule
        Dim colourlist As New Recognition.SrgsGrammar.SrgsOneOf("red", "yellow", "indigo", "aqua", "green")
        colourr.Add(colourlist)
        gram.Rules.Add(colourr)
        gram.Root = colourr
        recog.LoadGrammar(New Recognition.Grammar(gram))
        recog.RecognizeAsync()

    End Sub

    Private Sub recog_RecognizeCompleted(sender As Object, e As RecognizeCompletedEventArgs) Handles recog.RecognizeCompleted
        recog.RecognizeAsync()
    End Sub

    Private Sub recog_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs) Handles recog.SpeechRecognized
        Select Case e.Result.Text
            Case "red"
                setcolo(Color.Red)
            Case "yellow"
                setcolo(Color.Yellow)
            Case "aqua"
                setcolo(Color.Aqua)
            Case "green"
                setcolo(Color.Green)
            Case "indigo"
                setcolo(Color.Indigo)
            Case "blue"
                setcolo(Color.Blue)
        End Select
    End Sub
End Class
shellgon26
  • 23
  • 5
  • 4
    So C# is an *OOP language* and VB.Net is not an *OOP language*. When did this happen? OOP is not related to voice recognition, anyway. Post the code you have tried. – Jimi Jul 19 '19 at 11:24
  • 3
    Any nuget libraries working for C# will work for VB.net – Magnus Jul 19 '19 at 12:17

1 Answers1

1

This is a translation of the example at https://learn.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine. I tried it out on my own Windows 10 PC, and can confirm that it works. I used Visual Studio Community 2019 and went with its defaults for a VB Console App on .NET FW. Hope this helps!

On a side note, VB comes in two flavors: (1) the old VBA/VB6; (2) and the latest VB.NET. VB.NET is a fully modern OOP language; VB.NET and C# are siblings "joined at the hip" by a common compilation platform known as Roslyn. VB6 is discontinued, and VBA lives on as a part of Microsoft Office.

About VBA/VB6 and OOP, check out Is VBA an OOP language, and does it support polymorphism?, it's a fun and thoughtful read.

' NOTE: Must target .NET Framework, 3.0 or later (not .NET Core!)
Imports System.Console

' Make reference to System.Speech (System.Speech.dll)
' https://learn.microsoft.com/en-us/dotnet/api/system.speech.recognition.speechrecognitionengine
Imports System.Speech.Recognition

Module Program

    Sub Main()

        ' Create an in-process speech recognizer for the en-US locale.  
        Using recognizer As New SpeechRecognitionEngine(
          New Globalization.CultureInfo("en-US"))

            ' Create and load a dictation grammar.  
            recognizer.LoadGrammar(New DictationGrammar())

            ' Add a handler for the speech recognized event.
            AddHandler recognizer.SpeechRecognized,
                AddressOf recognizer_SpeechRecognized

            ' Configure input to the speech recognizer.  
            recognizer.SetInputToDefaultAudioDevice()

            ' Start asynchronous, continuous speech recognition.  
            recognizer.RecognizeAsync(RecognizeMode.Multiple)

            ' Keep the console window open.  
            Do
                ReadLine()
            Loop

        End Using

    End Sub

    ' Handle the SpeechRecognized event.  
    Sub recognizer_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
        WriteLine("Recognized text: " + e.Result.Text)
    End Sub

End Module
rskar
  • 4,607
  • 25
  • 21
  • Thank you so much, this works, i assume i can just specify what to do when it detects certain words right? – shellgon26 Jul 21 '19 at 11:49
  • Cool, glad you can get this started! Regarding detecting certain words, TBH, I have no experience in this, but I'd say that doing voice commands looks straightforward to me. Have a look at this (C#) article for ideas on how to proceed: https://www.codeproject.com/Articles/483347/Speech-recognition-speech-to-text-text-to-speech-a. – rskar Jul 21 '19 at 13:05