ulu
ulu

Reputation: 6092

Converting wav file to mono

I'm trying to convert a stereo .wav file to mono using NAudio. One requirement is that I cannot use any native calls, as I need to run this code on Azure. Here's what I came with:

using (var waveFileReader = new WaveFileReader(sourceFileName))
    {
        var toMono = new StereoToMonoProvider16(waveFileReader);
        WaveFileWriter.CreateWaveFile(destFileName, toMono);
    }

My code works without errors, but the output is a file that contains pure silence.

Is there any other way to convert a file to mono?

Upvotes: 0

Views: 4496

Answers (3)

Mark Heath
Mark Heath

Reputation: 49482

You need to provide values for the LeftVolume and RightVolume properties of the StereoToMonoProvider16. For example, set them both to 0.5f to mix the channels, or set left to 1.0 and right to 0.0 to discard the right channel

Upvotes: 1

ulu
ulu

Reputation: 6092

This code worked for me, even on Azure:

private void ConvertToMono(string sourceFileName, string destFileName) {
    var monoFormat = new WaveFormat(44100, 1);
    using (var waveFileReader = new WaveFileReader(sourceFileName))
    {
        var floatTo16Provider = new WaveFloatTo16Provider(waveFileReader);
        using (var provider = new WaveFormatConversionProvider(monoFormat, floatTo16Provider))
        {
            WaveFileWriter.CreateWaveFile(destFileName, provider);
        }
    }

}

Note that I'm using an additional converter (WaveFloatTo16Provider), since the source was in the float format.

Upvotes: 2

Beginner
Beginner

Reputation: 1030

This code worked for me.

public static void StereoToMono(string sourceFile, string outputFile)
    {
        using (var waveFileReader = new WaveFileReader(sourceFile))
        {
            var outFormat = new WaveFormat(waveFileReader.WaveFormat.SampleRate, 1);
            using (var resampler = new MediaFoundationResampler(waveFileReader, outFormat))
            {
                WaveFileWriter.CreateWaveFile(outputFile, resampler);
            }
        }
    }

Notice that channels parameter is passed 1 for outFomat.

Upvotes: 2

Related Questions