如何获得Windows音频播放的当前采样率? [英] How do you get the current sample rate of Windows audio playback?

查看:2611
本文介绍了如何获得Windows音频播放的当前采样率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows waveOut API(aka MME或多媒体扩展)mmsystem.h。一些程序改变音频回放采样率(例如,从44.1kHz到48kHz),并且它对于我的程序检测当前回放采样率将是非常有用的,因此它可以警告用户Windows将重新采样程序的输出。 / p>

根据此文档 http ://msdn.microsoft.com/en-us/library/aa909811.aspx ,waveOutGetPlaybackRate返回设备当前正在执行的重采样%(例如,设备在44.1播放,并且程序在44.1播放音频它会返回1.0)。我很好奇,如果有一种方式来获得设备的绝对采样率,而不是一些相对的。在Windows Vista / 7/8中,您可以通过转到:
控制面板>声音>播放,右键单击默认播放设备并选择属性,然后选择高级选项卡。所以我试图通过查询操作系统获得这里的默认格式值。



有问题的程序是用Pascal编写的,但是我通常使用C / C ++参考。

解决方案

  //#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);

//获取设备枚举
hr = CoCreateInstance(__ uuidof(MMDeviceEnumerator),NULL,CLSCTX_ALL,__uuidof(IMMDeviceEnumerator),(LPVOID *)& pEnumerator);

//获取默认音频端点
hr = pEnumerator-> GetDefaultAudioEndpoint(eRender,eMultimedia,& pDevice);

hr = pDevice-> OpenPropertyStore(STGM_READ,& store);
if(FAILED(hr)){
std :: cout< OpenPropertyStore失败! << 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<< 比特深度=< deviceFormatProperties-> wBitsPerSample<< std :: endl;

系统(pause);
return 0;
}


I am using the Windows waveOut API (aka MME or Multimedia Extension) mmsystem.h. Some programs change the audio playback sample rate (eg. from 44.1kHz to 48kHz), and it would be very useful for my program to detect the current playback sample rate, so it can warn users that Windows will be resampling the program's output.

According to this documentation http://msdn.microsoft.com/en-us/library/aa909811.aspx, waveOutGetPlaybackRate returns the resampling % that the device is currently performing (eg, device plays at 44.1, and program is playing audio at 44.1 so it would return 1.0). I am curious if there is a way to get the absolute sample rate of the device, rather than something relative. In Windows Vista/7/8 you would manually find this value by going to: Control Panel > Sound > Playback, right-click on default playback device and choose Properties, and choose Advanced tab. So I am trying to get this "default format" value found here, by querying the OS.

The program in question is written in Pascal, however, I usually use C/C++ references.

解决方案

    //#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;
}

这篇关于如何获得Windows音频播放的当前采样率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