微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

audio – Silverlight Speex以极快的速度播放

我正在使用Speex对原始数据进行编码,但在对数据进行解码后,音频以更快的速度播放,因为它会让您听起来像花栗鼠.我正在使用 NSpeex和Silverlight 4.

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,inDataSize,encodedData,encodedData.Length);

        byte[] encodedAudioData = null;
        if (encodedBytes != 0)
        {
            encodedAudioData = new byte[encodedBytes];
            Array.copy(encodedData,encodedAudioData,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,encodedData.Length,decodedFrame,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;
        }

    }

播放音频

void PlayWave(byte[] PCMBytes)
    {
        byte[] decodedBuffer = Decode(PCMBytes);
        MemoryStream ms_PCM = new MemoryStream(decodedBuffer);
        MemoryStream ms_Wave = new MemoryStream();

        _pcm.SavePcmToWav(ms_PCM,ms_Wave,16,8000,1);

        Wavemediastreamsource WaveStream = new Wavemediastreamsource(ms_Wave);
        mediaElement1.SetSource(WaveStream);
        mediaElement1.Play();
    }

解决方法

对于迟到的回应抱歉,但我弄清楚问题是什么.

在我的解码函数中,我遍历解码的短数组,但我只是将一半的字节复制到我的新字节数组中.

它需要看起来像这样:

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];
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