forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealtimeEffectManager.cpp
More file actions
623 lines (523 loc) · 16.7 KB
/
Copy pathRealtimeEffectManager.cpp
File metadata and controls
623 lines (523 loc) · 16.7 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
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
612
613
614
615
616
617
618
619
620
621
622
623
/**********************************************************************
Audacity: A Digital Audio Editor
RealtimeEffectManager.cpp
Paul Licameli split from EffectManager.cpp
**********************************************************************/
#include "../Audacity.h"
#include "RealtimeEffectManager.h"
#include "audacity/EffectInterface.h"
#include "MemoryX.h"
#include <atomic>
#include <wx/time.h>
class RealtimeEffectState
{
public:
explicit RealtimeEffectState( EffectClientInterface &effect );
EffectClientInterface &GetEffect() const { return mEffect; }
bool RealtimeSuspend();
bool RealtimeResume();
bool RealtimeAddProcessor(int group, unsigned chans, float rate);
size_t RealtimeProcess(int group,
unsigned chans, float **inbuf, float **outbuf, size_t numSamples);
bool IsRealtimeActive();
private:
EffectClientInterface &mEffect;
std::vector<int> mGroupProcessor;
int mCurrentProcessor;
std::atomic<int> mRealtimeSuspendCount{ 1 }; // Effects are initially suspended
};
RealtimeEffectManager & RealtimeEffectManager::Get()
{
static RealtimeEffectManager rem;
return rem;
}
RealtimeEffectManager::RealtimeEffectManager()
{
mRealtimeLock.Enter();
mRealtimeActive = false;
mRealtimeSuspended = true;
mRealtimeLatency = 0;
mRealtimeLock.Leave();
}
RealtimeEffectManager::~RealtimeEffectManager()
{
}
#if defined(EXPERIMENTAL_EFFECTS_RACK)
void RealtimeEffectManager::RealtimeSetEffects(const EffectArray & effects)
{
// Block RealtimeProcess()
RealtimeSuspend();
decltype( mStates ) newStates;
auto begin = mStates.begin(), end = mStates.end();
for ( auto pEffect : effects ) {
auto found = std::find_if( begin, end,
[=]( const decltype( mStates )::value_type &state ){
return state && &state->GetEffect() == pEffect;
}
);
if ( found == end ) {
// Tell New effect to get ready
pEffect->RealtimeInitialize();
newStates.emplace_back(
std::make_unique< RealtimeEffectState >( *pEffect ) );
}
else {
// Preserve state for effect that remains in the chain
newStates.emplace_back( std::move( *found ) );
}
}
// Remaining states that were not moved need to clean up
for ( auto &state : mStates ) {
if ( state )
state->GetEffect().RealtimeFinalize();
}
// Get rid of the old chain
// And install the NEW one
mStates.swap( newStates );
// Allow RealtimeProcess() to, well, process
RealtimeResume();
}
#endif
bool RealtimeEffectManager::RealtimeIsActive()
{
return mStates.size() != 0;
}
bool RealtimeEffectManager::RealtimeIsSuspended()
{
return mRealtimeSuspended;
}
void RealtimeEffectManager::RealtimeAddEffect(EffectClientInterface *effect)
{
// Block RealtimeProcess()
RealtimeSuspend();
// Add to list of active effects
mStates.emplace_back( std::make_unique< RealtimeEffectState >( *effect ) );
auto &state = mStates.back();
// Initialize effect if realtime is already active
if (mRealtimeActive)
{
// Initialize realtime processing
effect->RealtimeInitialize();
// Add the required processors
for (size_t i = 0, cnt = mRealtimeChans.size(); i < cnt; i++)
{
state->RealtimeAddProcessor(i, mRealtimeChans[i], mRealtimeRates[i]);
}
}
// Allow RealtimeProcess() to, well, process
RealtimeResume();
}
void RealtimeEffectManager::RealtimeRemoveEffect(EffectClientInterface *effect)
{
// Block RealtimeProcess()
RealtimeSuspend();
if (mRealtimeActive)
{
// Cleanup realtime processing
effect->RealtimeFinalize();
}
// Remove from list of active effects
auto end = mStates.end();
auto found = std::find_if( mStates.begin(), end,
[&](const decltype(mStates)::value_type &state){
return &state->GetEffect() == effect;
}
);
if (found != end)
mStates.erase(found);
// Allow RealtimeProcess() to, well, process
RealtimeResume();
}
void RealtimeEffectManager::RealtimeInitialize(double rate)
{
// The audio thread should not be running yet, but protect anyway
RealtimeSuspend();
// (Re)Set processor parameters
mRealtimeChans.clear();
mRealtimeRates.clear();
// RealtimeAdd/RemoveEffect() needs to know when we're active so it can
// initialize newly added effects
mRealtimeActive = true;
// Tell each effect to get ready for action
for (auto &state : mStates) {
state->GetEffect().SetSampleRate(rate);
state->GetEffect().RealtimeInitialize();
}
// Get things moving
RealtimeResume();
}
void RealtimeEffectManager::RealtimeAddProcessor(int group, unsigned chans, float rate)
{
for (auto &state : mStates)
state->RealtimeAddProcessor(group, chans, rate);
mRealtimeChans.push_back(chans);
mRealtimeRates.push_back(rate);
}
void RealtimeEffectManager::RealtimeFinalize()
{
// Make sure nothing is going on
RealtimeSuspend();
// It is now safe to clean up
mRealtimeLatency = 0;
// Tell each effect to clean up as well
for (auto &state : mStates)
state->GetEffect().RealtimeFinalize();
// Reset processor parameters
mRealtimeChans.clear();
mRealtimeRates.clear();
// No longer active
mRealtimeActive = false;
}
void RealtimeEffectManager::RealtimeSuspend()
{
mRealtimeLock.Enter();
// Already suspended...bail
if (mRealtimeSuspended)
{
mRealtimeLock.Leave();
return;
}
// Show that we aren't going to be doing anything
mRealtimeSuspended = true;
// And make sure the effects don't either
for (auto &state : mStates)
state->RealtimeSuspend();
mRealtimeLock.Leave();
}
void RealtimeEffectManager::RealtimeSuspendOne( EffectClientInterface &effect )
{
auto begin = mStates.begin(), end = mStates.end();
auto found = std::find_if( begin, end,
[&effect]( const decltype( mStates )::value_type &state ){
return state && &state->GetEffect() == &effect;
}
);
if ( found != end )
(*found)->RealtimeSuspend();
}
void RealtimeEffectManager::RealtimeResume()
{
mRealtimeLock.Enter();
// Already running...bail
if (!mRealtimeSuspended)
{
mRealtimeLock.Leave();
return;
}
// Tell the effects to get ready for more action
for (auto &state : mStates)
state->RealtimeResume();
// And we should too
mRealtimeSuspended = false;
mRealtimeLock.Leave();
}
void RealtimeEffectManager::RealtimeResumeOne( EffectClientInterface &effect )
{
auto begin = mStates.begin(), end = mStates.end();
auto found = std::find_if( begin, end,
[&effect]( const decltype( mStates )::value_type &state ){
return state && &state->GetEffect() == &effect;
}
);
if ( found != end )
(*found)->RealtimeResume();
}
//
// This will be called in a different thread than the main GUI thread.
//
void RealtimeEffectManager::RealtimeProcessStart()
{
// Protect ourselves from the main thread
mRealtimeLock.Enter();
// Can be suspended because of the audio stream being paused or because effects
// have been suspended.
if (!mRealtimeSuspended)
{
for (auto &state : mStates)
{
if (state->IsRealtimeActive())
state->GetEffect().RealtimeProcessStart();
}
}
mRealtimeLock.Leave();
}
//
// This will be called in a different thread than the main GUI thread.
//
size_t RealtimeEffectManager::RealtimeProcess(int group, unsigned chans, float **buffers, size_t numSamples)
{
// Protect ourselves from the main thread
mRealtimeLock.Enter();
// Can be suspended because of the audio stream being paused or because effects
// have been suspended, so allow the samples to pass as-is.
if (mRealtimeSuspended || mStates.empty())
{
mRealtimeLock.Leave();
return numSamples;
}
// Remember when we started so we can calculate the amount of latency we
// are introducing
wxMilliClock_t start = wxGetUTCTimeMillis();
// Allocate the in/out buffer arrays
float **ibuf = (float **) alloca(chans * sizeof(float *));
float **obuf = (float **) alloca(chans * sizeof(float *));
// And populate the input with the buffers we've been given while allocating
// NEW output buffers
for (unsigned int i = 0; i < chans; i++)
{
ibuf[i] = buffers[i];
obuf[i] = (float *) alloca(numSamples * sizeof(float));
}
// Now call each effect in the chain while swapping buffer pointers to feed the
// output of one effect as the input to the next effect
size_t called = 0;
for (auto &state : mStates)
{
if (state->IsRealtimeActive())
{
state->RealtimeProcess(group, chans, ibuf, obuf, numSamples);
called++;
}
for (unsigned int j = 0; j < chans; j++)
{
float *temp;
temp = ibuf[j];
ibuf[j] = obuf[j];
obuf[j] = temp;
}
}
// Once we're done, we might wind up with the last effect storing its results
// in the temporary buffers. If that's the case, we need to copy it over to
// the caller's buffers. This happens when the number of effects processed
// is odd.
if (called & 1)
{
for (unsigned int i = 0; i < chans; i++)
{
memcpy(buffers[i], ibuf[i], numSamples * sizeof(float));
}
}
// Remember the latency
mRealtimeLatency = (int) (wxGetUTCTimeMillis() - start).GetValue();
mRealtimeLock.Leave();
//
// This is wrong...needs to handle tails
//
return numSamples;
}
//
// This will be called in a different thread than the main GUI thread.
//
void RealtimeEffectManager::RealtimeProcessEnd()
{
// Protect ourselves from the main thread
mRealtimeLock.Enter();
// Can be suspended because of the audio stream being paused or because effects
// have been suspended.
if (!mRealtimeSuspended)
{
for (auto &state : mStates)
{
if (state->IsRealtimeActive())
state->GetEffect().RealtimeProcessEnd();
}
}
mRealtimeLock.Leave();
}
int RealtimeEffectManager::GetRealtimeLatency()
{
return mRealtimeLatency;
}
RealtimeEffectState::RealtimeEffectState( EffectClientInterface &effect )
: mEffect{ effect }
{
}
bool RealtimeEffectState::RealtimeSuspend()
{
auto result = mEffect.RealtimeSuspend();
if ( result ) {
mRealtimeSuspendCount++;
}
return result;
}
bool RealtimeEffectState::RealtimeResume()
{
auto result = mEffect.RealtimeResume();
if ( result ) {
mRealtimeSuspendCount--;
}
return result;
}
// RealtimeAddProcessor and RealtimeProcess use the same method of
// determining the current processor index, so updates to one should
// be reflected in the other.
bool RealtimeEffectState::RealtimeAddProcessor(int group, unsigned chans, float rate)
{
auto ichans = chans;
auto ochans = chans;
auto gchans = chans;
// Reset processor index
if (group == 0)
{
mCurrentProcessor = 0;
mGroupProcessor.clear();
}
// Remember the processor starting index
mGroupProcessor.push_back(mCurrentProcessor);
const auto numAudioIn = mEffect.GetAudioInCount();
const auto numAudioOut = mEffect.GetAudioOutCount();
// Call the client until we run out of input or output channels
while (ichans > 0 && ochans > 0)
{
// If we don't have enough input channels to accommodate the client's
// requirements, then we replicate the input channels until the
// client's needs are met.
if (ichans < numAudioIn)
{
// All input channels have been consumed
ichans = 0;
}
// Otherwise fulfill the client's needs with as many input channels as possible.
// After calling the client with this set, we will loop back up to process more
// of the input/output channels.
else if (ichans >= numAudioIn)
{
gchans = numAudioIn;
ichans -= gchans;
}
// If we don't have enough output channels to accommodate the client's
// requirements, then we provide all of the output channels and fulfill
// the client's needs with dummy buffers. These will just get tossed.
if (ochans < numAudioOut)
{
// All output channels have been consumed
ochans = 0;
}
// Otherwise fulfill the client's needs with as many output channels as possible.
// After calling the client with this set, we will loop back up to process more
// of the input/output channels.
else if (ochans >= numAudioOut)
{
ochans -= numAudioOut;
}
// Add a NEW processor
mEffect.RealtimeAddProcessor(gchans, rate);
// Bump to next processor
mCurrentProcessor++;
}
return true;
}
// RealtimeAddProcessor and RealtimeProcess use the same method of
// determining the current processor group, so updates to one should
// be reflected in the other.
size_t RealtimeEffectState::RealtimeProcess(int group,
unsigned chans,
float **inbuf,
float **outbuf,
size_t numSamples)
{
//
// The caller passes the number of channels to process and specifies
// the number of input and output buffers. There will always be the
// same number of output buffers as there are input buffers.
//
// Effects always require a certain number of input and output buffers,
// so if the number of channels we're currently processing are different
// than what the effect expects, then we use a few methods of satisfying
// the effects requirements.
const auto numAudioIn = mEffect.GetAudioInCount();
const auto numAudioOut = mEffect.GetAudioOutCount();
float **clientIn = (float **) alloca(numAudioIn * sizeof(float *));
float **clientOut = (float **) alloca(numAudioOut * sizeof(float *));
float *dummybuf = (float *) alloca(numSamples * sizeof(float));
decltype(numSamples) len = 0;
auto ichans = chans;
auto ochans = chans;
auto gchans = chans;
unsigned indx = 0;
unsigned ondx = 0;
int processor = mGroupProcessor[group];
// Call the client until we run out of input or output channels
while (ichans > 0 && ochans > 0)
{
// If we don't have enough input channels to accommodate the client's
// requirements, then we replicate the input channels until the
// client's needs are met.
if (ichans < numAudioIn)
{
for (size_t i = 0; i < numAudioIn; i++)
{
if (indx == ichans)
{
indx = 0;
}
clientIn[i] = inbuf[indx++];
}
// All input channels have been consumed
ichans = 0;
}
// Otherwise fulfill the client's needs with as many input channels as possible.
// After calling the client with this set, we will loop back up to process more
// of the input/output channels.
else if (ichans >= numAudioIn)
{
gchans = 0;
for (size_t i = 0; i < numAudioIn; i++, ichans--, gchans++)
{
clientIn[i] = inbuf[indx++];
}
}
// If we don't have enough output channels to accommodate the client's
// requirements, then we provide all of the output channels and fulfill
// the client's needs with dummy buffers. These will just get tossed.
if (ochans < numAudioOut)
{
for (size_t i = 0; i < numAudioOut; i++)
{
if (i < ochans)
{
clientOut[i] = outbuf[i];
}
else
{
clientOut[i] = dummybuf;
}
}
// All output channels have been consumed
ochans = 0;
}
// Otherwise fulfill the client's needs with as many output channels as possible.
// After calling the client with this set, we will loop back up to process more
// of the input/output channels.
else if (ochans >= numAudioOut)
{
for (size_t i = 0; i < numAudioOut; i++, ochans--)
{
clientOut[i] = outbuf[ondx++];
}
}
// Finally call the plugin to process the block
len = 0;
const auto blockSize = mEffect.GetBlockSize();
for (decltype(numSamples) block = 0; block < numSamples; block += blockSize)
{
auto cnt = std::min(numSamples - block, blockSize);
len += mEffect.RealtimeProcess(processor, clientIn, clientOut, cnt);
for (size_t i = 0 ; i < numAudioIn; i++)
{
clientIn[i] += cnt;
}
for (size_t i = 0 ; i < numAudioOut; i++)
{
clientOut[i] += cnt;
}
}
// Bump to next processor
processor++;
}
return len;
}
bool RealtimeEffectState::IsRealtimeActive()
{
return mRealtimeSuspendCount == 0;
}