Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become an Expert!

Join 244,033 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,300 people online right now. Registration is fast and FREE... Join Now!




Simple Speech Dictation in VB.Net

 
Reply to this topicStart new topic

> Simple Speech Dictation in VB.Net, Speech SDK and VB.Net Express

ultimateq
Group Icon



post 28 Sep, 2007 - 12:06 PM
Post #1


Very Simple Speech Dictation in VB.net

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.

Attached Image

Next is the code. The code is commented. I suggest yo thoroughly read through the code, and get a good understanding of it.

CODE

'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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

skyhawk133
Group Icon



post 4 Oct, 2007 - 08:33 AM
Post #2
Wouldn't have thought it would be that simple. Amazing what you can do with SDKs.


Vote for this tutorial on DZone: http://www.dzone.com/links/simple_speech_d...n_in_vbnet.html
Go to the top of the page
+Quote Post

samamoni
*



post 7 Feb, 2008 - 04:39 AM
Post #3
QUOTE(skyhawk133 @ 4 Oct, 2007 - 09:33 AM) *

Wouldn't have thought it would be that simple. Amazing what you can do with SDKs.


Vote for this tutorial on DZone: http://www.dzone.com/links/simple_speech_d...n_in_vbnet.html


Samamoni
In this code is it possible to add a simple grammar file of say five words?
I tried the following
instead of Grammar.DictationLoad() I used
Grammar.CmdLoadfrom File (..//pathname of mygrammar. xml file ,SLODynamic)
It is throwing an exception in HRESULT
Could you help me
Go to the top of the page
+Quote Post

ZachR
Group Icon



post 18 Jun, 2008 - 10:14 PM
Post #4
QUOTE(skyhawk133 @ 4 Oct, 2007 - 09:33 AM) *

Wouldn't have thought it would be that simple. Amazing what you can do with SDKs.


Vote for this tutorial on DZone: http://www.dzone.com/links/simple_speech_d...n_in_vbnet.html


SDK's are truly amazing. xD pirate.gif
Go to the top of the page
+Quote Post

jacobjordan
Group Icon



post 22 Jun, 2008 - 12:33 PM
Post #5
Seems easy, but it's not working for me. I set up all the references, literally copied your code in my project, and it doesn't work. Every time i click the btnStart, i get an error on this line

vb

Grammar = RecoContext.CreateGrammar(1) 'Setup the Grammar

The error says "Exception from HRESULT: 0x8004504C".
Go to the top of the page
+Quote Post

Damage
Group Icon



post 26 Jul, 2008 - 03:23 AM
Post #6
how do do you increase the accuracy of this thing? I know Vista has you like "train" their one by reading to it, is there a way to do something similar for this one?
Go to the top of the page
+Quote Post

edwingtr34
*



post 30 Jul, 2008 - 01:05 PM
Post #7
Anyone actually succesfully load the grammar in ? I am actually stuck there now. I wanted to create a simple program - when I say "I like Visual Basic" only the form load the logo, else it will not response. I think setup the XML grammar file right , but no luck I still get the same error that every one got.

Pls help.

If some one can show how to add grammar on XML , just in case i did it wrong and also how to load them in the program code shown here , that is the perfect tutorial for ppl that wanted to learn how to use the SDK well

Thanks
Go to the top of the page
+Quote Post

F-Dog
*



post 24 Sep, 2008 - 01:56 PM
Post #8
Hey, this toturial works! i have a question though. I was trying to get the program to talk back like say "Hello Sir" when i start the program. please tell me if that is posible. Also, if there was a way to configer it so it knows my profile and teh grammer file is acuratly designed just for me as the user cause its not that accurate. it says some realy randome stuff sometimes. please provide me with an answer. thanx biggrin.gif
Go to the top of the page
+Quote Post

ABYEE
*



post 2 Oct, 2008 - 05:11 AM
Post #9
QUOTE(skyhawk133 @ 4 Oct, 2007 - 09:33 AM) *

Wouldn't have thought it would be that simple. Amazing what you can do with SDKs.


Vote for this tutorial on DZone: http://www.dzone.com/links/simple_speech_d...n_in_vbnet.html

its prove really helpful to me if u have some code for voice security plz share with me
thanks !!!!!!!!!!
Go to the top of the page
+Quote Post

stevew
*



post 30 Jun, 2009 - 07:48 AM
Post #10
This is excellent but I only want to use a small list of words. From what I've been reading I don't think we even need to use a grammer file, I think we can do it from a string. I can't figure it out though, does anyone out there know how to do that?

I've searched everywhere and I can only find the old way to do it and those commands (SR.Direct...) aren't supported anymore.


Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 7/3/09 09:48PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month