' btnStart: Loads Engine - calling loadvoice, which calls loadgrammar - if loaded it unloads it
' btnlisten: if the engine is loaded - enables / disables VC listening - if engine is unloaded it loads it first
Option Explicit
' the actual control we are using
Dim WithEvents RecoContext As SpSharedRecoContext
' the grammar we are working from
Dim Grammar As ISpeechRecoGrammar
' weather or not the grammar and recognition engine are loaded
Dim m_bRecoRunning As Boolean
' < this will b false if the engine is not loaded, but it if the grammar fails to load also...
Dim bGrammarLoaded As Boolean
' to load the voice rec engine
Public Function LoadVoice() As Boolean
On Error GoTo ErrHand:
' just a note as we a loading the API
btnStart.Caption = "Loading sAPI..."
DoEvents
' create a new engine
Set RecoContext = New SpSharedRecoContext
' make a new grammar file to edit
Set Grammar = RecoContext.CreateGrammar(0)
' Load Dictation but set it to Inactive
Grammar.DictationLoad "", SLOStatic
Grammar.DictationSetState SGDSInactive
' Attempt to load the default .xml file and set the RuleId State to Inactive until
' the user starts recognition.
'bGrammarLoaded = LoadDefaultCnCGrammar(App.Path & "\Grammar.xml")
'bGrammarLoaded = LoadDefaultCnCGrammar("E:\Documents and Settings\Wayne\Desktop\Carstra Media Player\speech rec\sapitutorial00\sapi0.xml")
bGrammarLoaded = LoadDefaultCnCGrammar(App.Path & "\sapi0.xml")
' if the grammar file was loaded - it may fail if the syntax is messed up
If bGrammarLoaded = True Then
' say that we are now running.
LoadVoice = True
' you may wish to inform the user that voice activation is enabled now...
' .speak is SLOW for me on my user machine, so im using it only when necessary
With RecoContext.Voice
'.Speak "Hello. I am " & App.Title & ". "
'.Speak "If you require help just say help. "
'.Speak "Don't forget to press the command button before speaking."
End With
Else
' if the file didnt load raise an error
Err.Raise -1
End If
Exit Function
ErrHand:
On Error Resume Next
' destry the things created
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
btnStart.Caption = "Error: Failed loading voice"
' and return that we failed to load the engine
LoadVoice = False
Err.Clear
End Function
Private Function LoadDefaultCnCGrammar(sFilepath As String) As Boolean
On Error Resume Next
' First load attempt
Grammar.CmdLoadFromFile sFilepath, SLODynamic
' Second load attempt
If Err Then
On Error GoTo Err_CFGLoad
Grammar.CmdLoadFromFile sFilepath, SLODynamic
End If
' Set rule state to inactive until user clicks Recognition button
Grammar.CmdSetRuleIdState 0, SGDSInactive
LoadDefaultCnCGrammar = True
Exit Function
Err_CFGLoad:
LoadDefaultCnCGrammar = False
Exit Function
End Function
' i use a frame control to enable and disable recognition - using mouse down and up
Private Sub btnListen_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If RecoContext Is Nothing Then if not LoadVoice then beep: Exit
btnListen.BackColor = vbGreen
' Start the dictation. - this is if your using normal dictation and not a grammar XML
'Grammar.DictationSetState SGDSActive
' listen for the rules in XML file - this is what we want...
Grammar.CmdSetRuleIdState 0, SGDSActive
Debug.Print "mouse down event..."
End Sub
Private Sub btnListen_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Debug.Print "mouse up event..."
' stop it
'Grammar.DictationSetState SGDSInactive
Grammar.CmdSetRuleIdState 0, SGDSInactive
btnListen.BackColor = BackColor
End Sub
Private Sub btnStart_Click()
' check if voice recognition is running.
Select Case m_bRecoRunning
' if it is running
Case True
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
m_bRecoRunning = False
' if not started
Case False:
' load the speech engine
m_bRecoRunning = LoadVoice
If Not m_bRecoRunning Then btnStart.Caption = "Cannot Start!": Exit Sub
End Select
' make a note of voice command state
btnStart.Caption = IIf(m_bRecoRunning, "*** ON ***", "- OFF -")
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 Variant, _
ByVal RecognitionType As SpeechRecognitionType, _
ByVal Result As ISpeechRecoResult _
)
Dim strText As String
strText = Result.PhraseInfo.GetText
Debug.Print "Recognition: " & strText & ", " & _
StreamNumber & ", " & StreamPosition
' i plan to handle like:
' shandletext() = split(strtext," ")
' select case shandletext(0) -> case "Search" ETC - im sure you get the idea...
End Sub
Private Sub Form_Unload(Cancel As Integer)
' i try to always clean up after myself... frees the resources incase its not done for me
If m_bRecoRunning = True Then
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
End If
End Sub