我有一些简单的代码,使用TTS生成波形文件并播放它:
public void TestSpeech()
{
SpeechSynthesizer synth = new SpeechSynthesizer();
using (MemoryStream stream = new MemoryStream())
{
synth.SetOutputToWaveStream(stream);
synth.Speak("Hello world");
stream.Seek(0, SeekOrigin.Begin);
IWaveSource source = new WaveFileReader(stream);
EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
var soundOut = new WasapiOut();
soundOut.Initialize(source);
soundOut.Stopped += (s, e) => waitHandle.Set();
soundOut.Play();
waitHandle.WaitOne();
soundOut.Dispose();
source.Dispose();
}
}
一切都运行良好, 但在播放音频文件之前,我想知道它将持续多长时间。是否有一种计算方法,或者是否可以从某处获取这个信息?
如果可获取,如何计算?我假设它与流中的数据量有关,但具体如何计算呢?