如何在SpeechSynthesizer中使用词典?

Chr*_*pin 15 c# text-to-speech

我正在执行一些文本到语音,我想在词典文件中指定一些特殊的发音.我已经逐字地运行了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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

和词典文件:

<lexicon version="1.0" 
      xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon 
        http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
      alphabet="x-microsoft-ups" xml:lang="en-US">


  <lexeme>
    <grapheme> whatchamacallit </grapheme>
    <phoneme> W S1 AX T CH AX M AX K S2 AA L IH T </phoneme>
  </lexeme>

</lexicon>
Run Code Online (Sandbox Code Playgroud)

控制台打开,说出文字,但不使用新的发音.我当然将文件保存到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.但是从我可以看出它们是相同的,除了一些额外的信息和示例,并且两者都指向相同的规范.这仍然是问题吗?

我验证了该项目已设置为使用正确的.NET Framework 4.

我将MSDN中的示例与引用规范中的示例进行了比较,并尝试了这些示例,但它没有帮助.考虑到文件似乎没有访问我并不感到惊讶.

(我可以使用PromptBuilder.AppendTextWithPronunciation得很好,但对于我的用例来说,这是一个糟糕的选择.)

MSDN上的示例是否已损坏?如何在SpeechSynthesizer中使用词典?

M.S*_*amm 10

经过大量的研究和陷阱,我可以向你保证,你的假设是完全错误的.由于某种原因,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位版本导致COM异常(因为库没有正确安装),我在64位Windows 7机器上证实了这一点
    • 使用x86版本可以解决问题
  • 一定要在SDK之前安装运行时
  • 请务必同时安装运行时语言(如链接页面上的建议),因为SDK不使用默认的系统语音引擎