You are reading help file online using chmlib.com
|
Interface: ISpeechPhraseAlternate
The following Visual Basic form code displays use of the ISpeechPhraseAlternate object. It demonstrates recognition and the EmulateRecognition method. In addition, it demonstrates the use of the SpeechDocs DLL created in the Sample DLL section. This DLL must be created before this code can be run.
To run this code, create a form with the following controls:
Paste this code into the Declarations section of the form.
Because this code uses the SpeechDocs DLL, it is necessary to add a reference to "SpeechDocs" in the Project, References dialog.
The solitaire grammar is located in the default directory for a standard SAPI SDK installation. If the file is not there, use the path to an existing version. If no file is available, use the example code from VB Application Sample: Command and Control Recognition if needed.
The Form_Load procedure creates a recognizer, a recognition context, and a grammar object. It loads the grammar object with sol.xml, the Solitaire grammar from the SAPI sample code, then activates the command and control (C and C) and dictation components of the grammar, and places a Solitaire command in the text box. Text matching the rules of the Solitaire grammar will produce better recognition results, but in order to demonstrate recognition alternates, it is necessary to enter phrases that do not satisfy grammar rules.
The command button, captioned Recognition, speaks text from the text box into an audio file, and then performs speech recognition of that file. The command button captioned EmulateRecognition simply calls the EmulateRecognition method.
When alternates are available, the code displays three alternates in the top list box, and shows the phrase elements of one alternate in the lower list box. Click any alternate to display its phrase elements in the lower list box. Double click any alternate to perform the Commit method on that alternate. In the illustration below, the first alternate is incorrect; the second alternate is highlighted, and its phrase elements are displayed in the lower list box.
The data displayed in the lower list box is the same data displayed in the ISpeechPhraseElement code example. Please see that page for full details.
Option Explicit
Dim D As SpeechDocs.Class1 'DLL created from Speech docs
Const WAVEFILENAME = "C:\ISpeechPhraseElement.wav"
Private blnAlternatesExist As Boolean
Dim R As SpeechLib.SpInprocRecognizer
Dim G As SpeechLib.ISpeechRecoGrammar
Dim F As SpeechLib.SpFileStream
Dim V As SpeechLib.SpVoice
Dim PI As SpeechLib.ISpeechPhraseInfo
Dim A As SpeechLib.ISpeechPhraseAlternate
Dim AA As SpeechLib.ISpeechPhraseAlternates
Dim E As SpeechLib.ISpeechPhraseElement
Dim EE As SpeechLib.ISpeechPhraseElements
Dim WithEvents C As SpeechLib.SpInProcRecoContext
Private Sub Command1_Click()
List1.Clear
List2.Clear
Screen.MousePointer = vbHourglass
'Call this method in the DLL to speak into a file
Set F = D.SpeakToFile(Text1.Text, WAVEFILENAME, "Microsoft Sam")
'Set the file as recognizer's input stream
F.Open WAVEFILENAME
Set R.AudioInputStream = F
End Sub
Private Sub Command2_Click()
List1.Clear
List2.Clear
Screen.MousePointer = vbHourglass
C.Recognizer.EmulateRecognition Text1.Text
End Sub
Private Sub Form_Load()
' Create Recognizer, RecoContext, Grammar, and Voice
Set R = New SpInprocRecognizer
Set C = R.CreateRecoContext
Set G = C.CreateGrammar(16)
Set V = New SpVoice
Set D = New SpeechDocs.Class1 'Create Class1 object from SpeechDocs DLL
' Load Grammar with solitaire XML, set active
G.CmdLoadFromFile "C:\Program Files\Microsoft Speech SDK 5.1\Samples\CPP\Reco\sol.xml", SLOStatic
G.CmdSetRuleIdState 0, SGDSActive 'Set C & C active
G.DictationSetState SGDSActive 'Set Dictation active
Text1.Text = "let's go over minor details"
Command1.Caption = "&Recognition"
Command2.Caption = "&EmulateRecognition"
End Sub
Private Function PhonesToString(ByVal arrV As Variant) As String
Dim ii As Integer, S As String
If IsEmpty(arrV) Then
PhonesToString = ""
Else
For ii = 0 To UBound(arrV)
If Len(S) Then
S = S & "," & arrV(ii)
Else
S = arrV(ii)
End If
Next ii
PhonesToString = S
End If
End Function
Private Sub C_Recognition(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
ByVal Result As SpeechLib.ISpeechRecoResult)
Dim nn As Integer
Set AA = Result.Alternates(3) 'May or may not be available
If AA Is Nothing Then
'No alternates when speech matches a grammar rule
'No alternates when using EmulateRecognition
blnAlternatesExist = False
List1.AddItem Result.PhraseInfo.GetText
If Len(Result.PhraseInfo.Rule.Name) Then
List1.AddItem " matches rule """ & Result.PhraseInfo.Rule.Name & """"
End If
Set PI = Result.PhraseInfo
Else
blnAlternatesExist = True
nn = 0
For Each A In AA
List1.AddItem "alt" & Format(nn) & ": " & A.PhraseInfo.GetText
nn = nn + 1
Next
Set PI = AA.Item(0).PhraseInfo
End If
List1.ListIndex = 0 'Highlight the selected alternate
Call DisplayPhraseElements(0)
Screen.MousePointer = vbDefault
End Sub
Private Sub C_EndStream(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal StreamReleased As Boolean)
'Recognition uses the Filestream, EmulateReco does not
If ActiveControl.Caption = "&Recognition" Then
F.Close
DoEvents
F.Open WAVEFILENAME
V.SpeakStream F
F.Close
End If
Screen.MousePointer = vbDefault
End Sub
Private Sub DisplayPhraseElements(Index As Integer)
Dim X As String
Dim T As String
Dim A1 As Long, A2 As Long
Dim T1 As Long, T2 As Long
Dim C1 As Single, C2 As Integer, C3 As Integer
List2.Clear
'If no alternates, then there is only one PhraseInfo
If blnAlternatesExist = True Then
Set PI = AA.Item(Index).PhraseInfo
End If
For Each E In PI.Elements
'Audio data
A1 = E.AudioStreamOffset
A2 = E.AudioSizeBytes
X = Format(A1, "000000") & " " & Format(A2, "000000") & " "
'Time data
T1 = E.AudioTimeOffset
T2 = E.AudioSizeTime
X = X & Format(T1, "000000000") & " " & Format(T2, "000000000") & " "
'Display attributes
X = X & Format(E.DisplayAttributes) & " "
'Confidences
C1 = E.EngineConfidence
C2 = E.ActualConfidence
C3 = E.RequiredConfidence
T = "(" & Format(C1) & " " & Format(C2) & " " & Format(C3) & ")"
X = X & Left(T & " ", 14)
'Text and pronunciation
X = X & Left(E.DisplayText & " ", 14)
X = X & PhonesToString(E.Pronunciation)
List2.AddItem X
Next
End Sub
Private Sub List1_Click()
Call DisplayPhraseElements(List1.ListIndex)
End Sub
Private Sub List1_DblClick()
AA.Item(List1.ListIndex).Commit
End Sub
You are reading help file online using chmlib.com
|