-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvolumechanger.cpp
More file actions
126 lines (105 loc) · 2.82 KB
/
volumechanger.cpp
File metadata and controls
126 lines (105 loc) · 2.82 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
#include <endpointvolume.h>
#include <mmdeviceapi.h>
#include <Windows.h>
#include "logging.h"
#include "volumechanger.h"
/*
* Details of winapi methods used: https://msdn.microsoft.com/en-us/library/aa964574.aspx
* Big thanks to this guy: http://blogs.msdn.com/b/larryosterman/archive/2007/03/06/how-do-i-change-the-master-volume-in-windows-vista.aspx
*/
VolumeChanger::VolumeChanger()
:_initialized(false),_endpointVolume(NULL)
{
this->init();
}
VolumeChanger::~VolumeChanger()
{
this->unInit();
}
void VolumeChanger::init()
{
if (!_initialized)
{
IMMDeviceEnumerator *deviceEnumerator = NULL;
IMMDevice *defaultDevice = NULL;
CoInitialize(NULL);
//TODO: add error handling
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
if (hr != S_OK)
{
ERR("CoCreateInstance error: " << hr);
return;
}
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
if (hr != S_OK)
{
ERR("Cannot GetDefaultAudioEndpoint: " << hr);
return;
}
deviceEnumerator->Release();
deviceEnumerator = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&_endpointVolume);
if (hr != S_OK)
{
ERR("Cannot activate default device: " << hr);
return;
}
defaultDevice->Release();
defaultDevice = NULL;
_initialized = true;
}
}
void VolumeChanger::unInit()
{
_endpointVolume->Release();
CoUninitialize();
_initialized = false;
}
// Public methods
VolumeChanger& VolumeChanger::Instance()
{
static VolumeChanger singleton;
return singleton;
}
bool VolumeChanger::setVolume(double iVol)
{
if (!_initialized) return false;
HRESULT hr = _endpointVolume->SetMasterVolumeLevelScalar(static_cast<float>(iVol), NULL);
if (hr != 0)
{
//TODO: throw exceptions?
ERR( "["<<__func__<<"]: Error "<< hr );
return false;
}
return true;
}
double VolumeChanger::getVolume() const
{
if (!_initialized){ return -1;}
float fVol;
HRESULT hr = _endpointVolume->GetMasterVolumeLevelScalar(&fVol);
if (hr != 0)
{
//TODO: throw exceptions?
ERR(hr);
return -2;
}
return static_cast<double>(fVol);
}
bool VolumeChanger::setMute (bool iMute)
{
HRESULT hr = _endpointVolume->SetMute(iMute, NULL);
if (hr != 0)
{
ERR(hr);
return false;
}
return true;
}
bool VolumeChanger::isMute() const
{
BOOL mute = true;
HRESULT hr = _endpointVolume->GetMute(&mute);
if (hr != 0){ERR(hr);}
return (mute)?true:false; //f*cking vs warnings!!!
}