ISpeechPhraseElement Code Example (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

Interface: ISpeechRecoResultTimes

ISpeechRecoResultTimes Code Example

The following Visual Basic form code demonstrates the properties and methods of the ISpeechRecoResultTimes object. This object is contained in the Times property of the ISpeechRecoResult object that is returned by a RecoContext's Recognition event.

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.

The Form_Load procedure creates and sets up two SpAudioPlug objects (one for output and one for input), a SpVoice object, a SpInProcRecoContext object, and an ISpeechRecoGrammar object. The SpVoice object's EndStream event procedure and the SpInProcRecoContext object's Recognition event procedure are declared. Finally, the ISpeechRecoGrammar object's DictationLoad and DictationSetState methods are called.

Once the preceding objects have been correctly set up, the code passes the phrase "One of the world's seven wonders" to the SpVoice object's Speak method asynchronously. Then, until the SpVoice object's EndStream event gets raised, the spoken phrase stream is retrieved from the SpAudioPlug output object and passed in to the SpAudioPlug input object. After the entire stream has been processed (signaled by the EndStream event), the SpInProcRecoContext object's Recognition event is raised. Code in this event procedure then reads its Result parameter, which evaluates to an ISpeechRecoResult object.

This ISpeechRecoResult object's Times property returns an ISpeechRecoResultTimes object that contains four properties (Length, OffsetFromStart, StreamTime, and TickCount). The example code displays a message box that lists the settings of these properties for the current recognition object.

Note A SpInProcRecoContext context restricts available resources to one context or application. That is, an SR engine or microphone usedSpInProcRecoContext/shared by such a recognition context may not be used by any other applications. In situations requiring the highest performance standards, response time or exacting recognition quality, use an InProc context. InProc contexts are important to embedded systems in other hardware platforms and are also used for non-microphone recognition such as recognizing streams from a file.


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:
    Set RecoContext = Recognizer.CreateRecoContext
    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)

    Const NL = vbNewLine
    Dim MyTimes As ISpeechRecoResultTimes
    Dim T As String

    On Error GoTo EH

    Set MyTimes = Result.Times

    With MyTimes
        T = "Length: " & .Length & NL
        T = T & "OffsetFromStart: " & .OffsetFromStart & NL
        T = T & "StreamTime: " & .StreamTime & NL
        T = T & "TickCount: " & .TickCount
        MsgBox T, vbInformation
        End
    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