Search code examples
c#naudiopcm

Why is the wav file size exceeding 4gb?


I am using Naudio library in c#.

I have two pcm data (byte array) and I want to mix.

For pcm data, the sample rate is 16000hz, channal is mono, bitpersample is 8.

The size of the byte array is not that big.

Each of them is about 100 to 200 kb in size.

In the code below, mainAudioData and RepeaterAudioData are byte arrays.

var imawaveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, 16000, 1, 16000, 256, 8);
var mainbwp = new BufferedWaveProvider(imawaveFormat);
mainbwp.BufferLength = mainAudioData.Length;
mainbwp.AddSamples(mainAudioData, 0, mainAudioData.Length);

var repeaterbwp = new BufferedWaveProvider(imawaveFormat);
repeaterbwp.BufferLength = RepeaterAudioData.Length;
repeaterbwp.AddSamples(RepeaterAudioData, 0, RepeaterAudioData.Length);


var mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(16000,1));
mixer.AddMixerInput(mainbwp.ToSampleProvider());
mixer.AddMixerInput(repeaterbwp.ToSampleProvider());
WaveFileWriter.CreateWaveFile16("D:\\mixed.wav", mixer);

but, when creating a file in'WaveFileWriter.CreateWaveFile16("D:\mixed.wav", mixer);', the file size exceeds 4gb and an exception of 'wav file too long' occurs.

Why are files over 4 GB created?


Solution

  • it's because BufferedWaveProvider will produce a never-ending audio stream (that's what it's designed for). RawSourceWaveStream is a more appropriate choice for your scenario where you have raw audio data in a byte[] and want to play it directly