Skip to content

Commit

Permalink
Merge pull request #233 from Ahmed-Abdelhameed/patch-1
Browse files Browse the repository at this point in the history
Improved support for mono AAC
  • Loading branch information
markheath authored Sep 5, 2017
2 parents 8226aa3 + 4fb4ae2 commit c6ebcaa
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions NAudio/Wave/WaveStreams/MediaFoundationReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -150,19 +150,27 @@ protected virtual IMFSourceReader CreateReader(MediaFoundationReaderSettings set

var currentMediaType = GetCurrentMediaType(reader);

// HE-AAC (and v2) seems to halve the samplerate
if (currentMediaType.SubType == AudioSubtypes.MFAudioFormat_AAC && currentMediaType.ChannelCount == 1)
{
currentMediaType.SampleRate *= 2;
currentMediaType.ChannelCount *= 2;
}
// mono, low sample rate files can go wrong on Windows 10 unless we specify here
partialMediaType.ChannelCount = currentMediaType.ChannelCount;
partialMediaType.SampleRate = currentMediaType.SampleRate;

// set the media type
// can return MF_E_INVALIDMEDIATYPE if not supported
reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject);
try
{
// set the media type
// can return MF_E_INVALIDMEDIATYPE if not supported
reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject);
}
catch (COMException ex) when (ex.GetHResult() == MediaFoundationErrors.MF_E_INVALIDMEDIATYPE)
{
// HE-AAC (and v2) seems to halve the samplerate
if (currentMediaType.SubType == AudioSubtypes.MFAudioFormat_AAC && currentMediaType.ChannelCount == 1)
{
partialMediaType.SampleRate = currentMediaType.SampleRate *= 2;
partialMediaType.ChannelCount = currentMediaType.ChannelCount *= 2;
reader.SetCurrentMediaType(MediaFoundationInterop.MF_SOURCE_READER_FIRST_AUDIO_STREAM, IntPtr.Zero, partialMediaType.MediaFoundationObject);
}
else { throw; }
}

Marshal.ReleaseComObject(currentMediaType.MediaFoundationObject);
return reader;
Expand Down

0 comments on commit c6ebcaa

Please sign in to comment.