How to get the system volume level as a scalar from 0 to 100?

I am currently working on a “Volume Mixer” to control the volume of each program on my PC (Windows 10).

How to get the volume level of each program / audio session as a scalar from 0 to 100?

As you can see, in the code below I found a function GetPeakValue, but returns values ​​such as 0.0812654 or 0.021352.

I am sure that these values ​​represent the volume of each audio session in a scalar from 1.0 to 0.0. But what I want is a volume limit that you can set, for example, in the Windows audio mixer, and not at the current level. Therefore, if I set the volume level of the program to 50%, I need a value equal to 0.5.

In the second function ( getVolume), you will see that I already received the master volume in the 0-100 scanner, but it already has a function for the scalar level. Therefore, I will need at least one and the same function or calculation to get such a scalar for each audio session.

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, NULL, (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 << "\n\n";
        Sleep(1000);
    }

    CoUninitialize();
}

double getVolume() {
    float currentVolume = 0;

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

    IAudioEndpointVolume *endpointVolume = NULL;
    defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (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);
}
+4
source share
1 answer

Ok guys, I found a solution for my problem!

QueryInterface SessionControl, ISimpleAudioVolume, GetMasterVolume SetMasterVolume. 0-1, 100, 0-100. 50%, 1, 50%, !

!

:

void getSessions() {
    CoInitialize(NULL);

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

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

    pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (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();
}
0

Source: https://habr.com/ru/post/1683107/


All Articles