ISpeechRecoResult Audio Method (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

Interface: ISpeechRecoResult

Audio Method

The Audio method creates an audio stream from the audio data in the result object.

The resulting stream can be used as input for SPVoice.SpeakStream. However, a single call to ISpeechRecoResults.SpeakAudio performs the same action of speaking the recognized result.

The audio portion of the recognition is not automatically available. By default, the recognition context does not retain audio. To retain it, call the RetainedAudio property and set it to SRAORetainAudio. While RetainedAudio can be toggled at anytime, audio for a specific phrase must be retained before recognition attempts begin. Therefore, RetainedAudio must be called before the phrase is spoken.

ISpeechRecoResult.Audio(
     [StartElement As Long = 0],
     [Elements As Long = -1]
) As ISpeechBaseStream

Parameters

StartElement
[Optional] Specifies the starting element. The default value is zero, indicating the first element is used.
Elements
[Optional] Specifies the number of elements to speak. Default value is -1, indicating all elements are to be played.

Return Value

The Audio method returns an ISpeechBaseStream stream.

Remarks

Even if there are no elements, that is, StartElement = 0 and Elements = 0, the audio will still be played. Unrecognized results not having elements will still have audio.

Example

The following Visual Basic form code calls the Audio method of a RecoResult. The stream is returned by the method and then the code passes it to SPVoice.SpeakStream for you to hear.

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 Stream As ISpeechBaseStream

    On Error GoTo EH

    ' Retrieve stream to speak.
    Set Stream = Result.Audio

    ' Create voice and have it speak:
    Set Voice = New SpVoice
    Voice.SpeakStream Stream

    End

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 identifier:
    Dim T As String

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

End Sub