ISpeechRecoResult PhraseInfo Property (SAPI 5.4)

Speech API 5.4
Microsoft Speech API 5.4

Interface: ISpeechRecoResult

PhraseInfo Property

The PhraseInfo property returns an ISpeechPhraseInfo structure containing detailed information about the last recognized phrase.

ISpeechPhraseInfo elements contains read-only data about timing for the phrase and audio stream, elements (words and phrases) in the recognized phrase, grammar and grammar rules information. The structure is used to examine the recognition information. It is also used by other calls such as ISpeechRecoResult.PhraseInfo.GetText to retrieve the phrase text.


Set: (This property is read-only)
Get: ISpeechPhraseInfo = ISpeechRecoResult.PhraseInfo


The owning object.
Set: (This property is read-only)
Get: An ISpeechPhraseInfo variable that gets the property.


The following Visual Basic form code reads the PhraseInfo property of a RecoResult and parses out selected information about stream times and element times and displays the results. Although the preferred method of retrieving the text uses ISpeechRecoResult.GetText, this code not only retrieves the text on an element by element basis, but also retrieves the stream time associated with the word.

To run this code, paste it into the Declarations section of a form that contains no controls. In addition to the usual reference to the Microsoft Speech Object Library, this code also needs a reference to the simpleaudio 1.0 Type Library.

Option Explicit


' Text-to-Speech variables:
Dim WithEvents Voice As SpVoice
Dim EndofStream As Boolean
Dim AudioPlugOut As SpAudioPlug

' Speech Recognition variables:
Dim WithEvents RecoContext As SpInProcRecoContext
Dim Grammar As ISpeechRecoGrammar
Dim Recognizer As SpInprocRecognizer
Dim AudioPlugIn As SpAudioPlug

Private Sub Form_Load()

    Const Text = "One of the world's seven wonders"
    Dim Output As Variant

    On Error GoTo EH

    Set Voice = New SpVoice

    ' Set up output audio:
    Set AudioPlugOut = New SpAudioPlug
    AudioPlugOut.Init True, AUDIOFORMAT
    Set Voice.AudioOutputStream = AudioPlugOut

    ' Set up input audio:
    Set AudioPlugIn = New SpAudioPlug
    AudioPlugIn.Init False, AUDIOFORMAT
    Set Recognizer = New SpInprocRecognizer
    Set Recognizer.AudioInputStream = AudioPlugIn

    ' Set up speech recognition and explicitly set
    ' flag to retain audio portion of recognition
    ' (default behavior is not to retain):
    Set RecoContext = Recognizer.CreateRecoContext
    RecoContext.RetainedAudio = SRAORetainAudio
    Set Grammar = RecoContext.CreateGrammar(1)
    Grammar.DictationSetState SGDSActive

    ' Speak some text to be recognized.
    Voice.Speak Text, SVSFlagsAsync

    Do While EndofStream = False

        ' Get audio data from audio object.
        Output = AudioPlugOut.GetData

        ' Output audio data to input audio object--
        If (Len(Output) * 2 <> 0) Then
            AudioPlugIn.SetData (Output)
        End If


    Grammar.DictationSetState SGDSInactive

    If Err.Number Then ShowErrMsg
End Sub

Private Sub RecoContext_Recognition _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)

    Const NL = vbNewLine
    Dim i As Integer
    Dim T As String

    On Error GoTo EH

    With Result.PhraseInfo
        T = T & "LangID= " & .LanguageId & NL
        T = T & "AudioBytes= " & .AudioSizeBytes & NL
        T = T & "AudioTime= " & .AudioSizeTime & NL

        For i = 0 To .Elements.Count - 1
            T = T & "Stream offset:" & .Elements(i).AudioStreamOffset & NL
            T = T & "Text form: " & .Elements(i).DisplayText & NL
            T = T & "Lex form: " & .Elements(i).LexicalForm & NL
    End With

    MsgBox T, vbInformation

    If Err.Number Then ShowErrMsg
End Sub

Private Sub Voice_EndStream _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant)

    EndofStream = True

End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Dim T As String

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

End Sub