SoundEffect raised an error while assigning buffer from SpeechLib
I am developing Windows Phone 7 application which read some text and speak the text.
The micsoroft library - interop.speechlib.dll converts my text into buffer(byte array). And SoundEffect plugin of Windows Phone 7 speaks it.
This all working fine, but sometimes it creates problems by raising bellow error-
Buffer is invalid. Ensure that the buffer length is non-zero and meets the block alignment requirement for the audio format
Code to speak is as per below:
SoundEffect se = new SoundEffect(buffer, 15000, AudioChannels.Stereo);
FrameworkDispatcher.Update();
se.Play();
Please suggest me, where I am doing wrong.
Edit From some test I conclude that the root cause of issue is generation of buffer. Below is code to generate buffer from text.
using (MemoryStream ms = new Memo开发者_如何转开发ryStream())
{
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;
}
Thanks, Naresh Goradara
After some test it works by setting channel mode. It works by setting channel mode to mono.
SoundEffect se = new SoundEffect(buffer, 30000, AudioChannels.Mono);
View the difference between these at mono vs stereo
I suspect that you did not honor the block alignment requirements. Quote
What exactly is block alignment and how do I calculate it?
In a single sentence, block alignment value is the number of bytes in an atomic unit (aka block) of audio for a particular format. For PCM formats the formula is extremely simple: “Block Alignment = Bytes per Sample * Number of Channels”. For example, block alignment value for mono 16-bit PCM format is 2 and for stereo 16-bit PCM format it’s 4. We have a couple handy helpers that can help calculate the block aligned values – GetSampleSizeInBytes and GetSampleDuration convert from time units to block aligned byte value and back.
Source http://blogs.msdn.com/b/ashtat/archive/2010/06/03/soundeffect-creation-in-xna-game-studio-4.aspx
精彩评论