使用 System.Speech.Synthesis 时最小化延迟
我正在使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以某种方式阅读了我需要的 API 调用至少一百遍。我正在寻找
SpeechSynthesizer.SetOutputToWaveStream
。此代码将使用 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
.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 aSoundPlayer
from it, it starts reading the stream from the beginning instead of the end. Once you have theSoundPlayer
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.