如何使用 SAPI 在 VB6 中启用自由词典语音识别

How to enable free diction speech recognition in VB6 using SAPI

最近我一直在尝试为学校创建一个聊天机器人,我想要的功能之一是语音识别。不幸的是,由于 VB6 的弃用性质,很少有关于使用 SAPI 进行 VB6 语音识别的教程,并且 none 完全没有启用自由词典(简单地说没有语法集并将语音转换为文本)。

Automation Interfaces and Objects (SAPI 5.4) 有文档。

简单的例子:

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