SoundEffect 在从 SpeechLib 分配缓冲区时引发错误

本文关键字:错误 缓冲区 在从 SpeechLib 分配 SoundEffect | 更新日期: 2023-09-27 17:56:13

我正在开发Windows Phone 7应用程序,它可以阅读一些文本并说出文本。

micsoroft 库 - interop.speechlib.dll 将我的文本转换为缓冲区(字节数组)。Windows Phone 7的SoundEffect插件就是这样说的。

这一切都工作正常,但有时它会通过引发波纹管误差来产生问题-

缓冲区无效。确保缓冲区长度不为零,并满足音频格式的块对齐要求

说话代码如下:

SoundEffect se = new SoundEffect(buffer, 15000, AudioChannels.Stereo);
FrameworkDispatcher.Update();
se.Play();

请建议我,我做错了哪里。

编辑从一些测试中,我得出结论,问题的根本原因是缓冲区的生成。下面是从文本生成缓冲区的代码。

 using (MemoryStream ms = new MemoryStream())
 {
     SpeechLib.SpVoice oVoice = new SpeechLib.SpVoice();
     SpeechLib.SpFileStream cpFileStream = new SpeechLib.SpFileStream();
     cpFileStream.Open(filename, SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite, false);
     oVoice.AudioOutputStream = cpFileStream;
     oVoice.Speak(value, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);
     oVoice = null;
     cpFileStream.Close();
     cpFileStream = null;
     byte[] ImageData=File.ReadAllBytes(filename);
     return ImageData;
 }

谢谢纳雷什·戈拉达拉

SoundEffect 在从 SpeechLib 分配缓冲区时引发错误

经过一些测试,它通过设置通道模式来工作。它通过将通道模式设置为单声道来工作。

SoundEffect se = new SoundEffect(buffer, 30000, AudioChannels.Mono);

在单声道与立体声下查看它们之间的区别

我怀疑您没有遵守块对齐要求。报价

究竟什么是块对齐和 我该如何计算?

在一句话中,块对齐 值是 音频的原子单位(又名块) 特定格式。对于 PCM 格式 公式非常简单:"块 对齐 = 每个样本的字节数 * 数量 的频道"。例如,块 单声道 16 位 PCM 的对齐值 格式为 2,用于立体声 16 位 PCM 格式为 4。我们有几个方便的 可以帮助计算 块对齐值 – GetSampleSize(以字节为单位)和 获取样本持续时间从时间转换 要阻止对齐字节值的单位和 返回。

来源 http://blogs.msdn.com/b/ashtat/archive/2010/06/03/soundeffect-creation-in-xna-game-studio-4.aspx