Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SpeechRecognizer EmulateRecognition Method (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

Interface: ISpeechRecognizer

EmulateRecognition Method

The EmulateRecognition method emulates recognition from a textual source rather than from a spoken source.

Using EmulateRecognition, applications can accept input from either speech or a textual source. All the events are fired back to the application exactly as if a normal recognition had taken place. The result phrase will have the semantic properties set in the same way as a spoken result. A recognition event will be produced only if the text actually parses through the active rules (if dictation is active, any text will parse).

Since the recognition attempt will not use an audio data, certain events such Interference, Hypothesis, and AudioLevel cannot occur.

     TextElements As Variant,
     [ElementDisplayAttributes As Variant = SDA_No_Trailing_Space],
     [LanguageId As Long = 0]


Specifies the elements of the phrase to to recognize. It must be one of two cases.
  • If TextElements is a BSTR string then it is assumed that the elements in TextElements are assumed to be space delimited and DisplayAttributes parameter is ignored.
  • If TextElements is an array of BSTR words then this parameter specifically lists each element in the phrase. ElementDisplayAttributes can be optionally specified as appropriate to the phrase's need.
In either case, additional information may be specified for each element by using the following syntax on each TextElement: "/display_text/lexical_form/pronunciation;". This syntax can be used in both the BSTR and the array of BSTRs case.
[Optional] Specifies the SpeechDisplayAttributes value for each word element. This value is specific to the language and usually determined by the speech recognition engine. By default the value is SDA_No_Trailing_Space and is considered standard for English languages. This parameter is only valid if an array of BSTRs for the TextElements parameter is specified. It must be one of three cases.
  • If ElementDisplayAttributes is a NULL pointer, VT_NULL, or VT_EMPTY then SDA_No_Trailing_Space is assumed (which is the default).
  • If it is a BSTR then it can be "" (empty string), " " (space), or " " (double space) and SAPI matches the SpeechDisplayAttribute uses it for all text elements. If an integer value (VT_I1 to VT_I4) is specified, then this value is the SpeechDisplayAttribute value and will is used for each element in the words array.
  • If it is an array of integer values (VT_I1 to VT_I4) SAPI uses those values for the SpeechDisplayAttribute.
[Optional] Specifies the LanguageId. This is the same as the Win32 Language Identifier (LANGID). By default the value is zero, indicating the system default is used.

Return Value



Use this method (simulating speech) to test applications that use speech recognition. Also the restrictions in the parameters TextElements and ElementDisplayAttributes accommodate languages not using spaces to separate words.


The following Visual Basic form code demonstrates the use of EmulateRecognition. The application displays the successful recognition result of dictation. It also emulates speech by clicking the button.

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

  • A command button called Command1

Paste this code into the Declarations section of the form.

The Form_Load procedure creates and activates a dictation grammar. Click Command1 to start emulated speech and display results.

Option Explicit

Public WithEvents RC As SpSharedRecoContext
Public myGrammar As ISpeechRecoGrammar

Private Sub Form_Load()
    On Error GoTo EH

    Set RC = New SpSharedRecoContext
    Set myGrammar = RC.CreateGrammar
    myGrammar.DictationSetState SGDSActive

    If Err.Number Then ShowErrMsg
End Sub

Private Sub Command1_Click()
    On Error GoTo EH

    RC.Recognizer.EmulateRecognition ("We the people")

    If Err.Number Then ShowErrMsg
End Sub

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

    On Error GoTo EH
    MsgBox Result.PhraseInfo.GetText, vbInformation

    If Err.Number Then ShowErrMsg
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
© 2015 Microsoft