How do you get the current sample rate of Windows audio playback?

邮差的信 提交于 2019-12-04 12:59:20
    //#include <iostream>
//#include <initguid.h>
//#include <Mmdeviceapi.h>

int main() {
    HRESULT hr;
    IMMDevice * pDevice = NULL;
    IMMDeviceEnumerator * pEnumerator = NULL;
    IPropertyStore* store = nullptr;
    PWAVEFORMATEX deviceFormatProperties;
    PROPVARIANT prop;

    CoInitialize(NULL);

    // get the device enumerator
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID *)&pEnumerator);

    // get default audio endpoint
    hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);

    hr = pDevice->OpenPropertyStore(STGM_READ, &store);
    if (FAILED(hr)) {
        std::cout << "OpenPropertyStore failed!" << std::endl;
    }

    hr = store->GetValue(PKEY_AudioEngine_DeviceFormat, &prop);
    if (FAILED(hr)) {
        std::cout << "GetValue failed!" << std::endl;
    }

    deviceFormatProperties = (PWAVEFORMATEX)prop.blob.pBlobData;
    std::cout << "Channels    = " << deviceFormatProperties->nChannels << std::endl;
    std::cout << "Sample rate = " << deviceFormatProperties->nSamplesPerSec << std::endl;
    std::cout << "Bit depth   = " << deviceFormatProperties->wBitsPerSample << std::endl;

    system("pause");
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!