我在用 System.speech.Synthesis.speechsynthesizer 将文本转换为语音。而且,由于Microsoft的贫血文档(请参阅我的链接,没有备注或代码示例),我很难使两种方法之间的差异或尾巴差不多:

setOutputtoAudioStream和setOutputtodtowaveStream。

这是我推论的:

setOutputtoAudioStream采用了一个流和segreataudioformatinfo实例,该实例定义了波浪文件的格式(每秒示例,每秒,每秒,音频通道等),并将文本写入流。

SetOutputtoWaveStream仅采用一个流,然后将16位,单声道,22kHz,PCM Wave文件写入流。无法通过SpeechAudioFormatinfo。

我的问题是SetOutputtoAudioStream不会在流中写有效的Wave文件。例如,当将流传递到system.media.soundplayer时,我会得到一个无效的exception(“ Wave头是损坏”)。如果我将流写入磁盘并尝试使用WMP播放它,我会得到“ Windows Media Player无法播放文件...”错误,但是SetOutputtodtowaveStream编写的流都在两者中正确地播放。我的理论是,setOutputtoAudioStream没有编写(有效)标头。

奇怪的是,setOutputto* blah*的命名约定是不一致的。 setOutputtoWaveFile采用SegressAudioFormatInfo,而SetOutputtoftowaveStream则没有。

我需要能够将8kHz,16位,单波文件写入流,这是setOutputtoaudoadtream或setOutputtodtowaveStream允许我做的。有人对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生产RAW PCM音频,“数字”。没有容器文件格式(标题),例如.WAV文件中使用的内容。是的,这不能通过常规媒体程序进行回播。

setoutputtowavestream的丢失的过载是segressaudioformatinfo的奇怪。尽管在.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()将其临时流式传输到文件肯定会起作用。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top