Search code examples
naudio

Why is NAudio creating a WAV file that's twice the size it should be?


I'm using this fairly standard code sample to mix multiple WAV files into one output file (Dispose calls omitted for clarity):

public static void MixAudioFiles(IEnumerable<FileObject> input, FileObject output)
{
    var mixer = new WaveMixerStream32 { AutoStop = true };

    foreach (var file in input)
    {
        var reader = new WaveFileReader(file.FullName);

        mixer.AddInputStream(new WaveChannel32(reader));
    }

    WaveFileWriter.CreateWaveFile(output.FullName, new Wave32To16Stream(mixer));
}

But the output file is always twice the size of the input file, despite the use of Wave32To16Stream.

What am I missing here?


Solution

  • Thanks, Mark, that pointed me in the right direction. For anyone else with this issue, here's the working code:

        public static void MixAudioFiles(IEnumerable<string> input, string output)
        {
            var wf = new WaveFormat(16000, 1);
    
            var mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(wf.SampleRate, wf.Channels));
    
            foreach (var file in input)
            {
                var reader = new WaveFileReader(file);
    
                mixer.AddMixerInput(reader);
            }
    
            //write the mix file
            WaveFileWriter.CreateWaveFile16(output, mixer);
    }
    

    Note that I've omitted the Dispose calls. Make sure to add these to prevent resource leaks.