English 中文(简体)
Trouble converting an MP3 file to a WAV file using Naudio
原标题:

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	oMP3")) {
        using(WaveFileWriter writer = new WaveFileWriter(@"C:	est.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?

最佳回答

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	oMP3")) {
        using (var writer = new WaveFileWriter(@"C:	est.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);
            }
        }
    }
问题回答

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	oMP3")) {
        using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader)) {
            WaveFileWriter.CreateWaveFile(outputFileName, convertedStream);
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签