There is much more you can do than is shown in this tiny example, which uses the first audio input source found and uses defaults for many other things (such as free dictation).
Code:
Option Explicit
'See "Automation Interfaces and Objects (SAPI 5.4)" at MSDN.
Private WithEvents RC As SpeechLib.SpInProcRecoContext
Private RG As SpeechLib.ISpeechRecoGrammar
Private Sub Form_Load()
With New SpeechLib.SpInprocRecognizer
Set RC = .CreateRecoContext()
Set .AudioInput = .GetAudioInputs().Item(0)
End With
With RC
.EventInterests = SRERecognition Or SREFalseRecognition
Set RG = .CreateGrammar()
End With
RG.DictationSetState SGDSActive
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
Text1.Move 0, 0, ScaleWidth, ScaleHeight
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
RG.DictationSetState SGDSInactive
End Sub
Private Sub RC_FalseRecognition( _
ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal Result As SpeechLib.ISpeechRecoResult)
With Text1
.SelStart = &H7FFF
.SelText = "False Rec: "
.SelText = Result.PhraseInfo.GetText()
.SelText = vbNewLine
End With
End Sub
Private Sub RC_Recognition( _
ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
ByVal Result As SpeechLib.ISpeechRecoResult)
With Text1
.SelStart = &H7FFF
.SelText = "Rec: "
.SelText = Result.PhraseInfo.GetText()
.SelText = vbNewLine
End With
End Sub
SAPI 5.4 requires the dying Windows 7 or later. SAPI 5.3 is highly compatible on the dead Windows Vista. Those are part of Windows and preinstalled. You may limp along even on the dead Windows XP, 98, 2000, etc. if you install the SAPI 5.1 SDK. SAPI 5.2 was a special release only used on an old MS Speech Server product.
Man, I remember playing with this stuff so many years ago when MS offered it up. Pain to use at that time because you had to train the speech engine for you and if the mike wasn't close enough -- good luck. Maybe it's gotten a lot better. I do remember writing a sample project that activated mouse actions, like move and click and also opening preset apps like Windows explorer, IE, etc.
Insomnia is just a byproduct of, "It can't be done"
If you don't do the training then recognition can be poor. A big part of having the mic close is to overcome echos and background noises.
I'm not sure that it is viable or we'd see it used a lot more. We usually don't have good microphones positioned well enough for this. I think it is better, but probably only by inches.
There is much more you can do than is shown in this tiny example, which uses the first audio input source found and uses defaults for many other things (such as free dictation).
Code:
Option Explicit
'See "Automation Interfaces and Objects (SAPI 5.4)" at MSDN.
Private WithEvents RC As SpeechLib.SpInProcRecoContext
Private RG As SpeechLib.ISpeechRecoGrammar
Private Sub Form_Load()
With New SpeechLib.SpInprocRecognizer
Set RC = .CreateRecoContext()
Set .AudioInput = .GetAudioInputs().Item(0)
End With
With RC
.EventInterests = SRERecognition Or SREFalseRecognition
Set RG = .CreateGrammar()
End With
RG.DictationSetState SGDSActive
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
Text1.Move 0, 0, ScaleWidth, ScaleHeight
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
RG.DictationSetState SGDSInactive
End Sub
Private Sub RC_FalseRecognition( _
ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal Result As SpeechLib.ISpeechRecoResult)
With Text1
.SelStart = &H7FFF
.SelText = "False Rec: "
.SelText = Result.PhraseInfo.GetText()
.SelText = vbNewLine
End With
End Sub
Private Sub RC_Recognition( _
ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
ByVal Result As SpeechLib.ISpeechRecoResult)
With Text1
.SelStart = &H7FFF
.SelText = "Rec: "
.SelText = Result.PhraseInfo.GetText()
.SelText = vbNewLine
End With
End Sub
SAPI 5.4 requires the dying Windows 7 or later. SAPI 5.3 is highly compatible on the dead Windows Vista. Those are part of Windows and preinstalled. You may limp along even on the dead Windows XP, 98, 2000, etc. if you install the SAPI 5.1 SDK. SAPI 5.2 was a special release only used on an old MS Speech Server product.
Results vary with different microphones, ambient noise, room acoustics, and mic placement. There is a speech recognition training feature in Control Panel too that might help.