16 #ifndef R8B_CDSPRESAMPLER_INCLUDED 17 #define R8B_CDSPRESAMPLER_INCLUDED 116 const int aMaxInLen,
const double ReqTransBand = 2.0,
117 const double ReqAtten = 206.91,
121 , MaxInLen( aMaxInLen )
122 , CurMaxOutLen( aMaxInLen )
129 R8BCONSOLE(
"* CDSPResampler: src=%.1f dst=%.1f len=%i tb=%.1f " 130 "att=%.2f ph=%i\n", SrcSampleRate, DstSampleRate, aMaxInLen,
131 ReqTransBand, ReqAtten, (
int) ReqPhase );
133 if( SrcSampleRate == DstSampleRate )
138 TmpBufCapacities[ 0 ] = 0;
139 TmpBufCapacities[ 1 ] = 0;
144 const int CommonRatioCount = 5;
145 const int CommonRatios[ CommonRatioCount ][ 2 ] = {
155 for( i = 0; i < CommonRatioCount; i++ )
157 const int num = CommonRatios[ i ][ 0 ];
158 const int den = CommonRatios[ i ][ 1 ];
160 if( SrcSampleRate * num == DstSampleRate * den )
164 1.0 / ( num > den ? num : den ), ReqTransBand,
165 ReqAtten, ReqPhase, num ), num, den, LatencyFrac ));
174 for( i = 2; i <= 3; i++ )
176 bool WasFound =
false;
181 const double NewSR = SrcSampleRate * ( i << c );
183 if( NewSR == DstSampleRate )
189 if( NewSR > DstSampleRate )
201 ReqAtten, ReqPhase, i ), i, 1, LatencyFrac ));
203 const bool IsThird = ( i == 3 );
205 for( i = 0; i < c; i++ )
216 if( DstSampleRate * 2 > SrcSampleRate )
220 const double NormFreq = ( DstSampleRate > SrcSampleRate ? 0.5 :
221 0.5 * DstSampleRate / SrcSampleRate );
225 ReqAtten, ReqPhase, 2.0 ), 2, 1, LatencyFrac ));
230 const double ThreshSampleRate = SrcSampleRate * 1.01;
236 const int ndiv = div * 2;
238 if( DstSampleRate < ThreshSampleRate * ndiv )
252 const int ndiv = div * ( c2 == 0 ? 3 : 2 );
254 if( DstSampleRate < ThreshSampleRate * ndiv )
263 const double SrcSampleRate2 = SrcSampleRate * 2.0;
282 if( c2 > 0 && div2 > div )
294 DstSampleRate, ReqAtten,
false, LatencyFrac ));
296 const double tb = 100.0 * ( 1.0 - SrcSampleRate * div /
297 DstSampleRate ) / 1.75;
302 ReqAtten, ReqPhase, num ), num, 1, LatencyFrac ));
304 for( i = 1; i < c; i++ )
307 ( num == 3 ), LatencyFrac ));
313 DstSampleRate, ReqAtten,
false, LatencyFrac ));
322 double CheckSR = DstSampleRate * 4.0;
324 double FinGain = 1.0;
326 while( CheckSR <= SrcSampleRate )
333 const int SrcSRDiv = ( 1 << c );
335 double NormFreq = 0.5;
336 bool UseInterp =
true;
337 bool IsThird =
false;
339 for( downf = 2; downf <= 3; downf++ )
341 if( DstSampleRate * SrcSRDiv * downf == SrcSampleRate )
343 NormFreq = 1.0 / downf;
345 IsThird = ( downf == 3 );
353 NormFreq = DstSampleRate * SrcSRDiv / SrcSampleRate;
354 IsThird = ( NormFreq * 3.0 <= 1.0 );
357 for( i = 0; i < c; i++ )
370 ReqAtten, ReqPhase, FinGain ), 1, downf, LatencyFrac ));
375 DstSampleRate * SrcSRDiv, ReqAtten, IsThird, LatencyFrac ));
385 for( i = 0; i < StepCount; i++ )
398 return( LatencyFrac );
409 return( CurMaxOutLen );
428 for( i = 0; i < StepCount; i++ )
430 Steps[ i ] ->
clear();
462 virtual int process(
double* ip0,
int l,
double*& op0 )
469 for( i = 0; i < StepCount; i++ )
471 double* op = TmpBufs[ i & 1 ];
472 l = Steps[ i ] ->
process( ip, l, op );
493 template<
class Tin,
class Tout >
494 void oneshot(
const Tin* ip,
int iplen, Tout* op,
int oplen )
513 memset( p, 0, MaxInLen *
sizeof(
double ));
518 rc =
min( iplen, MaxInLen );
520 if(
sizeof( Tin ) ==
sizeof(
double ))
528 for( i = 0; i < rc; i++ )
539 int wc =
process( p, rc, op0 );
540 wc =
min( oplen, wc );
542 for( i = 0; i < wc; i++ )
544 op[ i ] = (Tout) op0[ i ];
572 if(
process( &ins, 1, op ) > 0 )
592 double* TmpBufs[ 2 ];
595 int TmpBufCapacities[ 2 ];
618 if( StepCount == StepCapacity )
622 const int NewCapacity = StepCapacity + 8;
623 Steps.
realloc( StepCapacity, NewCapacity );
624 StepCapacity = NewCapacity;
630 if( CurMaxOutLen > TmpBufCapacities[ CurTmpBuf ])
632 TmpBufCapacities[ CurTmpBuf ] = CurMaxOutLen;
637 Steps[ StepCount ] = Proc;
645 void createTmpBuffers()
647 const int ol = TmpBufCapacities[ 0 ] + TmpBufCapacities[ 1 ];
651 TmpBufAll.
alloc( ol );
652 TmpBufs[ 0 ] = &TmpBufAll[ 0 ];
653 TmpBufs[ 1 ] = &TmpBufAll[ TmpBufCapacities[ 0 ]];
683 const int aMaxInLen,
const double ReqTransBand = 2.0 )
684 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
714 const int aMaxInLen,
const double ReqTransBand = 2.0 )
715 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
744 const int aMaxInLen,
const double ReqTransBand = 2.0 )
745 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
753 #endif // R8B_CDSPRESAMPLER_INCLUDED CDSPResampler(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0, const double ReqAtten=206.91, const EDSPFilterPhaseResponse ReqPhase=fprLinearPhase)
Constructor initalizes the resampler object.
Definition: CDSPResampler.h:115
#define R8BCONSOLE(...)
Console output macro, used to output various resampler status strings, including filter design parame...
Definition: r8bconf.h:85
Half-band downsampling convolver class.
virtual int getLatency() const
Definition: CDSPResampler.h:391
Half-band upsampling class.
Definition: CDSPHBUpsampler.h:30
The resampler class for 16-bit impulse response resampling.
Definition: CDSPResampler.h:699
CDSPResampler24(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Constructor initializes the 24-bit resampler (including 32-bit floating point).
Definition: CDSPResampler.h:743
#define R8BASSERT(e)
Assertion macro used to check for certain run-time conditions.
Definition: r8bconf.h:72
Single-block overlap-save convolution processor class.
Linear-phase response.
Definition: CDSPFIRFilter.h:29
virtual int getMaxOutLen(const int) const
This function ignores the supplied parameter and returns the maximal output buffer length that depend...
Definition: CDSPResampler.h:407
The resampler class for 16-bit resampling.
Definition: CDSPResampler.h:668
The resampler class for 24-bit resampling.
Definition: CDSPResampler.h:729
Fractional delay interpolator and filter bank classes.
void oneshot(const Tin *ip, int iplen, Tout *op, int oplen)
Function performs resampling of an input sample buffer of the specified length in the "one-shot" mode...
Definition: CDSPResampler.h:494
Half-band downsampler class.
Definition: CDSPHBDownsampler.h:29
CDSPResampler16IR(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Constructor initializes the 16-bit impulse response resampler.
Definition: CDSPResampler.h:713
T min(const T &v1, const T &v2)
Definition: r8bbase.h:1118
Single-block overlap-save convolution processing class.
Definition: CDSPBlockConvolver.h:38
CDSPResampler16(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Constructor initializes the 16-bit resampler.
Definition: CDSPResampler.h:682
bool getWholeStepping(const double SSampleRate, const double DSampleRate, int &ResInStep, int &ResOutStep)
Function evaluates source and destination sample rate ratio and returns the required input and output...
Definition: CDSPFracInterpolator.h:573
virtual void clear()
Function clears (resets) the state of *this object and returns it to the state after construction...
Definition: CDSPResampler.h:424
virtual double getLatencyFrac() const
Definition: CDSPResampler.h:396
The master sample rate converter (resampler) class.
Definition: CDSPResampler.h:46
void realloc(const int PrevCapacity, const int NewCapacity)
Function reallocates memory so that the specified number of elements of type T can be stored in *this...
Definition: r8bbase.h:338
static CDSPFIRFilter & getLPFilter(const double ReqNormFreq, const double ReqTransBand, const double ReqAtten, const EDSPFilterPhaseResponse ReqPhase, const double ReqGain, const double *const AttenCorrs=NULL)
Function calculates or returns reference to a previously calculated (cached) low-pass FIR filter...
Definition: CDSPFIRFilter.h:602
The base virtual class for DSP processing algorithms.
Definition: CDSPProcessor.h:31
Fractional delay filter bank-based interpolator class.
Definition: CDSPFracInterpolator.h:619
EDSPFilterPhaseResponse
Enumeration of filter's phase responses.
Definition: CDSPFIRFilter.h:27
The "r8brain-free-src" library namespace.
Definition: CDSPBlockConvolver.h:21
int getInLenBeforeOutStart()
Function obtains overall input sample count required to produce first output sample.
Definition: CDSPResampler.h:563
virtual int process(double *ip0, int l, double *&op0)
Function performs sample rate conversion.
Definition: CDSPResampler.h:462
void alloc(const int Capacity)
Function allocates memory so that the specified number of elements of type T can be stored in *this b...
Definition: r8bbase.h:318
Half-band upsampling class.