-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAudioCapture.cpp
153 lines (123 loc) · 3.42 KB
/
AudioCapture.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "AudioCapture.h"
#include AUDIO_IMPL_HEADER
#include "ProjectMWrapper.h"
#include "notifications/DisplayToastNotification.h"
#include <Poco/NotificationCenter.h>
#include <Poco/Util/Application.h>
const char* AudioCapture::name() const
{
return "Audio Capturing";
}
void AudioCapture::initialize(Poco::Util::Application& app)
{
_config = app.config().createView("audio");
auto& projectMWrapper = app.getSubsystem<ProjectMWrapper>();
if (!_impl)
{
_impl = new AudioCaptureImpl;
}
auto deviceList = _impl->AudioDeviceList();
int audioDeviceIndex = GetInitialAudioDeviceIndex(deviceList);
PrintDeviceList(deviceList);
_impl->StartRecording(projectMWrapper.ProjectM(), audioDeviceIndex);
}
void AudioCapture::uninitialize()
{
if (_impl)
{
_impl->StopRecording();
delete _impl;
_impl = nullptr;
}
}
void AudioCapture::NextAudioDevice()
{
if (_impl)
{
_impl->NextAudioDevice();
Poco::NotificationCenter::defaultCenter().postNotification(new DisplayToastNotification(_impl->AudioDeviceName()));
}
}
void AudioCapture::AudioDeviceIndex(int index)
{
if (_impl)
{
_impl->AudioDeviceIndex(index);
Poco::NotificationCenter::defaultCenter().postNotification(new DisplayToastNotification(_impl->AudioDeviceName()));
}
}
int AudioCapture::AudioDeviceIndex() const
{
if (_impl)
{
return _impl->AudioDeviceIndex();
}
return -1;
}
std::string AudioCapture::AudioDeviceName() const
{
if (!_impl)
{
return {};
}
return _impl->AudioDeviceName();
}
AudioCapture::AudioDeviceMap AudioCapture::AudioDeviceList()
{
if (!_impl)
{
return {{-1, "(No audio devices available)"}};
}
return _impl->AudioDeviceList();
}
void AudioCapture::FillBuffer()
{
if (!_impl)
{
return;
}
_impl->FillBuffer();
}
void AudioCapture::PrintDeviceList(const AudioDeviceMap& deviceList) const
{
if (_config->getBool("listDevices", false))
{
poco_information(_logger, "Available audio capturing devices:");
for (const auto& device : deviceList)
{
poco_information_f2(_logger, " %?d = %s", device.first, device.second);
}
}
}
int AudioCapture::GetInitialAudioDeviceIndex(const AudioDeviceMap& deviceList)
{
int audioDeviceIndex{ -1 };
// Check if configured device is a number or string
try
{
audioDeviceIndex = _config->getInt("device", -1);
if (deviceList.find(audioDeviceIndex) == deviceList.end())
{
poco_debug(_logger,
"audio.device was set to a numerical value, but out of bounds. Reverting to default device.");
audioDeviceIndex = -1;
}
}
catch (Poco::SyntaxException& ex)
{
auto audioDeviceName = _config->getString("device", "");
poco_debug_f1(_logger, R"(audio.device is set to non-numerical value. Searching for device name "%s".)",
audioDeviceName);
for (const auto& device: deviceList)
{
if (device.second == audioDeviceName)
{
audioDeviceIndex = device.first;
break;
}
}
}
poco_information_f2(_logger, R"(Recording audio from device "%s" (ID %?d).)",
deviceList.at(audioDeviceIndex), audioDeviceIndex);
return audioDeviceIndex;
}