gpt4 book ai didi

.net - 使用 SpeechSynthesizer 使用 SpeechAudioFormatInfo 流式传输 TTS

转载 作者:行者123 更新时间:2023-12-03 17:53:13 25 4
gpt4 key购买 nike

我正在使用 System.Speech.Synthesis.SpeechSynthesizer将文本转换为语音。而且由于 Microsoft 的文档贫乏(请参阅我的链接,没有备注或代码示例),我无法确定两种方法之间的区别:

SetOutputToAudioStream 和 SetOutputToWaveStream。

以下是我的推断:

SetOutputToAudioStream 接受一个流和一个 SpeechAudioFormatInfo 实例,该实例定义了波形文件的格式(每秒样本数、每秒比特数、音频 channel 等)并将文本写入流。

SetOutputToWaveStream 只接受一个流并将一个 16 位、单声道、22kHz、PCM 波形文件写入流。没有办法传入 SpeechAudioFormatInfo。

我的问题是 SetOutputToAudioStream 没有将有效的波形文件写入流。例如,当将流传递给 System.Media.SoundPlayer 时,我得到一个 InvalidOperationException(“波头已损坏”)。如果我将流写入磁盘并尝试使用 WMP 播放它,我会收到“Windows Media Player 无法播放文件...”错误,但 SetOutputToWaveStream 写入的流在两者中都能正常播放。我的理论是 SetOutputToAudioStream 没有写一个(有效的)标题。

奇怪的是,SetOutputTo*Blah* 的命名约定不一致。 SetOutputToWaveFile 采用 SpeechAudioFormatInfo 而 SetOutputToWaveStream 没有。

我需要能够将 8kHz、16 位、单声道文件写入流,而 SetOutputToAudioStream 或 SetOutputToWaveStream 都不允许我这样做。有人了解 SpeechSynthesizer 和这两种方法吗?

作为引用,这里有一些代码:

Stream ret = new MemoryStream();
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
synth.SelectVoice(voiceName);
synth.SetOutputToWaveStream(ret);
//synth.SetOutputToAudioStream(ret, new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
synth.Speak(textToSpeak);
}

解决方案:

非常感谢@Hans Passant,这是我现在使用的要点:
Stream ret = new MemoryStream();
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
var mi = synth.GetType().GetMethod("SetOutputStream", BindingFlags.Instance | BindingFlags.NonPublic);
var fmt = new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Sixteen, AudioChannel.Mono);
mi.Invoke(synth, new object[] { ret, fmt, true, true });
synth.SelectVoice(voiceName);
synth.Speak(textToSpeak);
}
return ret;

对于我的粗略测试,它工作得很好,虽然使用反射有点恶心,但它比将文件写入磁盘并打开流更好。

最佳答案

您的代码片段很无聊,您在处理后使用了合成器。但这不是我确定的真正问题。 SetOutputToAudioStream 生成原始 PCM 音频,即“数字”。没有像 .wav 文件中使用的容器文件格式(标题)。是的,不能用常规媒体程序播放。

采用 SpeechAudioFormatInfo 的 SetOutputToWaveStream 缺少重载很奇怪。尽管这在 .NET 框架中极为罕见,但对我来说确实看起来像是一个疏忽。它不应该工作没有令人信服的理由,底层 SAPI 接口(interface)确实支持它。它可以通过反射来调用私有(private) SetOutputStream 方法。当我测试它时效果很好,但我不能保证它:

using System.Reflection;
...
using (Stream ret = new MemoryStream())
using (SpeechSynthesizer synth = new SpeechSynthesizer()) {
var mi = synth.GetType().GetMethod("SetOutputStream", BindingFlags.Instance | BindingFlags.NonPublic);
var fmt = new SpeechAudioFormatInfo(8000, AudioBitsPerSample.Eight, AudioChannel.Mono);
mi.Invoke(synth, new object[] { ret, fmt, true, true });
synth.Speak("Greetings from stack overflow");
// Testing code:
using (var fs = new FileStream(@"c:\temp\test.wav", FileMode.Create, FileAccess.Write, FileShare.None)) {
ret.Position = 0;
byte[] buffer = new byte[4096];
for (;;) {
int len = ret.Read(buffer, 0, buffer.Length);
if (len == 0) break;
fs.Write(buffer, 0, len);
}
}
}

如果您对 hack 感到不舒服,那么使用 Path.GetTempFileName() 将其临时流式传输到文件肯定会起作用。

关于.net - 使用 SpeechSynthesizer 使用 SpeechAudioFormatInfo 流式传输 TTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3875778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com