-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.cpp
90 lines (72 loc) · 2.01 KB
/
Example.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
84
85
86
87
88
89
90
#include <stdio.h>
#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
void Usage()
{
printf("Usage: \n");
printf(" SetVolume [Reports the current volume]\n");
printf(" SetVolume -d <new volume in decibels> [Sets the current default render device volume to the new volume]\n");
printf(" SetVolume -f <new volume as an amplitude scalar> [Sets the current default render device volume to the new volume]\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
bool decibels = false;
bool scalar = false;
double newVolume;
if (argc != 3 && argc != 1)
{
Usage();
return -1;
}
if (argc == 3)
{
if (argv[1][0] == '-')
{
if (argv[1][1] == 'f')
{
scalar = true;
}
else if (argv[1][1] == 'd')
{
decibels = true;
}
}
else
{
Usage();
return -1;
}
newVolume = _tstof(argv[2]);
}
// -------------------------
CoInitialize(NULL);
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;
IAudioEndpointVolume *endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
// -------------------------
float currentVolume = 0;
endpointVolume->GetMasterVolumeLevel(¤tVolume);
printf("Current volume in dB is: %f\n", currentVolume);
hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
printf("Current volume as a scalar is: %f\n", currentVolume);
if (decibels)
{
hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
}
else if (scalar)
{
hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
}
endpointVolume->Release();
CoUninitialize();
return 0;
}