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 )
163 CDSPFIRFilterCache :: getLPFilter(
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 )
200 CDSPFIRFilterCache :: getLPFilter( 1.0 / i, ReqTransBand,
201 ReqAtten, ReqPhase, i ), i, 1, LatencyFrac ));
203 const bool IsThird = ( i == 3 );
205 for( i = 0; i < c; i++ )
216 if( DstSampleRate * 2.0 > SrcSampleRate )
220 const double NormFreq = ( DstSampleRate > SrcSampleRate ? 0.5 :
221 0.5 * DstSampleRate / SrcSampleRate );
224 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
225 ReqAtten, ReqPhase, 2.0 ), 2, 1, LatencyFrac ));
230 const double tbw = 0.0175;
232 const double ThreshSampleRate = SrcSampleRate /
233 ( 1.0 - tbw * ReqTransBand );
242 const int ndiv = div * 2;
244 if( DstSampleRate < ThreshSampleRate * ndiv )
258 const int ndiv = div * ( c2 == 0 ? 3 : 2 );
260 if( DstSampleRate < ThreshSampleRate * ndiv )
269 const double SrcSampleRate2 = SrcSampleRate * 2.0;
288 if( c2 > 0 && div2 > div )
300 DstSampleRate, ReqAtten,
false, LatencyFrac ));
302 double tb = ( 1.0 - SrcSampleRate * div / DstSampleRate ) /
306 if( tb > CDSPFIRFilter :: getLPMaxTransBand() )
308 tb = CDSPFIRFilter :: getLPMaxTransBand();
312 CDSPFIRFilterCache :: getLPFilter( 1.0 / num, tb,
313 ReqAtten, ReqPhase, num ), num, 1, LatencyFrac ));
315 const bool IsThird = ( num == 3 );
317 for( i = 1; i < c; i++ )
320 IsThird, LatencyFrac ));
326 DstSampleRate, ReqAtten,
false, LatencyFrac ));
335 double CheckSR = DstSampleRate * 4.0;
337 double FinGain = 1.0;
339 while( CheckSR <= SrcSampleRate )
346 const int SrcSRDiv = ( 1 << c );
348 double NormFreq = 0.5;
349 bool UseInterp =
true;
350 bool IsThird =
false;
352 for( downf = 2; downf <= 3; downf++ )
354 if( DstSampleRate * SrcSRDiv * downf == SrcSampleRate )
356 NormFreq = 1.0 / downf;
358 IsThird = ( downf == 3 );
366 NormFreq = DstSampleRate * SrcSRDiv / SrcSampleRate;
367 IsThird = ( NormFreq * 3.0 <= 1.0 );
370 for( i = 0; i < c; i++ )
382 CDSPFIRFilterCache :: getLPFilter( NormFreq, ReqTransBand,
383 ReqAtten, ReqPhase, FinGain ), 1, downf, LatencyFrac ));
388 DstSampleRate * SrcSRDiv, ReqAtten, IsThird, LatencyFrac ));
398 for( i = 0; i < StepCount; i++ )
408 int ReqInSamples = ReqOutPos;
416 return( ReqInSamples );
452 outc +=
process( &ins, 1, op );
454 if( outc > ReqOutPos )
476 if( ReqOutSamples < 1 )
491 return( LatencyFrac );
502 return( CurMaxOutLen );
521 for( i = 0; i < StepCount; i++ )
523 Steps[ i ] ->
clear();
555 virtual int process(
double* ip0,
int l,
double*& op0 )
562 for( i = 0; i < StepCount; i++ )
564 double* op = TmpBufs[ i & 1 ];
565 l = Steps[ i ] ->
process( ip, l, op );
586 template<
typename Tin,
typename Tout >
587 void oneshot(
const Tin* ip,
int iplen, Tout* op,
int oplen )
606 memset( p, 0, MaxInLen *
sizeof( p[ 0 ]));
611 rc =
min( iplen, MaxInLen );
613 if(
sizeof( Tin ) ==
sizeof(
double ))
621 for( i = 0; i < rc; i++ )
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 ]];
738 const int aMaxInLen,
const double ReqTransBand = 2.0 )
739 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
769 const int aMaxInLen,
const double ReqTransBand = 2.0 )
770 :
CDSPResampler( SrcSampleRate, DstSampleRate, aMaxInLen, ReqTransBand,
799 const int aMaxInLen,
const double ReqTransBand = 2.0 )
800 :
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)
Definition: r8bconf.h:27
#define R8BCONSOLE(...)
Definition: r8bconf.h:40
The "r8brain-free-src" library namespace.
Definition: CDSPBlockConvolver.h:21
bool getWholeStepping(const double SSampleRate, const double DSampleRate, int &ResInStep, int &ResOutStep)
Definition: CDSPFracInterpolator.h:642
EDSPFilterPhaseResponse
Definition: CDSPFIRFilter.h:28
@ fprLinearPhase
Linear-phase response. Features a linear-phase, high-latency response, with the latency expressed as ...
Definition: CDSPFIRFilter.h:29
T min(const T &v1, const T &v2)
Definition: r8bbase.h:1063
Single-block overlap-save convolution processing class.
Definition: CDSPBlockConvolver.h:39
Fractional delay filter bank-based interpolator class.
Definition: CDSPFracInterpolator.h:689
Half-band downsampler class.
Definition: CDSPHBDownsampler.h:30
Half-band upsampling class.
Definition: CDSPHBUpsampler.h:31
The base virtual class for DSP processing algorithms.
Definition: CDSPProcessor.h:32
The master sample rate converter (resampler) class.
Definition: CDSPResampler.h:47
virtual int getMaxOutLen(const int) const
Definition: CDSPResampler.h:500
void oneshot(const Tin *ip, int iplen, Tout *op, int oplen)
Definition: CDSPResampler.h:587
virtual double getLatencyFrac() const
Definition: CDSPResampler.h:489
virtual int getInLenBeforeOutPos(const int ReqOutPos) const
Definition: CDSPResampler.h:404
int getInLenBeforeOutStart(const int ReqOutPos=0)
Definition: CDSPResampler.h:441
virtual void clear()
Definition: CDSPResampler.h:517
virtual int getLatency() const
Definition: CDSPResampler.h:484
CDSPResampler(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0, const double ReqAtten=206.91, const EDSPFilterPhaseResponse ReqPhase=fprLinearPhase)
Definition: CDSPResampler.h:115
virtual int process(double *ip0, int l, double *&op0)
Definition: CDSPResampler.h:555
int getInputRequiredForOutput(const int ReqOutSamples) const
Definition: CDSPResampler.h:474
The resampler class for 16-bit resampling.
Definition: CDSPResampler.h:724
CDSPResampler16(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Definition: CDSPResampler.h:737
The resampler class for 16-bit impulse response resampling.
Definition: CDSPResampler.h:755
CDSPResampler16IR(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Definition: CDSPResampler.h:768
The resampler class for 24-bit resampling.
Definition: CDSPResampler.h:785
CDSPResampler24(const double SrcSampleRate, const double DstSampleRate, const int aMaxInLen, const double ReqTransBand=2.0)
Definition: CDSPResampler.h:798
void alloc(const int Capacity)
Definition: r8bbase.h:343
void realloc(const int PrevCapacity, const int NewCapacity)
Definition: r8bbase.h:363