Trouble converting an MP3 file to a WAV file using

2019-05-26 01:57发布

问题:

Naudio Library: http://naudio.codeplex.com/

I'm trying to convert an MP3 file to a WAV file, but I've run in to a small error. I know what's going wrong, but I don't really know how to go about fixing it.

Here's the piece of code I'm running:

private void button1_Click(object sender, EventArgs e) {
    using(Mp3FileReader reader = new Mp3FileReader(@"path\to\MP3")) {
        using(WaveFileWriter writer = new WaveFileWriter(@"C:\test.wav", new WaveFormat())) {
            int counter = 0;
            while(reader.Read(test, counter, test.Length + counter) != 0) {
                writer.WriteData(test, counter, test.Length + counter);
                counter += 512;
            }
        }
    }
}

reader.Read() goes into the Mp3FileReader class, and the method looks like this:

public override int Read(byte[] sampleBuffer, int offset, int numBytes)
{
    if (numBytes % waveFormat.BlockAlign != 0)
        //throw new ApplicationException("Must read complete blocks");
        numBytes -= (numBytes % waveFormat.BlockAlign);
    return mp3Stream.Read(sampleBuffer, offset, numBytes);
}

mp3Stream is an object of the Stream class.

The problem is: I'm getting an ArgumentException. MSDN says that this is because the sum of offset and numBytes is greater than the length of sampleBuffer.

Documentation: http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx

This happens because I increase the counter every time, but the size of the byte array test remains the same.

What I've been wondering is: do I need to increase the size of the array dynamically, or do I need to find out the needed size at the beginning and set it right away?

And also, instead of 512, the method in Mp3FileReader returns 365 the first time. Which is the size of a whole block. But I'm writing the full 512. I'm basically just using the read to check if I'm not at the end of the file yet. Do I need to catch the return value and do something with that, or am I good here?

回答1:

You'll need to use the return value of Read() to determine how many bytes you actually got. It doesn't have to be 512, you already found that out. And keep in mind that you are working with streams, not arrays. Make it look similar to this:

   using (var reader = new Mp3FileReader(@"path\to\MP3")) {
        using (var writer = new WaveFileWriter(@"C:\test.wav", new WaveFormat())) {
            var buf = new byte[4096];
            for (;;) {
                var cnt = reader.Read(buf, 0, buf.Length);
                if (cnt == 0) break;
                writer.WriteData(buf, 0, cnt);
            }
        }
    }


回答2:

You're not actually performing any conversion from MP3 into WAV in your example. You need the WaveFormatConversionStream. Try something like this:

private void button1_Click(object sender, EventArgs e) {
    using(Mp3FileReader reader = new Mp3FileReader(@"path\to\MP3")) {
        using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader)) {
            WaveFileWriter.CreateWaveFile(outputFileName, convertedStream);
        }
    }
}


标签: c# mp3 wav naudio