• (2 Pages)
  • +
  • 1
  • 2

Simple Speech Dictation in VB.Net Speech SDK and VB.Net Express Rate Topic: -----

#1 ultimateq  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 19
  • Joined: 25-September 07

Posted 28 September 2007 - 01:06 PM

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.

'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.

Is This A Good Question/Topic? 3
  • +

Replies To: Simple Speech Dictation in VB.Net

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1868
  • View blog
  • Posts: 20,280
  • Joined: 17-March 01

Posted 04 October 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...n_in_vbnet.html
Was This Post Helpful? 0
  • +
  • -

#3 samamoni  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-February 08

Posted 07 February 2008 - 05:39 AM

View Postskyhawk133, on 4 Oct, 2007 - 09:33 AM, said:

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...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
Was This Post Helpful? 0
  • +
  • -

#4 ZachR  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 126
  • Joined: 15-June 08

Posted 18 June 2008 - 11:14 PM

View Postskyhawk133, on 4 Oct, 2007 - 09:33 AM, said:

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...n_in_vbnet.html


SDK's are truly amazing. xD :pirate:
Was This Post Helpful? 0
  • +
  • -

#5 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 22 June 2008 - 01:33 PM

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

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


The error says "Exception from HRESULT: 0x8004504C".
Was This Post Helpful? 1
  • +
  • -

#6 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 28
  • View blog
  • Posts: 1,090
  • Joined: 05-June 08

Posted 26 July 2008 - 04:23 AM

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?
Was This Post Helpful? 0
  • +
  • -

#7 edwingtr34  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-July 08

Posted 30 July 2008 - 02:05 PM

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
Was This Post Helpful? 1
  • +
  • -

#8 F-Dog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-September 08

Posted 24 September 2008 - 02:56 PM

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 :D
Was This Post Helpful? 0
  • +
  • -

#9 ABYEE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 08

Posted 02 October 2008 - 06:11 AM

View Postskyhawk133, on 4 Oct, 2007 - 09:33 AM, said:

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...n_in_vbnet.html

its prove really helpful to me if u have some code for voice security plz share with me
thanks !!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#10 stevew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-June 09

Posted 30 June 2009 - 08:48 AM

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.
Was This Post Helpful? 0
  • +
  • -

#11 Umaid123  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 15-December 08

Posted 30 July 2009 - 01:13 PM

This code works fine for default SAPI dictation but can anyone tell me that how can I load my own custom words or XML grammer file .

I am trying to load it by :
grammer.CmdLoadFromFile("test3.xml", SpeechLoadOption.SLOStatic)

but getting HResult exception, so plz help me over coming this problem. Any assistance will be highly appreciated.

HRESULT: 0x80045048

XML FILE:

<?xml version="1.0" encoding="utf-8" ?>
<GRAMMAR LANGID="409">
<RULE ID="1" NAME="CHECK1" TOPLEVEL="ACTIVE">
<P PROPNAME="request" VAL="meeting">schedule a meeting</P>
<P>with</P>
<L PROPNAME="participants">
<P VAL="AmyAnd">Amy Anderson</P>
<P VAL="tbremer">Ted Bremer</P>
<P VAL="fralee">Frank Lee</P>
<P VAL="crandall">Cynthia Randall</P>
<P VAL="swhite">Suki White</P>
<P VAL="kyoshida">Kim Yoshida</P>
</L>
</RULE>
</GRAMMAR>

View Poststevew, on 30 Jun, 2009 - 07:48 AM, said:

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.

Was This Post Helpful? 0
  • +
  • -

#12 arrazuky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-December 09

Posted 06 December 2009 - 09:08 PM

Hey i've succed in command&control mode,
but it only work for one rule. it only recognize 1 word in rule "maju"(xml file)
but it won't be problem if u can make a good rule. coz i dont' know xml format.
The file path must be write complete:
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 SpeechLib.SpSharedRecoContext() 'SpSharedRecoContextClass	
			Grammar = RecoContext.CreateGrammar(1)			  'Setup the Grammar
		  Grammar.CmdLoadFromFile("C:\Documents and Settings\UserId\my documents\visual studio 2010\Projects\SD\SD\test.xml", SpeechLoadOption.SLOStatic)
		End If

		lblStatus.Text = "Recognition Started"				  'Change the Label to let the user know whats up
	   
	  Grammar.CmdSetRuleState("maju", SpeechRuleState.SGDSActive)
		'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



the xml file is below :

GRAMMAR LANGID="409">
		<RULE NAME="maju" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>maju</P>
		<O>please</O>
	</RULE>
		<RULE NAME="mundur" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>mundur</P>
		<O>please</O>
	</RULE>
		<RULE NAME="kanan" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>khanan</P>
		<O>please</O>
	</RULE>
		<RULE NAME="kiri" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>kiwo</P>
		<O>please</O>
	</RULE>
		<RULE NAME="belok kanan" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>turn right</P>
		<O>please</O>
	</RULE>
		<RULE NAME="belok kiri" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>bee lock kiwo</P>
		<O>please</O>
	</RULE>
		<RULE NAME="berhenti" TOPLEVEl="ACTIVE">
	<O>Please</O>
		<P>stop</P>
		<O>please</O>
	</RULE>
</GRAMMAR>


sorry 4 my bad english

This post has been edited by arrazuky: 06 December 2009 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#13 arrazuky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-December 09

Posted 08 December 2009 - 03:42 AM

it' great i've solved it. if you want to use all rule just set the rule name to be blank : ("")
the code that must be change is :
Grammar.CmdSetRuleState("", SpeechRuleState.SGDSActive)

Was This Post Helpful? 0
  • +
  • -

#14 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Posted 18 December 2009 - 01:18 PM

So useful... XD TY!
Was This Post Helpful? 0
  • +
  • -

#15 Guest_Vasem*


Reputation:

Posted 02 March 2010 - 11:15 PM

Grammar.Dictationload() is giving me an error and it's not loading in VBA...Help!

I am trying the program in a VBA environment...

I would appreciate any help or advice on how to change things to make them work in VBA...for example:

The imports at the beginning did't work in VBA environment, is there an alternative?

Also the Parameters for the subs and the handles keyword...

Thanks
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2