Skip to content

Commit

Permalink
Fix different behavior between Win7 and Win10.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed-Abdelhameed authored Sep 5, 2017
1 parent 31e0d67 commit 4fb4ae2
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 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,34 +150,26 @@ protected virtual IMFSourceReader CreateReader(MediaFoundationReaderSettings set

var currentMediaType = GetCurrentMediaType(reader);

// HE-AAC (and v2) seems to halve the samplerate
bool sampleRateMultiplied = false;
if (currentMediaType.SubType == AudioSubtypes.MFAudioFormat_AAC && currentMediaType.ChannelCount == 1)
{
sampleRateMultiplied = true;
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
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
{
// revert SampleRate & ChannelCount multiplication (not needed for aac_lc mono)
if (sampleRateMultiplied)
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 /= 2;
partialMediaType.ChannelCount /= 2;
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);
Expand Down

0 comments on commit 4fb4ae2

Please sign in to comment.