SpeechSynthesizer 的 SpeakProgressEventArgs 是否不准确?

2023-11-23

使用 .Net 3.5 中的 System.Speech.Synthesis.SpeechSynthesizer 类,SpeakProgressEventArgs 的 AudioPosition 属性似乎不准确。

以下代码产生以下输出:

Code:

using System;
using System.Speech.Synthesis;
using System.Threading;

namespace SpeechTest
{
    class Program
    {
        static ManualResetEvent speechDoneEvent = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            SpeechSynthesizer synthesizer = new SpeechSynthesizer();

            synthesizer.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synthesizer_SpeakProgress);

            synthesizer.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synthesizer_SpeakCompleted);

            synthesizer.SetOutputToWaveFile("Test.wav");

            synthesizer.SpeakAsync("This holiday season, support the music you love by shopping at Made in Washington, online and at one of five local stores. Made in Washington chocolates, bountiful gift baskets and ornaments are the perfect holiday gifts for family, friends and co-workers.");

            speechDoneEvent.WaitOne();
        }

        static void synthesizer_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            speechDoneEvent.Set();
        }

        static void synthesizer_SpeakProgress(object sender, SpeakProgressEventArgs e)
        {
            Console.WriteLine("SpeakProgress: AudioPosition=" + e.AudioPosition + ",\tCharacterPosition=" + e.CharacterPosition + ",\tCharacterCount=" + e.CharacterCount + ",\tText=" + e.Text);
        }
    }
}

Output:

SpeakProgress: AudioPosition=00:00:00.0043750,  CharacterPosition=0,    CharacterCount=4,       Text=This
SpeakProgress: AudioPosition=00:00:00.2925625,  CharacterPosition=5,    CharacterCount=7,       Text=holiday
SpeakProgress: AudioPosition=00:00:00.9086250,  CharacterPosition=13,   CharacterCount=6,       Text=season
SpeakProgress: AudioPosition=00:00:01.9421250,  CharacterPosition=21,   CharacterCount=7,       Text=support
SpeakProgress: AudioPosition=00:00:02.5621250,  CharacterPosition=29,   CharacterCount=3,       Text=the
SpeakProgress: AudioPosition=00:00:02.6760625,  CharacterPosition=33,   CharacterCount=5,       Text=music
SpeakProgress: AudioPosition=00:00:03.2648125,  CharacterPosition=39,   CharacterCount=3,       Text=you
SpeakProgress: AudioPosition=00:00:03.5199375,  CharacterPosition=43,   CharacterCount=4,       Text=love
SpeakProgress: AudioPosition=00:00:03.8435625,  CharacterPosition=48,   CharacterCount=2,       Text=by
SpeakProgress: AudioPosition=00:00:04.0701875,  CharacterPosition=51,   CharacterCount=8,       Text=shopping
SpeakProgress: AudioPosition=00:00:04.6840625,  CharacterPosition=60,   CharacterCount=2,       Text=at
SpeakProgress: AudioPosition=00:00:04.8036250,  CharacterPosition=63,   CharacterCount=4,       Text=Made
SpeakProgress: AudioPosition=00:00:05.0698125,  CharacterPosition=68,   CharacterCount=2,       Text=in
SpeakProgress: AudioPosition=00:00:05.2521250,  CharacterPosition=71,   CharacterCount=10,      Text=Washington
SpeakProgress: AudioPosition=00:00:06.2961875,  CharacterPosition=83,   CharacterCount=6,       Text=online
SpeakProgress: AudioPosition=00:00:07.0540625,  CharacterPosition=90,   CharacterCount=3,       Text=and
SpeakProgress: AudioPosition=00:00:07.3331250,  CharacterPosition=94,   CharacterCount=2,       Text=at
SpeakProgress: AudioPosition=00:00:07.6818750,  CharacterPosition=97,   CharacterCount=3,       Text=one
SpeakProgress: AudioPosition=00:00:08.0598750,  CharacterPosition=101,  CharacterCount=2,       Text=of
SpeakProgress: AudioPosition=00:00:08.2163750,  CharacterPosition=104,  CharacterCount=4,       Text=five
SpeakProgress: AudioPosition=00:00:08.5971875,  CharacterPosition=109,  CharacterCount=5,       Text=local
SpeakProgress: AudioPosition=00:00:09.0243750,  CharacterPosition=115,  CharacterCount=6,       Text=stores
SpeakProgress: AudioPosition=00:00:10.5325625,  CharacterPosition=123,  CharacterCount=4,       Text=Made
SpeakProgress: AudioPosition=00:00:10.7700625,  CharacterPosition=128,  CharacterCount=2,       Text=in
SpeakProgress: AudioPosition=00:00:10.9377500,  CharacterPosition=131,  CharacterCount=10,      Text=Washington
SpeakProgress: AudioPosition=00:00:11.6708125,  CharacterPosition=142,  CharacterCount=10,      Text=chocolates
SpeakProgress: AudioPosition=00:00:12.9798750,  CharacterPosition=154,  CharacterCount=9,       Text=bountiful
SpeakProgress: AudioPosition=00:00:13.6303125,  CharacterPosition=164,  CharacterCount=4,       Text=gift
SpeakProgress: AudioPosition=00:00:14.0959375,  CharacterPosition=169,  CharacterCount=7,       Text=baskets
SpeakProgress: AudioPosition=00:00:14.7848125,  CharacterPosition=177,  CharacterCount=3,       Text=and
SpeakProgress: AudioPosition=00:00:15.0507500,  CharacterPosition=181,  CharacterCount=9,       Text=ornaments
SpeakProgress: AudioPosition=00:00:15.7195000,  CharacterPosition=191,  CharacterCount=3,       Text=are
SpeakProgress: AudioPosition=00:00:15.9872500,  CharacterPosition=195,  CharacterCount=3,       Text=the
SpeakProgress: AudioPosition=00:00:16.1488750,  CharacterPosition=199,  CharacterCount=7,       Text=perfect
SpeakProgress: AudioPosition=00:00:16.7275000,  CharacterPosition=207,  CharacterCount=7,       Text=holiday
SpeakProgress: AudioPosition=00:00:17.3336875,  CharacterPosition=215,  CharacterCount=5,       Text=gifts
SpeakProgress: AudioPosition=00:00:17.9813125,  CharacterPosition=221,  CharacterCount=3,       Text=for
SpeakProgress: AudioPosition=00:00:18.2216875,  CharacterPosition=225,  CharacterCount=6,       Text=family
SpeakProgress: AudioPosition=00:00:19.0973750,  CharacterPosition=233,  CharacterCount=7,       Text=friends
SpeakProgress: AudioPosition=00:00:19.7726250,  CharacterPosition=241,  CharacterCount=3,       Text=and
SpeakProgress: AudioPosition=00:00:19.9655625,  CharacterPosition=245,  CharacterCount=10,      Text=co-workers
SpeakProgress: AudioPosition=00:00:20.2518750,  CharacterPosition=245,  CharacterCount=10,      Text=co-workers

