-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManager.cpp
More file actions
149 lines (119 loc) · 4 KB
/
Copy pathAudioManager.cpp
File metadata and controls
149 lines (119 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "AudioManager.hpp"
#include <iostream>
#include <cmath>
// {A316E243-764D-4700-9423-93E1030F5522}
const GUID AudioManager::m_contextGuid = { 0xa316e243, 0x764d, 0x4700, { 0x94, 0x23, 0x93, 0xe1, 0x3, 0xf, 0x55, 0x22 } };
AudioManager::AudioManager() {}
AudioManager::~AudioManager() {
CleanupDevice();
if (m_enumerator) {
m_enumerator->UnregisterEndpointNotificationCallback(this);
}
}
bool AudioManager::Initialize() {
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&m_enumerator);
if (FAILED(hr)) return false;
hr = m_enumerator->RegisterEndpointNotificationCallback(this);
if (FAILED(hr)) return false;
return SetupDefaultDevice();
}
bool AudioManager::SetupDefaultDevice() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
CleanupDevice();
HRESULT hr = m_enumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &m_device);
if (FAILED(hr)) return false;
hr = m_device->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&m_volumeControl);
if (FAILED(hr)) return false;
hr = m_volumeControl->RegisterControlChangeNotify(this);
if (FAILED(hr)) return false;
// Initial enforcement
EnforceVolume();
return true;
}
void AudioManager::CleanupDevice() {
if (m_volumeControl) {
m_volumeControl->UnregisterControlChangeNotify(this);
m_volumeControl.Reset();
}
if (m_device) {
m_device.Reset();
}
}
void AudioManager::SetTargetVolume(float volume) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
m_targetVolume = volume;
EnforceVolume();
}
void AudioManager::SetEnabled(bool enabled) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
m_enabled = enabled;
if (m_enabled) {
EnforceVolume();
}
}
void AudioManager::ApplyVolumeImmediately(float volume) {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (volume < 0.0f) {
volume = 0.0f;
}
if (volume > 1.0f) {
volume = 1.0f;
}
m_targetVolume = volume;
if (!m_volumeControl) {
return;
}
m_volumeControl->SetMasterVolumeLevelScalar(m_targetVolume, &m_contextGuid);
BOOL mute = FALSE;
m_volumeControl->GetMute(&mute);
if (mute) {
m_volumeControl->SetMute(FALSE, &m_contextGuid);
}
}
void AudioManager::EnforceVolume() {
std::lock_guard<std::recursive_mutex> lock(m_mutex);
if (!m_enabled || !m_volumeControl) return;
float currentVolume = 0.0f;
m_volumeControl->GetMasterVolumeLevelScalar(¤tVolume);
// Use a small epsilon for float comparison
if (std::abs(currentVolume - m_targetVolume) > 0.001f) {
m_volumeControl->SetMasterVolumeLevelScalar(m_targetVolume, &m_contextGuid);
}
BOOL mute = FALSE;
m_volumeControl->GetMute(&mute);
if (mute) {
m_volumeControl->SetMute(FALSE, &m_contextGuid);
}
}
STDMETHODIMP AudioManager::QueryInterface(REFIID riid, void** ppv) {
if (riid == __uuidof(IUnknown) || riid == __uuidof(IAudioEndpointVolumeCallback)) {
*ppv = static_cast<IAudioEndpointVolumeCallback*>(this);
} else if (riid == __uuidof(IMMNotificationClient)) {
*ppv = static_cast<IMMNotificationClient*>(this);
} else {
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
STDMETHODIMP_(ULONG) AudioManager::AddRef() {
return ++m_refCount;
}
STDMETHODIMP_(ULONG) AudioManager::Release() {
long count = --m_refCount;
// Since we are using this as a member in main, we don't delete this
return count;
}
STDMETHODIMP AudioManager::OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA pNotify) {
if (pNotify->guidEventContext != m_contextGuid) {
EnforceVolume();
}
return S_OK;
}
STDMETHODIMP AudioManager::OnDefaultDeviceChanged(EDataFlow flow, ERole role, [[maybe_unused]] LPCWSTR pwstrDefaultDeviceId) {
if (flow == eCapture && role == eConsole) {
SetupDefaultDevice();
}
return S_OK;
}