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

How to make/control # of bytes read from IWavePlayer?

Feb 23, 2011 at 7:12 AM
Edited Feb 26, 2011 at 2:36 PM

Hi! I have the following code:


IWavePlayer player = new WaveOut();
WaveStream reader = new WaveFileReader(filePath);
WaveChannel32 input = new WaveChannel32(reader);

AmplitudeStream ampStream = new AmplitudeStream(input);
ampStream.AmplitudeEventHandler += new EventHandler<AmplitudeArgs>(AmplitudeStreamHandler);



AmplitudeStream is a class I made that implements WaveStream so that I can 'track' and send an event whenever data is read from the file. However, my problem is that I want to control how much bytes are read. Since the Read function of AmplitudeStream is being called by the Play() function, I have no idea on how I can limit the number of bytes read (it is always 35,280). How to do this? I am using the Play() command because I want my file to play so that I can observe. So if there is another way to play the file during the Read() command, which would look something like:


byte[] buffer = byte[8192]
int bytesRead = 0;


bytesRead = ampStream.Read(buffer,0,buffer.Length);

}while(bytesRead != 0);

private int Read(byte[] buffer, int offset, int count)

//some command to play the file here


I would like to know. Thanks! Aside from that, is there a way to not use WaveChannel32? Because for some reason, Im getting some errors when I use the reader for input into the AmplitudeStream (mainly because of some byte-float conversion issue).

Mar 2, 2011 at 11:10 AM

the WaveOut buffer sizes control the amount that is Read each time. You can always simply call Read yourself on the wave stream with whatever buffer sizes you like.

WaveChannel32 converts from 16 bit short samples to 32 bit floating point samples. You don't have to use it if you don't want to


Mar 2, 2011 at 11:15 AM


Thanks! Currently that is what I do, I just manually read from AmplitudeStream. However, what if I want to play the file at the same time? Is there an alternative way to play the file instead of the play function?

Mar 2, 2011 at 11:40 AM

well your choices are either to modify the WaveOut buffer sizes, or to put your own buffering code in AmplitudeStream.Read so that it deals with blocks of the size you want. This is how the metering code in the NAudioDemo application works


Mar 2, 2011 at 11:41 AM

Ok I will try that. Ill post again when I have other questions.


Mar 7, 2011 at 7:30 AM


So ive tried experimenting with modifying the WaveOut and here is what I have so far:

WaveOut outw = new WaveOut();
WaveStream reader = new WaveFileReader(filePath);

int buffersize = (int)(reader.Length / 8192);
outw.NumberOfBuffers = buffersize;
player = outw;

ampStream = new Algo2Stream(reader, int.Parse(txtBoxSPS2.Text));
ampStream.Algo2EventHandler += new EventHandler<Algo2Args>(Algo2StreamHandler);

overallStart = DateTime.Now;

However I am having a bit of a problem since whenever I go in between and observe the buffersize during the Read command, I see that it is only a very small number (I think around 80-200). So it seems I am mis-using the NumberOfBuffers variable of the WaveOut.

Can you help me with this? Thanks!

Mar 7, 2011 at 5:15 PM

number of buffers should be either 2 or 3. You control their size by specifying the latency you want.