Share via


ISpeechRecoResult DiscardResultInfo Method (SAPI 5.3)

Microsoft Speech API 5.3

Interface: ISpeechRecoResult

DiscardResultInfo Method

The DiscardResultInfo method discards the requested data from a phrase object.

Applications that have no use for certain types of retained data, and will be persisting or serializing the phrase or result objects, may discard unnecessary data.

For example, an application performing offline transcription may need to retain only the audio and the final result. It can remove the alternates with object.DiscardResultInfo(SPDF_ALTERNATES) to eliminate the alternate data (possibly including a large amount of private engine data). Once the result information is discarded, all attempts to access that data will be unsuccessful. For example, once retained audio has been discarded, a call to ISpeechRecoResult.Audio will fail.

  
    ISpeechRecoResult.DiscardResultInfo(
     ValueTypes As SpeechDiscardType)

Parameters

  • ValueTypes
    Flags indicating elements to discard. Multiple values may be combined with logical operands.

Return Value

None.

Example

The following Visual Basic form code demonstrates discarding the audio portion of a recognition by calling the DiscardResultInfo method of a RecoResult. After discarding the retained audio, the next attempt to speak it fails and the code's exception handler catches the error and displays its description and number (SPERR_NO_AUDIO_DATA or -2147200976).

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

Const AUDIOFORMAT = SAFT8kHz16BitMono

' 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.DictationLoad
    Grammar.DictationSetState SGDSActive

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

    Do While EndofStream = False
        DoEvents

        ' 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

    Loop

    Grammar.DictationSetState SGDSInactive

EH:
    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)

    Dim GetFormat As SpAudioFormat
    Dim SetFormat As SpAudioFormat

    On Error GoTo EH

    With Result

        ' Speak back recognized text.
        .SpeakAudio

        ' Discard retained audio and note that next
        ' attempt at speaking that audio fails
        ' (exception handler catches run-time error):
        .DiscardResultInfo SDTAudio
        Result.SpeakAudio

    End With

EH:
    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

End Sub