forked from avaneev/r8brain-free-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDSPHBDownsampler.h
393 lines (339 loc) · 11 KB
/
CDSPHBDownsampler.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
//$ nobt
//$ nocpp
/**
* @file CDSPHBDownsampler.h
*
* @brief Half-band downsampling convolver class.
*
* This file includes half-band downsampling convolver class.
*
* r8brain-free-src Copyright (c) 2019 Aleksey Vaneev
* See the "License.txt" file for license.
*/
#ifndef R8B_CDSPHBDOWNSAMPLER_INCLUDED
#define R8B_CDSPHBDOWNSAMPLER_INCLUDED
#include "CDSPHBUpsampler.h"
namespace r8b {
/**
* @brief Half-band downsampler class.
*
* Class implements brute-force half-band 2X downsampling that uses small
* sparse symmetric FIR filters. The output has 2.0 gain.
*/
class CDSPHBDownsampler : public CDSPProcessor
{
public:
/**
* Constructor initalizes the half-band downsampler.
*
* @param ReqAtten Required half-band filter attentuation.
* @param SteepIndex Steepness index - 0=steepest. Corresponds to general
* downsampling ratio, e.g. at 4x downsampling 0 is used, at 8x
* downsampling 1 is used, etc.
* @param IsThird "True" if 1/3 resampling is performed.
* @param PrevLatency Latency, in samples (any value >=0), which was left
* in the output signal by a previous process. Whole-number latency will
* be consumed by *this object while remaining fractional latency can be
* obtained via the getLatencyFrac() function.
*/
CDSPHBDownsampler( const double ReqAtten, const int SteepIndex,
const bool IsThird, const double PrevLatency )
{
static const CConvolveFn FltConvFn[ 14 ] = {
&CDSPHBDownsampler :: convolve1, &CDSPHBDownsampler :: convolve2,
&CDSPHBDownsampler :: convolve3, &CDSPHBDownsampler :: convolve4,
&CDSPHBDownsampler :: convolve5, &CDSPHBDownsampler :: convolve6,
&CDSPHBDownsampler :: convolve7, &CDSPHBDownsampler :: convolve8,
&CDSPHBDownsampler :: convolve9, &CDSPHBDownsampler :: convolve10,
&CDSPHBDownsampler :: convolve11, &CDSPHBDownsampler :: convolve12,
&CDSPHBDownsampler :: convolve13,
&CDSPHBDownsampler :: convolve14 };
int fltt;
double att;
if( IsThird )
{
CDSPHBUpsampler :: getHBFilterThird( ReqAtten, SteepIndex, fltp,
fltt, att );
}
else
{
CDSPHBUpsampler :: getHBFilter( ReqAtten, SteepIndex, fltp, fltt,
att );
}
convfn = FltConvFn[ fltt - 1 ];
fll = fltt * 2 - 1;
fl2 = fll;
flo = fll + fl2;
LatencyFrac = PrevLatency * 0.5;
Latency = (int) LatencyFrac;
LatencyFrac -= Latency;
R8BCONSOLE( "CDSPHBDownsampler: taps=%i third=%i att=%.1f io=1/2\n",
fltt, (int) IsThird, att );
clear();
}
virtual int getLatency() const
{
return( 0 );
}
virtual double getLatencyFrac() const
{
return( LatencyFrac );
}
virtual int getMaxOutLen( const int MaxInLen ) const
{
R8BASSERT( MaxInLen >= 0 );
return(( MaxInLen + 1 ) / 2 );
}
virtual void clear()
{
LatencyLeft = Latency;
BufLeft = 0;
WritePos = 0;
ReadPos = BufLen - fll; // Set "read" position to
// account for filter's latency.
memset( &Buf[ ReadPos ], 0, fll * sizeof( double ));
}
virtual int process( double* ip, int l, double*& op0 )
{
R8BASSERT( l >= 0 );
double* op = op0;
while( l > 0 )
{
// Add new input samples to both halves of the ring buffer.
const int b = min( min( l, BufLen - WritePos ),
BufLen - fll - BufLeft );
double* const wp1 = Buf + WritePos;
memcpy( wp1, ip, b * sizeof( double ));
if( WritePos < flo )
{
const int c = min( b, flo - WritePos );
memcpy( wp1 + BufLen, wp1, c * sizeof( double ));
}
ip += b;
WritePos = ( WritePos + b ) & BufLenMask;
l -= b;
BufLeft += b;
if( BufLeft > fl2 )
{
const int c = ( BufLeft - fl2 + 1 ) >> 1;
double* const opend = op + c;
( *convfn )( op, opend, fltp, Buf + fll, ReadPos );
op = opend;
BufLeft -= c + c;
}
}
int ol = (int) ( op - op0 );
if( LatencyLeft > 0 )
{
if( LatencyLeft >= ol )
{
LatencyLeft -= ol;
return( 0 );
}
ol -= LatencyLeft;
op0 += LatencyLeft;
LatencyLeft = 0;
}
return( ol );
}
private:
static const int BufLenBits = 8; ///< The length of the ring buffer,
///< expressed as Nth power of 2. This value can be reduced if it is
///< known that only short input buffers will be passed to the
///< interpolator. The minimum value of this parameter is 5, and
///< 1 << BufLenBits should be at least 3 times larger than the
///< FilterLen.
///<
static const int BufLen = 1 << BufLenBits; ///< The length of the ring
///< buffer. The actual length is twice as long to allow "beyond max
///< position" positioning.
///<
static const int BufLenMask = BufLen - 1; ///< Mask used for quick buffer
///< position wrapping.
///<
double Buf[ BufLen + 54 ]; ///< The ring buffer, including overrun
///< protection for the largest filter.
///<
const double* fltp; ///< Half-band filter taps.
///<
int fll; ///< Input latency.
///<
int fl2; ///< Right-side filter length.
///<
int flo; ///< Overrun length.
///<
int Latency; ///< Initial latency that should be removed from the output.
///<
double LatencyFrac; ///< Fractional latency left on the output.
///<
int BufLeft; ///< The number of samples left in the buffer to process.
///< When this value is below FilterLenD2Plus1, the interpolation
///< cycle ends.
///<
int WritePos; ///< The current buffer write position. Incremented together
///< with the BufLeft variable.
///<
int ReadPos; ///< The current buffer read position.
///<
int LatencyLeft; ///< Latency left to remove.
///<
typedef void( *CConvolveFn )( double* op, double* const opend,
const double* const flt, const double* const rp0, int& ReadPos0 ); ///<
///< Convolution funtion type.
///<
CConvolveFn convfn; ///< Convolution function in use.
///<
#define R8BHBC1( fn ) \
static void fn( double* op, double* const opend, const double* const flt, \
const double* const rp0, int& ReadPos0 ) \
{ \
int rpos = ReadPos0; \
while( op < opend ) \
{ \
const double* const rp = rp0 + rpos; \
*op = rp[ 0 ] +
#define R8BHBC2 \
rpos = ( rpos + 2 ) & BufLenMask; \
op++; \
} \
ReadPos0 = rpos; \
}
R8BHBC1( convolve1 )
flt[ 0 ] * ( rp[ 1 ] + rp[ -1 ]);
R8BHBC2
R8BHBC1( convolve2 )
flt[ 0 ] * ( rp[ 1 ] + rp[ -1 ]) +
flt[ 1 ] * ( rp[ 3 ] + rp[ -3 ]);
R8BHBC2
R8BHBC1( convolve3 )
flt[ 0 ] * ( rp[ 1 ] + rp[ -1 ]) +
flt[ 1 ] * ( rp[ 3 ] + rp[ -3 ]) +
flt[ 2 ] * ( rp[ 5 ] + rp[ -5 ]);
R8BHBC2
R8BHBC1( convolve4 )
flt[ 0 ] * ( rp[ 1 ] + rp[ -1 ]) +
flt[ 1 ] * ( rp[ 3 ] + rp[ -3 ]) +
flt[ 2 ] * ( rp[ 5 ] + rp[ -5 ]) +
flt[ 3 ] * ( rp[ 7 ] + rp[ -7 ]);
R8BHBC2
R8BHBC1( convolve5 )
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 ] + rp[ -7 ]) +
flt[ 4 ] * ( rp[ 9 ] + rp[ -9 ]) +
flt[ 5 ] * ( rp[ 11 ] + rp[ -11 ]) +
flt[ 6 ] * ( rp[ 13 ] + rp[ -13 ]) +
flt[ 7 ] * ( rp[ 15 ] + rp[ -15 ]) +
flt[ 8 ] * ( rp[ 17 ] + rp[ -17 ]) +
flt[ 9 ] * ( rp[ 19 ] + rp[ -19 ]) +
flt[ 10 ] * ( rp[ 21 ] + rp[ -21 ]) +
flt[ 11 ] * ( rp[ 23 ] + rp[ -23 ]) +
flt[ 12 ] * ( rp[ 25 ] + rp[ -25 ]) +
flt[ 13 ] * ( rp[ 27 ] + rp[ -27 ]);
R8BHBC2
#undef R8BHBC1
#undef R8BHBC2
};
// ---------------------------------------------------------------------------
} // namespace r8b
#endif // R8B_CDSPHBDOWNSAMPLER_INCLUDED