I implemented a TTS in my C# WPF project.
Previously, I use the TTS in System.Speech.Synthesis namespace to speak. The speaking content is in SSML format (Speech Synthesizer Markup Language, support customize the speaking rate, voice, emphasize) like following:
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US"><prosody rate="x-fast">hello world. This is a long sentence speaking very fast!</prosody></speak>
But unfortunately the System.Speech.Synthesis TTS has a memory leak problem, as I mentioned in question Memory leak in .Net Speech.Synthesizer?.
So I decide to use SAPI COM component. I can easily let SAPI to speak plain text content. But then I continue to try letting it speak SSML string, I failed. The code is like following:
//Initialize TTS instance
SpeechLib.SpVoiceClass tts = new SpeechLib.SpVoiceClass();
//Generate SSML string
string textToSpeak = "hello world speak Extra Fast.";
PromptBuilder pb = new PromptBuilder();
pb.StartStyle(new PromptStyle(PromptRate.ExtraFast));
pb.AppendText(textToSpeak);
pb.EndStyle();
ssmlString = pb.ToXml(); //ssmlString = @"<speak version=""1.0"" ....
//Speak!
tts.Speak(ssmlString, SpeechLib.SpeechVoiceSpeakFlags.SVSFParseSsml);
The essential part of the code is
tts.Speak(ssmlString, SpeechLib.SpeechVoiceSpeakFlags.SVSFParseSsml);
Which uses the SpeechVoiceSpeakFlags enumerations to specify the TTS speaking behavior. I have tried several combinations of the flags, but none of them successfully speak out the SSML content.
Particularly, the above code will also raise following exceptions:
System.Runtime.InteropServices.COMException was unhandled
Message="Exception from HRESULT: 0x80045003"
Source="Interop.SpeechLib" ErrorCode=-2147201021 StackTrace: at SpeechLib.SpVoiceClass.Speak(String Text, SpeechVoiceSpeakFlags Flags) at SpeechSynthesisMemLeakTest.Program.Test2() in D:\Proj\TestSolutions\CSharp_Quick_Apps\SpeechSynthesisMemLeakTest\Program.cs:line 60 at SpeechSynthesisMemLeakTest.Program.Main(String[] args) in D:\Proj\TestSolutions\CSharp_Quick_Apps\SpeechSynthesisMemLeakTest\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
Could anyone tell me how to correctly use the flag to speak out SSML content?
What TTS engine/voice are you using? The Microsoft TTS engines definitely support SSML using the code that you're using; however, other voices/engines may not support SSML.
Error 0x80045003 is SPERR_UNSUPPORTED_FORMAT (The caller has specified an unsupported format), which leads me to believe that you need to use a different TTS engine (that supports SSML).
Use this flag instead
tts.Speak(ssmlString, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsXML);
Tested using
- SpeechLib 5.4
- C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Speech.dll
来源:https://stackoverflow.com/questions/11215761/can-c-sharp-sapi-speak-ssml-string