Okomprimerade ljudmedietyper
Om du vill skapa en fullständig okomprimerad ljudtyp anger du minst följande attribut på IMFMediaType gränssnittspekare.
Attribut | Beskrivning |
---|---|
MF_MT_MAJOR_TYPE | Huvudtyp. Ange till MFMediaType_Audio. |
MF_MT_SUBTYPE | Subtyp. Se ljudundertyps-GUID:er. |
MF_MT_AUDIO_NUM_CHANNELS | Antal ljudkanaler. |
MF_MT_AUDIO_SAMPLES_PER_SECOND | Antal ljudexempel per sekund. |
MF_MT_AUDIO_BLOCK_ALIGNMENT | Blockjustering. |
MF_MT_AUDIO_AVG_BYTES_PER_SECOND | Genomsnittligt antal byte per sekund. |
MF_MT_AUDIO_BITS_PER_SAMPLE | Antal bitar per ljudexempel. |
MF_MT_ALL_SAMPLES_INDEPENDENT | Anger om varje ljudexempel är oberoende. Ange till TRUE- för MFAudioFormat_PCM och MFAudioFormat_Float format. |
Dessutom krävs följande attribut för vissa ljudformat.
Attribut | Beskrivning |
---|---|
MF_MT_AUDIO_VALID_BITS_PER_SAMPLE | Antal giltiga bitar av ljuddata i varje ljudexempel. Ange det här attributet om ljudexemplen har utfyllnad, dvs. om antalet giltiga bitar i varje ljudexempel är mindre än exempelstorleken. |
MF_MT_AUDIO_CHANNEL_MASK | Tilldelning av ljudkanaler till talarpositioner. Ange det här attributet för ljudströmmar med flera kanaler, till exempel 5.1. Det här attributet krävs inte för mono- eller stereoljud. |
Exempelkod
Följande kod visar hur du skapar en medietyp för okomprimerat PCM-ljud.
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;
}
Nästa exempel tar ett kodat ljudformat som indata och skapar en matchande PCM-ljudtyp. Den här typen skulle vara lämplig att ställa in på en kodare eller avkodare, till exempel.
//-------------------------------------------------------------------
// 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;
}
Relaterade ämnen