IndexOutOfRangeException: my solution

Nov 28, 2011 at 6:42 AM

Like many others, I ran into this issue when decoding.

As some who've read the code have noticed, Bits.cs:48 defines a DefaultBufferSize of 1024 bytes, and if you pass an input buffer/length larger than this size to the Decode method, you'll get this exception.

Reading further through the code, it appears that there's no inherent limitation on the input size besides the fact that the internal bytes[] buffer never gets reallocated - a very simple fix.

Adding the following lines of code at Bits.cs:104 (i.e. beginning of ReadFrom()) solved this issue for me:

            // added 2011-11-27 OG to support buffers > 1K
            if (bytes.Length < len)
                bytes = new byte[len];
            // end added code

This way, I don't have to decode in 1K chunks...

Cristoph - any chance you could add this to the source?  (Or explain why the decoding should only happen in smaller chunks)?  FWIW, decoding like this has worked for me for large buffer sizes (e.g. original input buffer lengths of 64000 bytes which compressed to about 6000 bytes).

Thanks,

Omri.

p.s. BTW, the other "gotcha" is that the fourth parameter passed into the SpeexDecoder.Decode() method - the short[] array, appears to have to be exactly sized like the one that was passed into SpeexEncoder.Encode().  (And of course this array size needs to be a multiple of SpeexEncoder.FrameSize).  Assuming you are encoding/decoding in chunks like I am, and may have variable-sized chunks: I solved this issue by prefixing each encoded chunk with its pre-encoding short[] array size, in order to make sure I have the proper size for the short[] array to pass into the Decoder.

 

Coordinator
Nov 28, 2011 at 3:08 PM

Hi Omri,

I have anticipated the problem in Bits.cd but never found some time to fix it. So thanks for you mentioned fix and I hope I can bring that into the code by end of this week.

Regarding frame size: Speex works on 160 samples per frame and it is designed for voip applications. So it does not make sense to have large audio chunks. In fact, you want to encode very small audio chunks which is then somewhat in conflict with compression. That said the short array being passed to the decode method has to at least of a size that can hold the decoded data. There should not be a limitation to multiplies of framesize.

Christoph

Am 28.11.2011 07:42 schrieb "ogazitt" <notifications@codeplex.com>:

From: ogazitt

Like many others, I ran into this issue when decoding.

As some who've read the code have noticed, Bits.cs:48 defines a DefaultBufferSize of 1024 bytes, and if you pass an input buffer/length larger than this size to the Decode method, you'll get this exception.

Reading further through the code, it appears that there's no inherent limitation on the input size besides the fact that the internal bytes[] buffer never gets reallocated - a very simple fix.

Adding the following lines of code at Bits.cs:104 (i.e. beginning of ReadFrom()) solved this issue for me:

            // added 2011-11-27 OG to support buffers > 1K
            if (bytes.Length < len)
                bytes = new byte[len];
            // end added code

This way, I don't have to decode in 1K chunks...

Cristoph - any chance you could add this to the source? (Or explain why the decoding should only happen in smaller chunks)? FWIW, decoding like this has worked for me for large buffer sizes (e.g. original input buffer lengths of 64000 bytes which compressed to about 6000 bytes).

Thanks,

Omri.

p.s. BTW, the other "gotcha" is that the fourth parameter passed into the SpeexDecoder.Decode() method - the short[] array, appears to have to be exactly sized like the one that was passed into SpeexEncoder.Encode(). (And of course this array size needs to be a multiple of SpeexEncoder.FrameSize). Assuming you are encoding/decoding in chunks like I am, and may have variable-sized chunks: I solved this issue by prefixing each encoded chunk with its pre-encoding short[] array size, in order to make sure I have the proper size for the short[] array to pass into the Decoder.

Read the full discussion online.

To add a post to this discussion, reply to this email (nspeex@discussions.codeplex.com)

To start a new discussion for this project, email nspeex@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Coordinator
Nov 30, 2011 at 4:33 PM

Hi Omri,

I just commited a fix for the Bits buffer as well as for the decode buffer based on your suggestions. So if you download the latest release it should work as expected!

Christoph

Dec 1, 2011 at 7:07 PM
Awesome - thanks Christoph!

Sent from my Windows Phone

From: balistof
Sent: 11/30/2011 8:33 AM
To: ogazitt@hotmail.com
Subject: Re: IndexOutOfRangeException: my solution [nspeex:281000]

From: balistof

Hi Omri,

I just commited a fix for the Bits buffer as well as for the decode buffer based on your suggestions. So if you download the latest release it should work as expected!

Christoph