Tipe Media Audio Tidak Dikompresi

Untuk membuat jenis audio lengkap yang tidak dikompresi, atur setidaknya atribut berikut pada penunjuk antarmuka IMFMediaType .

Atribut Deskripsi
MF_MT_MAJOR_TYPE Jenis utama. Atur ke MFMediaType_Audio.
MF_MT_SUBTYPE Subtipe. Lihat GUID Subjenis Audio.
MF_MT_AUDIO_NUM_CHANNELS Jumlah saluran audio.
MF_MT_AUDIO_SAMPLES_PER_SECOND Jumlah sampel audio per detik.
MF_MT_AUDIO_BLOCK_ALIGNMENT Perataan blok.
MF_MT_AUDIO_AVG_BYTES_PER_SECOND Jumlah rata-rata byte per detik.
MF_MT_AUDIO_BITS_PER_SAMPLE Jumlah bit per sampel audio.
MF_MT_ALL_SAMPLES_INDEPENDENT Menentukan apakah setiap sampel audio independen. Atur ke TRUE untuk format MFAudioFormat_PCM dan MFAudioFormat_Float.

 

Selain itu, atribut berikut diperlukan untuk beberapa format audio.

Atribut Deskripsi
MF_MT_AUDIO_VALID_BITS_PER_SAMPLE Jumlah bit data audio yang valid di setiap sampel audio. Atur atribut ini jika sampel audio memiliki padding—yaitu, jika jumlah bit yang valid di setiap sampel audio kurang dari ukuran sampel.
MF_MT_AUDIO_CHANNEL_MASK Penetapan saluran audio ke posisi pembicara. Atur atribut ini untuk aliran audio multisaluran, seperti 5.1. Atribut ini tidak diperlukan untuk audio mono atau stereo.

 

Contoh Kode

Kode berikut menunjukkan cara membuat jenis media untuk audio PCM yang tidak dikompresi.

HRESULT CreatePCMAudioType(
    UINT32 sampleRate,        // Samples per second
    UINT32 bitsPerSample,     // Bits per sample
    UINT32 cChannels,         // Number of channels
    IMFMediaType **ppType     // Receives a pointer to the media type.
    )
{
    HRESULT hr = S_OK;

    IMFMediaType *pType = NULL;

    // Calculate derived values.
    UINT32 blockAlign = cChannels * (bitsPerSample / 8);
    UINT32 bytesPerSecond = blockAlign * sampleRate;

    // Create the empty media type.
    hr = MFCreateMediaType(&pType);
    if (FAILED(hr))
    {
        goto done;
    }

    // Set attributes on the type.
    hr = pType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, cChannels);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sampleRate);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, blockAlign);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, bytesPerSecond);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bitsPerSample);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = pType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE);
    if (FAILED(hr))
    {
        goto done;
    }

    // Return the type to the caller.
    *ppType = pType;
    (*ppType)->AddRef();

done:
    SafeRelease(&pType);
    return hr;
}

Contoh berikutnya mengambil format audio yang dikodekan sebagai input, dan membuat jenis audio PCM yang cocok. Jenis ini akan cocok untuk diatur pada encoder atau decoder, misalnya.

//-------------------------------------------------------------------
// ConvertAudioTypeToPCM
//
// Given an audio media type (which might describe a compressed audio
// format), returns a media type that describes the equivalent
// uncompressed PCM format.
//-------------------------------------------------------------------

HRESULT ConvertAudioTypeToPCM(
    IMFMediaType *pType,        // Pointer to an encoded audio type.
    IMFMediaType **ppType       // Receives a matching PCM audio type.
    )
{
    HRESULT hr = S_OK;

    GUID majortype = { 0 };
    GUID subtype = { 0 };

    UINT32 cChannels = 0;
    UINT32 samplesPerSec = 0;
    UINT32 bitsPerSample = 0;

    hr = pType->GetMajorType(&majortype);
    if (FAILED(hr)) 
    { 
        return hr;
    }

    if (majortype != MFMediaType_Audio)
    {
        return MF_E_INVALIDMEDIATYPE;
    }

    // Get the audio subtype.
    hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
    if (FAILED(hr)) 
    { 
        return hr;
    }

    if (subtype == MFAudioFormat_PCM)
    {
        // This is already a PCM audio type. Return the same pointer.

        *ppType = pType;
        (*ppType)->AddRef();

        return S_OK;
    }

    // Get the sample rate and other information from the audio format.

    cChannels = MFGetAttributeUINT32(pType, MF_MT_AUDIO_NUM_CHANNELS, 0);
    samplesPerSec = MFGetAttributeUINT32(pType, MF_MT_AUDIO_SAMPLES_PER_SECOND, 0);
    bitsPerSample = MFGetAttributeUINT32(pType, MF_MT_AUDIO_BITS_PER_SAMPLE, 16);

    // Note: Some encoded audio formats do not contain a value for bits/sample.
    // In that case, use a default value of 16. Most codecs will accept this value.

    if (cChannels == 0 || samplesPerSec == 0)
    {
        return MF_E_INVALIDTYPE;
    }

    // Create the corresponding PCM audio type.
    hr = CreatePCMAudioType(samplesPerSec, bitsPerSample, cChannels, ppType);

    return hr;
}

Tipe Media Audio