Is asynchronous text-to-speech voicing with SAPI 5

2019-08-20 21:56发布

问题:

I have a form and I want to allow the user to receive asynchronous (possibly overlapping) text-to-speech output based on the context of a text box whenever a button is pressed. I'm trying to do this via SAPI 5.4 (Interop.SpeechLib.dll). I recognize that System.Speech or other more "modern" functionality would work much better, but this is my current constraint. Here is a simplified version of my function:

private void VoiceText(string myText)
{
    SpVoice voice = new SpVoice(); // Create new SPVoice instance
    voice.Volume = 100; // Set the volume level of the text-to-speech voice
    voice.Rate = -2; // Set the rate at which text is spoken by the text-to-speech engine
    voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync); // Voice text (asynchronously?)
}

Using SVSFlagsAsync DOES allow subsequent code to execute, however the actual voicing always outputs synchronously (no overlapping, and there are brief pauses between voicing instances). I've tried calling this function as an async Task as well as in a separate thread, and still this behavior remains. Is this simply a limitation of SpVoice?

回答1:

You can cancel any currently running TTS request by using the SVSFPurgeBeforeSpeak flag on your Speak call, like this:

voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync | SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);