SpeechRecoContext RecognitionForOtherContext Event (SAPI 5.3)
Interface: ISpeechRecoContext Events
The RecognitionForOtherContext event occurs when the recognition context encounters a recognition result that belongs to another recognition context.
A RecognitionForOtherContext event indicates a successful recognition but that another application currently running claims it. This is a useful event if multiple shared instances are running at the same time. If the owning application cannot use or claim the recognition, the recognition event is sent to another context that can claim it. In that case, the recognition context claiming it receives the Recognition event and all other contexts receive a RecognitionForOtherContext event.
This event is more applicable to command and control grammars than to dictation. For example, the current recognition context returns a recognition result such as "file." If that recognition context does not have the word "file" in its active grammar, the speech recognition engine searches all the other open and active grammars, even if they belong to other applications. If another context has the word "file" available, the recognition event is sent there instead.
If other recognition contexts should not receive recognition events, because the window or application is not the current one for example, change state of those contexts using ISpeechRecoContext.State.
SpeechRecoContext.RecognitionForOtherContext( StreamNumber As Long, StreamPosition As Variant )
- Specifies the stream number.
- Specifies the position within the stream.
There are three possible results from a recognition attempt:
- The first, a Recognition event is the result of a successful recognition. This event indicates that the word or phrase was matched to elements in an open grammar, and that the match had a sufficiently high confidence rating.
- The second possible result is a FalseRecognition event. This event indicates that speech was detected but it either did not match words in an open grammar, or the match did not merit a high enough confidence rating. The recognition result returned with a FalseRecognition is still valid and contains all the information of a Recognition event, including the text.
- The third possible result is RecognitionForOtherContext event. This event indicates a successful recognition result, but a different recognition context was used to match words. SAPI attempts to match the word or phrase in the current recognition context. However, if no match is possible (perhaps the recognition context is currently inactive, the rule is inactive, or the word or phrase is simply not included in the grammar), SAPI then attempts to find a match in other recognition contexts. If a match is found in another application whose grammar is available, the Recognition event is sent to that application instead and the current application receives a RecognitionForOtherContext event.
The following Visual Basic form code demonstrates the use of the RecognitionForOtherContext event. The application displays the text of a successful recognition or indicates that the recognition belongs to another application.
The setup for this scenario is somewhat complex. To duplicate two applications running command and control instances, the following code must be compiled into two different applications. Create a form with the following controls:
- Two labels called Label1 and Label2
Paste this code into the Declarations section of the form.
The Form_Load procedure creates and activates a dictation grammar. Create the grammar file sol.xml and use the XML code from the RecoCC sample application. The second application should open sol2.xml. This will be the only modification.
The xml files have only one phrase each that can be recognized. Run both applications. Now speak, "new game." The first application gets the recognition while the second application displays "For another context." Now speak "file game" and the situation reverses. These applications will assign and display commands properly regardless of the front-most application.
Public WithEvents RC As SpSharedRecoContext Public myGrammar As ISpeechRecoGrammar Private Sub Form_Load() Set RC = New SpSharedRecoContext Set myGrammar = RC.CreateGrammar myGrammar.CmdLoadFromFile "sol.xml", SLODynamic myGrammar.CmdSetRuleIdState 0, SGDSActive End Sub Private Sub RC_Recognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult) Label1.Caption = Result.PhraseInfo.GetText Label4.Caption = "" End Sub Private Sub RC_RecognitionForOtherContext(ByVal StreamNumber As Long, ByVal StreamPosition As Variant) Label4.Caption = "For another context" Label1.Caption = "" End Sub