ISpeechRecoGrammar CmdLoadFromMemory Method (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

Interface: ISpeechRecoGrammar

CmdLoadFromMemory Method

The CmdLoadFromMemory method loads a compiled speech grammar from memory.


ISpeechRecoGrammar.CmdLoadFromMemory(
     GrammarData As Variant,
     [LoadOption As SpeechLoadOption = SLOStatic]
)

Parameters

GrammarData
Specifies the GrammarData.
LoadOption
[Optional] Specifies whether the grammar is to be loaded for static or dynamic use. The default is static.

Return Value

None.


Example

The following Visual Basic form code demonstrates use of the CmdLoadFromMemory method. To run this code, create a form and paste this code into the Declarations section.

The Form_Load procedure creates a recognition context and a CFG grammar object. It adds a simple rule to the grammar, and saves the grammar data to a Variant variable with the CommitAndSave method. It then uses the CmdLoadFromMemory method to load the second grammar object with the grammar data in the Variant variable. Finally, it activates the rule in the second grammar.

If the computer has a microphone, run this code and speak the phrase, "Hello, world" into the microphone. The recognition context's Recognition and FalseRecognition event procedures display messages indicating successful or unsuccessful recognition.



Option Explicit

Dim WithEvents recoCtx As SpSharedRecoContext
Dim grammar As ISpeechRecoGrammar
Dim grammar2 As ISpeechRecoGrammar
Dim gRules As ISpeechGrammarRules
Dim gRule As ISpeechGrammarRule
Dim state As ISpeechGrammarRuleState

Dim buffer As String    ' Any error information when committing and saving the grammar
Dim gData As Variant    ' The contents of the grammar held in memory

' Typically there is no reason to do something like this.
' This is just the simplest possible example to demonstrate the use of
' ISpeechRecoGrammar.CmdLoadFromMemory.

Private Sub Form_Load()
    On Error GoTo EH

    Set recoCtx = New SpSharedRecoContext
    Set grammar = recoCtx.CreateGrammar
    Set gRules = grammar.Rules

    ' Grammar has one rule, one state, and one transition
    Set gRule = gRules.Add("greeting", SRATopLevel, 1)
    Set state = gRule.InitialState
    state.AddWordTransition Nothing, "hello world", " "

    ' Save the grammar to memory (contents saved in gData)
    gData = grammar.Rules.CommitAndSave(buffer)

    Set grammar2 = recoCtx.CreateGrammar                'Second grammar
    grammar2.CmdLoadFromMemory gData, SLOStatic         'Load from memory
    grammar2.CmdSetRuleState "greeting", SGDSActive     'Set rule active

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub recoCtx_FalseRecognition _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal Result As SpeechLib.ISpeechRecoResult)

    ' Gets here only when the phrase "hello world" is falsely recognized
    MsgBox "(not recognized!)"

End Sub

Private Sub recoCtx_Recognition _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
    ByVal Result As SpeechLib.ISpeechRecoResult)

    On Error GoTo EH

    ' Gets here only when the phrase "hello world" is recognized
    MsgBox Result.PhraseInfo.GetText

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Const NL = vbNewLine
    Dim T As String

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

End Sub