Search code examples
c#audionaudiotranscoding

Using NAudio, ADTS AAC to WAV Transcoding, how to?


I am attempting to implement the following code which I found here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using NAudio.Wave;


namespace AAC2WAV
{
    class Program
    {
        static void Main(string[] args)
        {
            // create media foundation reader to read the AAC encoded file
            using (MediaFoundationReader reader = new MediaFoundationReader(args[0]))
            // resample the file to PCM with same sample rate, channels and bits per sample
            using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader,
                new WaveFormat(reader.WaveFormat.SampleRate, reader.WaveFormat.BitsPerSample, reader.WaveFormat.Channels)))
            // create WAVe file
            using (WaveFileWriter waveWriter = new WaveFileWriter(args[1], resampledReader.WaveFormat))
            {
                // copy samples
                resampledReader.CopyTo(waveWriter);
            }

        }
    }
}

Input file is *.aac (args[0]) output file should be *.wav (args[ 1 ]). I am running the complied code as a console application and I get no errors, however it just seems to hang once it creates the wav file with 0 KB size

I'm wondering is there there is something I am missing or perhaps something I need to further understand.

Windows is reporting the *.aac files as ADTS.

Perhaps it is that I need to extract and rewrite the header, but I am not familiar with AAC at all so would seek some guidance on that aspect if deemed necessary.

When I try to open the file with WMP it says it cannot connect to the server (suggests codec issue), however I can convert it with FFMPEG to a wav file without any trouble. Using FFMPEG is not ideal for the particular implementation I am looking at.

Any assistance would be greatly appreciated.


Info on the actual file:

General 
Complete name : C:\Users\....\AAC2WAV\bin\Debug\0cc409aa-f66c-457a-ac10-6286509ec409.aac 
Format : ADIF 
Format/Info : Audio Data Interchange Format 
File size : 180 KiB 
Overall bit rate mode : Constant 

Audio 
Format : AAC 
Format/Info : Advanced Audio Codec 
Format profile : Main 
Bit rate mode : Constant 
Channel(s) : 14 channels 
Channel positions : , Back: 14 
Sampling rate : 96.0 kHz 
Frame rate : 93.750 FPS (1024 spf) 
Compression mode : Lossy 
Stream size : 180 KiB (100%) 

Information gathered from file using: MediaInfo

I should add here that the lower section of the above info is incorrect I believe. The sample rate is not 96k it is 22050 and there is only 1 channel


Solution

  • You don't need the resampler - MediaFoundationReader already returns PCM. You should also use WaveFileWriter.CreateWaveFile

    This should be all you need:

    using (var reader = new MediaFoundationReader("myfile.aac"))
    {
        WaveFileWriter.CreateWaveFile("pcm.wav", reader);
    }