但是,生成的 .wav 文件的持续时间为 15.69 秒。如果输出到 Stream 或 null,也会发生相同的行为。

The 文档该属性表示该属性是“一个 TimeSpan 对象,表示音频输出流中事件的时间位置”。

它应该是一个准确的时间,指示输出文件中单词开始或结束的时间,还是我误解了它?


the audioPosition取决于语音合成器所选的声音。根据我的经验,对于某些 Microsoft 声音,例如 Anna、Zira、David、Hazel,支持的音频格式是 16000Hz PCM。所以下面的解决方案可以纠正auido位置:

var format = 
new System.Speech.AudioFormat.SpeechAudioFormatInfo(EncodingFormat.Pcm, 
                                                    16000, 16, 1, 32000, 2, null);
synthesizer.SetOutputToWaveFile("Test.wav", format);

如果您注意到,默认采样率SetOutputToWaveFile为 22050,正确时间(15.69)与下式所示时间的比率AudipPosition(20.25)约为0.77。如果将此比率乘以 22050,您将得到大约 16000,这是正确的采样率。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpeechSynthesizer 的 SpeakProgressEventArgs 是否不准确? 的相关文章

  • 我如何才能等待多个事情

    我正在使用 C 11 和 stl 线程编写一个线程安全队列 WaitAndPop 方法当前如下所示 我希望能够将一些内容传递给 WaitAndPop 来指示调用线程是否已被要求停止 如果 WaitAndPop 等待并返回队列的元素 则应返回
  • GLKit的GLKMatrix“列专业”如何?

    前提A 当谈论线性存储器中的 列主 矩阵时 列被一个接一个地指定 使得存储器中的前 4 个条目对应于矩阵中的第一列 另一方面 行主 矩阵被理解为依次指定行 以便内存中的前 4 个条目指定矩阵的第一行 A GLKMatrix4看起来像这样 u
  • 动态加载程序集的应用程序配置

    我正在尝试将模块动态加载到我的应用程序中 但我想为每个模块指定单独的 app config 文件 假设我的主应用程序有以下 app config 设置
  • 按成员序列化

    我已经实现了template
  • 秒表有最长运行时间吗?

    多久可以Stopwatch在 NET 中运行 如果达到该限制 它会回绕到负数还是从 0 重新开始 Stopwatch Elapsed返回一个TimeSpan From MSDN https learn microsoft com en us
  • 查找c中结构元素的偏移量

    struct a struct b int i float j x struct c int k float l y z 谁能解释一下如何找到偏移量int k这样我们就可以找到地址int i Use offsetof 找到从开始处的偏移量z
  • 类模板参数推导 - clang 和 gcc 不同

    下面的代码使用 gcc 编译 但不使用 clang 编译 https godbolt org z ttqGuL template
  • 用于登录 .NET 的堆栈跟踪

    我编写了一个 logger exceptionfactory 模块 它使用 System Diagnostics StackTrace 从调用方法及其声明类型中获取属性 但我注意到 如果我在 Visual Studio 之外以发布模式运行代
  • 在 Windows 窗体中保存带有 Alpha 通道的单色位图会保存不同(错误)的颜色

    在 C NET 2 0 Windows 窗体 Visual Studio Express 2010 中 我保存由相同颜色组成的图像 Bitmap bitmap new Bitmap width height PixelFormat Form
  • HTTPWebResponse 响应字符串被截断

    应用程序正在与 REST 服务通信 Fiddler 显示作为 Apps 响应传入的完整良好 XML 响应 该应用程序位于法属波利尼西亚 在新西兰也有一个相同的副本 因此主要嫌疑人似乎在编码 但我们已经检查过 但空手而归 查看流读取器的输出字
  • 堆栈溢出:堆栈空间中重复的临时分配?

    struct MemBlock char mem 1024 MemBlock operator const MemBlock b const return MemBlock global void foo int step 0 if ste
  • C++ OpenSSL 导出私钥

    到目前为止 我成功地使用了 SSL 但遇到了令人困惑的障碍 我生成了 RSA 密钥对 之前使用 PEM write bio RSAPrivateKey 来导出它们 然而 手册页声称该格式已经过时 实际上它看起来与通常的 PEM 格式不同 相
  • 将多个表映射到实体框架中的单个实体类

    我正在开发一个旧数据库 该数据库有 2 个具有 1 1 关系的表 目前 我为每个定义的表定义了一种类型 1Test 1Result 我想将这些特定的表合并到一个类中 当前的类型如下所示 public class Result public
  • 使用 Bearer Token 访问 IdentityServer4 上受保护的 API

    我试图寻找此问题的解决方案 但尚未找到正确的搜索文本 我的问题是 如何配置我的 IdentityServer 以便它也可以接受 授权带有 BearerTokens 的 Api 请求 我已经配置并运行了 IdentityServer4 我还在
  • Windows 窗体:如果文本太长,请添加新行到标签

    我正在使用 C 有时 从网络服务返回的文本 我在标签中显示 太长 并且会在表单边缘被截断 如果标签不适合表单 是否有一种简单的方法可以在标签中添加换行符 Thanks 如果您将标签设置为autosize 它会随着您输入的任何文本自动增长 为
  • 对现有视频添加水印

    我正在寻找一种用 C 在视频上加水印的方法 就像在上面写文字一样 图片或文字标签 我该怎么做 谢谢 您可以使用 Nreco 视频转换器 代码看起来像 NReco VideoConverter FFMpegConverter wrap new
  • 如何在Xamarin中删除ViewTreeObserver?

    假设我需要获取并设置视图的高度 在 Android 中 众所周知 只有在绘制视图之后才能获取视图高度 如果您使用 Java 有很多答案 最著名的方法之一如下 取自这个答案 https stackoverflow com a 24035591
  • 测试用例执行完成后,无论是否通过,如何将测试用例结果保存在变量中?

    我正在使用 NUNIT 在 Visual Studio 中使用 Selenium WebDriver 测试用例的代码是 我想在执行测试用例后立即在变量中记录测试用例通过或失败的情况 我怎样才能实现这一点 NUnit 假设您使用 NUnit
  • 是否可以在 .NET Core 中将 gRPC 与 HTTP/1.1 结合使用?

    我有两个网络服务 gRPC 客户端和 gRPC 服务器 服务器是用 NET Core编写的 然而 客户端是托管在 IIS 8 5 上的 NET Framework 4 7 2 Web 应用程序 所以它只支持HTTP 1 1 https le
  • C++ 标准是否指定了编译器的 STL 实现细节?

    在写答案时this https stackoverflow com questions 30909296 can you put a pimpl class inside a vector我遇到了一个有趣的情况 这个问题演示了这样一种情况

随机推荐