about the exception "Invalid mode encountered:m"

Aug 29, 2011 at 5:01 AM
Edited Aug 29, 2011 at 5:17 AM

Hi, now i'm using NSpeex to capture the sound from a microphone (using SL microphone demo) but can not decode the data with the exception "Invalid mode encountered: 9". Can any one know the reason or solution of the exception?

Here is my code:

        private byte[] Encode(byte[] soundData)
        {
            int frameSize = m_speexEncoder.FrameSize;

            int inDataSize = soundData.Length / 2;
            inDataSize = inDataSize - inDataSize % frameSize;

            //Convert byte array to short array
            short[] data = new short[inDataSize];
            for (int i = 0; i < inDataSize; i++)
            {
                data[i] = BitConverter.ToInt16(soundData, i * 2);
            }

            int encodedSize = 0;
            List<byte[]> encodedData = new List<byte[]>();
            byte[] outData = new byte[1024];
            for (int i = 0; i + frameSize < inDataSize; i += frameSize)
            {
                int bytesEncoded = m_speexEncoder.Encode(data, i, frameSize, outData, 0, frameSize);

                byte[] encodedFrame = new byte[bytesEncoded];
                Array.Copy(outData, encodedFrame, bytesEncoded);

                encodedData.Add(encodedFrame);
                encodedSize += bytesEncoded;
            }

            int offset = 0;
            byte[] retData = new byte[encodedSize];
            foreach (var bs in encodedData)
            {
                Array.Copy(bs, 0, retData, offset, bs.Length);
                offset += bs.Length;
            }

            return retData;
        }

        private byte[] Decode(byte[] encodedData)
        {
            int frameSize = m_speexDecoder.FrameSize;
            short[] data = new short[1024];

            int totalBytes = 0;
            List<byte[]> tempBuffer = new List<byte[]>();

            for (int i = 0; i + frameSize < encodedData.Length; i += frameSize)
            {
                int shortsDecoded = m_speexDecoder.Decode(encodedData, i, frameSize, data, 0, false);

                byte[] decodedData = new byte[shortsDecoded * 2];
                for (int j = 0; j < shortsDecoded; j++)
                {
                    BitConverter.GetBytes(data[i]).CopyTo(decodedData, j * 2);
                }

                tempBuffer.Add(decodedData);

                totalBytes += (shortsDecoded * 2);
            }

            byte[] outData = new byte[totalBytes];
            int offset = 0;
            foreach (var bs in tempBuffer)
            {
                Array.Copy(bs, 0, outData, offset, bs.Length);
                offset += bs.Length;
            }

            return outData;
        }

Thanks very much!

Coordinator
Aug 29, 2011 at 5:07 AM

You need to know the exact frame size after encoding. The frame size of the decoder tells you the decoded size.

Please check other posts to find more details on that.

Am 29.08.2011 06:01 schrieb "imliyd" <notifications@codeplex.com>:
> From: imliyd
>
> Hi, now i'm using NSpeex to capture the sound from a microphone (using SL microphone demo) but can not decode the data with the exception "Invalid mode encountered: 9". Can any one know the reason or solution of the exception?Here is my code:private byte[] Encode(byte[] soundData)
> {
> int frameSize = m_speexEncoder.FrameSize;
>
> int inDataSize = soundData.Length / 2;
> inDataSize = inDataSize - inDataSize % frameSize;
>
> //Convert byte array to short array
> short[] data = new short[inDataSize];
> for (int i = 0; i < inDataSize; i++)
> {
> data[i] = BitConverter.ToInt16(soundData, i * 2);
> }
>
> int encodedSize = 0;
> List<byte[]> encodedData = new List<byte[]>();
> byte[] outData = new byte[1024];
> for (int i = 0; i + frameSize < inDataSize; i += frameSize)
> {
> int bytesEncoded = m_speexEncoder.Encode(data, i, frameSize, outData, 0, frameSize);
>
> byte[] encodedFrame = new byte[bytesEncoded];
> Array.Copy(outData, encodedFrame, bytesEncoded);
>
> encodedData.Add(encodedFrame);
> encodedSize += bytesEncoded;
> }
>
> int offset = 0;
> byte[] retData = new byte[encodedSize];
> foreach (var bs in encodedData)
> {
> Array.Copy(bs, 0, retData, offset, bs.Length);
> offset += bs.Length;
> }
>
> return retData;
> }private byte[] Decode(byte[] encodedData)
> {
> int frameSize = m_speexDecoder.FrameSize;
> short[] data = new short[1024];
>
> int totalBytes = 0;
> List<byte[]> tempBuffer = new List<byte[]>();
>
> for (int i = 0; i + frameSize < encodedData.Length; i += frameSize)
> {
> int shortsDecoded = m_speexDecoder.Decode(encodedData, i, frameSize, data, 0, false);
>
> byte[] decodedData = new byte[shortsDecoded * 2];
> for (int j = 0; j < shortsDecoded; j++)
> {
> BitConverter.GetBytes(data[i]).CopyTo(decodedData, j * 2);
> }
>
> tempBuffer.Add(decodedData);
>
> totalBytes += (shortsDecoded * 2);
> }
>
> byte[] outData = new byte[totalBytes];
> int offset = 0;
> foreach (var bs in tempBuffer)
> {
> Array.Copy(bs, 0, outData, offset, bs.Length);
> offset += bs.Length;
> }
>
> return outData;
> }Thanks very much!
>
>
Aug 29, 2011 at 7:04 AM

Hi balistof, thx for your rapid reply. Do you mean using the enconder.FrameSize during decoding? or the total bytes after encoding? I have referred the post with name "Having trouble encoding/decoding" but still not very clear about how to set the buffer size. In my code, both encoder and decoder are initialized with "BandMode.Wide".

Thx, br.

Aug 29, 2011 at 8:30 AM

I checked other posts carefully, and finally got how to set the decode parameters...

Thanks very much.

May 15, 2013 at 11:57 PM
Hi imliyd,

How do you solve your problem?
I trying to do Encode and Decode, but I have problems when I try do play in ohters device.
Can you post your code here?