17#ifndef R8B_CDSPRESAMPLER_INCLUDED
18#define R8B_CDSPRESAMPLER_INCLUDED
123 const int aMaxInLen,
const double ReqTransBand = 2.0,
124 const double ReqAtten = 206.91,
128 , MaxInLen( aMaxInLen )
129 , CurMaxOutLen( aMaxInLen )
136 R8BCONSOLE(
"* CDSPResampler: src=%.1f dst=%.1f len=%i tb=%.1f "
137 "att=%.2f ph=%i\n", SrcSampleRate, DstSampleRate, aMaxInLen,
138 ReqTransBand, ReqAtten, (
int) ReqPhase );
140 if( SrcSampleRate == DstSampleRate )
145 TmpBufCapacities[ 0 ] = 0;
146 TmpBufCapacities[ 1 ] = 0;
151 const int CommonRatioCount = 5;
152 const int CommonRatios[ CommonRatioCount ][ 2 ] = {
162 for( i = 0; i < CommonRatioCount; i++ )
164 const int num = CommonRatios[ i ][ 0 ];
165 const int den = CommonRatios[ i ][ 1 ];
167 if( SrcSampleRate * num == DstSampleRate * den )
170 CDSPFIRFilterCache :: getLPFilter(
171 1.0 / ( num > den ? num : den ), ReqTransBand,
172 ReqAtten, ReqPhase, num ), num, den, LatencyFrac ));
181 for( i = 2; i <= 3; i++ )
183 bool WasFound =
false;
189 const double NewSR = SrcSampleRate * sm;
191 if( NewSR == DstSampleRate )
197 if( NewSR > DstSampleRate )
209 CDSPFIRFilterCache :: getLPFilter( 1.0 / i, ReqTransBand,
210 ReqAtten, ReqPhase, i ), i, 1, LatencyFrac ));
212 const bool IsThird = ( i == 3 );
214 for( i = 0; i < c; i++ )
225 if( DstSampleRate * 2.0 > SrcSampleRate )
229 const double NormFreq = ( DstSampleRate > SrcSampleRate ? 0.5 :
230 0.5 * DstSampleRate / SrcSampleRate );
233 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
234 ReqAtten, ReqPhase, 2.0 ), 2, 1, LatencyFrac ));
239 const double tbw = 0.0175;
241 const double ThreshSampleRate = SrcSampleRate /
242 ( 1.0 - tbw * ReqTransBand );
251 const double ndiv = div * 2.0;
253 if( DstSampleRate < ThreshSampleRate * ndiv )
267 const double ndiv = div * ( c2 == 0 ? 3.0 : 2.0 );
269 if( DstSampleRate < ThreshSampleRate * ndiv )
278 const double SrcSampleRate2 = SrcSampleRate * 2.0;
297 if( c2 > 0 && div2 > div )
309 DstSampleRate, ReqAtten,
false, LatencyFrac ));
311 double tb = ( 1.0 - SrcSampleRate * div / DstSampleRate ) /
315 if( tb > CDSPFIRFilter :: getLPMaxTransBand() )
317 tb = CDSPFIRFilter :: getLPMaxTransBand();
321 CDSPFIRFilterCache :: getLPFilter( 1.0 / num, tb,
322 ReqAtten, ReqPhase, num ), num, 1, LatencyFrac ));
324 const bool IsThird = ( num == 3 );
326 for( i = 1; i < c; i++ )
329 IsThird, LatencyFrac ));
335 DstSampleRate, ReqAtten,
false, LatencyFrac ));
344 double CheckSR = DstSampleRate * 4.0;
345 double SrcSRDiv = 1.0;
348 while( CheckSR <= SrcSampleRate )
355 const double FinGain = 1.0 / SrcSRDiv;
357 double NormFreq = 0.5;
358 bool UseInterp =
true;
359 bool IsThird =
false;
362 for( downf = 2; downf <= 3; downf++ )
364 if( DstSampleRate * SrcSRDiv * downf == SrcSampleRate )
366 NormFreq = 1.0 / downf;
368 IsThird = ( downf == 3 );
376 NormFreq = DstSampleRate * SrcSRDiv / SrcSampleRate;
377 IsThird = ( NormFreq * 3.0 <= 1.0 );
380 for( i = 0; i < c; i++ )
392 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
393 ReqAtten, ReqPhase, FinGain ), 1, downf, LatencyFrac ));
398 DstSampleRate * SrcSRDiv, ReqAtten, IsThird, LatencyFrac ));
408 for( i = 0; i < StepCount; i++ )
418 int ReqInSamples = ReqOutPos;
426 return( ReqInSamples );
462 outc +=
process( &ins, 1, op );
464 if( outc > ReqOutPos )
486 if( ReqOutSamples < 1 )
501 return( LatencyFrac );
512 return( CurMaxOutLen );
533 for( i = 0; i < StepCount; i++ )
535 Steps[ i ] ->
clear();
567 virtual int process(
double* ip0,
int l,
double*& op0 )
574 for( i = 0; i < StepCount; i++ )
576 double* op = TmpBufs[ i & 1 ];
577 l = Steps[ i ] ->
process( ip, l, op );
600 template<
typename Tin,
typename Tout >
601 void oneshot( Tin* ip,
int iplen, Tout* op,
int oplen )
620 memset( p, 0, MaxInLen *
sizeof( p[ 0 ]));
625 rc =
min( iplen, MaxInLen );
626 p = getOneshotBuf( ip, Buf, rc );
632 int wc =
process( p, rc, op0 );
633 wc =
min( oplen, wc );
635 for( i = 0; i < wc; i++ )
637 op[ i ] = (Tout) op0[ i ];
654 double* TmpBufs[ 2 ];
655 int TmpBufCapacities[ 2 ];
673 if( StepCount == StepCapacity )
677 const int NewCapacity = StepCapacity + 8;
678 Steps.
realloc( StepCapacity, NewCapacity );
679 StepCapacity = NewCapacity;
685 if( CurMaxOutLen > TmpBufCapacities[ CurTmpBuf ])
687 TmpBufCapacities[ CurTmpBuf ] = CurMaxOutLen;
692 Steps[ StepCount ] = Proc;
700 void createTmpBuffers()
702 const int ol = TmpBufCapacities[ 0 ] + TmpBufCapacities[ 1 ];
706 TmpBufAll.alloc( ol );
707 TmpBufs[ 0 ] = &TmpBufAll[ 0 ];
708 TmpBufs[ 1 ] = &TmpBufAll[ TmpBufCapacities[ 0 ]];
722 static double* getOneshotBuf(
double*
const ip,
double*
const,
const int )
737 static double* getOneshotBuf(
float*
const ip,
double*
const Buf,
742 for( i = 0; i < rc; i++ )
774 const int aMaxInLen,
const double ReqTransBand = 2.0 )
775 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
805 const int aMaxInLen,
const double ReqTransBand = 2.0 )
806 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
835 const int aMaxInLen,
const double ReqTransBand = 2.0 )
836 :
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:1092
Single-block overlap-save convolution processing class.
Definition CDSPBlockConvolver.h:40
Fractional delay filter bank-based interpolator class.
Definition CDSPFracInterpolator.h:694
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:53
virtual int getMaxOutLen(const int) const
This implementation ignores the supplied parameter and returns the maximal output buffer length that ...
Definition CDSPResampler.h:510
virtual double getLatencyFrac() const
Returns fractional latency, in samples, which is present in the output signal.
Definition CDSPResampler.h:499
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:414
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:451
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:601
virtual void clear()
Clears (resets) the state of this object and returns it to the state after construction.
Definition CDSPResampler.h:529
virtual int getLatency() const
Return the latency, in samples, which is present in the output signal.
Definition CDSPResampler.h:494
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:122
virtual int process(double *ip0, int l, double *&op0)
Performs sample rate conversion.
Definition CDSPResampler.h:567
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:484
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:773
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:804
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:834
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:353