VB.net with Speech SDK Reco

Voice Recognition in VB.net

Page 1 of 1

6 Replies - 12492 Views - Last Post: 30 July 2008 - 07:29 PM 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

VB.net with Speech SDK Reco

Posted 25 September 2007 - 03:17 PM

Greetings!

First of all, Thanks for your help.

I am looking for just a little bit of help to nudge me in the right direction.

I am experimenting with the Microsoft Speech SDK 5.1, with Visual Basic.net Express.

I have already added:
Imports SpeechLib


and I understand the text to speech:
Dim Voice As New SpeechLib.SpVoice
Voice.Speak("Hello World!")


Now I just need some basic help with the Voice Recognition.

I did some searching and found some older resources like (Link)

However this article does not do me much good since I am using vb.net. The article posted seems to use vb6.

I have also looked at the examples included with the SDK, However I find them confusing.

Thank you in advance for any help regarding this matter.

This post has been edited by ultimateq: 25 September 2007 - 03:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VB.net with Speech SDK Reco

#2 ultimateq  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: VB.net with Speech SDK Reco

Posted 25 September 2007 - 04:07 PM

Also I have what I believe is called the Grammar in a set of arrays that I am loading from an xml file.

So really just experimenting with the voice recognition with just 1 basic command is all I need. I am sure I can figure out the rest.

I guess just a basic command will do. Perhaps:
		Me.Close() 

Was This Post Helpful? 0
  • +
  • -

#3 ultimateq  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: VB.net with Speech SDK Reco

Posted 25 September 2007 - 05:00 PM

Well after digging around for another 2 hours using dozens of different google search phrases I found:

This

I believe this will help me plenty.

Edit: Nevermind this does not work. I am betting he is using an old version of the Speech SDK. But for some his post is not working for me.
I do not have the vCommand class.

This post has been edited by ultimateq: 25 September 2007 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 ultimateq  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: VB.net with Speech SDK Reco

Posted 27 September 2007 - 12:01 AM

Well I've made some progress. Maybe someone can debug and tell me exactly how to fix my problem.

I am experiementing with the VB Samples that come with the Speech SDK 5.1. The example I am messing around with is "VB SimpleDict".

Mind you I am more interested in making commands more than dictation, but I think I can figure it out once I understand this a little bit better.

Ok So I started a brand new project.

Added a TextBox(MultiLine) named "txtSpeech"
Added a button named "btnStart"
Added another button named "btnStop"

I also Imported the Com Reference "Microsoft Speech Object Library".

That is all for the interface, and references.

Here is the complete code adapted from the sample project.
Option Explicit On
Public Class Form1
	'=============================================================================
	' This sample demonstrates how to do simple dictation in VB with SAPI 5.1.
	'
	' It uses shared reco context object, uses the default audio input, loads in
	' dictation grammar, sets up event handlers, and shows the recognized text in
	' the dialog text box.
	'
	' Note: since the text box is using system locale, it may not correctly show
	' characters in other languages. For example, if you use Chinese Speech
	' Recognition engine as the default engine on your English OS, the text box
	' may show garbage even though the engine recognizes Chinese.
	'
	' Copyright @ 2001 Microsoft Corporation All Rights Reserved.
	'
	'=============================================================================


	Dim WithEvents RecoContext As SpeechLib.SpSharedRecoContext
	'Attribute RecoContext.VB_VarHelpID = -1
	Dim Grammar As SpeechLib.ISpeechRecoGrammar

	Dim m_bRecoRunning As Boolean
	Dim m_cChars As Integer


	Private Sub Form1_Load()
		SetState(False)
		m_cChars = 0
	End Sub


	' This function handles Recognition event from the reco context object.
	' Recognition event is fired when the speech recognition engines recognizes
	' a sequences of words.
	Private Sub RecoContext_Recognition(ByVal StreamNumber As Long, _
										ByVal StreamPosition As Object, _
										ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
										ByVal Result As SpeechLib.ISpeechRecoResult _
										)
		Dim strText As String
		strText = Result.PhraseInfo.GetText
		Debug.Print("Recognition: " & strText & ", " & _
			StreamNumber & ", " & StreamPosition)

		' Append the new text to the text box, and add a space at the end of the
		' text so that it looks better
		txtSpeech.Selectionstart = m_cChars
		txtSpeech.SelectedText = strText & " "
		m_cChars = m_cChars + 1 + Len(strText)
	End Sub

	' This function handles the state of Start and Stop buttons according to
	' whether dictation is running.
	Private Sub SetState(ByVal bNewState As Boolean)
		m_bRecoRunning = bNewState
		btnStart.Enabled = Not m_bRecoRunning
		btnStop.Enabled = m_bRecoRunning
	End Sub

	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		Debug.Assert(Not m_bRecoRunning)

		' Initialize recognition context object and grammar object, then
		' start dictation
		If (RecoContext Is Nothing) Then
			Debug.Print("Initializing SAPI reco context object...")
			RecoContext = New SpeechLib.SpSharedRecoContextClass
			Grammar = RecoContext.CreateGrammar(1)
			Grammar.Dictationload()
		End If

		Grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive)
		SetState(True)
	End Sub

	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		Debug.Assert(m_bRecoRunning)
		Grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive)
		SetState(False)
	End Sub
