Interface: ISpeechPhraseReplacement
Example
The following Visual Basic form code demonstrates the ISpeechPhraseReplacement interface and its relation to the ISpeechPhraseElement interface. To run this code, create a form with the following controls:
- A text box called Text1
- A list box called List1
- Two command buttons called Command1 and Command2
Paste this code into the Declarations section of the form.
This example is based on the ISpeechPhraseElement code example. Individual phrase elements from the recognition result are listed in the list box. The DisplayAttributes and DisplayText properties of each phrase element are shown. The DisplayAttributes and Text properties of the phrase replacements are displayed next to the elements which can be replaced by them.
Option Explicit
Const WAVEFILENAME = "C:\ISpeechPhraseReplacement.wav"
Dim MyRecognizer As SpeechLib.SpInprocRecognizer
Dim MyGrammar As SpeechLib.ISpeechRecoGrammar
Dim MyFileStream As SpeechLib.SpFileStream
Dim PhraseElem As SpeechLib.ISpeechPhraseElement
Dim PhraseRepl As SpeechLib.ISpeechPhraseReplacement
Dim PhraseRepls As SpeechLib.ISpeechPhraseReplacements
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 Command2_Click()
On Error GoTo EH
List1.Clear
MyRecoContext.Recognizer.EmulateRecognition Text1.Text
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", SLOStatic
MyGrammar.CmdSetRuleIdState 0, SGDSActive 'Set C & C active
MyGrammar.DictationSetState SGDSActive 'Set Dictation active
Text1.Text = "I spent twenty-eight dollars on route sixty-six"
Command1.Caption = "&Recognition;"
Command2.Caption = "&EmulateRecognition;"
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 ECount As Integer 'Count of elements
Dim ii As Integer
Dim nn As Integer
Dim R1 As Integer, R2 As Integer
On Error GoTo EH
'Arrays with an entry for every PhraseElem
Dim arrElements() As String
Dim arrReplaces() As String
ECount = Result.PhraseInfo.Elements.Count - 1
ReDim arrElements(ECount)
ReDim arrReplaces(ECount)
'Load PhraseElements into an array
nn = 0
For Each PhraseElem In Result.PhraseInfo.Elements
arrElements(nn) = PhraseElem.DisplayAttributes & " " & PhraseElem.DisplayText
nn = nn + 1
Next
'Load PhraseReplacements (if any) in an array
Set PhraseRepls = Result.PhraseInfo.Replacements
If Not PhraseRepls Is Nothing Then
For Each PhraseRepl In PhraseRepls
'Get the first and last elements
'replaced by this Replacement
R1 = PhraseRepl.FirstElement
R2 = R1 + PhraseRepl.NumberOfElements - 1
For ii = 0 To ECount
'Is element within the range of this replacement?
If (ii >= R1) And (ii <= R2) Then
arrReplaces(ii) = PhraseRepl.DisplayAttributes & " " & PhraseRepl.Text
End If
Next ii
Next
End If
Dim X As String
For ii = 0 To ECount
'Get PhraseElem and pad with blanks
X = arrElements(ii) & String(15, " ")
X = Left(X, 15)
'Put element index in front
X = "Element" & Format(ii, "00") & ": " & X
'Put Replacement elements at the end
X = X & arrReplaces(ii)
List1.AddItem X
Next ii
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
'Recognition uses the Filestream, EmulateReco does not
If ActiveControl.Caption = "&Recognition;" Then
MyFileStream.Close
DoEvents
MyFileStream.Open WAVEFILENAME
Voice2.SpeakStream MyFileStream
MyFileStream.Close
End If
List1.AddItem ""
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