如何在SpeechSynthesizer中使用词典?
我正在执行一些文本到语音,我想在词典文件中指定一些特殊的发音。 我已经逐字地运行了MSDN的AddLexicon示例 ,并且它说出了句子,但它没有使用给定的词典,似乎有些东西被打破了。
这是提供的示例:
using System; using Microsoft.Speech.Synthesis; namespace SampleSynthesis { class Program { static void Main(string[] args) { // Initialize a new instance of the SpeechSynthesizer. using (SpeechSynthesizer synth = new SpeechSynthesizer()) { // Configure the audio output. synth.SetOutputToDefaultAudioDevice(); PromptBuilder builder = new PromptBuilder(); builder.AppendText("Gimme the whatchamacallit."); // Append the lexicon file. synth.AddLexicon(new Uri("c:\\test\\whatchamacallit.pls"), "application/pls+xml"); // Speak the prompt and play back the output file. synth.Speak(builder); } Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } }
和词典文件:
whatchamacallit W S1 AX T CH AX M AX K S2 AA L IH T
控制台打开,说出文字,但不使用新的发音。 我当然将文件保存到指定的c:\test\whatchamacallit.pls
。
我尝试过Uri和文件位置的变体(例如@"C:\Temp\whatchamacallit.pls"
, @"file:///c:\test\whatchamacallit.pls"
),绝对和相对路径,复制它进入构建文件夹等
我运行了Process Monitor ,但没有访问该文件。 如果它是一个目录/文件权限问题(它不是),我仍然会看到访问被拒绝的消息,但是我没有记录任何引用,除了偶尔从我的文本编辑器。 我在尝试File.OpenRead
时看到访问过的文件。
不幸的是,使用垃圾Uri时没有错误消息。
在进一步调查中,我意识到这个例子来自Microsoft.Speech.Synthesis ,而我在这里使用System.Speech.Synthesis 。 但是从我可以看出它们是相同的,除了一些额外的信息和示例,并且两者都指向相同的规范。 这仍然是问题吗?
我validation了该项目已设置为使用正确的.NET Framework 4。
我将MSDN中的示例与引用的规范中的示例进行了比较 ,并且直接尝试了这些示例,但它没有帮助。 考虑到文件似乎没有被访问我并不感到惊讶。
(我可以使用PromptBuilder.AppendTextWithPronunciation
,但对于我的用例来说,这是一个不好的选择。)
MSDN上的示例是否已损坏? 如何在SpeechSynthesizer中使用词典?
经过大量的研究和陷阱,我可以向你保证,你的假设是完全错误的。 由于某种原因, System.Speech.Synthesis.SpeechSynthesizer.AddLexicon()
将词典添加到内部列表,但根本不使用它。 似乎没有人尝试过使用它,这个bug没有引起注意。
另一方面, Microsoft.Speech.Synthesis.SpeechSynthesizer.AddLexicon()
(属于Microsoft Speech SDK)按预期工作(它将词典传递给COM对象,该对象将其解释为广告)。
有关如何安装SDK的信息,请参阅本指南: http : //msdn.microsoft.com/en-us/library/hh362873%28v=office.14%29.aspx
笔记:
- 人们报告64位版本导致COMexception(因为库没有正确安装),我在64位Windows 7机器上证实了这一点
- 使用x86版本可以解决问题
- 一定要在SDK之前安装运行时
- 请务必同时安装运行时语言(如链接页面上所述),因为SDK不使用默认的系统语音引擎