Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe "gbvoicemail.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
Dialog Font "Tahoma",12,0
Dialog New Pixels, 0, "Speech-To-Text",300,300,600,200, %WS_OverlappedWindow To hDlg
Control Add Label, hDlg, %IDC_Body,"Voice Test", 0,0,600,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
? "GetText error: " & ObjResult$
Else
If Len(bstrText) Then
Control Set Text hDlg, %IDC_Body, bstrText 'display all text
oRecoContext.Pause() 'Pause Recognition
oRecoContext.Resume() 'Resume Recognition
End If
End If
End Method
End Interface
End Class
http://www.garybeene.com/sw/gbsnippets.htm