WaveIn - get specific number of samples

Nov 23, 2009 at 9:45 AM
Edited Nov 23, 2009 at 9:50 AM


I tried to get a specific number of samples from the soundcard. The only way until now seems to set the bufferlength over the "BufferMillisconds" (think the misseplling will be corrected in the next release).

This background brought me to the following solution (for testing purposes):


private void btnGetSamples_Click(object sender, EventArgs e)
btnGetSamples.Enabled = false;
nudMs.Enabled = false;

waveInStream = new WaveIn();
waveInStream.BufferMillisconds = (int) nudMs.Value;
waveInStream.WaveFormat = new WaveFormat(44100, 16, 1);
waveInStream.NumberOfBuffers = 1;
waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);


void waveInStream_DataAvailable(object sender, WaveInEventArgs e)

Debug.WriteLine("BytesRecorded: " + e.BytesRecordehttp://naudio.codeplex.com/Thread/Create.aspxd);


btnGetSamples.Enabled = true;
nudMs.Enabled = true;

Note that I use 44100 Hz, 16 bit deepth and one channel. Moreover, I use only one buffer because the recording is stopped in the DataAvailable-method.

This works fine for most cases but when I try to get the samples for 105 ms, I get 9261 bytes (16 bits per sample (two bytes per sample), so there is a half sample).
That is not nice for converting to double and I don't know what to do with the half sample in case of recording for a while (and not stop directly after getting the data).

Little calculation for understanding what is going on:
44100 samples per second - I want to get 105 ms => 44100 Hz * 0.105 = 4630,5 samples
16 bit deepth leads to: 4630,5 samples * 2 bytes per sample = 9261 bytes.

I don't know if this is a bug or a feature^^


Nov 23, 2009 at 10:00 AM


the mispelling has been fixed.

I should probably do a block-alignment adjustment on the buffer size when you set BufferMilliseconds.



Nov 23, 2009 at 11:58 AM
Edited Nov 23, 2009 at 12:03 PM

That would be nice. There could be an additional property for setting (and getting) the number of samples to get ;-)



I asked myself in which cases the BytesRecorded differ from the buffersize?