使用 System.Speech.Synthesis 时最小化延迟

发布于 2024-09-26 13:44:11 字数 290 浏览 2 评论 0原文

我正在使用 .NET 4 中内置的 TTS,并希望语音立即发生,但我在拨打 Speak 和获取音频之间遇到了延迟。

我正在开发一个简单的倒计时器,它会取消最后五秒和完成(5...4...3...2...1...完成),但是当屏幕更新为新的时随着时间的推移,TTS 会滞后,每次调用都会变得更糟。我尝试使用 SpeakAsync,但这只会让情况变得更糟。目前,Speak 是在 UI 线程外部(在 Timer Tick 事件处理程序中)调用的。

有没有办法最大限度地减少这种延迟,例如预先计算语音并缓存它或创建某种特殊的 TTS 线程?

I am playing with using TTS built into .NET 4 and want the speech to happen immediately, but am instead encountering a lag between when I call Speak and when I get the audio.

I am developing a simple count-down timer that calls off the last five seconds and completion (5... 4... 3... 2... 1... Done), but when the screen updates with the new time, the TTS lags behind, getting worse for every invocation. I tried using SpeakAsync, but this only made it worse. Currently, Speak is being called outside the UI thread (in the Timer Tick event handler).

Is there a way to minimize this lag, such as pre-computing the speech and caching it or creating some kind of special TTS thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-10-03 13:44:11

我以某种方式阅读了我需要的 API 调用至少一百遍。我正在寻找SpeechSynthesizer.SetOutputToWaveStream

MemoryStream stream = new MemoryStream();
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SetOutputToWaveStream(stream);
synth.Play(text);
stream.Position = 0;
SoundPlayer player = new SoundPlayer(stream);
player.Play();

此代码将使用 TTS 将文本转换为流式传输到流中的 WAV 文件。您需要重置 MemoryStream 的位置,以便当您从中创建 SoundPlayer 时,它会从开头而不是末尾开始读取流。初始化 SoundPlayer 后,您可以将其保存在某处,以便稍后立即播放,而不必等待 TTS 初始化并播放声音。

I somehow read past the API call I needed at least a hundred times. I was looking for SpeechSynthesizer.SetOutputToWaveStream.

MemoryStream stream = new MemoryStream();
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SetOutputToWaveStream(stream);
synth.Play(text);
stream.Position = 0;
SoundPlayer player = new SoundPlayer(stream);
player.Play();

This code will use TTS to turn text into a WAV file that is streamed into stream. You need to reset the position of the MemoryStream so that when you create a SoundPlayer from it, it starts reading the stream from the beginning instead of the end. Once you have the SoundPlayer initialized, you can save it somewhere so you can play it later instantly instead of having to wait for the TTS to initialize and play the sound.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文