如何将系统音量级别作为标量从 0 到 100?
How to get system volume level as a scalar from 0 to 100?
我目前正在研究"音量混合器"来控制我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(¤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), 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();
}
相关文章:
- C++,系统无法执行指定的程序
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在UNIX系统中使用DIR查找文件的字节大小
- 错误处理.将系统错误代码映射到泛型
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在C++游戏中与库存系统作斗争
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 文件系统:复制功能的速度秘诀是什么
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 在gtest.中使用fff.h模拟系统API
- 如何制作无限制照明系统
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 如何传递多个 std::文件系统选项?
- 遍历顺序由 std::文件系统directory_iterator给出
- C++系统找不到指定的文件错误
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- libstdc++ 文件系统中未初始化的用法?
- 如何在ECS框架中更新组件数据和通知系统
- 如何将系统音量级别作为标量从 0 到 100?