You are reading help file online using chmlib.com
|
Interface: ISpeechRecoGrammar
The CmdLoadFromMemory method loads a compiled speech grammar from memory.
The
ISpeechRecoGrammar.CmdLoadFromMemory(
GrammarData As Variant,
[LoadOption As SpeechLoadOption = SLOStatic]
)
None.
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()
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
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)
' Gets here only when the phrase "hello world" is recognized
MsgBox Result.PhraseInfo.GetText
End Sub
You are reading help file online using chmlib.com
|