Option Explicit
Dim WithEvents RecoContext As SpSharedRecoContext
Dim Grammar As ISpeechRecoGrammar
Dim m_bRecoRunning As Boolean
Public Sub LoadVoice()
' just a note as we a loading the API
btnStart.Caption = "Loading sAPI..."
DoEvents
' load some custom commands
'Dim addlex As SpUnCompressedLexicon
Dim addlex As SpLexicon
Set addlex = New SpLexicon
'Set addlex = New SpLexicon
Set RecoContext = New SpSharedRecoContext
Set Grammar = RecoContext.CreateGrammar(1)
' load the dictation application
Grammar.DictationLoad
addlex.AddPronunciation "Search", 1 ', SPSNoun
addlex.AddPronunciation "Search Artist", 1 ', SPSNoun
addlex.AddPronunciation "Search Album", 1 ', SPSNoun
addlex.AddPronunciation "Volume Mute", 1 ', SPSNoun
addlex.AddPronunciation "Volume On", 1 ', SPSNoun
'
addlex.AddPronunciation "Volume Up", 1 ', SPSNoun
addlex.AddPronunciation "Volume Down", 1 ', SPSNoun
addlex.AddPronunciation "Volume One Percent", 1 ', SPSNoun
addlex.AddPronunciation "Volume Twenty Percent", 1 ', SPSNoun
addlex.AddPronunciation "Player Play", 1 ', SPSNoun
addlex.AddPronunciation "Player Pause", 1 ', SPSNoun
addlex.AddPronunciation "Player Next", 1 ', SPSNoun
addlex.AddPronunciation "Player Previous", 1 ', SPSNoun
addlex.AddPronunciation "Mode Day", 1 ', SPSNoun
addlex.AddPronunciation "Mode Night", 1 ', SPSNoun
addlex.AddPronunciation "Voice OFF", 1 ', SPSNoun
addlex.AddPronunciation "View rear camera", 1 ', SPSNoun
addlex.AddPronunciation "View front camera", 1 ', SPSNoun
addlex.AddPronunciation "Help", 1 ', SPSNoun
' say the voice commands are now listening
RecoContext.Voice.Speak "Hello. I am " & App.Title & ". " & _
"If you require help just say help. " & _
"Don't forget to press the command button before speaking."
End Sub
Private Sub btnExit_Click()
If m_bRecoRunning = True Then
Grammar.DictationUnload
Set Grammar = Nothing
Set RecoContext = Nothing
End If
Unload Me
End Sub
Private Sub btnListen_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If RecoContext Is Nothing Then LoadVoice
btnListen.BackColor = vbGreen
' Start the dictation.
Grammar.DictationSetState 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
btnListen.BackColor = clBack
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
End Sub