Speech recognition help

john19992010

Member
Joined
Jun 8, 2014
Messages
5
Programming Experience
1-3
Hi

I'm creating a voice control program and this is the first speech recognition program I have made. I have a problem in that if the user says "Jarvis what is (X)" then the program will run system.diagnostics.process.start ("http://www.google.com/(X). I know how to do all this normally but when it comes to voice control I don't know how to make the variable (X) be the search term . The code is something like this

case is = "JARVIS WHAT IS (X)
jarvis.speak "checking now sir"
system.diagnostics.process.start (http://www.google.com/(X)
jarvis.speak "here we are sir"


so my question is how do I make the search term be what is spoken

thanks
 
yes I think so, this is the fist couple of lines of the coding, its quite long and im not sure whats relevant or not but here it is.


Imports System.Speech.Recognition
Imports System.Speech.Recognition.SrgsGrammar
Imports System.Runtime.InteropServices 'For Monitor Command
Imports System.IO
Imports System.Reflection


Public Class Form1
'This object represents the Speech recognition engine
Private recognizer As SpeechRecognizer
Dim QuestionEvent As String
Dim Jarvis = CreateObject("Sapi.spvoice")


Public Sub New()


InitializeComponent() ' This call is required by the Windows Form Designer.


recognizer = New SpeechRecognizer() ' Add any initialization after the InitializeComponent() call.


AddHandler recognizer.SpeechDetected, AddressOf recognizer_SpeechDetected 'this event is raised when the user begins to speak


AddHandler recognizer.SpeechRecognitionRejected, AddressOf recognizer_SpeechRecognitionRejected 'this is raised when spoken words are not recognized as compliant to our grammar rules


AddHandler recognizer.SpeechRecognized, AddressOf recognizer_SpeechRecognized 'this is raised when the application correctly recognizes spoken words


Dim grammar As New GrammarBuilder()
grammar.Append(New Choices(System.IO.File.ReadAllLines("C:\Documents and Settings\IT\My Documents\Commands_txt.txt")))


recognizer.LoadGrammar(New Grammar(grammar))







and then after this it goes into the (CASE IS = "") stuff


thanks
 
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.(y)

Constants can make your program more readable...:eek:
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.:cool:

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.(y)(y)(y):eek::eek::eek:

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
 
Last edited:
Hi

I'm creating a voice control program and this is the first speech recognition program I have made. I have a problem in that if the user says "Jarvis what is (X)" then the program will run system.diagnostics.process.start ("http://www.google.com/(X). I know how to do all this normally but when it comes to voice control I don't know how to make the variable (X) be the search term . The code is something like this

case is = "JARVIS WHAT IS (X)
jarvis.speak "checking now sir"
system.diagnostics.process.start (http://www.google.com/(X)
jarvis.speak "here we are sir"


so my question is how do I make the search term be what is spoken

thanks

You would basically find the site you want to go to and copy & paste the URL into your process.start("HERE").
Please note that when I try to apply the url I use to this post site, it changes what it should look like.

Case "Jarvis show me the weather"
jarvis.speak "checking sir"
process.start ("National and Local Weather Radar, Daily Forecast, Hurricane and information from The Weather Channel and weather.com")

This is mine: (I use audio files for replies verse synth speaking back at you)

Case "scarlett show the weather", "scarlett whats the weather like"
My.Computer.Audio.Play(My.Resources.weather, AudioPlayMode.Background)
Process.Start("MSN")

Below is an even broader approach:

Case "search for animals"
My.Computer.Audio.Play(My.Resources.asyouwish, AudioPlayMode.Background)
If e.Result.Text.StartsWith("search") Then
Dim spec = e.Result.Text.Replace("search", " ")
spec.Trim()
spec.Replace(" ", "+")
Process.Start("URL_TO-Animals" + spec)

End If
 
Here's another approach at conducting a URL search: This is a youtube search for Metallica, but you can change it to google and it will function the same.

Dim neo as string "Metallica"
Dim strURL As String = "https://www.youtube.com/search?q=" & neo & "&start="
Process.Start(strURL)
 
Last edited:
Back
Top