none
The output audio has been run faster when converting audio sample rate from 16Khz to 44.1 Khz using Micrsoft Media Foudation RRS feed

  • Question

  • I created a SinkWriter that is able to encode video and audio using Microsoft's Media Foundation Platform.
    Video is working fine so far but I have some troubles with audio only.
    I converted audio sample rate from 16Khz to 44.1 Khz, the ouput audio ran faster than the input audio.
    The only bug that appears in the audio codec is AAC with input audio  sample rate under 32kHZ.

    What problem in Writesample function?
    Given below is the sample test application:

    #include "stdafx.h"

    #include <windows.h>
    #include <windowsx.h>

    #include <comdef.h>
    #include <stdio.h>
    #include <mfapi.h>
    #include <mfidl.h>
    #include <mfreadwrite.h>
    #include <Mferror.h>

    #pragma comment(lib, "ole32")
    #pragma comment(lib, "mfplat")
    #pragma comment(lib, "mfreadwrite")
    #pragma comment(lib, "mfuuid")

    int main()
    {
    HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);

    hr = MFStartup(MF_VERSION);

    IMFMediaType *pMediaType;
    IMFMediaType *pMediaTypeOut;
    IMFSourceReader *pSourceReader;
    IMFAttributes *pAttributes;
    IMFSinkWriter *pSinkWriter;
    // Load souce file
    hr = MFCreateSourceReaderFromURL(
    L"input.mp4",
    NULL,
    &pSourceReader
    );

    // Load create SinkWriter of output file
    hr = MFCreateSinkWriterFromURL(
    L"Output.mp4",
    NULL,
    NULL,
    &pSinkWriter
    );
    // get media type of input file
    hr = pSourceReader->GetCurrentMediaType(
    MF_SOURCE_READER_FIRST_AUDIO_STREAM,
    &pMediaType);

    // set media type for output file
    hr = MFCreateMediaType(&pMediaTypeOut);

    // set major type for output file
    hr = pMediaTypeOut->SetGUID(
    MF_MT_MAJOR_TYPE,
    MFMediaType_Audio
    );

    pMediaTypeOut->SetUINT32(MF_MT_AAC_AUDIO_PROFILE_LEVEL_INDICATION, 0x29);
    // set audio format for output file
    hr = pMediaTypeOut->SetGUID(
    MF_MT_SUBTYPE,
    MFAudioFormat_AAC
    );

    // set audio sample rate for output file
    hr = pMediaTypeOut->SetUINT32(
    MF_MT_AUDIO_SAMPLES_PER_SECOND,
    44100
    );

    // set audio number channal for output file
    hr = pMediaTypeOut->SetUINT32(
    MF_MT_AUDIO_NUM_CHANNELS,
    2
    );

    // set audio bit depth for output file
    hr = pMediaTypeOut->SetUINT32(
    MF_MT_AUDIO_BITS_PER_SAMPLE,
    16
    );

    hr = pMediaTypeOut->SetUINT32(
    MF_MT_AUDIO_AVG_BYTES_PER_SECOND,
    (UINT32)(96000/8 + 0.5) 
    );

    hr = pMediaTypeOut->SetUINT32(
    MF_MT_AUDIO_BLOCK_ALIGNMENT,
    1
    );

    DWORD nWriterStreamIndex = -1;
    hr = pSinkWriter->AddStream(pMediaTypeOut, &nWriterStreamIndex);
    hr = pSinkWriter->BeginWriting();

    _com_error err(hr);
    LPCTSTR errMsg = err.ErrorMessage();

    for (;;)
    {
    DWORD nStreamIndex, nStreamFlags;
    LONGLONG nTime;
    IMFSample *pSample;

    hr = pSourceReader->ReadSample(
    MF_SOURCE_READER_FIRST_AUDIO_STREAM,
    0,
    &nStreamIndex,
    &nStreamFlags,
    &nTime,
    &pSample);

    if (pSample)
    {
    hr = pSinkWriter->WriteSample(
    nWriterStreamIndex,
    pSample
    );
    }

    if (nStreamFlags & MF_SOURCE_READERF_ENDOFSTREAM)
    {
    break;
    }
    }
    hr = pSinkWriter->Finalize();
    return 0;
    }



    • Edited by DinhChuyen Saturday, August 31, 2019 2:36 AM
    Thursday, August 22, 2019 3:41 PM

All replies

  • Hi, DinhChuyen

    If we put audio data into the sound card, we can hear the sound. Of course, we need to tell the sound card the sampling rate of these data. If we tell the sound card that the sampling rate is greater than the sampling rate of the data itself, the data will play faster than its original speed. It's the function of fast-release.

    So, Increasing the sample rate will increase the playback speed of the sound. For example, going from a 22 KHz sampling rate to 44 KHz will make the playback sound twice as fast as the original.

    Therefore, the sampling rate of your output audio should be the same as the sampling rate of the sample, so that the sound will not be distorted.

    Best regards,

    Strive


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 23, 2019 3:15 AM