Export (0) Print
Expand All

SpeechRecoContext CreateResultFromMemory Method (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

Interface: ISpeechRecoContext

CreateResultFromMemory Method


The CreateResultFromMemory method creates a recognition result object from a saved recognition result.

The result must have been created with ISpeechRecoResult.SaveToMemory.


SpeechRecoContext.CreateResultFromMemory(
     ResultBlock As Variant
) As ISpeechRecoResult

Parameters

ResultBlock
A Variant variable containing a saved recognition result.

Return Value

An ISpeechRecoResult object.


Example

The following Visual Basic form code demonstrates the use of the SpeechRecoContext.CreateResultFromMemory and ISpeechRecoResult.SaveToMemory methods. The example displays the text of the current recognition as well as the previous one; it also plays the retained audio associated with the last recognition.

To run this code, create a form with the following controls:

  • A command button called Command1
  • A command button called Command2

Paste this code into the Declarations section of the form. The code also needs a reference to be set to simpleaudio 1.0 Type Library to run correctly.


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

Dim LastPhrase As Variant
Dim Output As Variant

Private Sub Form_Load()
    On Error GoTo EH

    LastPhrase = Empty
    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
    RecoContext.RetainedAudio = SRAORetainAudio
    Set Grammar = RecoContext.CreateGrammar(1)
    Grammar.DictationLoad
    Grammar.DictationSetState SGDSActive

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Command1_Click()

    Const Text = "One of the world's seven wonders"

    On Error GoTo EH
    EndofStream = False

    ' 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

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Command2_Click()

    Const Text = "Good man is hard to find"

    On Error GoTo EH
    EndofStream = False

    ' 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

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 SavedText As String
    Static T As String

    On Error GoTo EH

    If IsEmpty(LastPhrase) = False Then
        Dim GetRecoResult As ISpeechRecoResult
        Set GetRecoResult = RecoContext.CreateResultFromMemory(LastPhrase)

        SavedText = GetRecoResult.PhraseInfo.GetText
        T = "Last phrase: " & SavedText
        GetRecoResult.SpeakAudio
    End If

    LastPhrase = Result.SaveToMemory

    T = T & vbNewLine & "New phrase: " & Result.PhraseInfo.GetText
    MsgBox T, vbInformation

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
Show:
© 2014 Microsoft