-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioPipeline.cpp
More file actions
214 lines (165 loc) · 4.98 KB
/
Copy pathAudioPipeline.cpp
File metadata and controls
214 lines (165 loc) · 4.98 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <voicesmith/io/AudioPipeline.h>
#include <voicesmith/Source.h>
AudioPipeline::AudioPipeline(const std::shared_ptr<AudioSource> source,
const std::shared_ptr<AudioSink> sink,
const std::shared_ptr<AudioEffect> effect) :
source(source),
sink(sink),
effect(effect) {
}
AudioPipeline::~AudioPipeline() {
close();
}
void AudioPipeline::subscribe(const AudioEvent::Callback& callback) {
event.append(callback);
}
void AudioPipeline::open() {
source->open();
sink->open();
if (source->samplerate() != sink->samplerate()) {
throw std::runtime_error(
$("Unequal audio stream sample rate: {0} (source), {1} (sink)!",
source->samplerate(), sink->samplerate()));
}
if (source->blocksize() != sink->blocksize()) {
throw std::runtime_error(
$("Unequal audio stream block size: {0} (source), {1} (sink)!",
source->blocksize(), sink->blocksize()));
}
if (source->channels() != sink->channels()) {
throw std::runtime_error(
$("Unequal audio stream channel count: {0} (source), {1} (sink)!",
source->channels(), sink->channels()));
}
const auto samplerate = source->samplerate();
const auto blocksize = source->blocksize();
const auto channels = source->channels();
const auto fifosize = static_cast<size_t>(std::ceil(
(1.0 /* seconds */ * samplerate) / (1.0 * blocksize / channels)));
Log::i("Using samplerate={0} blocksize={1} channels={2} fifosize={3}",
samplerate, blocksize, channels, fifosize);
source->fifo()->resize(fifosize, blocksize);
sink->fifo()->resize(fifosize, blocksize);
source->subscribe([&](const AudioEventCode code, const std::string& data) {
onevent(code, data);
});
sink->subscribe([&](const AudioEventCode code, const std::string& data) {
onevent(code, data);
});
if (effect) {
effect->reset(samplerate, blocksize, channels);
}
}
void AudioPipeline::close() {
stop();
source->close();
sink->close();
}
void AudioPipeline::start() {
stop();
state.thread = std::make_shared<std::thread>(
[&]() { onloop(); });
sink->start();
source->start();
state.loop = true;
state.signal.notify_all();
}
void AudioPipeline::stop() {
state.loop = false;
if (state.thread != nullptr) {
if (state.thread->joinable()) {
state.thread->join();
}
state.thread = nullptr;
}
sink->stop();
source->stop();
sink->fifo()->flush();
source->fifo()->flush();
}
void AudioPipeline::onloop() {
const auto millis = [](const std::chrono::steady_clock::duration& duration) {
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
};
const auto now = []() {
return std::chrono::steady_clock::now();
};
auto ok = true;
auto index = uint64_t(0);
auto timeout = source->timeout();
auto timestamp = now();
timers_t timers;
debouncers_t debouncers;
debouncers.read.onflush([&](auto count){
event(
AudioEventCode::PipeRead,
$("index={0} count={1} timeout={2}ms",
index, count, timeout.count()));
});
debouncers.write.onflush([&](auto count){
event(
AudioEventCode::PipeWrite,
$("index={0} count={1}",
index, count));
});
if (!state.loop) {
std::unique_lock lock(state.mutex);
state.signal.wait_for(lock, std::chrono::seconds(1));
}
if (!state.loop) {
return;
}
while (state.loop && ok && (millis(now() - timestamp) < 1'000)) {
ok = oncycle(timers, debouncers, index, timeout * 3);
}
while (state.loop && ok) {
Log::d("Timing: inner {0} / outer {1}",
timers.inner.str(), timers.outer.str());
timers.outer.cls();
timers.inner.cls();
timestamp = now();
while (state.loop && ok && (millis(now() - timestamp) < 10'000)) {
ok = oncycle(timers, debouncers, index, timeout);
}
}
if (!ok) {
Log::e("Aborting pipe loop due to an error!");
}
}
bool AudioPipeline::oncycle(timers_t& timers, debouncers_t& debouncers, uint64_t& index, const std::chrono::milliseconds& timeout) const {
bool okcycle = true;
if (!index) {
timers.outer.tic();
}
const bool okread = source->fifo()->read(timeout, [&](AudioBlock& input) {
timers.outer.toc();
timers.outer.tic();
const bool okwrite = sink->fifo()->write([&](AudioBlock& output) {
timers.inner.tic();
try {
if (effect) {
effect->apply(index, input, output);
} else {
input.copyto(output);
}
}
catch (const std::exception& exception) {
event(AudioEventCode::PipeError, exception.what());
okcycle = false;
}
timers.inner.toc();
++index;
});
debouncers.write(!okwrite);
});
debouncers.read(!okread);
return okcycle;
}
void AudioPipeline::onevent(const AudioEventCode code, const std::string& data) {
if (code >= AudioEventCode::ERROR) {
Log::e("Aborting audio pipeline due to error: {0}!", data);
std::unique_lock lock(eventmutex);
close();
}
event(code, data);
}