Share via


VB Application Sample: Dictation Recognition (Inproc) (SAPI 5.3)

Microsoft Speech API 5.3

VB Application Sample: Dictation Recognition (Inproc)

The following code sample represents a simple, but functional, recognition application, using the in process (or InProc) recognizer. It uses a dictation grammar and allows free dictation. The commented lines refer to hypothetical labels in a form to possibly display information. To see the recognized phrase, add one label, named Label1. Of course you may modify this application as needed to fit your own requirements.

Before running the application, a speech reference must be included. Using the Project->References menu, find and select the Microsoft Speech Object Library.

An InProc recognizer requires additional lines that shared recognizers do not. For InProc recognizers, the audio object for either input or output must be explicitly assigned.

  
Dim WithEvents RC As SpInProcRecoContext
Dim Recognizer As SpInprocRecognizer
Dim myGrammar As ISpeechRecoGrammar

Private Sub Form_Load()
    On Error GoTo EH

    Set RC = New SpInProcRecoContext
    Set Recognizer = RC.Recognizer

    Set myGrammar = RC.CreateGrammar
    myGrammar.DictationSetState SGDSActive

    Dim Category As SpObjectTokenCategory
    Set Category = New SpObjectTokenCategory
    Category.SetId SpeechCategoryAudioIn

    Dim Token As SpObjectToken
    Set Token = New SpObjectToken
    Token.SetId Category.Default()
    Set Recognizer.AudioInput = Token

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub RC_Recognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult)
    On Error GoTo EH

    Label1.Caption = Result.PhraseInfo.GetText

EH:
    If Err.Number Then ShowErrMsg
End Sub


Private Sub ShowErrMsg()

    ' Declare identifiers:
    Const NL = vbNewLine
    Dim T As String

    T = "Desc: " & Err.Description & NL
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub