windows-audio-sync/AudioManager/CAudioEndpointVolumeCallback.h

87 lines
1.9 KiB
C
Raw Permalink Normal View History

2022-12-28 16:52:42 +00:00
#pragma once
#include <endpointvolume.h>
#include <algorithm>
2022-12-28 16:52:42 +00:00
#include <iostream>
class CAudioEndpointVolumeCallback : public IAudioEndpointVolumeCallback
{
private:
LONG _cRef;
IAudioEndpointVolume* g_pEndptVol;
IAudioEndpointVolume* g_cEndptVol;
GUID g_guidContext;
public:
CAudioEndpointVolumeCallback(IAudioEndpointVolume* g_pEndptVol, IAudioEndpointVolume* g_cEndptVol, GUID g_guidContext) :
_cRef(1), g_pEndptVol(g_pEndptVol), g_cEndptVol(g_cEndptVol), g_guidContext(g_guidContext)
{
}
~CAudioEndpointVolumeCallback()
{
}
// IUnknown methods -- AddRef, Release, and QueryInterface
ULONG STDMETHODCALLTYPE AddRef()
{
return InterlockedIncrement(&_cRef);
}
ULONG STDMETHODCALLTYPE Release()
{
ULONG ulRef = InterlockedDecrement(&_cRef);
if (0 == ulRef)
{
delete this;
}
return ulRef;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID** ppvInterface)
{
if (IID_IUnknown == riid)
{
AddRef();
*ppvInterface = (IUnknown*)this;
}
else if (__uuidof(IAudioEndpointVolumeCallback) == riid)
{
AddRef();
*ppvInterface = (IAudioEndpointVolumeCallback*)this;
}
else
{
*ppvInterface = NULL;
return E_NOINTERFACE;
}
return S_OK;
}
// Callback method for endpoint-volume-change notifications.
HRESULT STDMETHODCALLTYPE OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify)
{
if (pNotify == NULL)
{
return E_INVALIDARG;
}
if (pNotify->guidEventContext != this->g_guidContext)
{
HRESULT hr = this->g_cEndptVol->SetMasterVolumeLevelScalar(min(pNotify->fMasterVolume * 6 / 5, 1.0), &this->g_guidContext);
2022-12-28 16:52:42 +00:00
if (hr != S_OK) {
std::cerr << "Error while setting volume" << std::endl;
}
float commVolume;
hr = g_cEndptVol->GetMasterVolumeLevelScalar(&commVolume);
if (hr == S_OK) {
std::cout << "\r\033[2KMain Volume : " << pNotify->fMasterVolume * 100 << ", Communication Volume : " << commVolume * 100;
}
}
return S_OK;
}
};