Tuesday, June 30, 2009

How to convert MP3 files to WAV files in .NET (C#)

I have successfully converted compressed audio to PCM files with the Alvas.Audio .NET library. Its FAQ shows how to convert a WAV to a MP3 but not the contrary. So I had to write my own code:

static void Main(string[] args) { string mp3File = @"Theme - Simpsons.mp3"; string wavFile = @"Theme - Simpsons.wav"; using (var wr = new Mp3Reader(File.OpenRead(mp3File))) { IntPtr mp3Format = wr.ReadFormat(); byte[] mp3Data = wr.ReadData(); var wavFormat = AudioCompressionManager.GetWaveFormat(mp3Format); Decode2Pcm(ref mp3Format, ref mp3Data, ref wavFormat); MemoryStream ms = new MemoryStream(); using (var output = File.OpenWrite(wavFile)) using (WaveWriter ww = new WaveWriter(output, AudioCompressionManager.FormatBytes(AudioCompressionManager.GetFormatList(wavFormat)[0].FormatHandle))) { ww.WriteData(mp3Data); } } } private static void Decode2Pcm(ref IntPtr format, ref byte[] data, ref WaveFormat wf) { IntPtr newFormat = AudioCompressionManager.GetCompatibleFormat(format, AudioCompressionManager.PcmFormatTag); byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, false); wf = AudioCompressionManager.GetWaveFormat(newFormat); format = newFormat; data = buffer; }

5 comments:

Author said...

Thanks for the post..
Here is online free mp3 to wav converter
convert mp3 to wav

Arun said...

Can you please send the sample project with code for conversion from mp3 to wav at my email id tiwariarun4246@gmail.com.

Thank You

Unknown said...

Hi smarties) Can you tell me How I can convert this filetype wikiext.com/bnk ?

Anonymous said...

Share free cutting tools and music converter Mp3 Cutter Online

tanzir hossain said...

The blog post is really great. The details of the converter have been discussed. As a result, I was able to easily gain detailed knowledge about the converter. I can easily convert mp3 to wav file and another file.