Search code examples
naudio

What is the purpose of NAudio wavein NumberOfBuffers setting?


I have a very simple application that monitors the computer sound card for a signal produced by an external device attached to the sound card. I am using NAudio to interact with the sound card:

  wavein = New NAudio.Wave.WaveIn
  wavein.DeviceNumber = _deviceId
  wavein.WaveFormat = New NAudio.Wave.WaveFormat(44100, 1)
  wavein.BufferMilliseconds = 250
  AddHandler wavein.DataAvailable, AddressOf onDataAvailable
  wavein.StartRecording()

I noticed it is possible to adjust the number of buffers used by the wavein object, for example:

  wavein.NumberOfBuffers = 5

The default NumberOfBuffers setting is 3 I believe. Is there any reason one would ever want to change the number of buffers? If not for my specific application, then for another type of application?


Solution

  • The buffers are cycled through in turn, so if you have 3 buffers, it fills one and returns it, then fills the next, then fills the next, then goes back to the first one. Usually 2 is more than sufficient. The main thing is that your app finishes processing the audio in that buffer before waveIn starts writing over the top of it again.

    The reason this property is configurable and defaults to 3 is that when I started creating NAudio back in 2002, my computer wasn't powerful enough to play back audio without stuttering unless I used at least 3 buffers (I had a 400MHz Pentium III if I recall correctly). CPUs are so much faster these days that this is unlikely to be an issue for you.