tags:

views:

149

answers:

2

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?

+2  A: 

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(Mp3FileReader reader = new Mp3FileReader(@"path\to\MP3")) {
        using(WaveFileWriter writer = new WaveFileWriter(@"C:\test.wav", new WaveFormat())) {
            byte buf = new byte[4096];
            for (;;) {
                int cnt = reader.Read(buf, 0, buf.Length);
                if (cnt == 0) break;
                writer.WriteData(buf, 0, cnt);
            }
        }
    }
Hans Passant
Okay, I'm doing that now, and the result is a lot of static noise. Still, that's a whole different problem.
WebDevHobo
A: 

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);
        }
    }
}
Mark Heath
Looks good, but it won't run. I keep getting an "AcmNotPossible calling acmStreamOpen" exception.
WebDevHobo
that's probably because your target format doesn't match the natural converted format of the MP3. I'll update the code sample
Mark Heath