【Windows API】设置声卡采样率、位深、通道数

bool configureAudioDevice(int bitDepth, int sampleRate, const QString& channel, QString& errorString) 
{
    HRESULT hr;
    IMMDeviceEnumerator* deviceEnumerator = nullptr;
    IMMDevice* defaultDevice = nullptr;
    IAudioClient* audioClient = nullptr;
    WAVEFORMATEX* closestMatch;

    WAVEFORMATEX* nativeFormat;

    // 初始化COM库
    hr = CoInitialize(nullptr);
    if (S_OK != hr) {
        errorString = "COM library initialization failed.";
    }
    else 
    {
        // 创建设备枚举器
        hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, IID_PPV_ARGS(&deviceEnumerator));
        if (S_OK != hr) {
            errorString = "Failed to create device enumerator instance.";
        }
        else 
        {
            // 获取默认音频设备
            hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
            if (S_OK != hr) {
                errorString = "Failed to get default audio endpoint.";
            }
            else 
            {
                // 激活音频客户端
                hr = defaultDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, reinterpret_cast<void**>(&audioClient));
                if (S_OK != hr) {
                    errorString = "Failed to activate audio client.";
                }
                else 
                {
                    // 获取当前音频格式
                    hr = audioClient->GetMixFormat(&nativeFormat);
                    if (S_OK != hr) {
                        errorString = "Failed to get mix format.";
                    }
                    else 
                    {
                        nativeFormat->wFormatTag = static_cast<WORD>(65534);
                        nativeFormat->nChannels = static_cast<WORD>("mono" == channel ? 1 : 2);//单声道mono 双声道stereo
                        nativeFormat->wBitsPerSample = static_cast<WORD>(bitDepth*2);
                        nativeFormat->nSamplesPerSec = static_cast<DWORD>(sampleRate);
                        nativeFormat->nBlockAlign = static_cast<WORD>((nativeFormat->wBitsPerSample * nativeFormat->nChannels) / 8);
                        nativeFormat->nAvgBytesPerSec = static_cast<DWORD>(nativeFormat->nBlockAlign * nativeFormat->nSamplesPerSec);
                        // 独占模式
                        //WAVEFORMATEX *newFormat = nativeFormat;
                        //newFormat.wFormatTag = WAVE_FORMAT_PCM;
                        //newFormat.nChannels = static_cast<WORD>("mono" == channel ? 1 : 2);//单声道mono 双声道stereo
                        //newFormat.wBitsPerSample = static_cast<WORD>(bitDepth);
                        //newFormat.nSamplesPerSec = static_cast<DWORD>(sampleRate);
                        //newFormat.nBlockAlign = (newFormat.wBitsPerSample * newFormat.nChannels) / 8;
                        //newFormat.nAvgBytesPerSec = newFormat.nBlockAlign * newFormat.nSamplesPerSec;

                        hr = audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, nativeFormat, &closestMatch);
                        //独占模式
                        //hr = audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &newFormat, NULL);

                        if (S_OK != hr) {
                            errorString = "Requested format is not supported.";
                        }
                        else 
                        {
                            hr = audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 0, 0, nativeFormat, NULL);        
                            //独占模式
                            //hr = audioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, 2, 100000, 100000, &newFormat, NULL);
                            if (S_OK != hr) {
                                errorString = "Failed to initialize audio client with the specified format";
                            }
                            else {
                                errorString = "Audio client initialized with the specified format";
                            }
                        }
                    }
                }
            }
        }
    }
    // 清理COM对象
    if (closestMatch) {
        CoTaskMemFree(closestMatch);
    }
    if (nativeFormat) {
        CoTaskMemFree(nativeFormat);
    }
    if (audioClient) {
        audioClient->Release();
    }
    if (defaultDevice) {
        defaultDevice->Release();
    }
    if (deviceEnumerator) {
        deviceEnumerator->Release();
    }

    CoUninitialize();

    return SUCCEEDED(hr);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值