ISpeechRecoResult SpeakAudio Method (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

Interface: ISpeechRecoResult

SpeakAudio Method

The SpeakAudio method plays the audio sequence containing the recognized phrase.

The audio portion of the recognition is not automatically available. By default, the recognition context does not retain audio. Call RetainedAudio and set it to SRAORetainAudio to retain the audio. Although 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.

SpeakAudio combines two other methods. The first, ISpeechRecoResult.Audio, which retrieves the audio. The second call is SpVoice.SpeakStream, which plays it back.

ISpeechRecoResult.SpeakAudio(
     [StartElement As Long = 0],
     [Elements As Long = -1],
     [Flags As SpeechVoiceSpeakFlags = SVSFDefault]
) As Long

Parameters

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

Return Value

The SpeakAudio method returns a Long variable indicating the stream number.

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.

Also see ISpeechRecoContext.RetainedAudio for an additional code sample.

Example

The following Visual Basic form code plays the audio sequence containing a recognized phrase. RetainedAudio must have been set for the current recognizer.

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 i As Integer

    On Error GoTo EH

    For i = 0 To Result.PhraseInfo.Elements.Count - 1
       StreamNumber = Result.SpeakAudio(i, 1, SVSFDefault)
    Next i

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