This project has moved. For the latest updates, please go here.

MP3 to WAV Conversion Help

Nov 12, 2010 at 2:32 PM

It may be due to my extreme tiredness right now, but I can't seem to figure out the following problem.

I am trying to convert a MP3 file to a WAV file, as WAV is required for the project. However, whenever I try to read from an MP3 File, I get one of the 2 following situations:

1.) No data is read from the file at all.

2.) An infinite amount of data is read and written to a WAV file (1.38 GB in 48 sec).

The code pertaining to (1): (Does the same when using a BlockAlignedStream)

            WaveStream mp3Reader = new Mp3FileReader(fileName);
            WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
            
            byte[] buffer = new byte[8192];
            using(WaveFileWriter wavWriter = new WaveFileWriter(outFile, pcmStream.WaveFormat))
            {
                int read;
                while ((read = pcmStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    wavWriter.WriteData(buffer, 0, buffer.Length);
                }

                wavWriter.Close();
            }

The code pertaining to (2):

            WaveStream mp3Reader = new Mp3FileReader(fileName);
            WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
            WaveStream basStream = new BlockAlignReductionStream(pcmStream);
            WaveStream readStream = new WaveChannel32(basStream);            
            byte[] buffer = new byte[8192];
            using(WaveFileWriter wavWriter = new WaveFileWriter(outFile, pcmStream.WaveFormat))
            {
                int read;
                while ((read = readStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    wavWriter.WriteData(buffer, 0, buffer.Length);
                }

                wavWriter.Close();
            }

I'm not certain as to what I'm doing wrong here, any insight/help/smack-on-the-head is appreciated.

Coordinator
Nov 12, 2010 at 3:22 PM

are you able to grab hold of the latest code from CodePlex? It makes decoding mp3 much easier as it removes need for WaveFormatConversionStream and BlockAlignReduction stream. Just read out of the Mp3FileReader and write straight into the WaveFileWriter

WaveChannel32 is the reason for infinitely long files. It is intended for mixer inputs and is not really what you need here.

Nov 12, 2010 at 3:34 PM

Thanks, I'll go grab that then.