forked from s7726/PowerMate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeController.cpp
More file actions
84 lines (69 loc) · 2.13 KB
/
VolumeController.cpp
File metadata and controls
84 lines (69 loc) · 2.13 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
#include "pch.h"
#include <mmdeviceapi.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(nullptr)
{
HRESULT hr = NULL;
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
IMMDeviceEnumerator* deviceEnumerator = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), reinterpret_cast<LPVOID*>(&deviceEnumerator));
IMMDevice* defaultDevice = nullptr;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = nullptr;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
CLSCTX_INPROC_SERVER, nullptr, reinterpret_cast<LPVOID*>(&endpointVolume));
defaultDevice->Release();
defaultDevice = nullptr;
}
VolumeController::~VolumeController()
{
endpointVolume->Release();
CoUninitialize();
}
void VolumeController::volume_up() const
{
endpointVolume->VolumeStepUp(nullptr);
}
void VolumeController::volume_down() const
{
endpointVolume->VolumeStepDown(nullptr);
}
void VolumeController::mute() const
{
HRESULT hr { NULL };
BOOL muteState{ false };
hr = endpointVolume->GetMute(&muteState);
hr = endpointVolume->SetMute(!muteState, nullptr);
}
double VolumeController::get_volume(const bool scalar) const
{
HRESULT hr { NULL };
float current_volume { 0 };
if(scalar)
{
hr = endpointVolume->GetMasterVolumeLevelScalar(¤t_volume);
}
else
{
hr = endpointVolume->GetMasterVolumeLevel(¤t_volume);
}
return current_volume;
}
void VolumeController::set_volume(const double value, const bool scalar) const
{
HRESULT hr { NULL };
if (scalar)
{
hr = endpointVolume->SetMasterVolumeLevelScalar(static_cast<float>(value), nullptr);
}
else
{
hr = endpointVolume->SetMasterVolumeLevel(static_cast<float>(value), nullptr);
}
}