Imports System.Globalization
Imports System.Speech
Imports System.Speech.Recognition
Imports System.Speech.Synthesis
Module Module1

    Dim ss As New SpeechSynthesizer()
    Dim sre As SpeechRecognitionEngine
    Dim done As Boolean = False
    Dim speechOn As Boolean = True
    Public Sub Main()
        ss.SetOutputToDefaultAudioDevice()
        ss.Speak("Starting")
        Dim ci As New CultureInfo("en-us")
        sre = New SpeechRecognitionEngine(ci)
        sre.SetInputToDefaultAudioDevice()
        AddHandler sre.SpeechRecognized, AddressOf sre_SpeechRecognized

        LoadGrammer()
        sre.RecognizeAsync(RecognizeMode.Multiple)
        While done = False

        End While

        Try

            Console.WriteLine(vbLf & "Hit <enter> to close shell" & vbLf)
            Console.ReadLine()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub

    Private Function LoadGrammer()
        Try
            Dim ch_StartStopCommands As New Choices()
            ch_StartStopCommands.Add("rdp")
            ch_StartStopCommands.Add("speech on")
            ch_StartStopCommands.Add("speech off")
            ch_StartStopCommands.Add("Close")
            Dim gb_StartStop As New GrammarBuilder()
            gb_StartStop.Append(ch_StartStopCommands)
            Dim g_StartStop As New Grammar(gb_StartStop)
            sre.LoadGrammarAsync(g_StartStop)

            Dim ch_boolean As New Choices()
            ch_boolean.Add("dictate on")
            ch_boolean.Add("dictate off")
            ch_boolean.Add("dictation on")
            ch_boolean.Add("dictation off")
            Dim gb_dictate As New GrammarBuilder()
            gb_dictate.Append(ch_boolean)
            Dim g_dictate As New Grammar(gb_dictate)
            sre.LoadGrammarAsync(g_dictate)

            Dim ch_Numbers As New Choices()
            ch_Numbers.Add("1")
            ch_Numbers.Add("2")
            ch_Numbers.Add("3")
            ch_Numbers.Add("4")
            Dim gb_WhatIsXplusY As New GrammarBuilder()
            gb_WhatIsXplusY.Append("What Is")
            gb_WhatIsXplusY.Append(ch_Numbers)
            gb_WhatIsXplusY.Append("plus")
            gb_WhatIsXplusY.Append(ch_Numbers)
            Dim g_WhatIsXplusY As New Grammar(gb_WhatIsXplusY)
            sre.LoadGrammarAsync(g_WhatIsXplusY)

            Dim ch_servers As New Choices()
            ch_servers.Add("fileshare users")
            ch_servers.Add("provision")
            ch_servers.Add("vmServer")
            ch_servers.Add("file eye es people")
            Dim explorerGrammerBuilder As New GrammarBuilder()
            explorerGrammerBuilder.Append("explorer")
            explorerGrammerBuilder.Append(ch_servers)
            Dim explorerGrammer As New Grammar(explorerGrammerBuilder)
            sre.LoadGrammarAsync(explorerGrammer)

            Dim ch_apps As New Choices({"notepad", "hexeditor", "datacenter", "prtg", "cacti", "nagios", "eye es portal", "infoblox", "command"})
            Dim openGrammerBuilder As New GrammarBuilder()
            openGrammerBuilder.Append("open")
            openGrammerBuilder.Append(ch_apps)
            Dim openGrammer As New Grammar(openGrammerBuilder)
            sre.LoadGrammarAsync(openGrammer)

            Dim ch_rdp As New Choices({"vm", "nt"})
            Dim ch_hundres As New Choices({"1", "2", "3", "4", "5", "6", "7", "8", "9", "100", "200", "300", "400", "500", "600", "700", "800", "900"})
            Dim ch_tens As New Choices({"1", "2", "3", "4", "5", "6", "7", "8", "9", "eleven", "twelve", "thirteen", "fourteen", "fifthteen", "sixten", "seventeen", "eightteen", "nineteen", "20", "30", "40", "50", "60", "70", "80", "90"})
            Dim ch_ones As New Choices({"1", "2", "3", "4", "5", "6", "7", "8", "9"})
            Dim rdpGrammerBuilder As New GrammarBuilder()
            rdpGrammerBuilder.Append("rdp")
            rdpGrammerBuilder.Append(ch_rdp)
            rdpGrammerBuilder.Append(ch_hundres)
            rdpGrammerBuilder.Append(ch_tens)
            rdpGrammerBuilder.Append(ch_ones, 0, 1)
            'rdpGrammerBuilder.AppendDictation("spelling")
            Dim rdpGrammer As New Grammar(rdpGrammerBuilder)
            sre.LoadGrammarAsync(rdpGrammer)

            Dim ch_drive As New Choices({"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"})
            Dim TreeSizeGrammerBuilder As New GrammarBuilder()
            TreeSizeGrammerBuilder.Append("treesize")
            TreeSizeGrammerBuilder.Append(ch_rdp)
            TreeSizeGrammerBuilder.Append(ch_hundres)
            TreeSizeGrammerBuilder.Append(ch_tens)
            TreeSizeGrammerBuilder.Append(ch_ones, 0, 1)
            TreeSizeGrammerBuilder.Append(ch_drive, 0, 1)
            'rdpGrammerBuilder.AppendDictation("spelling")
            Dim TreeSizeGrammer As New Grammar(TreeSizeGrammerBuilder)
            sre.LoadGrammarAsync(TreeSizeGrammer)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Function
    Public Function URLDecode(ByVal Source As String) As String
        Dim x As Integer = 0
        Dim CharVal As Byte = 0
        Dim sb As New System.Text.StringBuilder()

        For x = 0 To (Source.Length - 1)
            Dim c As Char = Source(x)
            'Check for space
            If (c = "+") Then
                sb.Append(" ")
            ElseIf c <> "%" Then
                'Not hex value so add the chars to string builder.
                sb.Append(c)
            Else
                'Convert hex value to char value.
                CharVal = Int("&H" & Source(x + 1) & Source(x + 2))
                'Add the chars to string builder.
                sb.Append(Chr(CharVal))
                'INC by 2
                x += 2
            End If
        Next

        'Return the string.
        Return sb.ToString()

    End Function
    ' Main
    WithEvents DictationRecognition As SpeechRecognitionEngine
    Dim IsDicatating As Boolean = False
    Private Sub sre_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
        Dim txt As String = e.Result.Text
        Dim confidence As Single = e.Result.Confidence
        Console.WriteLine(Convert.ToString(vbLf & "Recognized: ") & txt)
        If confidence < 0.7 Then
            Return
        End If

        If txt.IndexOf("dictat") >= 0 Then
            Dim words As String() = txt.Split(" "c)
            Select Case words(1)
                Case "on"
                    IsDicatating = True
                    sre.UnloadAllGrammars()
                    Dim ch_StartStopCommands As New Choices()
                    ch_StartStopCommands.Add("dictate off")
                    Dim gb_StartStop As New GrammarBuilder()
                    gb_StartStop.Append(ch_StartStopCommands)
                    Dim g_StartStop As New Grammar(gb_StartStop)
                    'sre.LoadGrammarAsync(g_StartStop)

                    DictationRecognition = New SpeechRecognitionEngine(New System.Globalization.CultureInfo("en-US"))

                    Dim dg As New DictationGrammar("grammar:dictation")
                    dg.Name = "default dictation"
                    dg.Enabled = True
                    DictationRecognition.LoadGrammar(dg)
                    DictationRecognition.SetInputToDefaultAudioDevice()
                    DictationRecognition.RecognizeAsync(RecognizeMode.Multiple)
                Case "off"
                    If IsDicatating Then
                        DictationRecognition.UnloadAllGrammars()
                        DictationRecognition.Dispose()
                        LoadGrammer()
                        IsDicatating = False
                    Else
                        Return
                    End If
            End Select

        End If

        If txt.IndexOf("explorer") >= 0 Then
            Dim words As String() = txt.Split(" "c)
            Select Case words(1)
                Case "provision"
                    Process.Start("\\vmServer\c$\Program Files\proVision")
            End Select
        End If


        If txt.IndexOf("open") >= 0 Then
            Dim words As String() = txt.Split(" "c)
            Select Case words(1)
                Case "notepad"
                    Process.Start("notepad++")
                Case "datacenter"
                    Process.Start("https://mydatacenter/")
                Case "prtg"
                    Process.Start("https://prtg/")
                Case "nagios"
                    Process.Start("https://nagios/")
                Case "cacti"
                    Process.Start("https://cacti")
                Case "eye es portal"
                    Process.Start("https://isportal/")
                Case "infoblox"
                    Process.Start("https://infoblox/ui/")
                Case "command"
                    Process.Start("cmd")
            End Select
        End If

        If txt.IndexOf("rdp") >= 0 Then
            Dim words As String() = txt.Split(" "c)
            If words.Length = 5 Then
                words(3) = words(3).Substring(0, 1) & words(4)
            End If
            Dim MyCreated As String = "mstsc /v:" & words(1) & words(2) & words(3) & " /f"
            Process.Start("mstsc", "/v:" & words(1) & words(2) & words(3) & " /f")
        End If

        If txt.IndexOf("treesize") >= 0 Then
            Dim words As String() = txt.Split(" "c)
            If words.Length = 6 Then
                words(3) = words(3).Substring(0, 1) & words(4)
                Process.Start("C:\Program Files\JAM Software\TreeSize Professional\TreeSize.exe", "\\" & words(1) & words(2) & words(3) & "\" & words(5) & "$")
            Else
                Process.Start("C:\Program Files\JAM Software\TreeSize Professional\TreeSize.exe", "\\" & words(1) & words(2) & words(3) & "\" & words(4) & "$")
            End If


        End If

        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
        End If
        If txt.IndexOf("klatu") >= 0 AndAlso txt.IndexOf("barada") >= 0 Then
            DirectCast(sender, SpeechRecognitionEngine).RecognizeAsyncCancel()
            done = True
            Console.WriteLine("(Speaking: Farewell)")
            ss.Speak("Farewell")
        End If
        If txt.IndexOf("What") >= 0 AndAlso txt.IndexOf("plus") >= 0 Then
            Dim words As String() = txt.Split(" "c)
            Dim num1 As Integer = Integer.Parse(words(2))
            Dim num2 As Integer = Integer.Parse(words(4))
            Dim sum As Integer = num1 + num2
            Console.WriteLine("Test")
            Console.WriteLine("Speaking: " & words(2) & " plus " & words(4) & " equals " & sum & ")")
            ss.SpeakAsync(words(2) & " plus " & words(4) & " equals " & sum)
        End If
    End Sub

    Private Sub DictationRecognition_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs) Handles DictationRecognition.SpeechRecognized
        Debug.WriteLine("SpeechRecognized: " & e.Result.Confidence & " - " & e.Result.Text)
        If e.Result.Confidence > 0.55 Then
            System.Windows.Forms.SendKeys.SendWait(e.Result.Text)
        End If

    End Sub

    Private Sub DictationRecognition_SpeechDetected(sender As Object, e As SpeechDetectedEventArgs) Handles DictationRecognition.SpeechDetected
        Debug.WriteLine("SpeechDetected: " & e.AudioPosition.Seconds)
    End Sub

    Private Sub DictationRecognition_RecognizeCompleted(sender As Object, e As RecognizeCompletedEventArgs) Handles DictationRecognition.RecognizeCompleted
        Debug.WriteLine("RecognizeCompleted: " & e.Result.Confidence & " - " & e.Result.Text)
    End Sub

    Private Sub DictationRecognition_SpeechHypothesized(sender As Object, e As SpeechHypothesizedEventArgs) Handles DictationRecognition.SpeechHypothesized
        Debug.WriteLine("SpeechHypothesized: " & e.Result.Confidence & e.Result.Text)
    End Sub

    Private Sub DictationRecognition_SpeechRecognitionRejected(sender As Object, e As SpeechRecognitionRejectedEventArgs) Handles DictationRecognition.SpeechRecognitionRejected
        Debug.WriteLine("SpeechRecognitionRejected: " & e.Result.Text)
    End Sub

    Private Sub DictationRecognition_LoadGrammarCompleted(sender As Object, e As LoadGrammarCompletedEventArgs) Handles DictationRecognition.LoadGrammarCompleted
        Debug.WriteLine("Grammer Completed: " & e.Grammar.Name)
    End Sub
End Module

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax