我目前正在研究“音量混音器”来控制我电脑上每个程序的音量(
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 << "\n\n"; 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(¤tVolume); 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(); }