【问题标题】:TTS to Stream with SpeechAudioFormatInfo using SpeechSynthesizer使用 SpeechSynthesizer 使用 SpeechAudioFormatInfo 流式传输 TTS
【发布时间】:2011-04-22 00:07:56
【问题描述】:

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

SetOutputToAudioStream 和 SetOutputToWaveStream。

这是我的推断:

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

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;

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

【问题讨论】:

    标签: .net text-to-speech speechsynthesizer speech-synthesis


    【解决方案1】:

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

    使用 SpeechAudioFormatInfo 的 SetOutputToWaveStream 缺少重载很奇怪。尽管这在 .NET 框架中极为罕见,但在我看来确实是一种疏忽。没有令人信服的理由为什么它不应该工作,底层 SAPI 接口确实支持它。它可以通过反射来调用私有 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() 将其临时流式传输到文件肯定会奏效。

    【讨论】:

    • 想一想,最后一个参数可能应该是假的,所以它不会关闭流。不过,对于 MemoryStream 来说并不重要。
    • 你说得对,synth.Speak() 在我的代码中使用。我已经编辑了代码 sn-p。我会给你的代码一个镜头,看起来它会完成我的要求。我同意这看起来像是一个疏忽。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2014-06-14
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多