Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe "voice_recognition.exe"
#Dim All
%Unicode = 1
#Include "Win32API.inc"
#Include "sapi.inc"
%IDC_Body = 500
Global hDlg As Dword
Global SpVoice As ISpVoice
Global oRecoContext As ISpeechRecoContext
Global oRecognizer As ISpeechRecognizer
Global oMyGrammar As ISpeechRecoGrammar
Global oCategory As ISpeechObjectTokenCategory
Global oToken As ISpeechObjectToken
Global InProcEvents As ISpeechRecoContextEventsImplemented
Function PBMain() As Long
If IsRunning(Exe.Name$) Then Beep : Exit Function
Dialog Font "Tahoma",12,0
Dialog New Pixels, 0, "Voice Recognition",0,0,400,200, %WS_SysMenu To hDlg
Control Add Label, hDlg, %IDC_Body,"... waiting ...", 0,0,400,200
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
InitializeSpeechRecognition
End Select
End Function
Sub InitializeSpeechRecognition
oRecoContext = NewCom "SAPI.SpInProcRecoContext" 'Create an instance of the ISpeechRecoContext Interface
InProcEvents = Class "CISpeechRecoContextEventsImplemented" 'Link the events of oRecoContext to InProcEvents to process a recognition event.
Events From oRecoContext Call InProcEvents
oRecognizer = oRecoContext.Recognizer 'Create the InProc Speech Recognizer.
oMyGrammar = oRecoContext.CreateGrammar(1) 'Create the InProc Speech Grammar.
oMyGrammar.State = %SGSDisabled 'Disable Grammar while loading it.
oMyGrammar.DictationLoad("", %SLOstatic) 'Load the default Dictation Grammar.
oMyGrammar.State = %SGSEnabled 'Enable Grammar after loading it.
oMyGrammar.DictationSetState(%SGDSInactive) 'Turn Dictation off.
oCategory = NewCom "SAPI.SpObjectTokenCategory" 'Create the Audio Token Category.
oCategory.SetId("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput") 'Set the Audio Token category ID.
oToken = NewCom "SAPI.SpObjectToken" 'Create the Audio Token.
oToken.SetId(oCategory.Default) 'Set the Token Category ID.
oRecognizer.PutRef_AudioInput = oToken 'Give the Recognizer the Token.
oMyGrammar.DictationSetState(%SGDSActive) 'Start the recognition by turning Dictation back on.
'oRecoContext.Recognizer.EmulateRecognition("Recognition has started") 'Emmulate recognition to test the interface.
spVoice = NewCom $PROGID_SpVoice1 'Create an instance of the ISpeechVoice Interface.
End Sub
Class CISpeechRecoContextEventsImplemented Guid$("{5B344ADB-C0C7-4B5F-8046-7D2DB91A1D75}") As Event
' ########################################################################################
' Class CISpeechRecoContextEvents
' Interface name = _ISpeechRecoContextEvents
' IID = {7B8FCB42-0E9D-4F00-A048-7B04D6179D3D}
' Attributes = 4096 [&H1000] [Dispatchable]
' ########################################################################################
Interface ISpeechRecoContextEventsImplemented Guid$("{7B8FCB42-0E9D-4F00-A048-7B04D6179D3D}") As Event
Inherit IDispatch
Method Recognition <7> ( _
ByVal StreamNumber As Long _ ' __in long StreamNumber
, ByVal StreamPosition As Variant _ ' __in VARIANT StreamPosition
, ByVal RecognitionType As Long _ ' __in SpeechRecognitionType RecognitionType
, ByVal Result As ISpeechRecoResult _ ' __in ISpeechRecoResult *Result
) ' void
Local pDisp As IDispatch, bstrText As WString
If IsNothing(Result) Then Exit Method
pDisp = Result
Object Call pDisp.PhraseInfo.GetText To bstrText
If ObjResult Then
Beep : Control Set Text hDlg, %IDC_Body, "GetText error: " & ObjResult$
Else
If Len(bstrText) Then
RespondToCommands(LCase$(bstrText))
oRecoContext.Pause() 'Pause Recognition
oRecoContext.Resume() 'Resume Recognition
End If
End If
End Method
End Interface
End Class
Function IsRunning(EXEName As WStringZ * %Max_Path) As Long
'returns 0 if instance exits or cannot create mutex. otherwise returns 1.
Local UniqueName As WStringZ * %Max_Path
UniqueName = EXEName
If CreateMutex(ByVal %Null, 0, UniqueName) = 0 _
Or GetLastError = %ERROR_ALREADY_EXISTS Then Function = 1 : Exit Function
End Function
Sub RespondToCommands(sText$)
Local iResult As Long
Control Set Text hDlg, %IDC_Body, sText$
Select Case sText$
Case "exit"
Dialog End hDlg
Case "clear"
Control Set Text hDlg, %IDC_Body, ""
Case "open excel"
iResult = Shell("C:\Program Files\Microsoft Office\root\Office16\excel.exe",1)
Case "open word"
iResult = Shell("C:\Program Files\Microsoft Office\root\Office16\winword.exe",1)
Case "open google", "opengl"
iResult = ShellExecute(hDlg, "Open", "http://www.google.com", $Nul, $Nul, %SW_Maximize)
End Select
End Sub
http://www.garybeene.com/sw/gbsnippets.htm