End Class



Now I have added breaks on the button presses, and the "RecoContext_Recognition" Subs.

Everything seems to take off without a hitch on the button presses, However the RecoContext_Recognition sub doesn't seem to fire off at all.

I'm not sure how to remedy this issue. Any insight as to how to fix this would be helpful.
Was This Post Helpful? 0
  • +
  • -

#5 ultimateq  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: VB.net with Speech SDK Reco

Posted 27 September 2007 - 02:17 PM

Well I got it working mostly! I am very happy. Because I am beginning to understand this. I am hoping that someone out there will be able to figure out my last problem. Because I've no idea why its even doing this.

The problem is, it seems to print the text multiple times. Sometimes it does it 3 times sometimes it seems to do it 5 times. Here is my entire code again. With all but this 1 problem fixed.

Added a TextBox(MultiLine) named "txtSpeech"
Added a button named "btnStart"
Added another button named "btnStop"

Option Explicit On
Public Class Form1

	'Declares
	Dim WithEvents RecoContext As SpeechLib.SpSharedRecoContext
	Dim Grammar As SpeechLib.ISpeechRecoGrammar

	Dim m_bRecoRunning As Boolean
	Dim m_cChars As Integer


	'Start Button. Engages on Click
	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		Debug.Assert(Not m_bRecoRunning)

		' Initialize recognition context object and grammar object, then
		' start dictation
		If (RecoContext Is Nothing) Then
			Debug.Print("Initializing SAPI reco context object...")
			RecoContext = New SpeechLib.SpSharedRecoContextClass
			Grammar = RecoContext.CreateGrammar(1)
			Grammar.Dictationload()
		End If

		Grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive)
		SetState(True)
	End Sub

	'Stop Button. Engages On Click
	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		Debug.Assert(m_bRecoRunning)
		Grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSInactive)
		SetState(False)
	End Sub

	'Form Load Sub
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'RecoContext.VB_VarHelpID = -1
		SetState(False)
		m_cChars = 0
	End Sub


	' This function handles Recognition event from the reco context object.
	' Recognition event is fired when the speech recognition engines recognizes
	' a sequences of words.
	Private Sub RecoContext_Recognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As SpeechLib.ISpeechRecoResult) Handles RecoContext.Hypothesis
		Dim strText As String
		strText = Result.PhraseInfo.GetText

		' Append the new text to the text box, and add a space at the end of the
		' text so that it looks better
		txtSpeech.Selectionstart = m_cChars
		txtSpeech.SelectedText = strText & " "
		m_cChars = m_cChars + 1 + Len(strText)
	End Sub

	' This function handles the state of Start and Stop buttons according to
	' whether dictation is running.
	Private Sub SetState(ByVal bNewState As Boolean)
		m_bRecoRunning = bNewState
		btnStart.Enabled = Not m_bRecoRunning
		btnStop.Enabled = m_bRecoRunning
	End Sub
