-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VolumeController.cpp
83 lines (68 loc) · 1.96 KB
/
VolumeController.cpp
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
#include "pch.h"
#include "VolumeController.h"
// Pulled from:
// https://www.codeproject.com/Tips/233484/Change-Master-Volume-in-Visual-Cplusplus
// A little fixing up and nudging around, but mostly whole hog a copy
VolumeController::VolumeController():
endpointVolume(NULL)
{
HRESULT hr = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
IMMDeviceEnumerator* deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), (LPVOID*)&deviceEnumerator);
IMMDevice* defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
CLSCTX_INPROC_SERVER, NULL, (LPVOID*)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
}
VolumeController::~VolumeController()
{
endpointVolume->Release();
CoUninitialize();
}
void VolumeController::volumeUp()
{
endpointVolume->VolumeStepUp(NULL);
}
void VolumeController::volumeDown()
{
endpointVolume->VolumeStepDown(NULL);
}
void VolumeController::mute()
{
HRESULT hr{ NULL };
BOOL muteState{ false };
hr = endpointVolume->GetMute(&muteState);
hr = endpointVolume->SetMute(!muteState, NULL);
}
double VolumeController::getVolume(const bool scalar)
{
HRESULT hr{ NULL };
float currentVolume{ 0 };
if(scalar)
{
hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
}
else
{
hr = endpointVolume->GetMasterVolumeLevel(¤tVolume);
}
return currentVolume;
}
void VolumeController::setVolume(const double value, const bool scalar)
{
HRESULT hr{ NULL };
if (scalar)
{
hr = endpointVolume->SetMasterVolumeLevelScalar((float)value, NULL);
}
else
{
hr = endpointVolume->SetMasterVolumeLevel((float)value, NULL);
}
}