This project has moved. For the latest updates, please go here.

WaveBuffer wrong sampling?

Oct 31, 2009 at 3:46 PM

Hi everybody,

First I'd like to thanks for this great project who is helping me a lot managing with audio devices.

I've a question about how we use WaveBuffer for capturing (in order to not convert byte[] to short[] everytime I've a new buffer)...

All tests are made generating a sine of 1000Hz to the line-in.

In brief here is my problem:


format = new WaveFormat(44100, 16, 1);
            waveInStream = new WaveIn();
            waveInStream.WaveFormat = format;

waveInStream.DataAvailable += new EventHandler(waveInStream_DataAvailable);


Here is where I capture my data: (e.Buffer is a byte[] buffer so I use the WaveBuffer to "abstract" it)

private void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
            WaveBuffer wb = new WaveBuffer(e.Buffer);


This function prepare a debugBuffer who display in a Graph widget the last 500 elements of my last buffer and the first 500 elements of the new one:

        public void SetBufferDebug(WaveBuffer s)
            if (position == 0)
                for (int i = 0; i < 500; i++)
                    debugArray[499-i] = s.ShortBuffer[(s.ShortBuffer.Length-1)-i];

                position = 1;
                int j = 0;
                for (int i = 0; i < 500; i++)
                    debugArray[500+i] = s.ShortBuffer[i];

                position = 0;



The results are strange, once in a while the last part of my buffer is wrong, like noise:

Free Image Hosting at

and the other time the capturing is perfect:

Free Image Hosting at

I'm I using WaveBuffer wrong? There is another way to capture directly a buffer of shorts when using 16 bits sampling?

Thanks for the attention....



Oct 31, 2009 at 7:26 PM

have you checked that the BytesRecorded member of the WaveInEventArgs is what you expect?


Oct 31, 2009 at 10:47 PM

Thanks for the answer Mark,

What is expected is what I received, in case I sample at 16bits per sample, my single sample is composed by 2 elements of my bytes array, and if just @8 bits I've one sample each element.

So If I manually "convert" my buffer to short[] using:

 samplingBuffer[i] = (short)((buff[i] & 0xff) | (buff[i + 1] << 8));
// samplingBuffer is my destination array of shorts
// buff is my e.Buffer

The samplingBuffer returned is perfectly working and the graph show my signal correctly...

I'm asking myself if this is the same that happens when I use the WaveBuffer.ShortBuffer attribute. When I'm calling my WaveBuffer as a ShortBuffer did it automatically "aggregate" the two bytes in single one short?