SpPhraseInfoBuilder RestorePhraseFromMemory method (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

Object: SpPhraseInfoBuilder

RestorePhraseFromMemory Method

The RestorePhraseFromMemory method recreates phrase information from a phrase that has been saved to memory.

The ISpeechPhraseInfo.SaveToMemory method saves phrase information as a Variant variable. RestorePhraseFromMemory method uses this variable to recreate an object based on ISpeechPhraseInfo.

     PhraseInMemory As Variant
) As ISpeechPhraseInfo


A Variant variable containing a phrase saved to memory.

Return Value

A ISpeechPhraseInfo object returning the phrase information.


The following Visual Basic form code demonstrates storing and retrieving the phrase portion of a recognition result.

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


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

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

    Do While EndofStream = False

        ' 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


    Grammar.DictationSetState SGDSInactive

    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 PhraseBuilder As Object
    Dim PhraseInfo As ISpeechPhraseInfo
    Dim thePhrase As Variant
    Dim T As String

    On Error GoTo EH

    ' Store the phrase first.
    thePhrase = Result.PhraseInfo.SaveToMemory

    ' Retrieve the phrase from memory and display it:
    Set PhraseBuilder = CreateObject("SAPI.SpPhraseInfoBuilder")
    Set PhraseInfo = PhraseBuilder.RestorePhraseFromMemory(thePhrase)
    MsgBox PhraseInfo.GetText, vbInformation

    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 Sub