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; }

3 comments:

Author said...

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

arun tiwari 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

kutta song said...

Super Bowl LI will be the 51st Super Bowl and the 47th modern-era National Football League. The Touchdown Tour is a way to excite the Houston community about the 2016-2017 football season culminating in Super Bowl LI.
Super Bowl 51
Super Bowl 51 Live
Super Bowl 51 Live Stream
Watch Super Bowl 51
Watch Super Bowl 51 Live
Super Bowl 51 Live Online