17#ifndef R8B_CDSPRESAMPLER_INCLUDED
18#define R8B_CDSPRESAMPLER_INCLUDED
125 const int aMaxInLen,
const double ReqTransBand = 2.0,
126 const double ReqAtten = 206.91,
128 : MaxInLen( aMaxInLen )
129 , CurMaxOutLen( aMaxInLen )
138 R8BCONSOLE(
"* CDSPResampler: src=%.1f dst=%.1f len=%i tb=%.1f "
139 "att=%.2f ph=%i\n", SrcSampleRate, DstSampleRate, aMaxInLen,
140 ReqTransBand, ReqAtten, (
int) ReqPhase );
142 if( SrcSampleRate == DstSampleRate )
147 TmpBufCapacities[ 0 ] = 0;
148 TmpBufCapacities[ 1 ] = 0;
153 const int CommonRatioCount = 5;
154 const int CommonRatios[ CommonRatioCount ][ 2 ] = {
164 for( i = 0; i < CommonRatioCount; i++ )
166 const int num = CommonRatios[ i ][ 0 ];
167 const int den = CommonRatios[ i ][ 1 ];
169 if( SrcSampleRate * num == DstSampleRate * den )
172 CDSPFIRFilterCache :: getLPFilter(
173 1.0 / ( num > den ? num : den ), ReqTransBand,
174 ReqAtten, ReqPhase, num, den ), num, den, LatencyFrac ));
183 for( i = 2; i <= 3; i++ )
185 bool WasFound =
false;
191 const double NewSR = SrcSampleRate * sm;
193 if( NewSR == DstSampleRate )
199 if( NewSR > DstSampleRate )
211 CDSPFIRFilterCache :: getLPFilter( 1.0 / i, ReqTransBand,
212 ReqAtten, ReqPhase, i, 1 ), i, 1, LatencyFrac ));
214 const bool IsThirdFirst = ( i == 3 );
216 for( i = 0; i < c; i++ )
219 ( IsThirdFirst || i > 1 ), LatencyFrac ));
227 if( DstSampleRate * 2.0 > SrcSampleRate )
231 const double NormFreq = ( DstSampleRate > SrcSampleRate ? 0.5 :
232 0.5 * DstSampleRate / SrcSampleRate );
235 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
236 ReqAtten, ReqPhase, 2.0, 1 ), 2, 1, LatencyFrac ));
241 const double tbw = 0.0175;
243 const double ThreshSampleRate = SrcSampleRate /
244 ( 1.0 - tbw * ReqTransBand );
253 const double ndiv = div * 2.0;
255 if( DstSampleRate < ThreshSampleRate * ndiv )
264 const double SrcSampleRate2 = SrcSampleRate * 2.0;
284 DstSampleRate, ReqAtten,
false, LatencyFrac ));
286 double tb = ( 1.0 - SrcSampleRate * div / DstSampleRate ) /
290 if( tb > CDSPFIRFilter :: getLPMaxTransBand() )
292 tb = CDSPFIRFilter :: getLPMaxTransBand();
296 CDSPFIRFilterCache :: getLPFilter( 1.0 / num, tb,
297 ReqAtten, ReqPhase, num, 1 ), num, 1, LatencyFrac ));
299 for( i = 1; i < c; i++ )
302 ( i > 2 ), LatencyFrac ));
308 DstSampleRate, ReqAtten,
false, LatencyFrac ));
317 double CheckSR = DstSampleRate * 4.0;
318 double SrcSRDiv = 1.0;
321 while( CheckSR <= SrcSampleRate )
328 const double FinGain = 1.0 / SrcSRDiv;
330 double NormFreq = 0.5;
331 bool UseInterp =
true;
332 bool IsThirdFirst =
false;
335 for( downf = 2; downf <= 3; downf++ )
337 if( DstSampleRate * SrcSRDiv * downf == SrcSampleRate )
339 NormFreq = 1.0 / downf;
341 IsThirdFirst = ( downf == 3 );
349 NormFreq = DstSampleRate * SrcSRDiv / SrcSampleRate;
350 IsThirdFirst = ( NormFreq * 3.0 <= 1.0 );
353 for( i = 0; i < c; i++ )
361 ( IsThirdFirst || i < c - 2 ), LatencyFrac ));
365 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
366 ReqAtten, ReqPhase, FinGain, downf ), 1, downf, LatencyFrac ));
371 DstSampleRate * SrcSRDiv, ReqAtten, IsThirdFirst,
428 outc +=
process( &ins, 1, op );
430 if( outc > ReqOutPos )
452 if( ReqOutSamples < 1 )
467 return( LatencyFrac );
478 return( CurMaxOutLen );
497 CDSPProcessor* ps = StepFirst;
534 virtual int process(
double* ip0,
int l,
double*& op0 )
540 CDSPProcessor* ps = StepFirst;
544 double* op = TmpBufs[ tp ];
545 l = ps ->
process( ip, l, op );
570 template<
typename Tin,
typename Tout >
571 void oneshot( Tin* ip,
int iplen, Tout* op,
int oplen )
590 memset( p, 0, MaxInLen *
sizeof( p[ 0 ]));
595 rc =
min( iplen, MaxInLen );
596 p = getOneshotBuf( ip, Buf, rc );
602 int wc =
process( p, rc, op0 );
603 wc =
min( oplen, wc );
605 for( i = 0; i < wc; i++ )
607 op[ i ] = (Tout) op0[ i ];
625 double* TmpBufs[ 2 ];
626 int TmpBufCapacities[ 2 ];
646 StepLast ->
Next = Proc;
658 if( CurMaxOutLen > TmpBufCapacities[ CurTmpBuf ])
660 TmpBufCapacities[ CurTmpBuf ] = CurMaxOutLen;
670 void createTmpBuffers()
672 const int ol = TmpBufCapacities[ 0 ] + TmpBufCapacities[ 1 ];
676 TmpBufAll.alloc( ol );
677 TmpBufs[ 0 ] = &TmpBufAll[ 0 ];
678 TmpBufs[ 1 ] = &TmpBufAll[ TmpBufCapacities[ 0 ]];
692 static double* getOneshotBuf(
double*
const ip,
double*
const,
const int )
707 static double* getOneshotBuf(
float*
const ip,
double*
const Buf,
712 for( i = 0; i < rc; i++ )
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 R8B_NULL
The "null pointer" value, portable between C++11 and earlier C++ versions.
Definition r8bbase.h:101
#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:639
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:1257
Single-block overlap-save convolution processing class.
Definition CDSPBlockConvolver.h:40
Fractional delay filter bank-based interpolator class.
Definition CDSPFracInterpolator.h:687
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:36
virtual int getMaxOutLen(const int) const
This implementation ignores the supplied parameter and returns the maximal output buffer length that ...
Definition CDSPResampler.h:476
virtual double getLatencyFrac() const
Returns fractional latency, in samples, which is present in the output signal.
Definition CDSPResampler.h:465
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:417
void oneshot(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:571
virtual void clear()
Clears (resets) the state of this object and returns it to the state after construction.
Definition CDSPResampler.h:495
virtual int getLatency() const
Return the latency, in samples, which is present in the output signal.
Definition CDSPResampler.h:460
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:124
virtual int getInLenBeforeOutPos(int ReqOutPos) const
Returns the number of input samples required to advance to the specified output sample position (so t...
Definition CDSPResampler.h:378
virtual int process(double *ip0, int l, double *&op0)
Performs sample rate conversion.
Definition CDSPResampler.h:534
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:450
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
Pointer-to-object "keeper" class with automatic deletion.
Definition r8bbase.h:457
CDSPProcessor * Next
Definition r8bbase.h:652