Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ISpeechPhraseRule Code Example (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

ISpeechPhraseRule Code Example

The following Visual Basic form code demonstrates the use of the ISpeechPhraseRule interface. To run this code, create a form with the following controls:

  • A text box called Text1
  • A list box called List1
  • A command button called Command1

Paste this code into the Declarations section of the form.

The Form_Load procedure creates a recognizer, a recognition context and a grammar object. It loads the grammar object with the Solitaire grammar sol.xml and activates it.

The Command1 procedure speaks the text in the text box into a file for recognition. The recognition context's Recognition event displays selected information from the recognition result. It displays the phrase elements first, and then the properties of the ISpeechPhraseRule object.

Note that both the Parent and Children properties may have a value of Nothing. Attempting to reference them in this state will cause a run-time error.


Option Explicit

Const WAVEFILENAME = "C:\ISpeechPhraseRule.wav"

Dim MyRecognizer As SpeechLib.SpInprocRecognizer
Dim MyGrammar As SpeechLib.ISpeechRecoGrammar
Dim MyFileStream As SpeechLib.SpFileStream
Dim PhraseElem As SpeechLib.ISpeechPhraseElement
Dim Voice1 As SpeechLib.SpVoice
Dim Voice2 As SpeechLib.SpVoice     'Plays the wave file back

Dim WithEvents MyRecoContext As SpeechLib.SpInProcRecoContext

Private Sub Command1_Click()
    On Error GoTo EH

    List1.Clear

    ' Create ISpeechPhraseReplacement.wav file from text in text box.
    Set MyFileStream = MakeWAVFileFromText(Text1.Text, WAVEFILENAME)

    'Set the file as recognizer's input stream
    MyFileStream.Open WAVEFILENAME
    Set MyRecognizer.AudioInputStream = MyFileStream

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Form_Load()

    ' Create Recognizer, RecoContext, Grammar, and Voice
    Set MyRecognizer = New SpInprocRecognizer
    Set MyRecoContext = MyRecognizer.CreateRecoContext
    Set MyGrammar = MyRecoContext.CreateGrammar(16)
    Set Voice1 = New SpVoice
    Set Voice1.Voice = Voice1.GetVoices("gender=male").Item(0)
    Set Voice2 = New SpVoice

    On Error GoTo EH

    ' Load Grammar with solitaire XML, set active
    MyGrammar.CmdLoadFromFile "C:\sol.xml", SLODynamic
    MyGrammar.CmdSetRuleIdState 0, SGDSActive               'Set MyRecoContext & MyRecoContext active
    MyGrammar.DictationSetState SGDSActive                  'Set Dictation active

    Text1.Text = "play the eight of clubs"

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub SpeakToFile(ByVal strText As String, ByVal strFName As String)

    On Error GoTo EH

    Set MyFileStream = New SpFileStream                    'Create stream
    MyFileStream.Open strFName, SSFMCreateForWrite, True   'Open as the filename
    Set Voice1.AudioOutputStream = MyFileStream                 'Set voice output to file
    Voice1.Speak strText, SVSFIsXML                  'Speak synchronously
    MyFileStream.Close                                     'Close file

EH:
    If Err.Number Then ShowErrMsg
End Sub

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

    Dim X As String
    Dim ii As Integer
    Dim PR As ISpeechPhraseRule
    Dim PRs As ISpeechPhraseRules

    On Error GoTo EH

    ii = 0
    For Each PhraseElem In Result.PhraseInfo.Elements
        X = "element" & Str(ii) & ": " & PhraseElem.DisplayText
        List1.AddItem X
        ii = ii + 1
    Next

    'This is the rule that recognition was based on
    Set PR = Result.PhraseInfo.Rule

    List1.AddItem ""
    List1.AddItem "Id:               " & PR.Id
    List1.AddItem "Rule:             " & PR.Name
    List1.AddItem "NumberOfElements: " & PR.NumberOfElements
    List1.AddItem "FirstElement:     " & PR.FirstElement
    List1.AddItem "EngineConfidence: " & PR.EngineConfidence
    List1.AddItem "Confidence:       " & PR.Confidence
    List1.AddItem ""

    If PR.Parent Is Nothing Then
        List1.AddItem "Parent:           none"
    Else
        List1.AddItem "Parent:           " & PR.Parent.Name
    End If

    If PR.Children Is Nothing Then
        List1.AddItem "Children:         none"
    Else
        'This routine replaces the original PR object
        Set PRs = PR.Children
        ii = 0
        For Each PR In PRs
            List1.AddItem "Child" & Str(ii) & ":          " & PR.Name
            ii = ii + 1
        Next
    End If

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub MyRecoContext_EndStream _
   (ByVal StreamNumber As Long, _
    ByVal StreamPosition As Variant, _
    ByVal StreamReleased As Boolean)

    On Error GoTo EH

    MyFileStream.Close
    DoEvents
    MyFileStream.Open WAVEFILENAME
    Voice2.SpeakStream MyFileStream
    MyFileStream.Close

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Function MakeWAVFileFromText _
   (ByVal strText As String, _
    ByVal strFName As String) _
    As SpFileStream

    On Error GoTo EH

    ' Declare identifiers:
    Dim FileStream As SpFileStream
    Dim Voice As SpVoice

    ' Instantiate Voice and FileStream objects:
    Set Voice = New SpVoice
    Set FileStream = New SpFileStream

    ' Open specified .wav file, set voice output
    ' to file, and speak synchronously:
    FileStream.Open strFName, SSFMCreateForWrite, True
    Set Voice.AudioOutputStream = FileStream
    Voice.Speak strText, SVSFIsXML

    ' Close file and return reference to FileStream object:
    FileStream.Close
    Set MakeWAVFileFromText = FileStream

EH:
    If Err.Number Then ShowErrMsg
End Function

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.