SAPI 5.4 In Process speech recognition

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Since upgrading from VSEx2010 on 32 bit XP to a new 64 bit Win 8 machine with VSEx2012 Ive been trying to get all my old VB apps working. The only problem Ive had is an app that uses speech recognition. My old code used a shared instance of the recognizer and worked fine in XP. But in Win8 it opens the Microsoft Speech Recognition app whenever it runs. I believe the fix should be to run the recognizer in process. I found some code on MSDN and had to make some modifications to the class syntax so VB was happy with it. But it doesnt work. It never captures any speech. The default input device is the mic and I tried the others just to make sure with no luck.
The original sample code came from:
http://msdn.microsoft.com/en-us/library/ee125184(v=vs.85).aspx
Ive posted my modified sample code below. Imports SpeechLib


Public Class Form1
Dim WithEvents RC As SpInProcRecoContext
Dim Recognizer As SpInprocRecognizer
Dim myGrammar As ISpeechRecoGrammar

Private Sub RC_Recognition(ByVal StreamNumber As Long, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult)
On Error GoTo EH

Label1.Text = Result.PhraseInfo.GetText

EH:
If Err.Number Then ShowErrMsg()
End Sub


Private Sub ShowErrMsg()

Declare identifiers:
Const NL = vbNewLine
Dim T As String

T = "Desc: " & Err.Description & NL
T = T & "Err #: " & Err.Number
MsgBox(T, vbExclamation, "Run-Time Error")
End

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
On Error GoTo EH

RC = New SpInProcRecoContext
Recognizer = RC.Recognizer

myGrammar = RC.CreateGrammar
myGrammar.DictationSetState(SpeechRuleState.SGDSActive)


Dim Category As SpObjectTokenCategory
Category = New SpObjectTokenCategory
Category.SetId(SpeechStringConstants.SpeechCategoryAudioIn)

Dim Token As SpObjectToken
Token = New SpObjectToken
Token.SetId(Category.Default())
Recognizer.AudioInput = Token

EH:
If Err.Number Then ShowErrMsg()
End Sub
End Class

View the full article
 
Back
Top