Decoding problem: sound is playing faster then initial record

May 3, 2012 at 12:49 PM

After encoding PCM audio into Speex and decoding it back, result sound is faster then initial.

Can please anyone help ne with this?

 

This code is for WP7

 

        public byte[] Decode(byte[] raw)
        {
            var temp = EncodeAudio(raw);
            return DecodeAudio(temp);
        }


        private List<byte[]> EncodeAudio(byte[] rawSound)
        {

            var res = new List<byte[]>();

            int size = ((int) ((rawSound.Length/2)/encoder.FrameSize))*encoder.FrameSize;

            var data = new short[size];
            
            for (int index = 0, sampleIndex = 0; sampleIndex < data.Length; index += 2, sampleIndex++)
            {
                data[sampleIndex] = BitConverter.ToInt16(rawSound, index);
            }

                var ms = new MemoryStream();
                var pcmPacketSize = encoder.FrameSize;

                for (int i = 0; i < size; i+=pcmPacketSize)
                {
                    var tmpArr = new short[pcmPacketSize];
                    Array.Copy(data, i, tmpArr, 0, pcmPacketSize);
                    var encodedData = new byte[rawSound.Length];

                    var encodedBytes = encoder.Encode(tmpArr, 0, tmpArr.Length, encodedData, 0, encodedData.Length);

                    if (encodedBytes != 0)
                    {
                        var t = new byte[encodedBytes];
                        Array.Copy(encodedData, t, encodedBytes);

                        res.Add(t);
                    }

                }
                return res;

        }


        //Decoding Part
        public byte[] DecodeAudio(List<byte[]> encodedAudioData)
        {
            MemoryStream ms = new MemoryStream();

            foreach (byte[] dataChunk in encodedAudioData)
            {
                short[] decodedFrame = new short[encoder.FrameSize];
                int decodedBytes = decoder.Decode(dataChunk, 0, dataChunk.Length, decodedFrame, 0, false);
                byte[] DecodedBuffer = new byte[decodedBytes];

                for (int i = 0, j =0; i < decodedBytes/2; i++, j+= 2)
                {
                    DecodedBuffer[j] = BitConverter.GetBytes(decodedFrame[i])[0];
                    DecodedBuffer[j + 1] = BitConverter.GetBytes(decodedFrame[i])[1];
                }
                ms.Write(DecodedBuffer, 0, DecodedBuffer.Length);
            }

            return ms.ToArray();
        }

 

 

Coordinator
May 5, 2012 at 5:05 PM

Two things which come to my mind are:

- how are you playing it back? Have you set the correct frequency?

- Are decoder and encoder initialized with the same settings?

Christoph