-
Notifications
You must be signed in to change notification settings - Fork 71
/
SampledAudioNode.cpp
611 lines (515 loc) · 22.3 KB
/
SampledAudioNode.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// License: BSD 2 Clause
// Copyright (C) 2015+, The LabSound Authors. All rights reserved.
/// @TODO allow for reversed playback
#include "LabSound/core/SampledAudioNode.h"
#include "LabSound/core/AudioContext.h"
#include "LabSound/core/AudioNodeOutput.h"
#include "LabSound/core/AudioSetting.h"
#include "LabSound/extended/AudioContextLock.h"
#include "LabSound/extended/Registry.h"
#include "LabSound/extended/VectorMath.h"
#include "internal/Assertions.h"
#include "concurrentqueue/concurrentqueue.h"
#include "libsamplerate/include/samplerate.h"
using namespace lab;
namespace lab {
/*
* outputs silence when no source bus is playing
* overall playing state governed by audioscheduledsourcenode
* onended is dispatched both when the node is stopped, and when the end of a buffer is reached
* start/stop(when)
*/
struct SRC_Resampler
{
SRC_STATE* sampler = nullptr;
SRC_DATA data;
SRC_Resampler() = delete;
SRC_Resampler(SRC_STATE* s) : sampler(s)
{
memset(&data, 0, sizeof(data));
}
~SRC_Resampler()
{
if (sampler)
src_delete(sampler);
}
};
struct SampledAudioNode::Scheduled
{
double when; // in context temporal frame
int32_t grain_start; // in source bus temporal frame
int32_t grain_end;
int32_t cursor;
int loopCount; // -1 means forever, 0 means play once, 1 means repeat once -2 is a sentinel value meaning clear the schedule
std::shared_ptr<AudioBus> sourceBus;
std::vector<std::shared_ptr<SRC_Resampler>> resampler; // there is one resampler per source channel
};
struct SampledAudioNode::Internals
{
explicit Internals(AudioContext& ac_)
: greatest_cursor(-1)
, ac(ac_.audioContextInterface())
{
}
~Internals() = default;
moodycamel::ConcurrentQueue<Scheduled> incoming;
std::vector<Scheduled> scheduled;
int32_t greatest_cursor = -1;
std::weak_ptr<AudioContext::AudioContextInterface> ac;
bool bus_setting_updated = false;
};
static AudioParamDescriptor s_saParams[] = {
{"playbackRate", "RATE", 1.0, 0.0, 1024.},
{"detune", "DTUNE", 0.0, 0.0, 1200.},
{"dopplerRate", "DPLR", 1.0, 0.0, 1200.}, nullptr};
static AudioSettingDescriptor s_saSettings[] = {
{"sourceBus", "SBUS", SettingType::Bus}, nullptr};
AudioNodeDescriptor * SampledAudioNode::desc()
{
static AudioNodeDescriptor d = {s_saParams, s_saSettings, 2};
return &d;
}
SampledAudioNode::SampledAudioNode(AudioContext& ac)
: AudioScheduledSourceNode(ac, *desc())
, _internals(new Internals(ac))
{
m_sourceBus = setting("sourceBus");
m_playbackRate = param("playbackRate");
m_detune = param("detune");
m_dopplerRate = param("dopplerRate");
m_sourceBus->setValueChanged([this]() {
this->_internals->bus_setting_updated = true;
});
initialize();
}
SampledAudioNode::~SampledAudioNode()
{
clearSchedules();
delete _internals;
if (isInitialized())
uninitialize();
}
void SampledAudioNode::clearSchedules()
{
Scheduled s;
while (_internals->incoming.try_dequeue(s)) {}
_internals->incoming.enqueue({ 0., 0,0,0, -2 });
}
void SampledAudioNode::setBus(std::shared_ptr<AudioBus> sourceBus)
{
// loop count of -3 means set the bus.
_internals->incoming.enqueue({ 0, 0, 0, 0, -3, sourceBus });
initialize();
// set the pending pointer, so that a synchronous call to getBus will reflect
// the value last scheduled. This eliminates a confusing sitatuion where getBus
// will not be useful until the scheduling queue is serviced
m_pendingSourceBus = sourceBus;
}
void SampledAudioNode::setBus(ContextRenderLock&, std::shared_ptr<AudioBus> sourceBus) {
setBus(sourceBus);
}
void SampledAudioNode::start(float when)
{
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (!bus)
return;
auto ac = _internals->ac.lock();
if (!ac)
return;
when = static_cast<float>(when - ac->currentTime());
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
_internals->incoming.enqueue({when, 0, bus->length(), 0, 0});
initialize();
}
void SampledAudioNode::start(float when, int loopCount)
{
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (!bus)
return;
auto ac = _internals->ac.lock();
if (!ac)
return;
when = static_cast<float>(when - ac->currentTime());
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
_internals->incoming.enqueue({when, 0, bus->length(), 0, loopCount});
initialize();
}
void SampledAudioNode::start(float when, float grainOffset, int loopCount)
{
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (!bus)
return;
auto ac = _internals->ac.lock();
if (!ac)
return;
when = static_cast<float>(when - ac->currentTime());
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
float r = bus->sampleRate();
int32_t grainStart = static_cast<uint32_t>(grainOffset * r);
int32_t grainEnd = bus->length();
if (grainStart < grainEnd)
{
_internals->incoming.enqueue({when,
grainStart, grainEnd, grainStart,
loopCount});
}
initialize();
}
void SampledAudioNode::start(float when, float grainOffset, float grainDuration, int loopCount)
{
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (!bus)
return;
auto ac = _internals->ac.lock();
if (!ac)
return;
when = static_cast<float>(when - ac->currentTime());
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
float r = bus->sampleRate();
int32_t grainStart = static_cast<uint32_t>(grainOffset * r);
int32_t grainEnd = grainStart + static_cast<uint32_t>(grainDuration * r);
if (grainEnd > bus->length())
grainEnd = bus->length() - grainStart;
if (grainStart < grainEnd)
{
_internals->incoming.enqueue({when,
grainStart, grainEnd, grainStart,
loopCount});
}
initialize();
}
void SampledAudioNode::schedule(float when)
{
if (!isPlayingOrScheduled()) {
_self->_scheduler.start(0.);
}
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (bus) {
_internals->incoming.enqueue({when, 0, bus->length(), 0, 0});
}
else {
if (_internals->bus_setting_updated)
_internals->incoming.enqueue({when, 0, m_sourceBus->valueBus()->length(), 0, 0});
}
initialize();
}
void SampledAudioNode::schedule(float when, int loopCount)
{
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (bus)
_internals->incoming.enqueue({when, 0, bus->length(), 0, loopCount});
else {
if (_internals->bus_setting_updated)
_internals->incoming.enqueue({when, 0, m_sourceBus->valueBus()->length(), 0, loopCount});
}
initialize();
}
void SampledAudioNode::schedule(float when, float grainOffset, int loopCount)
{
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (bus) {
float r = bus->sampleRate();
int32_t grainStart = static_cast<uint32_t>(grainOffset * r);
int32_t grainEnd = bus->length();
if (grainStart < grainEnd)
{
_internals->incoming.enqueue({when,
grainStart, grainEnd, grainStart,
loopCount});
}
}
initialize();
}
void SampledAudioNode::schedule(float when, float grainOffset, float grainDuration, int loopCount)
{
if (!isPlayingOrScheduled())
_self->_scheduler.start(0.);
std::shared_ptr<AudioBus> bus = m_pendingSourceBus;
if (bus) {
float r = bus->sampleRate();
int32_t grainStart = static_cast<uint32_t>(grainOffset * r);
int32_t grainEnd = grainStart + static_cast<uint32_t>(grainDuration * r);
if (grainEnd > bus->length())
grainEnd = bus->length() - grainStart;
if (grainStart < grainEnd)
{
_internals->incoming.enqueue({when,
grainStart, grainEnd, grainStart,
loopCount});
}
}
initialize();
}
bool SampledAudioNode::renderSample(ContextRenderLock& r, Scheduled& schedule, size_t destinationSampleOffset, size_t frameSize)
{
std::shared_ptr<AudioBus> srcBus = m_sourceBus->valueBus();
AudioBus* dstBus = output(0)->bus(r);
size_t dstChannelCount = dstBus->numberOfChannels();
size_t srcChannelCount = srcBus->numberOfChannels();
ASSERT(dstChannelCount == srcChannelCount);
float* buffer = dstBus->channel(0)->mutableData();
float rate = totalPitchRate(r);
if (fabsf(rate - 1.f) < 1e-3f)
{
// no pitch modification
int write_index = (int) destinationSampleOffset;
while (write_index < AudioNode::ProcessingSizeInFrames)
{
int count = AudioNode::ProcessingSizeInFrames - write_index;
int remainder = schedule.grain_end - schedule.cursor;
bool ending = remainder < count;
count = std::min(count, remainder);
for (int i = 0; i < srcChannelCount; ++i)
{
float* buffer = dstBus->channel(i)->mutableData();
VectorMath::vadd(srcBus->channel(i)->data() + schedule.cursor, 1,
buffer + write_index, 1,
buffer + write_index, 1, count);
}
schedule.cursor += count;
write_index += count;
if (ending)
{
schedule.cursor = schedule.grain_start; // reset to start
if (schedule.loopCount > 0)
schedule.loopCount--;
else if (schedule.loopCount < 0)
{
// infinite looping
}
else
{
schedule.loopCount = -3; // signal retirement of the schedule
break; // and stop the write loop
}
}
}
}
else
{
while (schedule.resampler.size() < srcBus->numberOfChannels())
{
// samplers are expensive to allocate, so if there's one to recycle, use it
if (_resamplers.size())
{
auto resampler = _resamplers.back();
schedule.resampler.push_back(resampler);
_resamplers.pop_back();
src_reset(resampler->sampler);
memset(&resampler->data, 0, sizeof(resampler->data));
}
else
{
int err = 0;
schedule.resampler.push_back(std::make_shared<SRC_Resampler>(src_new(SRC_LINEAR, 1, &err)));
}
}
if (schedule.resampler.size() >= srcBus->numberOfChannels())
{
// pitch modification
int write_index = (int) destinationSampleOffset;
while (write_index < AudioNode::ProcessingSizeInFrames)
{
int count = AudioNode::ProcessingSizeInFrames - write_index;
int remainder = schedule.grain_end - schedule.cursor;
bool ending = remainder < count;
int src_increment = 0;
int dst_increment = 0;
for (int i = 0; i < srcChannelCount; ++i)
{
SRC_DATA* src_data = &schedule.resampler[i]->data;
float* buffer = dstBus->channel(i)->mutableData();
src_data->data_in = srcBus->channel(i)->data() + schedule.cursor;
src_data->input_frames = remainder;
std::array<float, AudioNode::ProcessingSizeInFrames> buff;
src_data->data_out = buff.data();
src_data->output_frames = AudioNode::ProcessingSizeInFrames;
src_data->src_ratio = 1. / rate;
src_data->end_of_input = ending ? 1 : 0;
src_process(schedule.resampler[i]->sampler, src_data);
VectorMath::vadd(buff.data(), 1, buffer + write_index, 1, buffer + write_index, 1, count);
src_increment = static_cast<int>(src_data->input_frames_used);
dst_increment = static_cast<int>(src_data->output_frames_gen);
}
schedule.cursor += src_increment;
write_index += dst_increment;
ending |= (src_increment == 0) || (dst_increment == 0);
if (ending)
{
if (write_index < AudioNode::ProcessingSizeInFrames) {
// if the buffer didn't fully fill the buffer, at the end, zero out the remainder
/// @TODO should lerp the end value to zero over a 10ms tail period.
/// Going to assume the source data doesn't end with a non-zero value for now.
for (int i = 0; i < srcChannelCount; ++i)
{
SRC_DATA * src_data = &schedule.resampler[i]->data;
float* buffer = dstBus->channel(i)->mutableData();
for (int j = write_index; j < AudioNode::ProcessingSizeInFrames; ++j)
buffer[j] = 0.f;
}
}
schedule.cursor = schedule.grain_start; // reset to start
if (schedule.loopCount > 0)
schedule.loopCount--;
else if (schedule.loopCount < 0)
{
// infinite looping, nothing to do
}
else
{
schedule.loopCount = -3; // signal retirement of the schedule
break; // and stop the write loop
}
}
}
}
}
//r.context()->appendDebugBuffer(dstBus, 0, AudioNode::ProcessingSizeInFrames);
dstBus->clearSilentFlag();
return true;
}
void SampledAudioNode::process(ContextRenderLock& r, int framesToProcess)
{
auto ac = r.context();
if (!ac)
return;
bool diagnosing_silence = ac->diagnosing().get() == this;
if (_internals->bus_setting_updated) {
_internals->bus_setting_updated = false;
setBus(r, m_sourceBus->valueBus());
}
_internals->greatest_cursor = -1;
AudioBus* dstBus = output(0)->bus(r);
size_t dstChannelCount = dstBus->numberOfChannels();
std::shared_ptr<AudioBus> srcBus = m_sourceBus->valueBus();
// move requested starts to the internal schedule if there's a source bus.
// if there's no source bus, the schedule requests are discarded.
{
Scheduled s;
while (_internals->incoming.try_dequeue(s))
{
if (s.loopCount == -3)
{
m_retainedSourceBus = s.sourceBus;
m_sourceBus->setBus(s.sourceBus.get());
srcBus = s.sourceBus;
_internals->bus_setting_updated = false; // setting bus causes this -3 state to occur so clear it immediately
if (diagnosing_silence)
ac->diagnosed_silence("SampledAudioNode::bus has been set");
}
else if (s.loopCount == -2)
{
_internals->scheduled.clear();
if (diagnosing_silence)
ac->diagnosed_silence("SampledAudioNode::clearing schedule");
}
else if (srcBus)
{
_internals->scheduled.push_back(s);
if (diagnosing_silence)
ac->diagnosed_silence("SampledAudioNode::push_back schedule");
}
else if (diagnosing_silence)
ac->diagnosed_silence("SampledAudioNode::schedule encountered, but no source bus has been set");
}
}
// zero out the buffer for summing, or for silence
for (int i = 0; i < dstChannelCount; ++i)
output(0)->bus(r)->zero();
// silence the outputs if there's nothing to play.
int schedule_count = static_cast<int>(_internals->scheduled.size());
if (!schedule_count || !srcBus) {
if (diagnosing_silence)
ac->diagnosed_silence("SampledAudioNode::process no schedule_count");
return;
}
// if there's something to play, conform the output channel count.
int srcChannelCount = srcBus->numberOfChannels();
if (dstChannelCount != srcChannelCount)
{
output(0)->setNumberOfChannels(r, srcChannelCount);
dstChannelCount = srcChannelCount;
dstBus = output(0)->bus(r);
}
// compute the frame timing in samples and seconds
uint64_t quantumStartFrame = r.context()->currentSampleFrame();
uint64_t quantumEndFrame = quantumStartFrame + AudioNode::ProcessingSizeInFrames;
double quantumDuration = static_cast<double>(AudioNode::ProcessingSizeInFrames) / r.context()->sampleRate();
double quantumStartTime = r.context()->currentTime();
double quantumEndTime = quantumStartTime + quantumDuration;
// is anything playing in this quantum?
for (int i = 0; i < schedule_count; ++i)
{
Scheduled& s = _internals->scheduled.at(i);
if (s.when < quantumDuration) // has s.when counted down to within this quantum?
{
int32_t offset = (s.when < quantumStartTime) ? 0 : static_cast<int32_t>(s.when * r.context()->sampleRate());
renderSample(r, s, (size_t) offset, AudioNode::ProcessingSizeInFrames);
output(0)->bus(r)->clearSilentFlag();
if (s.cursor > _internals->greatest_cursor)
_internals->greatest_cursor = s.cursor;
}
// keep counting the scheduled item down
s.when -= quantumDuration;
}
// retire any schedules whose loopcount is -2, which means that renderSample signalled a finish
for (int i = 0; i < schedule_count; ++i)
{
Scheduled& s = _internals->scheduled.at(i);
if (s.loopCount < -1)
{
while (s.resampler.size())
{
_resamplers.push_back(s.resampler.back()); // resamplers are expensive to construct, so cache them
s.resampler.pop_back();
}
if (schedule_count - 1 > i)
_internals->scheduled.at(i) = _internals->scheduled.at(schedule_count - 1);
_internals->scheduled.pop_back();
--schedule_count;
if (_self->_scheduler._onEnded)
r.context()->enqueueEvent(_self->_scheduler._onEnded);
}
}
}
int32_t SampledAudioNode::getCursor() const
{
return _internals->greatest_cursor;
}
/// @TODO change the interface:
/// // if true is returned, rate_array[0] applies to the entire quantum
/// // if the computed total rate has any illegal values, true will be returned, and a default rate of 1.f
/// bool SampledAudioNode::totalPitchRate(ContextRenderLock& r, float*& rate);
float SampledAudioNode::totalPitchRate(ContextRenderLock& r)
{
std::shared_ptr<AudioBus> srcBus = m_sourceBus->valueBus();
// if there's no bus, pitchrate is defaulted.
if (!srcBus)
return 1.f;
double sampleRateFactor = sampleRateFactor = srcBus->sampleRate() / r.context()->sampleRate();
/// @fixme these values should be per sample, not per quantum
/// -or- they should be settings if they don't vary per sample
double basePitchRate = playbackRate()->value();
/// @fixme these values should be per sample, not per quantum
double totalRate = m_dopplerRate->value() * sampleRateFactor * basePitchRate;
totalRate *= pow(2, detune()->value() / 1200);
// Sanity check the total rate. It's very important that the resampler not get any bad rate values.
totalRate = std::max(0.0, totalRate);
// Revisit these lower bounds if ever this node can compute ultra low resampling rates.
if (totalRate < 1.e-2f)
totalRate = 1; // a value of zero is considered an illegal value so the default is imposed
totalRate = std::min(100.0, totalRate);
bool isTotalRateValid = !std::isnan(totalRate) && !std::isinf(totalRate);
if (!isTotalRateValid)
totalRate = 1.0;
return (float) totalRate;
}
} // namespace lab