Export (0) Print
Expand All

SpeechRecoContext Voice Property (SAPI 5.3)

Speech API 5.3
Microsoft Speech API 5.3

Interface: ISpeechRecoContext

Voice Property


The Voice property specifies the SpVoice object associated with the recognition context.


Syntax

Set: SpeechRecoContext.Voice = SpVoice
Get: SpVoice = SpeechRecoContext.Voice

Parts

SpeechRecoContext
The owning object.
SpVoice
Set: An SpVoice object that sets the Voice property.
Get: An SpVoice object that gets the Voice property.

Remarks

ISpeechRecoContext.Voice allows the voice to be changed temporarily and for limited contexts. Change the voice using Speech properties in Control Panel.

Example

The following Visual Basic form code demonstrates speaking a successful recognition using the Voice property. The first text ("One of the world's seven wonders") is spoken using the default system voice, which is specified using Speech properties in Control Panel. The example code then changes the current voice to "Microsoft Sam" if it is available and speaks the name of the newly specified system voice.

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.


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 NewVoiceName = "name=Microsoft Sam"
    Dim RC As SpInProcRecoContext

	On Error GoTo EH
    Set RC = RecoContext

    RC.Voice.Speak Result.PhraseInfo.GetText
    Set RC.Voice.Voice = RC.Voice.GetVoices(NewVoiceName).Item(0)
    RC.Voice.Speak "I have changed to " & RC.Voice.Voice.GetDescription
    End

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:
    Dim T As String

    T = "Desc: " & Err.Description & vbNewLine
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub
Show:
© 2014 Microsoft