-
Notifications
You must be signed in to change notification settings - Fork 61
/
CDSPBlockConvolver.h
623 lines (531 loc) · 15.3 KB
/
CDSPBlockConvolver.h
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
//$ nobt
//$ nocpp
/**
* @file CDSPBlockConvolver.h
*
* @brief Single-block overlap-save convolution processor class.
*
* This file includes single-block overlap-save convolution processor class.
*
* r8brain-free-src Copyright (c) 2013-2022 Aleksey Vaneev
* See the "LICENSE" file for license.
*/
#ifndef R8B_CDSPBLOCKCONVOLVER_INCLUDED
#define R8B_CDSPBLOCKCONVOLVER_INCLUDED
#include "CDSPFIRFilter.h"
#include "CDSPProcessor.h"
namespace r8b {
/**
* @brief Single-block overlap-save convolution processing class.
*
* Class that implements single-block overlap-save convolution processing. The
* length of a single FFT block used depends on the length of the filter
* kernel.
*
* The rationale behind "single-block" processing is that increasing the FFT
* block length by 2 is more efficient than performing convolution at the same
* FFT block length but using two blocks.
*
* This class also implements a built-in resampling by any whole-number
* factor, which simplifies the overall resampling objects topology.
*/
class CDSPBlockConvolver : public CDSPProcessor
{
public:
/**
* Constructor initializes internal variables and constants of *this
* object.
*
* @param aFilter Pre-calculated filter data. Reference to this object is
* inhertied by *this object, and the object will be released when *this
* object is destroyed. If upsampling is used, filter's gain should be
* equal to the upsampling factor.
* @param aUpFactor The upsampling factor, positive value. E.g. value of 2
* means 2x upsampling should be performed over the input data.
* @param aDownFactor The downsampling factor, positive value. E.g. value
* of 2 means 2x downsampling should be performed over the output data.
* @param PrevLatency Latency, in samples (any value >=0), which was left
* in the output signal by a previous process. This value is usually
* non-zero if the minimum-phase filters are in use. This value is always
* zero if the linear-phase filters are in use.
* @param aDoConsumeLatency "True" if the output latency should be
* consumed. Does not apply to the fractional part of the latency (if such
* part is available).
*/
CDSPBlockConvolver( CDSPFIRFilter& aFilter, const int aUpFactor,
const int aDownFactor, const double PrevLatency = 0.0,
const bool aDoConsumeLatency = true )
: Filter( &aFilter )
, UpFactor( aUpFactor )
, DownFactor( aDownFactor )
, BlockLen2( 2 << Filter -> getBlockLenBits() )
, DoConsumeLatency( aDoConsumeLatency )
{
R8BASSERT( UpFactor > 0 );
R8BASSERT( DownFactor > 0 );
R8BASSERT( PrevLatency >= 0.0 );
int fftinBits;
UpShift = getBitOccupancy( UpFactor ) - 1;
if(( 1 << UpShift ) == UpFactor )
{
fftinBits = Filter -> getBlockLenBits() + 1 - UpShift;
PrevInputLen = ( Filter -> getKernelLen() - 1 + UpFactor - 1 ) /
UpFactor;
InputLen = BlockLen2 - PrevInputLen * UpFactor;
}
else
{
UpShift = -1;
fftinBits = Filter -> getBlockLenBits() + 1;
PrevInputLen = Filter -> getKernelLen() - 1;
InputLen = BlockLen2 - PrevInputLen;
}
OutOffset = ( Filter -> isZeroPhase() ? Filter -> getLatency() : 0 );
LatencyFrac = Filter -> getLatencyFrac() + PrevLatency * UpFactor;
Latency = (int) LatencyFrac;
const int InLatency = Latency + Filter -> getLatency() - OutOffset;
LatencyFrac -= Latency;
LatencyFrac /= DownFactor;
Latency += InputLen + Filter -> getLatency();
int fftoutBits;
InputDelay = 0;
DownSkipInit = 0;
DownShift = getBitOccupancy( DownFactor ) - 1;
if(( 1 << DownShift ) == DownFactor )
{
fftoutBits = Filter -> getBlockLenBits() + 1 - DownShift;
if( DownFactor > 1 )
{
if( UpShift > 0 )
{
// This case never happens in practice due to mutual
// exclusion of "power of 2" DownFactor and UpFactor
// values.
R8BASSERT( UpShift == 0 );
}
else
{
// Make sure InputLen is divisible by DownFactor.
const int ilc = InputLen & ( DownFactor - 1 );
PrevInputLen += ilc;
InputLen -= ilc;
Latency -= ilc;
// Correct InputDelay for input and filter's latency.
const int lc = InLatency & ( DownFactor - 1 );
if( lc > 0 )
{
InputDelay = DownFactor - lc;
}
if( !DoConsumeLatency )
{
Latency /= DownFactor;
}
}
}
}
else
{
fftoutBits = Filter -> getBlockLenBits() + 1;
DownShift = -1;
if( !DoConsumeLatency && DownFactor > 1 )
{
DownSkipInit = Latency % DownFactor;
Latency /= DownFactor;
}
}
R8BASSERT( Latency >= 0 );
fftin = new CDSPRealFFTKeeper( fftinBits );
if( fftoutBits == fftinBits )
{
fftout = fftin;
}
else
{
ffto2 = new CDSPRealFFTKeeper( fftoutBits );
fftout = ffto2;
}
WorkBlocks.alloc( BlockLen2 * 2 + PrevInputLen );
CurInput = &WorkBlocks[ 0 ];
CurOutput = &WorkBlocks[ BlockLen2 ]; // CurInput and
// CurOutput are address-aligned.
PrevInput = &WorkBlocks[ BlockLen2 * 2 ];
clear();
R8BCONSOLE( "CDSPBlockConvolver: flt_len=%i in_len=%i io=%i/%i "
"fft=%i/%i latency=%i\n", Filter -> getKernelLen(), InputLen,
UpFactor, DownFactor, (*fftin) -> getLen(), (*fftout) -> getLen(),
getLatency() );
}
virtual ~CDSPBlockConvolver()
{
Filter -> unref();
}
virtual int getInLenBeforeOutPos( const int ReqOutPos ) const
{
return( (int) (( Latency + (double) ReqOutPos * DownFactor ) /
UpFactor + LatencyFrac * DownFactor / UpFactor ));
}
virtual int getLatency() const
{
return( DoConsumeLatency ? 0 : Latency );
}
virtual double getLatencyFrac() const
{
return( LatencyFrac );
}
virtual int getMaxOutLen( const int MaxInLen ) const
{
R8BASSERT( MaxInLen >= 0 );
return(( MaxInLen * UpFactor + DownFactor - 1 ) / DownFactor );
}
virtual void clear()
{
memset( &PrevInput[ 0 ], 0, PrevInputLen * sizeof( PrevInput[ 0 ]));
if( DoConsumeLatency )
{
LatencyLeft = Latency;
}
else
{
LatencyLeft = 0;
if( DownShift > 0 )
{
memset( &CurOutput[ 0 ], 0, ( BlockLen2 >> DownShift ) *
sizeof( CurOutput[ 0 ]));
}
else
{
memset( &CurOutput[ BlockLen2 - OutOffset ], 0, OutOffset *
sizeof( CurOutput[ 0 ]));
memset( &CurOutput[ 0 ], 0, ( InputLen - OutOffset ) *
sizeof( CurOutput[ 0 ]));
}
}
memset( CurInput, 0, InputDelay * sizeof( CurInput[ 0 ]));
InDataLeft = InputLen - InputDelay;
UpSkip = 0;
DownSkip = DownSkipInit;
}
virtual int process( double* ip, int l0, double*& op0 )
{
R8BASSERT( l0 >= 0 );
R8BASSERT( UpFactor / DownFactor <= 1 || ip != op0 || l0 == 0 );
double* op = op0;
int l = l0 * UpFactor;
l0 = 0;
while( l > 0 )
{
const int Offs = InputLen - InDataLeft;
if( l < InDataLeft )
{
InDataLeft -= l;
if( UpShift >= 0 )
{
memcpy( &CurInput[ Offs >> UpShift ], ip,
( l >> UpShift ) * sizeof( CurInput[ 0 ]));
}
else
{
copyUpsample( ip, &CurInput[ Offs ], l );
}
copyToOutput( Offs - OutOffset, op, l, l0 );
break;
}
const int b = InDataLeft;
l -= b;
InDataLeft = InputLen;
int ilu;
if( UpShift >= 0 )
{
const int bu = b >> UpShift;
memcpy( &CurInput[ Offs >> UpShift ], ip,
bu * sizeof( CurInput[ 0 ]));
ip += bu;
ilu = InputLen >> UpShift;
}
else
{
copyUpsample( ip, &CurInput[ Offs ], b );
ilu = InputLen;
}
const size_t pil = PrevInputLen * sizeof( CurInput[ 0 ]);
memcpy( &CurInput[ ilu ], PrevInput, pil );
memcpy( PrevInput, &CurInput[ ilu - PrevInputLen ], pil );
(*fftin) -> forward( CurInput );
if( UpShift > 0 )
{
#if R8B_FLOATFFT
mirrorInputSpectrum( (float*) CurInput );
#else // R8B_FLOATFFT
mirrorInputSpectrum( CurInput );
#endif // R8B_FLOATFFT
}
if( Filter -> isZeroPhase() )
{
(*fftout) -> multiplyBlocksZP( Filter -> getKernelBlock(),
CurInput );
}
else
{
(*fftout) -> multiplyBlocks( Filter -> getKernelBlock(),
CurInput );
}
if( DownShift > 0 )
{
const int z = BlockLen2 >> DownShift;
#if R8B_FLOATFFT
float* const kb = (float*) Filter -> getKernelBlock();
float* const p = (float*) CurInput;
#else // R8B_FLOATFFT
const double* const kb = Filter -> getKernelBlock();
double* const p = CurInput;
#endif // R8B_FLOATFFT
p[ 1 ] = kb[ z ] * p[ z ] - kb[ z + 1 ] * p[ z + 1 ];
}
(*fftout) -> inverse( CurInput );
copyToOutput( Offs - OutOffset, op, b, l0 );
double* const tmp = CurInput;
CurInput = CurOutput;
CurOutput = tmp;
}
return( l0 );
}
private:
CDSPFIRFilter* Filter; ///< Filter in use.
CPtrKeeper< CDSPRealFFTKeeper* > fftin; ///< FFT object 1, used to produce
///< the input spectrum (can embed the "power of 2" upsampling).
CPtrKeeper< CDSPRealFFTKeeper* > ffto2; ///< FFT object 2 (can be NULL).
CDSPRealFFTKeeper* fftout; ///< FFT object used to produce the output
///< signal (can embed the "power of 2" downsampling), may point to
///< either "fftin" or "ffto2".
int UpFactor; ///< Upsampling factor.
int DownFactor; ///< Downsampling factor.
int BlockLen2; ///< Equals block length * 2.
int OutOffset; ///< Output offset, depends on filter's introduced latency.
int PrevInputLen; ///< The length of previous input data saved, used for
///< overlap.
int InputLen; ///< The number of input samples that should be accumulated
///< before the input block is processed.
double LatencyFrac; ///< Fractional latency, in samples, that is left in
///< the output signal.
int Latency; ///< Processing latency, in samples.
int UpShift; ///< "Power of 2" upsampling shift. Equals -1 if UpFactor is
///< not a "power of 2" value. Equals 0 if UpFactor equals 1.
int DownShift; ///< "Power of 2" downsampling shift. Equals -1 if
///< DownFactor is not a "power of 2". Equals 0 if DownFactor equals
///< 1.
int InputDelay; ///< Additional input delay, in samples. Used to make the
///< output delay divisible by DownShift. Used only if UpShift <= 0
///< and DownShift > 0.
double* PrevInput; ///< Previous input data buffer, capacity = BlockLen.
double* CurInput; ///< Input data buffer, capacity = BlockLen2.
double* CurOutput; ///< Output data buffer, capacity = BlockLen2.
int InDataLeft; ///< Samples left before processing input and output FFT
///< blocks. Initialized to InputLen on clear.
int LatencyLeft; ///< Latency in samples left to skip.
int UpSkip; ///< The current upsampling sample skip (value in the range
///< 0 to UpFactor - 1).
int DownSkip; ///< The current downsampling sample skip (value in the
///< range 0 to DownFactor - 1). Not used if DownShift > 0.
int DownSkipInit; ///< The initial DownSkip value after clear().
CFixedBuffer< double > WorkBlocks; ///< Previous input data, input and
///< output data blocks, overall capacity = BlockLen2 * 2 +
///< PrevInputLen. Used in the flip-flop manner.
bool DoConsumeLatency; ///< "True" if the output latency should be
///< consumed. Does not apply to the fractional part of the latency
///< (if such part is available).
/**
* Function copies samples from the input buffer to the output buffer
* while inserting zeros inbetween them to perform the whole-numbered
* upsampling.
*
* @param[in,out] ip0 Input buffer. Will be advanced on function's return.
* @param[out] op Output buffer.
* @param l0 The number of samples to fill in the output buffer, including
* both input samples and interpolation (zero) samples.
*/
void copyUpsample( double*& ip0, double* op, int l0 )
{
int b = min( UpSkip, l0 );
if( b != 0 )
{
UpSkip -= b;
l0 -= b;
*op = 0.0;
op++;
while( --b != 0 )
{
*op = 0.0;
op++;
}
}
double* ip = ip0;
const int upf = UpFactor;
int l = l0 / upf;
int lz = l0 - l * upf;
if( upf == 3 )
{
while( l != 0 )
{
op[ 0 ] = *ip;
op[ 1 ] = 0.0;
op[ 2 ] = 0.0;
ip++;
op += upf;
l--;
}
}
else
if( upf == 5 )
{
while( l != 0 )
{
op[ 0 ] = *ip;
op[ 1 ] = 0.0;
op[ 2 ] = 0.0;
op[ 3 ] = 0.0;
op[ 4 ] = 0.0;
ip++;
op += upf;
l--;
}
}
else
{
const size_t zc = ( upf - 1 ) * sizeof( op[ 0 ]);
while( l != 0 )
{
*op = *ip;
ip++;
memset( op + 1, 0, zc );
op += upf;
l--;
}
}
if( lz != 0 )
{
*op = *ip;
ip++;
op++;
UpSkip = upf - lz;
while( --lz != 0 )
{
*op = 0.0;
op++;
}
}
ip0 = ip;
}
/**
* Function copies sample data from the CurOutput buffer to the specified
* output buffer and advances its position. If necessary, this function
* "consumes" latency and performs downsampling.
*
* @param Offs CurOutput buffer offset, can be negative.
* @param[out] op0 Output buffer pointer, will be advanced.
* @param b The number of output samples available, including those which
* are discarded during whole-number downsampling.
* @param l0 The overall output sample count, will be increased.
*/
void copyToOutput( int Offs, double*& op0, int b, int& l0 )
{
if( Offs < 0 )
{
if( Offs + b <= 0 )
{
Offs += BlockLen2;
}
else
{
copyToOutput( Offs + BlockLen2, op0, -Offs, l0 );
b += Offs;
Offs = 0;
}
}
if( LatencyLeft != 0 )
{
if( LatencyLeft >= b )
{
LatencyLeft -= b;
return;
}
Offs += LatencyLeft;
b -= LatencyLeft;
LatencyLeft = 0;
}
const int df = DownFactor;
if( DownShift > 0 )
{
int Skip = Offs & ( df - 1 );
if( Skip > 0 )
{
Skip = df - Skip;
b -= Skip;
Offs += Skip;
}
if( b > 0 )
{
b = ( b + df - 1 ) >> DownShift;
memcpy( op0, &CurOutput[ Offs >> DownShift ],
b * sizeof( op0[ 0 ]));
op0 += b;
l0 += b;
}
}
else
{
if( df > 1 )
{
const double* ip = &CurOutput[ Offs + DownSkip ];
int l = ( b + df - 1 - DownSkip ) / df;
DownSkip += l * df - b;
double* op = op0;
l0 += l;
op0 += l;
while( l > 0 )
{
*op = *ip;
ip += df;
op++;
l--;
}
}
else
{
memcpy( op0, &CurOutput[ Offs ], b * sizeof( op0[ 0 ]));
op0 += b;
l0 += b;
}
}
}
/**
* Function performs input spectrum mirroring which is used to perform a
* fast "power of 2" upsampling. Such mirroring is equivalent to insertion
* of zeros into the input signal.
*
* @param p Spectrum data block to mirror.
* @tparam T Buffer's element type.
*/
template< typename T >
void mirrorInputSpectrum( T* const p )
{
const int bl1 = BlockLen2 >> UpShift;
const int bl2 = bl1 + bl1;
int i;
for( i = bl1 + 2; i < bl2; i += 2 )
{
p[ i ] = p[ bl2 - i ];
p[ i + 1 ] = -p[ bl2 - i + 1 ];
}
p[ bl1 ] = p[ 1 ];
p[ bl1 + 1 ] = (T) 0;
p[ 1 ] = p[ 0 ];
for( i = 1; i < UpShift; i++ )
{
const int z = bl1 << i;
memcpy( &p[ z ], p, z * sizeof( p[ 0 ]));
p[ z + 1 ] = (T) 0;
}
}
};
} // namespace r8b
#endif // R8B_CDSPBLOCKCONVOLVER_INCLUDED