SpeechSynthesizer 的 SpeakProgressEventArgs 是否不准确?
使用 .Net 3.5 中的 System.Speech.Synthesis.SpeechSynthesizer 类,SpeakProgressEventArgs 的 AudioPosition 属性似乎不准确。
以下代码产生以下输出:
代码:
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);
}
}
}
输出:
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,也会发生相同的行为。
该属性的文档表示该属性是“一个 TimeSpan 对象,表示音频输出流中事件的时间位置”。
它应该是一个准确的时间,指示输出文件中单词开始或结束的时间,还是我误解了它?
Using the System.Speech.Synthesis.SpeechSynthesizer class in .Net 3.5, the AudioPosition property of the SpeakProgressEventArgs appears to be inaccurate.
The following code produces the following output:
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
However, the duration of the .wav file produced is 15.69 seconds. The same behavior occurs if you output to a Stream or to null.
The documentation for the property says the property is "A TimeSpan object that represents the time position of the event in the audio output stream".
Should it be an accurate time indicating the time the word is started or finished speaking in the output file, or am I misinterpreting it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
audioPosition
取决于语音合成器所选的语音。根据我的经验,对于某些 Microsoft 声音,例如 Anna、Zira、David、Hazel,支持的音频格式是 16000Hz PCM。所以下面的解决方案可以纠正auido位置:如果你注意的话,
SetOutputToWaveFile
的默认采样率为22050,正确时间(15.69)与AudipPosition显示的时间的比率
(20.25) 约为 0.77。如果将此比率乘以 22050,您将得到大约 16000,这是正确的采样率。the
audioPosition
depends on the selected voice of the speech synthesizer. For some Microsoft voices, such as Anna, Zira, David, Hazel, as I have experienced, the supported audio format is a 16000Hz PCM. So the following solution can correct the auido position:if you note, the default sample rate of the
SetOutputToWaveFile
is 22050, and the ratio of the correct time (15.69) to the time shown byAudipPosition
(20.25) is about 0.77. If you multiply this ratio by 22050 you get about 16000, which is the correct sample rate.