Encode throwing "Index out of range"

Dec 21, 2010 at 9:04 PM
Edited Dec 21, 2010 at 9:06 PM

Hi there,

I'm trying to take data from an NAudio WaveIn and encode it with SpeexEncoder.Encode, but I'm getting an index out of range when I have bytesRecorded == 1600 (though, less than that seems to work...).  My encoding method looks pretty much just like the example, but maybe I screwed something up, I dunno.  Any help would be most appreciated :)

static private byte[] Encode( byte[] buffer, int bytesRecorded )
    short[] data = new short[ bytesRecorded / 2 ];
    int sampleIndex = 0;

    for ( int index = 0; index < bytesRecorded; index += 2, sampleIndex++ )
        data[ sampleIndex ] = BitConverter.ToInt16( buffer, index );

    var encodedData = new byte[ bytesRecorded ];
    // note: the number of samples per frame must be a multiple of encoder.FrameSize
    var encodedBytes = encoder.Encode( data, 0, sampleIndex, encodedData, 0, bytesRecorded );

    return encodedData;

Note:  buffer and bytesRecorded are filled directly from an NAudio's WaveIn's DaveAvailable event.

P.S.  Just remebered, here's how the objects are being initialized:

static private WaveIn waveInput = new WaveIn( WaveCallbackInfo.FunctionCallback() );
static private WaveFormat waveFormat = new WaveFormat( 8000, 1 );
static private SpeexEncoder encoder = new SpeexEncoder( BandMode.Wide );




Dec 21, 2010 at 11:50 PM

I think I figured this out, and decided to post a reply just in case someone comes across this.  My issue was that I was setting my WaveFormat's sample rate to 8000, but using wide-band in speaks which wanted 16000.  In any case, this stopped it from throwing exceptions :)