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

Feb 23, 2011 at 6:12 AM
Edited Feb 26, 2011 at 1: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);

player.Init(ampStream);
player.Play();

 

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;

do{

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

}while(bytesRead != 0);

@override
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).

Coordinator
Mar 2, 2011 at 10: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

Mark

Mar 2, 2011 at 10:15 AM

Hi!

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?

Coordinator
Mar 2, 2011 at 10: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

Mark

Mar 2, 2011 at 10:41 AM

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

Thanks!

Mar 7, 2011 at 6:30 AM

Hi!

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);

player.Init(ampStream);
overallStart = DateTime.Now;
player.Play();

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!

Coordinator
Mar 7, 2011 at 4:15 PM

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