加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何将系统音量级别设置为从0到100的标量?

发布时间:2020-12-16 06:53:56 所属栏目:百科 来源:网络整理
导读:我目前正在研究“音量混音器”来控制我电脑上每个程序的音量( Windows 10). 如何将每个节目/音频会话的音量级别设置为0到100的标量? 如您所见,在下面的代码中,我找到了GetPeakValue函数,但它返回的值如0.0812654或0.021352. 我确信这些值是标量中从1.0到0.0
我目前正在研究“音量混音器”来控制我电脑上每个程序的音量( Windows 10).

如何将每个节目/音频会话的音量级别设置为0到100的标量?

如您所见,在下面的代码中,我找到了GetPeakValue函数,但它返回的值如0.0812654或0.021352.

我确信这些值是标量中从1.0到0.0的每个音频会话的音量.但我想要的是音量限制,你可以在Windows音频混音器中设置,例如,而不是当前级别.因此,如果我将程序音量设置为50%,我想要一个像0.5的值.

在第二个函数(getVolume)中,您将看到我已经在0-100标量中获得了主卷,但是端点设备已经具有标量级别的函数.所以我至少需要相同的函数或计算,以便为每个音频会话获得这样的标量.

void getSessions() {
    CoInitialize(NULL);

    IMMDeviceEnumerator *pDeviceEnumerator = NULL;
    IMMDevice *pDevice = NULL;
    IAudioSessionManager2 *pAudioSessionManager2 = NULL;
    IAudioSessionEnumerator *pAudioSessionEnumerator = NULL;

    CoCreateInstance(__uuidof(MMDeviceEnumerator),NULL,CLSCTX_INPROC_SERVER,__uuidof(IMMDeviceEnumerator),(LPVOID *)&pDeviceEnumerator);
    pDeviceEnumerator->GetDefaultAudioEndpoint(eRender,eConsole,&pDevice);

    pDevice->Activate(__uuidof(IAudioSessionManager2),CLSCTX_ALL,(void **) &pAudioSessionManager2);
    pAudioSessionManager2->GetSessionEnumerator(&pAudioSessionEnumerator);

    int nSessionCount;
    pAudioSessionEnumerator->GetCount(&nSessionCount);

    std::cout << "Session Count: " << nSessionCount << std::endl;

    while (true) {
        for (int nSessionIndex = 0; nSessionIndex < nSessionCount; nSessionIndex++) {
            IAudioSessionControl *pSessionControl = NULL;
            if (FAILED(pAudioSessionEnumerator->GetSession(nSessionIndex,&pSessionControl)))
                continue;

            IAudioMeterInformation *pAudioMeterInformation = NULL;
            pSessionControl->QueryInterface(&pAudioMeterInformation);

            float fPeak = NULL;
            pAudioMeterInformation->GetPeakValue(&fPeak);

            std::cout << "fPeak Value: " << fPeak << std::endl;
        }

        std::cout << "nn";
        Sleep(1000);
    }

    CoUninitialize();
}

double getVolume() {
    float currentVolume = 0;

    CoInitialize(NULL);
    IMMDeviceEnumerator *deviceEnumerator = NULL;
    CoCreateInstance(__uuidof(MMDeviceEnumerator),(LPVOID *)&deviceEnumerator);
    IMMDevice *defaultDevice = NULL;
    deviceEnumerator->GetDefaultAudioEndpoint(eRender,&defaultDevice);
    deviceEnumerator->Release();
    deviceEnumerator = NULL;

    IAudioEndpointVolume *endpointVolume = NULL;
    defaultDevice->Activate(__uuidof(IAudioEndpointVolume),(LPVOID *)&endpointVolume);
    defaultDevice->Release();
    defaultDevice = NULL;

    float fLevel;
    endpointVolume->GetMasterVolumeLevel(&fLevel);
    qDebug() << "FLevel: ";
    qDebug() << fLevel;

    endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
    endpointVolume->Release();

    CoUninitialize();

    return (double)(currentVolume * 100);
}

解决方法

好的,我找到了解决问题的方法!

我不得不在SessionControl上调用QueryInterface来访问ISimpleAudioVolume,您可以在其中使用GetMasterVolume和SetMasterVolume函数.它是0-1标量,但你可以将它乘以100得到0-100标量.如果系统的主音量为50%,如果程序音量也为50%,则输出为1,因此输出基于系统的主音量!

感谢您的每一条评论和帮助!

这里的工作代码:

void getSessions() {
    CoInitialize(NULL);

    IMMDeviceEnumerator *pDeviceEnumerator;
    IMMDevice *pDevice;
    IAudioSessionManager2 *pAudioSessionManager2;
    IAudioSessionEnumerator *pAudioSessionEnumerator;

    CoCreateInstance(__uuidof(MMDeviceEnumerator),(void **) &pAudioSessionManager2);
    pAudioSessionManager2->GetSessionEnumerator(&pAudioSessionEnumerator);

    int nSessionCount;
    pAudioSessionEnumerator->GetCount(&nSessionCount);

    while (true) {
        for (int nSessionIndex = 0; nSessionIndex < nSessionCount; nSessionIndex++) {
            IAudioSessionControl *pSessionControl;
            if (FAILED(pAudioSessionEnumerator->GetSession(nSessionIndex,&pSessionControl)))
                continue;

            ISimpleAudioVolume *pSimpleAudioVolume;
            pSessionControl->QueryInterface(&pSimpleAudioVolume);

            float fLevel;
            pSimpleAudioVolume->GetMasterVolume(&fLevel);

            std::cout << "fLevel Value: " << fLevel << std::endl;
        }

        Sleep(1000);
    }

    CoUninitialize();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读