17#ifndef R8B_CDSPRESAMPLER_INCLUDED
18#define R8B_CDSPRESAMPLER_INCLUDED
118 const int aMaxInLen,
const double ReqTransBand = 2.0,
119 const double ReqAtten = 206.91,
123 , MaxInLen( aMaxInLen )
124 , CurMaxOutLen( aMaxInLen )
131 R8BCONSOLE(
"* CDSPResampler: src=%.1f dst=%.1f len=%i tb=%.1f "
132 "att=%.2f ph=%i\n", SrcSampleRate, DstSampleRate, aMaxInLen,
133 ReqTransBand, ReqAtten, (
int) ReqPhase );
135 if( SrcSampleRate == DstSampleRate )
140 TmpBufCapacities[ 0 ] = 0;
141 TmpBufCapacities[ 1 ] = 0;
146 const int CommonRatioCount = 5;
147 const int CommonRatios[ CommonRatioCount ][ 2 ] = {
157 for( i = 0; i < CommonRatioCount; i++ )
159 const int num = CommonRatios[ i ][ 0 ];
160 const int den = CommonRatios[ i ][ 1 ];
162 if( SrcSampleRate * num == DstSampleRate * den )
165 CDSPFIRFilterCache :: getLPFilter(
166 1.0 / ( num > den ? num : den ), ReqTransBand,
167 ReqAtten, ReqPhase, num ), num, den, LatencyFrac ));
176 for( i = 2; i <= 3; i++ )
178 bool WasFound =
false;
183 const double NewSR = SrcSampleRate * ( i << c );
185 if( NewSR == DstSampleRate )
191 if( NewSR > DstSampleRate )
202 CDSPFIRFilterCache :: getLPFilter( 1.0 / i, ReqTransBand,
203 ReqAtten, ReqPhase, i ), i, 1, LatencyFrac ));
205 const bool IsThird = ( i == 3 );
207 for( i = 0; i < c; i++ )
218 if( DstSampleRate * 2.0 > SrcSampleRate )
222 const double NormFreq = ( DstSampleRate > SrcSampleRate ? 0.5 :
223 0.5 * DstSampleRate / SrcSampleRate );
226 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
227 ReqAtten, ReqPhase, 2.0 ), 2, 1, LatencyFrac ));
232 const double tbw = 0.0175;
234 const double ThreshSampleRate = SrcSampleRate /
235 ( 1.0 - tbw * ReqTransBand );
244 const int ndiv = div * 2;
246 if( DstSampleRate < ThreshSampleRate * ndiv )
260 const int ndiv = div * ( c2 == 0 ? 3 : 2 );
262 if( DstSampleRate < ThreshSampleRate * ndiv )
271 const double SrcSampleRate2 = SrcSampleRate * 2.0;
290 if( c2 > 0 && div2 > div )
302 DstSampleRate, ReqAtten,
false, LatencyFrac ));
304 double tb = ( 1.0 - SrcSampleRate * div / DstSampleRate ) /
308 if( tb > CDSPFIRFilter :: getLPMaxTransBand() )
310 tb = CDSPFIRFilter :: getLPMaxTransBand();
314 CDSPFIRFilterCache :: getLPFilter( 1.0 / num, tb,
315 ReqAtten, ReqPhase, num ), num, 1, LatencyFrac ));
317 const bool IsThird = ( num == 3 );
319 for( i = 1; i < c; i++ )
322 IsThird, LatencyFrac ));
328 DstSampleRate, ReqAtten,
false, LatencyFrac ));
337 double CheckSR = DstSampleRate * 4.0;
339 double FinGain = 1.0;
341 while( CheckSR <= SrcSampleRate )
348 const int SrcSRDiv = ( 1 << c );
350 double NormFreq = 0.5;
351 bool UseInterp =
true;
352 bool IsThird =
false;
354 for( downf = 2; downf <= 3; downf++ )
356 if( DstSampleRate * SrcSRDiv * downf == SrcSampleRate )
358 NormFreq = 1.0 / downf;
360 IsThird = ( downf == 3 );
368 NormFreq = DstSampleRate * SrcSRDiv / SrcSampleRate;
369 IsThird = ( NormFreq * 3.0 <= 1.0 );
372 for( i = 0; i < c; i++ )
384 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
385 ReqAtten, ReqPhase, FinGain ), 1, downf, LatencyFrac ));
390 DstSampleRate * SrcSRDiv, ReqAtten, IsThird, LatencyFrac ));
400 for( i = 0; i < StepCount; i++ )
410 int ReqInSamples = ReqOutPos;
418 return( ReqInSamples );
454 outc +=
process( &ins, 1, op );
456 if( outc > ReqOutPos )
478 if( ReqOutSamples < 1 )
493 return( LatencyFrac );
504 return( CurMaxOutLen );
525 for( i = 0; i < StepCount; i++ )
527 Steps[ i ] ->
clear();
559 virtual int process(
double* ip0,
int l,
double*& op0 )
566 for( i = 0; i < StepCount; i++ )
568 double* op = TmpBufs[ i & 1 ];
569 l = Steps[ i ] ->
process( ip, l, op );
592 template<
typename Tin,
typename Tout >
593 void oneshot(
const Tin* ip,
int iplen, Tout* op,
int oplen )
612 memset( p, 0, MaxInLen *
sizeof( p[ 0 ]));
617 rc =
min( iplen, MaxInLen );
619 if(
sizeof( Tin ) ==
sizeof(
double ))
627 for( i = 0; i < rc; i++ )
638 int wc =
process( p, rc, op0 );
639 wc =
min( oplen, wc );
641 for( i = 0; i < wc; i++ )
643 op[ i ] = (Tout) op0[ i ];
660 double* TmpBufs[ 2 ];
661 int TmpBufCapacities[ 2 ];
679 if( StepCount == StepCapacity )
683 const int NewCapacity = StepCapacity + 8;
684 Steps.
realloc( StepCapacity, NewCapacity );
685 StepCapacity = NewCapacity;
691 if( CurMaxOutLen > TmpBufCapacities[ CurTmpBuf ])
693 TmpBufCapacities[ CurTmpBuf ] = CurMaxOutLen;
698 Steps[ StepCount ] = Proc;
706 void createTmpBuffers()
708 const int ol = TmpBufCapacities[ 0 ] + TmpBufCapacities[ 1 ];
712 TmpBufAll.alloc( ol );
713 TmpBufs[ 0 ] = &TmpBufAll[ 0 ];
714 TmpBufs[ 1 ] = &TmpBufAll[ TmpBufCapacities[ 0 ]];
744 const int aMaxInLen,
const double ReqTransBand = 2.0 )
745 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
775 const int aMaxInLen,
const double ReqTransBand = 2.0 )
776 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
805 const int aMaxInLen,
const double ReqTransBand = 2.0 )
806 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
Single-block overlap-save convolution processor class.
Fractional delay interpolator and filter bank classes.
Half-band downsampling convolver class.
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
bool getWholeStepping(const double SSampleRate, const double DSampleRate, int &ResInStep, int &ResOutStep)
Evaluates source and destination sample rate ratio and returns the required input and output stepping...
Definition CDSPFracInterpolator.h:644
EDSPFilterPhaseResponse
Enumeration of filter's phase responses.
Definition CDSPFIRFilter.h:29
@ fprLinearPhase
Linear-phase response. Features a linear-phase, high-latency response, with the latency expressed as ...
Definition CDSPFIRFilter.h:30
T min(const T &v1, const T &v2)
Returns minimum of two values.
Definition r8bbase.h:1079
Single-block overlap-save convolution processing class.
Definition CDSPBlockConvolver.h:40
Fractional delay filter bank-based interpolator class.
Definition CDSPFracInterpolator.h:691
Half-band downsampler class.
Definition CDSPHBDownsampler.h:31
Half-band upsampling class.
Definition CDSPHBUpsampler.h:32
The base virtual class for DSP processing algorithms.
Definition CDSPProcessor.h:33
The master sample rate converter (resampler) class.
Definition CDSPResampler.h:48
virtual int getMaxOutLen(const int) const
This implementation ignores the supplied parameter and returns the maximal output buffer length that ...
Definition CDSPResampler.h:502
void oneshot(const Tin *ip, int iplen, Tout *op, int oplen)
Performs resampling of an input sample buffer of the specified length in the "one-shot" mode.
Definition CDSPResampler.h:593
virtual double getLatencyFrac() const
Returns fractional latency, in samples, which is present in the output signal.
Definition CDSPResampler.h:491
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 CDSPResampler.h:406
int getInLenBeforeOutStart(const int ReqOutPos=0)
Returns the number of input samples required to advance to the specified output sample position (so t...
Definition CDSPResampler.h:443
virtual void clear()
Clears (resets) the state of this object and returns it to the state after construction.
Definition CDSPResampler.h:521
virtual int getLatency() const
Return the latency, in samples, which is present in the output signal.
Definition CDSPResampler.h:486
CDSPResampler(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0, const double ReqAtten=206.91, const EDSPFilterPhaseResponse ReqPhase=fprLinearPhase)
Initalizes the resampler object.
Definition CDSPResampler.h:117
virtual int process(double *ip0, int l, double *&op0)
Performs sample rate conversion.
Definition CDSPResampler.h:559
int getInputRequiredForOutput(const int ReqOutSamples) const
Returns the number of input samples required to produce at least the specified number of output sampl...
Definition CDSPResampler.h:476
CDSPResampler16(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Initializes the 16-bit resampler. See the r8b::CDSPResampler class for details.
Definition CDSPResampler.h:743
CDSPResampler16IR(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Initializes the 16-bit impulse response resampler. See the r8b::CDSPResampler class for details.
Definition CDSPResampler.h:774
CDSPResampler24(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Initializes the 24-bit resampler (including 32-bit floating point). See the r8b::CDSPResampler class ...
Definition CDSPResampler.h:804
void realloc(const int PrevCapacity, const int NewCapacity)
Reallocates memory so that the specified number of elements of type T can be stored in this buffer ob...
Definition r8bbase.h:340