This tutorial is for a very simple speech dictation in VB.net. I created this tutorial to make it easy to get into and understand the Microsoft Speech SDK 5.1.
Requirements:
1. Visual Basic 2005 – I am using Visual Basic 2005 Express. You may get it for free from Microsoft
2. Microsoft Speech SDK 5.1 – This is absolutely required. Also Known as Microsoft SAPI
I've attached and image so you can see the layout, and the element names. It is a very basic layout, with 2 buttons, a label, and a textbox.

Next is the code. The code is commented. I suggest yo thoroughly read through the code, and get a good understanding of it.
'Default Imports Imports System Imports System.Data Imports System.Deployment Imports System.Drawing Imports System.Windows.Forms Imports System.Xml 'Custom Imports Imports SpeechLib Public Class Form1 'Declares Dim WithEvents RecoContext As SpSharedRecoContext 'The Main Recognition Object Used throughout the whole program. -- Shared Object: More Info on this later. Dim Grammar As ISpeechRecoGrammar 'The Grammar Object so the program knows what is going on. -- Instanced Object: More Info on this later. Dim CharCount As Integer 'This is used to count the amount of chars that are in the text box. ''''Subs Start Here 'Start Button. This will engage reco, and start the entire process. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click 'First check to see if reco has been loaded before. If not lets load it. If (RecoContext Is Nothing) Then RecoContext = New SpSharedRecoContextClass 'Create a new Reco Context Class Grammar = RecoContext.CreateGrammar(1) 'Setup the Grammar Grammar.Dictationload() 'Load the Grammar End If lblStatus.Text = "Recognition Started" 'Change the Label to let the user know whats up Grammar.DictationSetState(SpeechRuleState.SGDSActive) 'Turns on the Recognition. This is Vitally important 'This is so the user doesn't break the program by 'trying to start the recognition after its already started. btnStart.Enabled = False btnStop.Enabled = True End Sub '''' 'Stop Button. This will stop stop the recoginition Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click Grammar.DictationSetState(SpeechRuleState.SGDSInactive) 'Turns off the Recognition. It will go dormant. lblStatus.Text = "Recognition Stopped" 'Change the label to let the user know whats up 'Again This is so the user doesn't go breaking things accidently btnStart.Enabled = True btnStop.Enabled = False End Sub '''' 'This is the hypothesis sub. The hypothesis is not the final recognition. This will fire many times per word. You do not want to print anything that is final from the hypothesis. 'This is not required for the final recognition. But it is vital to understand it. Private Sub OnHypo(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles RecoContext.Hypothesis btnStop.Enabled = False 'Don't allow the user to stop the recognition until it has completed. 'The button will re-enable in the OnReco Event 'This is so you don't kepp printing the same text over and over. It could take up just a tiny bit more processor power 'Its good to not do un-needed things. If lblStatus.Text <> "Receiving" Then lblStatus.Text = "Receiving" End If End Sub '''' 'This sub is fired when the reco engine detects a set of words. This is what you want to use to print or sendkey. 'Use this sub for the final printing of words. Private Sub OnReco(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles RecoContext.Recognition Dim recoResult As String = Result.PhraseInfo.GetText 'Create a new string, and assign the recognized text to it. 'This block will print to the textbox built into the program 'If you would prefer to use the SendKeys method, Comment out this entire block. And Uncomment the SendKeys Line. txtBox.Selectionstart = CharCount txtBox.SelectedText = recoResult & " " CharCount = CharCount + 1 + Len(recoResult) 'Uncomment the next line if you want to send the text to the selected window rather than constrain it to the textbox. 'SendKeys.Send(recoResult & " ") 'This line sends the result via SendKeys to the top window. lblStatus.Text = "Finished Dictating" btnStop.Enabled = True End Sub End Class
The code can pretty much explain itself. But I will explain a few things
There are 2 different types of engines. There is the Shared engine. Then there is the Instanced engine.
The Shared engine is used if you have multiple programs running that are using Speech Recognition.
The Instanced engine will start a brand new engine on its own.
(For the sake of this guide, I used the shared engine.)
Note: The grammar cannot be shared, the grammar for each program is instanced.
The grammar can be loaded from a file. This is useful if you want to make commands. But for dictation, the built in grammar is all you really need.
To enable the recognition you will use the same code for both shared and instanced engines.
SpeechRuleState.SGDSActive – This will Enable recognition.
SpeechRuleState.SGDSInactive – This will Disable recognition.
Note: On older versions you did not require the SpeechRuleState. You could simply type in the SGDSEnable/Disable lines, But with VB.net it is required to type in the full line.
On a side note, I suggest you train the speech engine (from the control panel) with at least 2 or 3 different training sessions. This will greatly increase the accuracy of the software.
Have fun, and Code well.






MultiQuote







|