Feb 21, 2013 10:59 AM
(This post was last modified: Feb 21, 2013 11:18 AM by SIM-1456245.)
This tutorial is about Voice Recognition using "Microsoft Speech SDK 5.1". It will listen for your voice from a microphone, and then it will check if the recorded voice is similar to the given words, if yes then it does something, in this case you say a color and the background color of the form will change accordingly(sorry no MsgBox tutorial today, or at least not here, in this tutorial).
1. Download and install the Microsoft Speech SDK :
NOTE: The link is from Microsoft, so I almost can guarantee that it is safe to download.
http://www.microsoft.com/en-us/download/...x?id=10121
![[Image: vQpygsA.png]](https://i.imgur.com/vQpygsA.png)
2. First thing is first, we need to make a new project :
You need to have Framework 3.0 or 3.5, not 4.0 or 4.5 because this SDK is a bit old.
![[Image: Gw9jns2.png]](https://i.imgur.com/Gw9jns2.png)
3. As you can see, we don't need to add anything to the form. You also don't need to edit any properties.
![[Image: FBH2vKV.png]](https://i.imgur.com/FBH2vKV.png)
4. Lets add the Speech library to our program :
Project > Add Reference > .NET (tab) > System.Speech (select from the list).
![[Image: G3dDvYM.png]](https://i.imgur.com/G3dDvYM.png)
![[Image: gBaAgUP.png]](https://i.imgur.com/gBaAgUP.png)
5. And now to the code:
First we need to import the SDK to the code :
Now lets declare the class that we are using here, in this case System.Speech:
Here we set up some stuff, this needs to be in the Form Load sub:
This is the sub that actually detects when you say something and then it recognizes what you sat.
In the newly created sub add the following code, it will just change the color of the form We are using here a Select Case statement, but you can use If as well.
And that was actually it, see a very simple thing. Now double check your code and run it, it should change the color of the form accordingly on that what you say on the microphone.
Here is the final code:
![[Image: siKw1DH.png]](https://i.imgur.com/siKw1DH.png)
I hope you liked it 😉
[img]http://electroalek.comoj.com/ElectroalekLogo.gif[/img]
1. Download and install the Microsoft Speech SDK :
NOTE: The link is from Microsoft, so I almost can guarantee that it is safe to download.
http://www.microsoft.com/en-us/download/...x?id=10121
![[Image: vQpygsA.png]](https://i.imgur.com/vQpygsA.png)
2. First thing is first, we need to make a new project :
You need to have Framework 3.0 or 3.5, not 4.0 or 4.5 because this SDK is a bit old.
![[Image: Gw9jns2.png]](https://i.imgur.com/Gw9jns2.png)
3. As you can see, we don't need to add anything to the form. You also don't need to edit any properties.
![[Image: FBH2vKV.png]](https://i.imgur.com/FBH2vKV.png)
4. Lets add the Speech library to our program :
Project > Add Reference > .NET (tab) > System.Speech (select from the list).
![[Image: G3dDvYM.png]](https://i.imgur.com/G3dDvYM.png)
![[Image: gBaAgUP.png]](https://i.imgur.com/gBaAgUP.png)
5. And now to the code:
First we need to import the SDK to the code :
Code
Imports System.SpeechNow lets declare the class that we are using here, in this case System.Speech:
Code
Dim WithEvents listen As New Recognition.SpeechRecognizerHere we set up some stuff, this needs to be in the Form Load sub:
Code
Dim gram As New Recognition.SrgsGrammar.SrgsDocument
Dim rule As New Recognition.SrgsGrammar.SrgsRule("word") '(ID) - This is used to tell the program what are you detecting, but for general use you can leave it as it is. For number(number), For colors(color)
Dim list As New Recognition.SrgsGrammar.SrgsOneOf("Red", "Green", "Blue", "White", "Black", "Gray", "Yellow", "Pink", "Purple", "Orange", "Gold", "Silver") ' Add the word that you want to use later in the program.
rule.Add(list)
gram.Rules.Add(rule)
gram.Root = rule
listen.LoadGrammar(New Recognition.Grammar(gram))This is the sub that actually detects when you say something and then it recognizes what you sat.
Code
Private Sub speech(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles listen.SpeechRecognized
End SubIn the newly created sub add the following code, it will just change the color of the form We are using here a Select Case statement, but you can use If as well.
Code
Select Case (e.Result.Text)
Case "Red"
Me.BackColor = Color.Red
Case "Green"
Me.BackColor = Color.Green
Case "Blue"
Me.BackColor = Color.Blue
Case "White"
Me.BackColor = Color.White
Case "Black"
Me.BackColor = Color.Black
Case "Gray"
Me.BackColor = Color.Gray
Case "Yellow"
Me.BackColor = Color.Yellow
Case "Pink"
Me.BackColor = Color.Pink
Case "Purple"
Me.BackColor = Color.Purple
Case "Orange"
Me.BackColor = Color.Orange
Case "Gold"
Me.BackColor = Color.Gold
Case "Silver"
Me.BackColor = Color.Silver
End SelectAnd that was actually it, see a very simple thing. Now double check your code and run it, it should change the color of the form accordingly on that what you say on the microphone.
Here is the final code:
Code
'Code by: Electroalek <http://www.electroalek.blogspot.com/>, 2013
Imports System.Speech
Public Class Form1
Dim WithEvents listen As New Recognition.SpeechRecognizer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim gram As New Recognition.SrgsGrammar.SrgsDocument
Dim rule As New Recognition.SrgsGrammar.SrgsRule("word")
Dim list As New Recognition.SrgsGrammar.SrgsOneOf("Red", "Green", "Blue", "White", "Black", "Gray", "Yellow", "Pink", "Purple", "Orange", "Gold", "Silver")
rule.Add(list)
gram.Rules.Add(rule)
gram.Root = rule
listen.LoadGrammar(New Recognition.Grammar(gram))
End Sub
Private Sub speech(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles listen.SpeechRecognized
Select Case (e.Result.Text)
Case "Red"
Me.BackColor = Color.Red
Case "Green"
Me.BackColor = Color.Green
Case "Blue"
Me.BackColor = Color.Blue
Case "White"
Me.BackColor = Color.White
Case "Black"
Me.BackColor = Color.Black
Case "Gray"
Me.BackColor = Color.Gray
Case "Yellow"
Me.BackColor = Color.Yellow
Case "Pink"
Me.BackColor = Color.Pink
Case "Purple"
Me.BackColor = Color.Purple
Case "Orange"
Me.BackColor = Color.Orange
Case "Gold"
Me.BackColor = Color.Gold
Case "Silver"
Me.BackColor = Color.Silver
End Select
End Sub
End Class![[Image: siKw1DH.png]](https://i.imgur.com/siKw1DH.png)
I hope you liked it 😉
[img]http://electroalek.comoj.com/ElectroalekLogo.gif[/img]



