Audio Silverlight Speex以极快的速度播放
我使用Speex对原始数据进行编码,但在我解码数据后,音频播放速度更快,因为它让你听起来像一只花栗鼠。我正在使用和Silverlight 4Audio Silverlight Speex以极快的速度播放,audio,silverlight-4.0,wavmss,nspeex,Audio,Silverlight 4.0,Wavmss,Nspeex,我使用Speex对原始数据进行编码,但在我解码数据后,音频播放速度更快,因为它让你听起来像一只花栗鼠。我正在使用和Silverlight 4 8kHz采样 编码功能: JSpeexEnc encoder = new JSpeexEnc(); int rawDataSize = 0; public byte[] EncodeAudio(byte[] rawData) { var encoder = new SpeexEncoder(BandMode.
8kHz采样
编码功能:
JSpeexEnc encoder = new JSpeexEnc();
int rawDataSize = 0;
public byte[] EncodeAudio(byte[] rawData)
{
var encoder = new SpeexEncoder(BandMode.Narrow);
var inDataSize = rawData.Length / 2;
var inData = new short[inDataSize];
for (var index = 0; index < rawData.Length; index += 2)
{
inData[index / 2] = BitConverter.ToInt16(rawData, index);
}
inDataSize = inDataSize - inDataSize % encoder.FrameSize;
var encodedData = new byte[rawData.Length];
var encodedBytes = encoder.Encode(inData, 0, inDataSize, encodedData, 0, encodedData.Length);
byte[] encodedAudioData = null;
if (encodedBytes != 0)
{
encodedAudioData = new byte[encodedBytes];
Array.Copy(encodedData, 0, encodedAudioData, 0, encodedBytes);
}
rawDataSize = inDataSize; // Count of encoded shorts, for debugging
return encodedAudioData;
}
SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow);
public byte[] Decode(byte[] encodedData)
{
try
{
short[] decodedFrame = new short[8000]; // should be the same number of samples as on the capturing side
int decoderBytes = decoder.Decode(encodedData, 0, encodedData.Length, decodedFrame, 0, false);
byte[] decodedData = new byte[encodedData.Length];
byte[] decodedAudioData = null;
decodedAudioData = new byte[decoderBytes * 2];
for (int shortIndex = 0, byteIndex = 0; byteIndex < decoderBytes; shortIndex++)
{
BitConverter.GetBytes(decodedFrame[shortIndex + byteIndex]).CopyTo(decodedAudioData, byteIndex * 2);
byteIndex++;
}
// todo: do something with the decoded data
return decodedAudioData;
}
catch (Exception ex)
{
ShowMessageBox(ex.Message.ToString());
return null;
}
}
抱歉,伙计们反应太晚了,但我知道问题出在哪里了 在我的decode函数中,我循环遍历解码的
short
数组,但我只将一半字节复制到我的新byte
数组中
它需要看起来像这样:
decodedAudioData = new byte[decoderBytes * 2];
for (int shortIndex = 0, byteIndex = 0; shortIndex < decodedFrame.Length; shortIndex++, byteIndex += 2)
{
byte[] temp = BitConverter.GetBytes(decodedFrame[shortIndex]);
decodedAudioData[byteIndex] = temp[0];
decodedAudioData[byteIndex + 1] = temp[1];
}
decodedAudioData=新字节[decoderBytes*2];
对于(int shortIndex=0,byteIndex=0;shortIndex
问题是什么还不太清楚。你能编辑你的问题吗?你能为你的SavePcmToWav
方法添加源代码吗?如果生成的WAV文件的采样率为44.1kHz而不是8kHz,则会产生花栗鼠声音(即音频数据播放得太快)。
decodedAudioData = new byte[decoderBytes * 2];
for (int shortIndex = 0, byteIndex = 0; shortIndex < decodedFrame.Length; shortIndex++, byteIndex += 2)
{
byte[] temp = BitConverter.GetBytes(decodedFrame[shortIndex]);
decodedAudioData[byteIndex] = temp[0];
decodedAudioData[byteIndex + 1] = temp[1];
}