I want to play wav files on NAudio BufferedWaveProviders that have a this format: WaveFormat.CreateIeeeFloatWaveFormat(8000, 1);
The format of my wave files is 16 bit PCM: 44kHz 2 Channels. I am reading bytes out of the file and adding them as samples to the bufferedWaveProvider. With the format I want to use (which is existing to the application) there is no audio at all. With a standard format (new WaveFormat()), the audio works just fine. Is it Possible to manipulate the wave file data to play on the requested format?
bufferedWaveProvider = new BufferedWaveProvider(WaveFormat.CreateIeeeFloatWaveFormat(8000, 1);
player = new WaveOut();
player.DeviceNumber = -1;
player.Init(bufferedWaveProvider);
player.Play();
using (WaveFileReader reader = new WaveFileReader ("filePath")
{
int end = (int)reader.Length;
byte[] buffer = new byte[336];
while (reader.Position < end)
{
int bytesRequired = (int)(end - reader.Position);
if (bytesRequired > 0)
{
int bytesToRead = Math.Min(bytesRequired,buffered.Length);
int bytesRead = reader.Read(buffer, 0 , bytesToRead);
if (bytesRead > 0)
{
bufferedWaveProvider.AddSamples(buffer, 0, bytesRead);
}
}
}
}
I also have a side question. while I was figuring out how to stream the wave file data I had to experiment with the bytes buffer size to send because if its too small the audio is choppy and if its too large the buffer overflows. Though trial and error I found 336 to be the best buffer size for a wave format of 16 bits, 44100 sample rate, 2 channels. How are you supposed to calculate the sample size so I can automatically know what size works for any given format?