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# 中转换 VbScript 函数(Right、Len、IsNumeric、CInt)

    同样 我在 VbScript 中得到了以下代码 您能建议一下 C 中的等效代码吗 Function GetNavID Title getNavID UCase Left Title InStr Title 1 End Function 我已
  • 了解 VerQueryValue

    在 MSDN 上 我注意到 VerQueryValue 函数的以下内容 lplp缓冲区 输出 低电压空洞当此方法返回时 包含指向 pBlock 指向的缓冲区中所请求版本信息的指针的地址 当关联的 pBlock 内存被释放时 lplpBuff
  • C++:Linux平台上的线程同步场景

    我正在为 Linux 平台实现多线程 C 程序 其中我需要类似于 WaitForMultipleObjects 的功能 在搜索解决方案时 我发现有一些文章描述了如何在 Linux 中实现 WaitForMultipleObjects 功能
  • 如何在Qt3D中优化点云渲染

    我正在尝试使用 Qt3D 显示大型点云 20M pts 我第一次发现这个图书馆https github com MASKOR Qt3DPointcloudRenderer https github com MASKOR Qt3DPointc
  • 起订量工作单元

    我是单元测试的新手 我想为我的搜索功能创建一个测试 我的服务层看起来像 public class EmployeeService BaseService IEmployeeService public EmployeeService IUn
  • 如何修复 TcpClient Ip 标头错误校验和

    我正在使用 System Net Sockets TcpClient 类 但每当我通过网络发送自定义数据包时 我都会在wireshark捕获上看到错误的校验和 我该如何修复它 问题是您在网络接口上设置了校验和卸载 这会导致您的网卡计算校验和
  • 如何获取 PropertyGrid 的单元格值 (c#)?

    如何在 C 中获取属性网格项和项的值 例如 Name Ali LastName Ahmadi Name 和 LastName 是 propertygrid 的 2 个属性 PropertyGrid只是对象的组件模型表示的视图 我会说 查看组
  • 会员提供商使用还是不使用?

    我正在开发一个使用 Facebook 的网站 现在为了管理用户我想使用MembershipProvider并选择开发一个定制的会员提供商 我的问题是我的数据库架构与标准成员资格架构不匹配 并且提供的用于覆盖的函数采用与我预期不同的参数 例如
  • DataGridView小数不排序

    好吧 我有一个 DataGridView 它的数据绑定如下 dataGridViewChartOre AutoGenerateColumns false dataGridViewChartOre DataSource xml GetOreC
  • 获取进程的所有 DLL

    我想获取为给定进程加载的所有 dll 的列表 我目前正在使用 NET框架4 0 我知道有一个bug https connect microsoft com VisualStudio feedback details 546430 syste
  • 使用 OpenSSL 库在 C++ 中生成 SHA 哈希值

    如何使用以下命令生成 SHA1 或 SHA2 哈希值OpenSSL https openssl org图书馆 我搜索了谷歌 找不到任何函数或示例代码 从命令行来看 很简单 printf compute sha1 openssl sha1 您
  • vs2010 c++ 通过debug查看指针内容

    我正在使用 Vs2010 c 处理 2D 数组 我从一维指针开始 并使用操作 如下 class CMatrix void clear public int nRows int nCols short MyMat CMatrix CMatri
  • 如何在PropertyGrid中自定义绘制GridItem?

    我想以与所有者在 ListView 详细信息 和其他控件中绘制项目类似的方式在 PropertyGrid 中绘制属性值 如果将属性声明为 Color 类型 则其值将使用字符串描述旁边的颜色样本来绘制 如果属性是图像类型 则在字符串描述旁边绘
  • 随机排列

    我无法找到一种随机洗牌元素的好方法std vector经过一些操作后 恢复原来的顺序 我知道这应该是一个相当简单的算法 但我想我太累了 由于我被迫使用自定义随机数生成器类 我想我不能使用std random shuffle 无论如何这没有帮
  • 如何查明我的字符串是否包含“micro”Unicode 字符?

    我有一个包含实验室数据的 Excel 电子表格 如下所示 g L ppb 我想测试希腊字母 是否存在 如果发现我需要做一些特别的事情 通常 我会写这样的东西 if cell StartsWith matchSequence lt unive
  • Sharepoint 的 CAML 查询中的日期时间比较

    我正在尝试从共享点列表中获取某些项目 具体取决于自定义列中的日期 我已经使用 U2U Caml Builder 创建了查询 这很有效 但是当我将其放入 Web 部件中自己的代码中时 它总是返回列表中的所有项目 这是我的代码 DateTime
  • XSD、泛型和 C# 类的困境

    我有以下简单的 XSD 文件
  • 如何获取数字列的确切类型,包括。规模和精度?

    有没有办法知道列中列的确切类型DataTable 现在我正在这样做 DataTable st dataReader GetSchemaTable foreach DataColumn col in st Columns var type c
  • 有没有办法将复选框列表绑定到 asp.net mvc 中的模型

    我在这里寻找一种快速简便的方法来在模型中发生回发时绑定复选框列表项的列表 显然现在常见的做法似乎是这样的form GetValues checkboxList 0 Contains true 这看起来很痛苦而且不太安全 有没有一种方法可以绑
  • 我可以创建一个 List> 吗?

    我正在尝试创建一个列表WeakReference使用 4 5 泛型实现 这样我就可以避免类型检查和转换WeakReference目标 但 WeakReference

随机推荐