End Class



The Problematic code seems to be here.
' This function handles Recognition event from the reco context object.
	' Recognition event is fired when the speech recognition engines recognizes
	' a sequences of words.
	Private Sub RecoContext_Recognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As SpeechLib.ISpeechRecoResult) Handles RecoContext.Hypothesis
		Dim strText As String
		strText = Result.PhraseInfo.GetText

		' Append the new text to the text box, and add a space at the end of the
		' text so that it looks better
		txtSpeech.Selectionstart = m_cChars
		txtSpeech.SelectedText = strText & " "
		m_cChars = m_cChars + 1 + Len(strText)
	End Sub


It seems to be getting the hypothesis 3 times. Yet I'm not sure why. If I add a
Debug.Print
line in there. The print shows up 3 times, showing me thats its going through the sub 3 times. For some unknown reason.

This post has been edited by ultimateq: 27 September 2007 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#6 ultimateq  Icon User is offline

  • New D.I.C Head
  • member icon

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

Re: VB.net with Speech SDK Reco

Posted 27 September 2007 - 04:35 PM

I got it working. The RecoContext.Hypothesis was the problem. it needed to be RecoContext.Recognition. Here is the full working code. With an extra label to display the status of what is happening. I will attempt to write a tutorial to introduce dictation to VB.net. It seems to be much more simple than it looks.


Added a TextBox(MultiLine) named "txtSpeech"
Added a button named "btnStart"
Added another button named "btnStop"
Added a Label Named "Label1"

Option Explicit On
Imports SpeechLib
Public Class Form1

	'Declares
	Dim WithEvents RecoContext As SpSharedRecoContext
	Dim Grammar As ISpeechRecoGrammar

	Dim m_bRecoRunning As Boolean
	Dim m_cChars As Integer

	'Start Button. Engages on Click
	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		Debug.Assert(Not m_bRecoRunning)

		' Initialize recognition context object and grammar object, then
		' start dictation
		If (RecoContext Is Nothing) Then
			Debug.Print("Initializing SAPI reco context object...")
			RecoContext = New SpSharedRecoContextClass
			Grammar = RecoContext.CreateGrammar(1)
			Grammar.Dictationload()
		End If

		Label1.Text = "Recognition Started"
		Grammar.DictationSetState(SpeechRuleState.SGDSActive)

		SetState(True)
	End Sub

	'Stop Button. Engages On Click
	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		Debug.Assert(m_bRecoRunning)
		Grammar.DictationSetState(SpeechRuleState.SGDSInactive)
		SetState(False)
		Debug.Print("Stopped")
		Label1.Text = "Recognition Stopped"
	End Sub

	'Form Load Sub
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'RecoContext.VB_VarHelpID = -1
		SetState(False)
		m_cChars = 0
	End Sub

	Private Sub OnHypothesis(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As ISpeechRecoResult) Handles RecoContext.Hypothesis
		Label1.Text = "Receiving"
	End Sub

	' This function handles Recognition event from the reco context object.
	' Recognition event is fired when the speech recognition engines recognizes
	' a sequences of words.
	Private Sub OnRecognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles RecoContext.Recognition

		Dim strText As String
		strText = Result.PhraseInfo.GetText

		' Append the new text to the text box, and add a space at the end of the
		' text so that it looks better
		txtSpeech.Selectionstart = m_cChars
		txtSpeech.SelectedText = strText & " "
		m_cChars = m_cChars + 1 + Len(strText)

		Debug.Print(StreamNumber)
		Label1.Text = "Finished Dictating"

	End Sub

	' This function handles the state of Start and Stop buttons according to
	' whether dictation is running.
	Private Sub SetState(ByVal bNewState As Boolean)
		m_bRecoRunning = bNewState
		btnStart.Enabled = Not m_bRecoRunning
		btnStop.Enabled = m_bRecoRunning
	End Sub
