naudio / NLayer

MPEG 1 & 2 Decoder for Layers 1, 2, & 3
MIT License
124 stars 30 forks source link

DownmixToMono doesn't convert to mono #5

Open uluhonolulu opened 7 years ago

uluhonolulu commented 7 years ago

Hi,

I'm trying to convert an .mp3 file to a mono .wav file. I'm using StereoMode.DownmixToMono in the decompressor, but I'm still getting a stereo file in the output. Here's my code:

var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf){StereoMode = StereoMode.DownmixToMono});
using (var reader = new Mp3FileReader(mp3FileName, builder)) {
    using (var writer = new WaveFileWriter(waveFileName, reader.WaveFormat))
        reader.CopyTo(writer);
}

If I specify a mono format, I get a double length mono record filled with garbage:

var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(wf){StereoMode = StereoMode.DownmixToMono});
using (var reader = new Mp3FileReader(mp3FileName, builder)) {
    var waveFormat = new WaveFormat(reader.WaveFormat.SampleRate, 1);
    using (var writer = new WaveFileWriter(waveFileName, waveFormat))
        reader.CopyTo(writer);
}

I also tried specifying a mono format in the Mp3FrameDecompressor constructor:

var builder = new Mp3FileReader.FrameDecompressorBuilder(wf => new Mp3FrameDecompressor(new WaveFormat(44100, 1)){StereoMode = StereoMode.DownmixToMono});
using (var reader = new Mp3FileReader(mp3FileName, builder)) {
    using (var writer = new WaveFileWriter(waveFileName, reader.WaveFormat))
        reader.CopyTo(writer);
}

The result was a mono record slown down 2x.

What am I doing wrong? How do you use the DownmixToMono option properly?

ioctlLR commented 7 years ago

To be honest, I haven't tested that option in quite some time. I'll take a look and see what needs to happen.