Interface: ISpeechPhraseInfo
ISpeechPhraseInfo Code Example
The following Visual Basic form code demonstrates the properties and methods of the ISpeechPhraseInfo object. The ISpeechPhraseInfo object is contained in the ISpeechRecoResult object returned by a RecoContext's Recognition event.
To run this code, paste it into the Declarations section of a form that contains no controls. In addition to the usual reference to the Microsoft Speech Object Library, this code also needs a reference to the simpleaudio 1.0 Type Library.
The Form_Load procedure creates and sets up two SpAudioPlug objects (one for output and one for input), a SpVoice object, a SpInProcRecoContext object, and an ISpeechRecoGrammar object. The SpVoice object's EndStream event procedure and the SpInProcRecoContext object's Recognition event procedure are declared. Finally, the ISpeechRecoGrammar object's DictationLoad and DictationSetState methods are called.
Once the preceding objects have been correctly set up, the code passes the phrase "One of the world's seven wonders" to the SpVoice object's Speak method asynchronously. Then, until the SpVoice object's EndStream event gets raised, the spoken phrase stream is retrieved from the SpAudioPlug output object and passed in to the SpAudioPlug input object. After the entire stream has been processed (signaled by the EndStream event), the SpInProcRecoContext object's Recognition event is raised. Code in this event procedure then reads its Result parameter, which evaluates to an ISpeechRecoResult object.
This ISpeechRecoResult object's PhraseInfo property returns an ISpeechPhraseInfo object that contains detailed information about the last recognized phrase. The example code displays a couple message boxes that list the settings returned by the ISpeechPhraseInfo object's properties and methods.
Note A SpInProcRecoContext context restricts available resources to one context or application. That is, an SR engine or microphone usedSpInProcRecoContext/shared by such a recognition context may not be used by any other applications. In situations requiring the highest performance standards, response time or exacting recognition quality, use an InProc context. InProc contexts are important to embedded systems in other hardware platforms and are also used for non-microphone recognition such as recognizing streams from a file.
Option Explicit
Const AUDIOFORMAT = SAFT8kHz16BitMono
' Text-to-Speech variables:
Dim WithEvents Voice As SpVoice
Dim EndofStream As Boolean
Dim AudioPlugOut As SpAudioPlug
' Speech Recognition variables:
Dim WithEvents RecoContext As SpInProcRecoContext
Dim Grammar As ISpeechRecoGrammar
Dim Recognizer As SpInprocRecognizer
Dim AudioPlugIn As SpAudioPlug
Private Sub Form_Load()
Const Text = "One of the world's seven wonders"
Dim Output As Variant
On Error GoTo EH
Set Voice = New SpVoice
' Set up output audio:
Set AudioPlugOut = New SpAudioPlug
AudioPlugOut.Init True, AUDIOFORMAT
Set Voice.AudioOutputStream = AudioPlugOut
' Set up input audio:
Set AudioPlugIn = New SpAudioPlug
AudioPlugIn.Init False, AUDIOFORMAT
Set Recognizer = New SpInprocRecognizer
Set Recognizer.AudioInputStream = AudioPlugIn
' Set up speech recognition:
Set RecoContext = Recognizer.CreateRecoContext
Set Grammar = RecoContext.CreateGrammar(1)
Grammar.DictationLoad
Grammar.DictationSetState SGDSActive
' Speak some text to be recognized.
Voice.Speak Text, SVSFlagsAsync
Do While EndofStream = False
DoEvents
' Get audio data from audio object.
Output = AudioPlugOut.GetData
' Output audio data to input audio object--
If (Len(Output) * 2 <> 0) Then
AudioPlugIn.SetData (Output)
End If
Loop
Grammar.DictationSetState SGDSInactive
EH:
If Err.Number Then ShowErrMsg
End Sub
Private Sub RecoContext_Recognition _
(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant, _
ByVal RecognitionType As SpeechLib.SpeechRecognitionType, _
ByVal Result As SpeechLib.ISpeechRecoResult)
Const NL = vbNewLine
Dim Phrase As Variant
Dim PhraseBuilder As Object
Dim PhraseInfo As ISpeechPhraseInfo
Dim SDAttributes As SpeechDisplayAttributes
Dim T As String
Dim TextRecognized As String
On Error GoTo EH
TextRecognized = Result.PhraseInfo.GetText
T = "The speech recognition engine's output is--" & NL & NL
T = T & TextRecognized & NL & NL
T = T & "The members of the ISpeechPhraseInfo interface are--"
T = T & NL & NL
With Result.PhraseInfo
T = T & "AudioSizeBytes: " & .AudioSizeBytes & NL
T = T & "AudioSizeTime: " & .AudioSizeTime & NL
T = T & "AudioStreamPosition: " & .AudioStreamPosition & NL
T = T & "Elements(3): " & .Elements.Item(3).DisplayText & NL
T = T & "EngineId: " & .EngineId & NL
If Len(.EnginePrivateData) = 0 Then
T = T & "EnginePrivateData: None" & NL
End If
T = T & "GetDisplayAttributes: " & .GetDisplayAttributes & NL
T = T & "GrammarId: " & .GrammarId & NL
T = T & "LanguageId: " & .LanguageId & NL
If Not .Properties Is Nothing Then
T = T & "Properties: " & .Properties.Count & NL
Else
T = T & "Properties: Nothing" & NL
End If
If Not .Replacements Is Nothing Then
T = T & "Replacements: " & .Replacements.Count & NL
Else
T = T & "Replacements: Nothing" & NL
End If
T = T & "RetainedSizeBytes: " & .RetainedSizeBytes & NL
T = T & "Rule (# of Elements): " & .Rule.NumberOfElements & NL
T = T & "StartTime: " & .StartTime
End With
MsgBox T, vbInformation
With Result.PhraseInfo
T = "Demo of SaveToMemory and RestorePhraseFromMemory methods--"
T = T & NL & NL
' Store phrase first.
Phrase = .SaveToMemory
' Retrieve phrase:
Set PhraseBuilder = CreateObject("SAPI.SpPhraseInfoBuilder")
Set PhraseInfo = PhraseBuilder.RestorePhraseFromMemory(Phrase)
T = T & PhraseInfo.GetText
MsgBox T, vbInformation
End With
EH:
If Err.Number Then ShowErrMsg
End Sub
Private Sub Voice_EndStream _
(ByVal StreamNumber As Long, _
ByVal StreamPosition As Variant)
EndofStream = True
End Sub
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