0

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?

ulu
  • 5,872
  • 4
  • 42
  • 51
  • .wav file is an array of bytes. so you can proccess it without any external library. The only question is how do you define the convertion? I mean, if you have two channels - L,R - what is youe expected output? their sum? average? – TDG Aug 15 '17 at 17:10
  • Yes, I know, but I thought that I didn't have to spend time on figuring wav file format and code all that from scratch. Sure a library like NAudio should have something like that already, no? In fact, I could do pretty much anything using mscorlib alone, but what's the point? – ulu Aug 15 '17 at 17:54
  • Why do you need mscorlib to read the wav file as byte array and then loop over the audio data and do simple calculation? – TDG Aug 17 '17 at 16:33
  • I mean, people can do all their tasks using mscorlib alone, but they still use external libraries to save time and effort. And I could solve my problem using bytes and stuff, but NAudio saved me a lot of time and effort. – ulu Aug 18 '17 at 07:12

3 Answers3

2

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.

Beginner
  • 1,010
  • 4
  • 20
  • 42
  • It works on my PC, but, unfortunately, not on Azure -- Unable to load DLL 'mfplat.dll'. I wonder if it's possible to convert without using native DLLs. – ulu Aug 15 '17 at 16:36
1

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.

ulu
  • 5,872
  • 4
  • 42
  • 51
1

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

Mark Heath
  • 48,273
  • 29
  • 137
  • 194