在从麦克风捕获期间,耳机中的音量降低到30%

时间:2014-12-15 07:53:28

标签: c++ windows audio ms-media-foundation

我正在编写一个应用程序,通过Microsoft Media Foundation从麦克风中捕获声音。 这是代码

#include <stdio.h>
#include <assert.h>
#include <mfapi.h>
#include <mfidl.h>
#include <Mfreadwrite.h>
#include <Mferror.h>

template <class T> void SafeRelease(T **ppT)
{
    if (*ppT)
    {
        (*ppT)->Release();
        *ppT = NULL;
    }
}

HRESULT ProcessSamples(IMFSourceReader *pReader, IMFSinkWriter *writer)
{
HRESULT hr = S_OK;
IMFSample *pSample = NULL;
size_t  cSamples = 0;

bool quit = false;
while (!quit)
{
    DWORD streamIndex, flags;
    LONGLONG llTimeStamp;

    hr = pReader->ReadSample(
        MF_SOURCE_READER_ANY_STREAM,    // Stream index.
        0,                              // Flags.
        &streamIndex,                   // Receives the actual stream index. 
        &flags,                         // Receives status flags.
        &llTimeStamp,                   // Receives the time stamp.
        &pSample                        // Receives the sample or NULL.
        );

    if (FAILED(hr))
    {
        break;
    }

    wprintf(L"Stream %d (%I64d)\n", streamIndex, llTimeStamp);
    if (flags & MF_SOURCE_READERF_ENDOFSTREAM)
    {
        wprintf(L"\tEnd of stream\n");
        quit = true;
    }
    if (flags & MF_SOURCE_READERF_NEWSTREAM)
    {
        wprintf(L"\tNew stream\n");
    }
    if (flags & MF_SOURCE_READERF_NATIVEMEDIATYPECHANGED)
    {
        wprintf(L"\tNative type changed\n");
    }
    if (flags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
    {
        wprintf(L"\tCurrent type changed\n");
    }
    if (flags & MF_SOURCE_READERF_STREAMTICK)
    {
        wprintf(L"\tStream tick\n");
    }

    if (pSample)
    {
        writer->WriteSample(streamIndex, pSample);
        ++cSamples;
        if (cSamples > 300)quit = true;
    }

    SafeRelease(&pSample);
}

if (FAILED(hr))
{
    wprintf(L"ProcessSamples FAILED, hr = 0x%x\n", hr);
}
else
{
    wprintf(L"Processed %d samples\n", cSamples);
}
SafeRelease(&pSample);
return hr;
}


 int _tmain(int argc, _TCHAR* argv[])
 {

IMFMediaSource *source = 0;
IMFMediaSession*        m_pSession;
IMFAttributes   *pAttributes = NULL;

 HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

if (SUCCEEDED(hr))
{
    hr = MFStartup(MF_VERSION);
}


 hr = MFCreateAttributes(&pAttributes, 2);

if (SUCCEEDED(hr))
{
    hr = pAttributes->SetGUID(
        MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
        MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID
        );
}

if (SUCCEEDED(hr))
{
    hr = MFCreateDeviceSource(pAttributes, &source);
}
SafeRelease(&pAttributes);
hr = MFCreateAttributes(&pAttributes, 1);

IMFSourceReader *pReader;

hr = MFCreateSourceReaderFromMediaSource(source, NULL, &pReader);
IMFMediaType *type;


IMFSinkWriter *writer = 0;
hr = MFCreateSinkWriterFromURL(L"D:\\wma.wma", NULL, NULL, &writer);
IMFMediaType *inputType = 0;
hr = MFCreateMediaType(&inputType);
hr = inputType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
hr = inputType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_Float); 
hr = inputType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, 2); 
hr = inputType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, 44100);
hr = inputType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, 8);
hr =inputType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, 352800); 
hr = inputType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, 32);
hr = inputType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, 1);
hr = inputType->SetUINT32(MF_MT_AUDIO_CHANNEL_MASK, 3);
writer->SetInputMediaType(0, inputType, NULL);

DWORD streamNum = 0;
hr = writer->AddStream(inputType, &streamNum);
hr = writer->BeginWriting();
ProcessSamples(pReader,writer);
writer->Finalize();

return 0;
}

它适用于从麦克风捕获声音,但在调用pReader->ReadSample后有不必要的功能 - 我的耳机中的音量降低了。我认为它是最佳捕获质量的媒体基础的特征,但我不需要它。有没有办法禁用这种行为?提前谢谢!

1 个答案:

答案 0 :(得分:1)

首先,欢迎来到StackOverflow!

您描述的行为称为Automatic ducking or Stream Attenuation(Win7的新功能)。它适用于通信应用程序。由于您的耳机是通信设备,因此会受到此行为的影响。简单来说,在传入的“呼叫”期间,其他设备的数量会减少。

关闭此功能的最简单方法是导航到“声音”控制面板上的最后一个标签(如下所示),您可以在其中更改音量选项。

enter image description here

否则,您可以提供here显示的自定义闪避行为。

您还可以使用与here类似的代码禁用自动闪避,但请记住您将混合使用WASAPI和Media Foundation,并且需要相应地管理会话。

希望这有帮助。