gpt4 book ai didi

c# - 如何将词典与 SpeechSynthesizer 一起使用?

转载 作者:可可西里 更新时间:2023-11-01 07:46:59 30 4
gpt4 key购买 nike

我正在执行一些文本到语音转换,我想在词典文件中指定一些特殊的发音。我跑了MSDN's AddLexicon example逐字逐句,它说出句子,但它使用给定的词典,似乎有些东西被破坏了。

这是提供的示例:

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();
}
}
}

和词典文件:

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

控制台打开,系统朗读文本,但未使用新发音。我当然已经按照指定将文件保存到 c:\test\whatchamacallit.pls

我尝试了 Uri 和文件位置的变体(例如 @"C:\Temp\whatchamacallit.pls"@"file:///c:\test\whatchamacallit.pls"),绝对路径和相对路径,将其复制到构建文件夹等。

我跑了 Process Monitor并且该文件未访问。如果它是目录/文件权限问题(事实并非如此),我仍然会看到访问被拒绝的消息,但是我根本没有记录任何引用,除了偶尔来 self 的文本编辑器的引用。当我尝试 File.OpenRead 时,我确实看到了访问的文件。

不幸的是,使用垃圾 Uri 时没有错误消息。

经过进一步调查,我意识到这个例子来自 Microsoft.Speech.Synthesis ,而我使用的是 System.Speech.Synthesis在这里。然而,据我所知,除了一些额外的信息和示异常(exception),它们是相同的,并且都指向相同的规范。这仍然是问题所在吗?

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

我将 MSDN 中的示例与 examples from the referenced spec 进行了比较,以及直接尝试这些但没有帮助。考虑到文件似乎没有被访问,我并不感到惊讶。

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

MSDN 上的例子有问题吗?如何将词典与 SpeechSynthesizer 一起使用?

最佳答案

经过大量研究和陷阱后,我可以向您保证,您的假设完全错误。出于某种原因,System.Speech.Synthesis.SpeechSynthesizer.AddLexicon() 将词典添加到内部列表中,但根本不使用它。似乎以前没有人尝试过使用它,这个错误也没有引起注意。

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 版本可以绕过这个问题
  • 一定要先安装runtime再安装SDK
  • 请务必安装运行时语言(如链接页面上的建议),因为 SDK 不使用默认的系统语音引擎

关于c# - 如何将词典与 SpeechSynthesizer 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529164/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com