0
votes

I do have a very particular problem, I wish I could find the answer to.

I'm trying to read an AAC stream from an URL (online streaming radio e.g. live.noroc.tv:8000/radionoroc.aacp) with NAudio library and get IEEE 32 bit floating samples.

Besides that I would like to resample the stream to a particular sample rate and channel count (rate 5512, mono).

Below is the code which accomplishes that:

int tenSecondsOfDownloadedAudio = 5512 * 10;
float[] buffer = new float[tenSecondsOfDownloadedAudio];
using (var reader = new MediaFoundationReader(pathToUrl))
{
    var ieeeFloatWaveFormat = WaveFormat.CreateIeeeFloatWaveFormat(5512, 1); // mono
    using (var resampler = new MediaFoundationResampler(reader, ieeeFloatWaveFormat))
    {
          var waveToSampleProvider = new WaveToSampleProvider(resampler);
          int readSamples = 0;
          int tempBuffer = new float[5512]; // 1 second buffer
          while(readSamples <= tenSecondsOfDownloadedAudio)
          {
              int read = waveToSampleProvider.Read(tempBuffer, 0, tempBuffer.Length);
              if(read == 0)
              {
                   Thread.Sleep(500); // allow streaming buffer to get loaded
                   continue;
              }

              Array.Copy(tempBuffer, 0, buffer, readSamples, tempBuffer.Length);
              readSamples += read;
          }
    }
}

These particular samples are then written to a Wave audio file using the following simple method:

using (var writer = new WaveFileWriter("path-to-audio-file.wav", WaveFormat.CreateIeeeFloatWaveFormat(5512, 1)))
{
      writer.WriteSamples(samples, 0, samples.Length);
}

What I've encountered is that NAudio does not read 10 seconds of audio (as it was requested) but only 5, though the buffer array gets fully loaded with samples (which at this rate and channel count should contain 10 seconds of audio samples).

Thus the final audio file plays the stream 2 times as slower as it should (5 second stream is played as 10).

Is this somewhat related to different bit depths (should I record at 64 bits per sample as opposite to 32).

I do my testing at Windows Server 2008 R2 x64, with MFT codecs installed.

Would really appreciate any suggestions.

1
what are you doing to play the audio? two times as slow suggests playing stereo audio as mono.Mark Heath
Hi Mark, thanks for looking at this. I'm writing the audio samples to a Wave file. I've updated the source code to depict how am I doing this. You suggestion makes sense, though this would mean that I'm getting stereo samples from MediaFoundationResampler, even though I've explicitly specified at the input mono format. Regards!AddictedCS

1 Answers

0
votes

The problem seems to be with MediaFoundationReader failing to handle HE-AACv2 in ADTS container with is a standard online radio stream format and most likely the one you are dealing with.

Adobe products have the same problem mistreating this format exactly the same way^ stretching the first half of the audio to the whole duration and : Corrupted AAC files recorded from online stream

Supposedly, it has something to do with HE-AACv2 stereo stream being actually a mono stream with additional info channel for Parametric Stereo.