【问题标题】:Can C# SAPI speak SSML string?C# SAPI 可以说 SSML 字符串吗?
【发布时间】:2012-06-28 06:26:29
【问题描述】:

我在我的 C# WPF 项目中实现了一个 TTS。

以前,我使用 System.Speech.Synthesis 命名空间中的 TTS 来说话。讲话内容为SSML格式(Speech Synthesizer Markup Language,支持自定义语速、语音、强调)如下:

<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>

但不幸的是,System.Speech.Synthesis TTS 存在内存泄漏问题,正如我在问题 Memory leak in .Net Speech.Synthesizer? 中提到的那样。

所以我决定使用 SAPI COM 组件。我可以轻松地让 SAPI 说出纯文本内容。但后来我继续尝试让它说 SSML 字符串,我失败了。代码如下:

//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);

代码的基本部分是

tts.Speak(ssmlString, SpeechLib.SpeechVoiceSpeakFlags.SVSFParseSsml);

其中使用SpeechVoiceSpeakFlags enumerations 指定TTS 说话行为。我尝试了几种标志组合,但没有一个能成功说出 SSML 内容。

特别是,上面的代码还会引发以下异常:

System.Runtime.InteropServices.COMException 未处理
Message="HRESULT 异常:0x80045003"
Source="Interop.SpeechLib" ErrorCode=-2147201021 StackTrace: 在 SpeechLib.SpVoiceClass.Speak(字符串文本,SpeechVoiceSpeakFlags 标志) 在 D:\Proj\TestSolutions\CSharp_Quick_Apps\SpeechSynthesisMemLeakTest\Program.cs:line 中的 SpeechSynthesisMemLeakTest.Program.Test2() 60 在 D:\Proj\TestSolutions\CSharp_Quick_Apps\SpeechSynthesisMemLeakTest\Program.cs:line 中的 SpeechSynthesisMemLeakTest.Program.Main(String[] args) 17 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:

谁能告诉我如何正确使用该标志说出 SSML 内容?

【问题讨论】:

    标签: c# speech sapi text-to-speech ssml


    【解决方案1】:

    您使用的是什么 TTS 引擎/语音? Microsoft TTS 引擎绝对支持使用您正在使用的代码的 SSML;但是,其他声音/引擎可能不支持 SSML。

    错误 0x80045003 是 SPERR_UNSUPPORTED_FORMAT(调用者指定了不受支持的格式),这让我相信您需要使用不同的 TTS 引擎(支持 SSML)。

    【讨论】:

      【解决方案2】:

      改用这个标志

      tts.Speak(ssmlString, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsXML);
      

      测试使用

      • 语音库 5.4
      • C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Speech.dll

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 2019-05-18
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        相关资源
        最近更新 更多