如何将系统音量级别作为标量从 0 到 100?

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

本文关键字:标量 系统      更新时间:2023-10-16

我目前正在研究"音量混合器"来控制我PC上每个程序的音量(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, 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 << "nn";
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);
}

好的伙计们,我已经找到了解决问题的方法!

我不得不在 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), 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();
}