This project has moved and is read-only. For the latest updates, please go here.

Converting M4A to Wav/PCM

Dec 20, 2012 at 11:32 PM

Hi,
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)) {
        sre.SetInputToWaveStream(baStream);
        RecognitionResult rr = sre.Recognize();
      }

 

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

Thanks !
Regards 

Dec 21, 2012 at 8:24 AM
Edited Dec 21, 2012 at 8: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)

Mark

Dec 21, 2012 at 12:33 PM

Thanks I'll try

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

Dec 21, 2012 at 2:54 PM

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

Dec 21, 2012 at 9:14 PM
Edited Dec 22, 2012 at 12:14 AM

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