Having Problem in Decode in Silverlight

May 9, 2011 at 5:27 AM
            short[] decodedFrame = new short[decoder.FrameSize]; 
            // should be the same number of samples as on the capturing side
            var decsize = decoder.Decode(encodedData, 0, decoder.FrameSize, decodedFrame, 0, false);


hi, balistof, i have problem in decode encodedData.

using encoder, i have encoded data , but when i decode them, i confused what value should set to decodedframe

when i capture audio, i set encoder is BandMode.Narrow

the decode function caused 'index out of range' (new short[decoder.FrameSize]) or 'Invalid sideband mode encountered (1st sideband): 7' (new short[decoder.FrameSize * 2])

what should i do ? i'm really need your help !

Coordinator
May 9, 2011 at 8:13 PM

Hi,

in the decode call you are passing in the decoder.FrameSize as the length of the encodedData. This length has to be the same value as you get on the encode side. For example, you have 160 uncompressed samples (encoder.FrameSize of datatype short) now after encoding you get 75 bytes of encoded data. This 75 bytes are transfered over the wire or written onto the file system. When decoding, you actually need to pass in the array (encodedData), the starting index and the length of the encodedData array, which is in our case here 75. As a hint: you need to transfer the actual size of the encodedData array between encoder and decoder for variable bit rate.

So, the call to encoder.Decode should look like:

decoder.Decode(encodedData, 0, encodedDataLength, decodedFrame, 0, false);

Chris

May 10, 2011 at 2:53 AM

hi,

really thx for your reply.

now i can decode data  without exception

in my case, when encode, frame size is 160 (length of short array), encoded data is 38

when decode, i set encodedData length is 38, and decodedframe is 160. the function run whitout any exception

but when i playback encodedData, it was a background noise in sound.

is there anything wrong i did in encode or decode ?

Encode:

                    short[] data = new short[_Buffer.Length / 2];
                    int sampleIndex = 0;

                    for (int index = 0; index < _Buffer.Length; index += 2, sampleIndex++)
                    {
                        data[sampleIndex] = BitConverter.ToInt16(_Buffer, index);
                    }

                    var encodedData = new byte[_Buffer.Length];
                    var encodedBytes = _SpeexEncoder.Encode(data, 0, sampleIndex, encodedData, 0, _Buffer.Length);

Decode:

            var list = new List<byte>();
            SpeexDecoder decoder = new SpeexDecoder(BandMode.Narrow);
            for(int i = 0; i < encoder.Data.Length;)
            {
                var outData = new short[160];
                decoder.Decode(encoder.Data, i, 38, outData, 0, false);
                foreach (var s in outData)
                {
                    var bytes = BitConverter.GetBytes(s);
                    foreach (var b in bytes)
                        list.Add(b);
                }
                i += 38;
            }
            return list.ToArray();