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

Converting M4A to Wav/PCM

Dec 20, 2012 at 10:32 PM

I'm little newbie to the NAudio API.

I'd like to convert an M4A audio file (produced by iOS Dropvox app) to a Wav or PCM MemoryStream. Then use this Stream as an AudioStream for a SpeechRecognitionEngine.

      using (var ms = File.OpenRead(file))
      using (var mp3Reader = new Mp3FileReader(ms))
      using (var pcmStream = WaveFormatConversionStream.CreatePcmStream(mp3Reader))
      using (var baStream = new BlockAlignReductionStream(pcmStream)) {
        RecognitionResult rr = sre.Recognize();


It's seems Mp3FileReader can't read M4A. I don't know if there is any workaround ? 

Thanks !

Dec 21, 2012 at 7:24 AM
Edited Dec 21, 2012 at 7:26 AM

MP3 is not the same as MP4 so the Mp3FileReader can't play it. The next NAudio includes MediaFoundationReader which will be able to decode M4A. There is no public release yet, but if you want to build the latest from Source Code (click the tab above) you can use it.

(by the way, there is no need for pcmStream or baStream above. For MP3s, just use mp3Reader)


Dec 21, 2012 at 11:33 AM

Thanks I'll try

I thought MediaFoundationReader was only working on Windows 8 ? Does it works with Windows 7 ?

Dec 21, 2012 at 1:54 PM

No, it will work on Windows 7 too. The Media Foundation API was actually introduced with Windows Vista.

Dec 21, 2012 at 8:14 PM
Edited Dec 21, 2012 at 11:14 PM

Ok get it alpha2 ! Works fine with wav output.

What is best practice to output into a MemoryStream ? The WaveFileWriter.CreateWaveFile only takes a string (filename) à as parameter. I see code with loop/read but there might be a more straigh forward function ?

using (var reader = new MediaFoundationReader(file))
using (var resampler = new MediaFoundationResampler(reader, 16000))
using (var memoryStream = new MemoryStream()) {
  WaveFileWriter.CreateWaveFile(new MemoryStream(), resampler);

EDIT: Found: resampler.CopyTo(memoryStream );

Now trying to get the good audio format for Microsoft and Google reconizer