-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmic.cpp
90 lines (73 loc) · 1.84 KB
/
mic.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 "mic.h"
#include <algorithm>
#include <atlbase.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
class CoInit
{
public:
CoInit()
{
ATLENSURE_SUCCEEDED(::CoInitialize(nullptr));
}
~CoInit()
{
::CoUninitialize();
}
};
class Mic::Impl : CoInit
{
public:
Impl()
{
// Create the device enumerator.
CComPtr<IMMDeviceEnumerator> deviceEnumerator;
ATLENSURE_SUCCEEDED(CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnumerator));
// Get the default audio endpoint.
CComPtr<IMMDevice> defaultDevice;
ATLENSURE_SUCCEEDED(deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &defaultDevice));
// Activate the endpoint volume interface.
ATLENSURE_SUCCEEDED(defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, nullptr, (void**)&m_endpointVolume));
}
public:
void ToggleMute()
{
BOOL mute;
ATLENSURE_SUCCEEDED(m_endpointVolume->GetMute(&mute));
ATLENSURE_SUCCEEDED(m_endpointVolume->SetMute(!mute, nullptr));
}
void IncVol()
{
ChangeVol(.02f);
}
void DecVol()
{
ChangeVol(-.02f);
}
private:
void ChangeVol(float delta)
{
float currentVolume;
ATLENSURE_SUCCEEDED(m_endpointVolume->GetMasterVolumeLevelScalar(¤tVolume));
ATLENSURE_SUCCEEDED(m_endpointVolume->SetMasterVolumeLevelScalar(std::clamp (currentVolume + delta, 0.f, 1.f), nullptr));
}
private:
CComPtr<IAudioEndpointVolume> m_endpointVolume;
};
Mic::Mic():
m_impl {std::make_unique<Impl>()}
{
}
Mic::~Mic() = default;
void Mic::ToggleMute()
{
m_impl->ToggleMute();
}
void Mic::IncVol()
{
m_impl->IncVol();
}
void Mic::DecVol()
{
m_impl->DecVol();
}