End Class


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

Re: VB.net with Speech SDK Reco

Posted 30 July 2008 - 07:29 PM

can you please share how did you manage to import the XML file to the code so that you can say "CLOSE WINDOW" then you code will jsut go to ME.Close() . Like what you were mentioning before in the previous post.

I am also suffering the same issue now. i got it to dictate, But I am having problem of creating a command to do something in the code.

I tried using the cmdloadfromfile("pathname") and it still giving me error

please help




View Postultimateq, on 27 Sep, 2007 - 02:17 PM, said:

Well I got it working mostly! I am very happy. Because I am beginning to understand this. I am hoping that someone out there will be able to figure out my last problem. Because I've no idea why its even doing this.

The problem is, it seems to print the text multiple times. Sometimes it does it 3 times sometimes it seems to do it 5 times. Here is my entire code again. With all but this 1 problem fixed.

Added a TextBox(MultiLine) named "txtSpeech"
Added a button named "btnStart"
Added another button named "btnStop"

Option Explicit On
Public Class Form1

	'Declares
	Dim WithEvents RecoContext As SpeechLib.SpSharedRecoContext
	Dim Grammar As SpeechLib.ISpeechRecoGrammar

	Dim m_bRecoRunning As Boolean
	Dim m_cChars As Integer


	'Start Button. Engages on Click
	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		Debug.Assert(Not m_bRecoRunning)

		' Initialize recognition context object and grammar object, then
		' start dictation
		If (RecoContext Is Nothing) Then
			Debug.Print("Initializing SAPI reco context object...")
			RecoContext = New SpeechLib.SpSharedRecoContextClass
			Grammar = RecoContext.CreateGrammar(1)
			Grammar.Dictationload()
		End If

		Grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive)
		SetState(True)
	End Sub

	'Stop Button. Engages On Click
	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		Debug.Assert(m_bRecoRunning)
		Grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSInactive)
		SetState(False)
	End Sub

	'Form Load Sub
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'RecoContext.VB_VarHelpID = -1
		SetState(False)
		m_cChars = 0
	End Sub


	' This function handles Recognition event from the reco context object.
	' Recognition event is fired when the speech recognition engines recognizes
	' a sequences of words.
	Private Sub RecoContext_Recognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As SpeechLib.ISpeechRecoResult) Handles RecoContext.Hypothesis
		Dim strText As String
		strText = Result.PhraseInfo.GetText

		' Append the new text to the text box, and add a space at the end of the
		' text so that it looks better
		txtSpeech.Selectionstart = m_cChars
		txtSpeech.SelectedText = strText & " "
		m_cChars = m_cChars + 1 + Len(strText)
	End Sub

	' This function handles the state of Start and Stop buttons according to
	' whether dictation is running.
	Private Sub SetState(ByVal bNewState As Boolean)
		m_bRecoRunning = bNewState
		btnStart.Enabled = Not m_bRecoRunning
		btnStop.Enabled = m_bRecoRunning
	End Sub
End Class



The Problematic code seems to be here.
' This function handles Recognition event from the reco context object.
	' Recognition event is fired when the speech recognition engines recognizes
	' a sequences of words.
	Private Sub RecoContext_Recognition(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal Result As SpeechLib.ISpeechRecoResult) Handles RecoContext.Hypothesis
		Dim strText As String
		strText = Result.PhraseInfo.GetText

		' Append the new text to the text box, and add a space at the end of the
		' text so that it looks better
		txtSpeech.Selectionstart = m_cChars
		txtSpeech.SelectedText = strText & " "
		m_cChars = m_cChars + 1 + Len(strText)
	End Sub


It seems to be getting the hypothesis 3 times. Yet I'm not sure why. If I add a
Debug.Print
line in there. The print shows up 3 times, showing me thats its going through the sub 3 times. For some unknown reason.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1