This is a basic speech recognition setup for you that I designed quite some time ago. It was originally written in C# but I converted it for you. It's quite effective. My advice is to "enumerate your strings" (enum).
Tips to being a better programmer:
In simple words, an enum allows us to declare a set of named constants such as a collection of related values that can be numeric or string values. There are three types of enums: Numeric enum. String enum. "Value" is everything when it comes to strings which brings me to const value.
PLEASE Convert to Constant is smart when you have a string variable or a field whose value never changes. Using constants makes your code safer.
Constants can make your program more readable...
For example, you can declare: Const PI = 3.141592654. Then, within the body of your program, you can make calculations that have something to do with a circle. Constants can make your program more readable.
INTERPOLATION: String interpolation is a great programming language feature that
allows injecting variables, function calls, arithmetic expressions directly into a string. You can use apostrophes and quotes easily that they can make our strings and therefore our code easier to read as well.
CONVERT TO 64-bit>>IS BETTER: To put this simply, a 64-bit processor is more capable than a 32-bit processor because it can handle more data at once. A 64-bit processor can store more computational values, including
memory addresses, which means it can access over 4 billion times the physical memory of a 32-bit processor. That's just as big as it sounds.
Example:
Before: Private thisString As String = "thisString"
After: Private Const thisString As String = "thisString"
I have a more advanced version of this code below, at this Post reply link:
How to Write Speech Grammar...Please Help
We can cover any aspect if you have questions. Be sure to "Imports System.Speech.Recognition" under add reference.
Happy Thanksgiving
Now for the code:
Imports System
Imports System.Diagnostics
Imports System.Media
Imports System.Runtime.InteropServices
Imports System.Speech.Recognition
Imports System.Speech.Synthesis
Imports System.Threading
Imports System.Windows.Forms
Public Class Form1
Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
Private Const WM_APPCOMMAND As Integer = &H319
<DllImport("user32.dll")>
Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Scarlett As SpeechSynthesizer = New SpeechSynthesizer()
Private recEngine As SpeechRecognitionEngine = New SpeechRecognitionEngine()
End Sub
'NOTE: This is an Enum example for the strings below:
'Below references the "Dim commands As Choices," which is the Parent of these nestles of strings
'You can't space out the words (as seen in the strings) when applying them to enum
Private Enum Commands
scarlettmutevolume
scarlettvolumeup
End Enum
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim commands As Choices = New Choices()
commands.Add(New String() {"scarlett mute volume", "scarlett volume up", "scarlett volume down", "stop listening", "open wiki", "show commands", "tell me who you are", "scarlett close", "scarlett what day is it", "scarlett out of the way", "scarlett come back", "disparta", "page up", "page down", "new tab", "switch tab", "magnify", "less", "scarlett notepad", "scarlett visual studios", "open command", "scarlett play rammstein", "scarlett open youtube", "scarlett movies", "scarlett open github", "scarlett microsoft office"})
Dim gramBuilder As GrammarBuilder = New GrammarBuilder()
gramBuilder.Append(commands)
Dim grammar As Grammar = New Grammar(gramBuilder)
Dim gram As Grammar = grammar
recEngine.LoadGrammarAsync(gram)
recEngine.SetInputToDefaultAudioDevice()
recEngine.RequestRecognizerUpdate()
recEngine.SetInputToDefaultAudioDevice()
recEngine.InitialSilenceTimeout = TimeSpan.FromSeconds(2.5)
recEngine.BabbleTimeout = TimeSpan.FromSeconds(1.5)
recEngine.EndSilenceTimeout = TimeSpan.FromSeconds(1.2)
recEngine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(1.5)
recEngine.SpeechRecognized += AddressOf RecEngine_SpeechRecognized
recEngine.RecognizeAsync(RecognizeMode.Multiple)
Scarlett.SelectVoiceByHints(VoiceGender.Female)
End Sub
Private Sub RecEngine_SpeechRecognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
Select Case e.Result.Text
Case "scarlett mute volume"
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, CType(APPCOMMAND_VOLUME_MUTE, IntPtr))
Case "scarlett volume up"
Dim repeat As Integer = 10
For i As Integer = 0 To repeat - 1
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, CType(APPCOMMAND_VOLUME_UP, IntPtr))
Next
Case "scarlett volume down"
For iter As Integer = 0 To 10 - 1
SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, CType(APPCOMMAND_VOLUME_DOWN, IntPtr))
Next
'You will have to add your own synth replies. I added one for an example. Also establish a command list for viewing in the event ' you have a lot of commands. It's not needed to run the app, just reference.
Case "show commands"
Scarlett.Speak("As you wish")
Process.Start("C:\Users\Your_Name\source\repos\Scarlett\Resources\Commands.txt")
Case "tell me who you are"
Case "scarlett open youtube"
Process.Start("chrome", "Http://Your_Path")
Case "scarlett movies"
Process.Start("chrome", "Http://Your_Path")
Case "scarlett open github"
Process.Start("chrome", "Http://Your_Path")
Case "open wiki"
Process.Start("
Wikipedia")
Case "scarlett microsoft office"
Process.Start("winword")
Case "stop listening"
recEngine.RecognizeAsyncStop()
Case "open command"
System.Diagnostics.Process.Start("cmd")
Case "scarlett close"
Sleep(TimeSpan.FromSeconds(2))
Application.[Exit]()
Case "scarlett what day is it"
Scarlett.Speak(DateTime.Today.ToString("dd-MM-yyyy"))
Case "scarlett out of the way"
If WindowState = FormWindowState.Normal Then
WindowState = FormWindowState.Minimized
End If
Case "scarlett come back"
If WindowState = FormWindowState.Minimized Then
WindowState = FormWindowState.Normal
End If
Case "disparta"
SendKeys.Send("%{F4}")
Case "scarlett play rammstein" 'Youtube Path...
Process.Start("chrome", "Http://Your_Path")
Case "page up"
SendKeys.Send("{PGUP}")
Case "page down"
SendKeys.Send("{PGDN}")
Case "new tab"
SendKeys.Send("^{t}")
Case "switch tab"
SendKeys.Send("^{TAB}")
Case "magnify"
SendKeys.Send("^{+}")
Case "less"
SendKeys.Send("^{-}")
Case "scarlett notepad"
Process.Start("notepad")
Case "scarlett visual studios"
Process.Start("devenv.exe")
Case Else
Scarlett.Speak("Unknown Command")
End Select
End Sub
Private Sub Sleep(ByVal timeSpan As TimeSpan)
Thread.Sleep(2000)
End Sub
End Class