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"
#Resource Icon xlogo, "..\_icons\microphone3.ico"
%IDC_Body = 500
Global hDlg, vMsg 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, "Speech-To-Text",0,0,400,200, %WS_SysMenu To hDlg
Dialog Set Icon hDlg, "xlogo"
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
vMsg = RegisterWindowMessage("vMessage")
InitializeSpeechRecognition
Case vMsg
If Cb.LParam = 9 Then Dialog End hDlg
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
? "GetText error: " & ObjResult$
Else
If Len(bstrText) Then
Clipboard Reset
If LCase$(bstrText) = "exit" Then
Dialog End hDlg
ElseIf LCase$(bstrText) = "clear" Then
Control Set Text hDlg, %IDC_Body, ""
Else
Control Set Text hDlg, %IDC_Body, bstrText
Clipboard Set Text bstrText
End If
PostMessage %Hwnd_Broadcast, vMsg, 0,1 '1= CC ready to get
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
http://www.garybeene.com/sw/gbsnippets.htm