How to program Voice Commands with XP

How to program Voice Commands with XP

Page 1 of 1

0 Replies - 1019 Views - Last Post: 22 November 2009 - 09:14 AM Rate Topic: -----

#1 codefinger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-October 09

How to program Voice Commands with XP

Post icon  Posted 22 November 2009 - 09:14 AM

This is actually mostly YOUR code (from the dream in code tutorial), but for some reason it does NOT work for me and several other people who also commented. Please I need someone to tell me how to avoid the error. Also, how to create and use a simple grammar file. I only want to use a few simple commands (30 or so), not the whole 60,000 word dictation dictionary.

Public Class Form1
	Dim WithEvents RecoContext As SpSharedRecoContext
	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

	Private Sub Buttonstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonstart.Click

		'The Main Recognition Object Used throughout the whole program. -- Shared Object: More Info on this later.
		Try
			If (RecoContext Is Nothing) Then

				RecoContext = New SpSharedRecoContextClass		  'Create a new Reco Context Class


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


				Grammar.Dictationload(1)							'Load the Grammar


			End If

			lblstatus.Text = "Recognition Started."

			Grammar.DictationSetState(SpeechRuleState.SGDSActive)   'Turns on the Recognition. This is Vitally important
			Buttonstart.Enabled = False
			Buttonstop.Enabled = True

		Catch ex As Exception

			MsgBox(ex.Message)


		End Try



	End Sub

	Private Sub Buttonstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonstop.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
		Buttonstart.Enabled = True
		Buttonstop.Enabled = False

	End Sub
	Private Sub OnHypo(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles RecoContext.Hypothesis

		Buttonstop.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 keep 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"
		Buttonstop.Enabled = True
	End Sub



Just like one of the responders, I am getting a result of
EXCEPTION FROM HRESULT: 0X8004503A when I try to load the grammar file.

Debugging just showed me a little more detail, but not enough to solve the problem:
Error is coming from Grammar.RecoContext.Recognizer.AudioInputStream

when I try to execute this line:

Grammar.Dictationload(1)

I am usiing XP and Visual Studio 2008 and SAPI 5.1.

I have been struggling with this for almost a month.

Please advise ASAP. Thanks in advance for your kind attention.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1