12

In my project I am receiving mp3 data in a byte array. I want to convert that data to wav format and store it in another byte array. I searched internet for mp3 to wav converters but all of them support file to file conversion. None of them seems to take raw data as inputs. Is there any way I can achieve this in C# ?
Here is the protoype of the function I am trying to create.

   bool ConvertToWav(byte[] buffer){
      //Do some processing and store the wav data in buffer2
      Buffer.BlockCopy(buffer2,0,buffer,0,buffer.Length-1);
   }
gibraltar
  • 1,678
  • 4
  • 20
  • 33
  • If you stream a file to a byte[], then you can proceed what you found in the internet? – Alex R. Jul 12 '12 at 06:28
  • 3
    I think this can get you somewhere - http://stackoverflow.com/questions/3432860/mp3-byte-array-convert-to-wav-and-navigate-to-time-index – Prateek Singh Jul 12 '12 at 06:29
  • @AlexR. I am not allowed to store data in a file in any case. Moreover for that to work, I would have to first save the data received in a file and then read the data from the converted file. – gibraltar Jul 12 '12 at 06:30
  • @gibraltar can you show us the algorithm you're describing? – Alex R. Jul 12 '12 at 06:32
  • Why cannot you use NAudio WaveStream? It supports opening MP3 data and decoding it with Windows MP3 decoder. http://naudio.codeplex.com/wikipage?title=Convert%20a%20MP3%20to%20WAV – Legoless Jul 12 '12 at 08:13

1 Answers1

26

This is quite the late response but I just figured it out myself. There is this NuGet package called NAudio, https://www.nuget.org/packages/NAudio/ This provides awesome functionality for what you want to do.

    using NAudio.Wave;        

    private static void ConvertMp3ToWav(string _inPath_, string _outPath_)
    {
        using (Mp3FileReader mp3 = new Mp3FileReader(_inPath_))
        {
            using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
            {
                WaveFileWriter.CreateWaveFile(_outPath_, pcm);
            }
        }
    }

There you go.