r8brain-free-src
High-quality pro audio sample rate converter library
 
Loading...
Searching...
No Matches
CDSPHBDownsampler.h
Go to the documentation of this file.
1//$ nobt
2//$ nocpp
3
15
16#ifndef R8B_CDSPHBDOWNSAMPLER_INCLUDED
17#define R8B_CDSPHBDOWNSAMPLER_INCLUDED
18
19#include "CDSPHBUpsampler.h"
20
21namespace r8b {
22
29
30class CDSPHBDownsampler : public CDSPProcessor
31{
32public:
46
47 CDSPHBDownsampler( const double ReqAtten, const int SteepIndex,
48 const bool IsThird, const double PrevLatency )
49 {
50 static const CConvolveFn FltConvFn[ 14 ] = {
51 &CDSPHBDownsampler :: convolve1, &CDSPHBDownsampler :: convolve2,
52 &CDSPHBDownsampler :: convolve3, &CDSPHBDownsampler :: convolve4,
53 &CDSPHBDownsampler :: convolve5, &CDSPHBDownsampler :: convolve6,
54 &CDSPHBDownsampler :: convolve7, &CDSPHBDownsampler :: convolve8,
55 &CDSPHBDownsampler :: convolve9, &CDSPHBDownsampler :: convolve10,
56 &CDSPHBDownsampler :: convolve11, &CDSPHBDownsampler :: convolve12,
57 &CDSPHBDownsampler :: convolve13,
58 &CDSPHBDownsampler :: convolve14 };
59
60 const double* fltp0;
61 int fltt;
62 double att;
63
64 if( IsThird )
65 {
66 CDSPHBUpsampler :: getHBFilterThird( ReqAtten, SteepIndex, fltp0,
67 fltt, att );
68 }
69 else
70 {
71 CDSPHBUpsampler :: getHBFilter( ReqAtten, SteepIndex, fltp0, fltt,
72 att );
73 }
74
75 // Copy obtained filter to address-aligned buffer.
76
77 fltp = alignptr( FltBuf, 16 );
78 memcpy( fltp, fltp0, (size_t) fltt * sizeof( fltp[ 0 ]));
79
80 convfn = FltConvFn[ fltt - 1 ];
81 fll = fltt;
82 fl2 = fltt - 1;
83 flo = fll + fl2;
84 flb = BufLen - fll;
85 BufRP = Buf + fll;
86
87 LatencyFrac = PrevLatency * 0.5;
88 Latency = (int) LatencyFrac;
89 LatencyFrac -= Latency;
90
91 R8BASSERT( Latency >= 0 );
92
93 R8BCONSOLE( "CDSPHBDownsampler: taps=%i third=%i att=%.1f io=1/2\n",
94 fltt, (int) IsThird, att );
95
96 clear();
97 }
98
99 virtual int getInLenBeforeOutPos( const int ReqOutPos ) const
100 {
101 return( flo + (int) (( Latency + LatencyFrac + ReqOutPos ) * 2.0 ));
102 }
103
104 virtual int getLatency() const
105 {
106 return( 0 );
107 }
108
109 virtual double getLatencyFrac() const
110 {
111 return( LatencyFrac );
112 }
113
114 virtual int getMaxOutLen( const int MaxInLen ) const
115 {
116 R8BASSERT( MaxInLen >= 0 );
117
118 return(( MaxInLen + 1 ) >> 1 );
119 }
120
121 virtual void clear()
122 {
123 LatencyLeft = Latency;
124 BufLeft = 0;
125 WritePos1 = 0;
126 WritePos2 = 0;
127 ReadPos = flb; // Set "read" position to account for filter's latency.
128
129 memset( &Buf[ ReadPos ], 0,
130 (size_t) ( BufLen - flb ) * sizeof( Buf[ 0 ]));
131
132 memset( &Buf[ BufLenPad + ReadPos ], 0,
133 (size_t) ( BufLen - flb ) * sizeof( Buf[ 0 ]));
134 }
135
136 virtual int process( double* ip, int l, double*& op0 )
137 {
138 R8BASSERT( l >= 0 );
139
140 double* op = op0;
141
142 while( l > 0 )
143 {
144 // Copy new input samples to 2 ring buffers.
145
146 if( WritePos1 != WritePos2 )
147 {
148 // If previous fill was asymmetrical, put a single sample to
149 // the second buffer.
150
151 double* const wp2 = Buf + BufLenPad + WritePos2;
152 *wp2 = *ip;
153
154 if( WritePos2 < flo )
155 {
156 wp2[ BufLen ] = *ip;
157 }
158
159 ip++;
160 WritePos2 = WritePos1;
161 l--;
162 BufLeft++;
163 }
164
165 const int b1 = min(( l + 1 ) >> 1,
166 min( BufLen - WritePos1, flb - BufLeft ));
167
168 const int b2 = b1 - ( b1 * 2 > l );
169
170 double* const wp1 = Buf + WritePos1;
171 double* const wp2 = Buf + BufLenPad + WritePos1;
172 int i;
173
174 for( i = 0; i < b2; i++ )
175 {
176 wp1[ i ] = ip[ 0 ];
177 wp2[ i ] = ip[ 1 ];
178 ip += 2;
179 }
180
181 if( b1 != b2 )
182 {
183 wp1[ b2 ] = *ip;
184 ip++;
185 }
186
187 const int ec = flo - WritePos1;
188
189 if( ec > 0 )
190 {
191 memcpy( wp1 + BufLen, wp1,
192 (size_t) min( b1, ec ) * sizeof( wp1[ 0 ]));
193
194 memcpy( wp2 + BufLen, wp2,
195 (size_t) min( b2, ec ) * sizeof( wp2[ 0 ]));
196 }
197
198 WritePos1 = ( WritePos1 + b1 ) & BufLenMask;
199 WritePos2 = ( WritePos2 + b2 ) & BufLenMask;
200 l -= b1 + b2;
201 BufLeft += b2;
202
203 // Produce output.
204
205 int c = BufLeft - fl2;
206
207 while( c > 0 )
208 {
209 const int cb = min( c, BufLen - ReadPos );
210 double* const opend = op + cb;
211 ( *convfn )( op, opend, fltp, BufRP + ReadPos );
212
213 op = opend;
214 ReadPos = ( ReadPos + cb ) & BufLenMask;
215 BufLeft -= cb;
216 c -= cb;
217 }
218 }
219
220 int ol = (int) ( op - op0 );
221
222 if( LatencyLeft != 0 )
223 {
224 if( LatencyLeft >= ol )
225 {
226 LatencyLeft -= ol;
227 return( 0 );
228 }
229
230 ol -= LatencyLeft;
231 op0 += LatencyLeft;
232 LatencyLeft = 0;
233 }
234
235 return( ol );
236 }
237
238private:
239 static const int BufLenBits = 10;
245 static const int BufLen = 1 << BufLenBits;
248 static const int BufLenMask = BufLen - 1;
250 static const int BufLenPad = BufLen + 27;
252 double Buf[ BufLenPad * 2 ];
254 double FltBuf[ 14 + 2 ];
256 const double* BufRP;
257 double* fltp;
258 double LatencyFrac;
259 int Latency;
260 int fll;
261 int fl2;
262 int flo;
263 int flb;
264 int LatencyLeft;
265 int BufLeft;
267 int WritePos1;
268 int WritePos2;
270 int ReadPos;
271
272 typedef void( *CConvolveFn )( double* op, double* const opend,
273 const double* const flt, const double* rp1 );
275 CConvolveFn convfn;
276
277#define R8BHBC1( fn ) \
278 static void fn( double* op, double* const opend, const double* const flt, \
279 const double* rp1 ) \
280 { \
281 while( op != opend ) \
282 { \
283 const double* const rp = rp1 + BufLenPad - 1;
284
285#define R8BHBC2 \
286 op++; \
287 rp1++; \
288 } \
289 }
290
291#include "CDSPHBDownsampler.inc"
292
293#undef R8BHBC1
294#undef R8BHBC2
295};
296
297// ---------------------------------------------------------------------------
298
299} // namespace r8b
300
301#endif // R8B_CDSPHBDOWNSAMPLER_INCLUDED
Half-band upsampling class.
#define R8BASSERT(e)
Assertion macro used to check for certain run-time conditions. By default, no action is taken if asse...
Definition r8bconf.h:28
#define R8BCONSOLE(...)
Console output macro, used to output various resampler status strings, including filter design parame...
Definition r8bconf.h:41
The "r8brain-free-src" library namespace.
Definition CDSPBlockConvolver.h:22
T * alignptr(T *const ptr, const uintptr_t align)
Forces the provided ptr pointer to be aligned to align bytes.
Definition r8bbase.h:269
T min(const T &v1, const T &v2)
Returns minimum of two values.
Definition r8bbase.h:1092
virtual int getInLenBeforeOutPos(const int ReqOutPos) const
Returns the number of input samples required to advance to the specified output sample position (so t...
Definition CDSPHBDownsampler.h:99
virtual int process(double *ip, int l, double *&op0)
Performs DSP processing.
Definition CDSPHBDownsampler.h:136
CDSPHBDownsampler(const double ReqAtten, const int SteepIndex, const bool IsThird, const double PrevLatency)
Initalizes the half-band downsampler.
Definition CDSPHBDownsampler.h:47
virtual int getLatency() const
Return the latency, in samples, which is present in the output signal.
Definition CDSPHBDownsampler.h:104
virtual void clear()
Clears (resets) the state of this object and returns it to the state after construction.
Definition CDSPHBDownsampler.h:121
virtual int getMaxOutLen(const int MaxInLen) const
Returns the maximal length of the output buffer required when processing the MaxInLen number of input...
Definition CDSPHBDownsampler.h:114
virtual double getLatencyFrac() const
Returns fractional latency, in samples, which is present in the output signal.
Definition CDSPHBDownsampler.h:109