Search code examples
audiouwpms-media-foundation

Does Windows Media Foundation on UWP have a resampler? If so how do I use it?


Using Win32 I have access to CLSID_CResamplerMediaObject which means I can reduce my channel count from say 6 to 2.

On UWP this is no longer defined and the only reference to a resampler I can find is CLSID_AudioResamplerMediaObject. When I create an instance of this class however, and pass it my MFMediaType_Float or MFMediaType_PCM type, it says that the provided types aren't supported...

ComPtr<IUnknown> pTransformUnknown = nullptr;
DxUtil::ThrowIfFailed(CoCreateInstance(CLSID_AudioResamplerMediaObject, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, &pTransformUnknown));
DxUtil::ThrowIfFailed(pTransformUnknown->QueryInterface(IID_PPV_ARGS(&mResampler)));

IMFMediaType* pInputType = nullptr;
DxUtil::ThrowIfFailed(MFCreateMediaType(&pInputType));
DxUtil::ThrowIfFailed(pInputType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));
DxUtil::ThrowIfFailed(pInputType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM)); // Or MFAudioFormat_Float
DxUtil::ThrowIfFailed(pInputType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 16));
DxUtil::ThrowIfFailed(pInputType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 6));
DxUtil::ThrowIfFailed(pInputType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 48000));
DxUtil::ThrowIfFailed(mAudioSourceReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, nullptr, pInputType)); // Fails here!

As usual, Microsoft docs aren't helpful, I would appreciate any help!

Input file is Mp4 with 1 aac stream, 6 channels, 16 bit audio.


Solution

  • CLSID_AudioResamplerMediaObject and CLSID_CResamplerMediaObject are the same thing, same GUID of {f447b69e-1884-4a7e-8055-346f74d6edb3}.

    The error you mentioned is not coming from audio resampler, you have it from Source Reader API. The error is presumably correctly reported indicating the situation that for given media source the reader cannot provide a conversion to supplied media type. There can be multiple reasons for this to happen.