fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
2e4d385
main
1/* SoX Resampler Library Copyright (c) 2007-16 robs@users.sourceforge.net 2* Licence for this file: LGPL v2.1 See LICENCE for details. */ 3 4/* Resample using an interpolated poly-phase FIR with length LEN. */ 5/* Input must be followed by FIR_LENGTH-1 samples. */ 6 7#if COEF_INTERP != 1 && COEF_INTERP != 2 && COEF_INTERP != 3 8#error COEF_INTERP 9#endif 10 11#if SIMD_AVX || SIMD_SSE || SIMD_NEON 12#define N (FIR_LENGTH>>2) 13 14#if COEF_INTERP == 1 15#define _ sum=vMac(vMac(b,X,a),vLdu(in+j*4),sum), ++j; 16#elif COEF_INTERP == 2 17#define _ sum=vMac(vMac(vMac(c,X,b),X,a),vLdu(in+j*4),sum), ++j; 18#else 19#define _ sum=vMac(vMac(vMac(vMac(d,X,c),X,b),X,a),vLdu(in+j*4),sum), ++j; 20#endif 21 22#define a coefs[(COEF_INTERP+1)*(N*phase+j)+(COEF_INTERP-0)] 23#define b coefs[(COEF_INTERP+1)*(N*phase+j)+(COEF_INTERP-1)] 24#define c coefs[(COEF_INTERP+1)*(N*phase+j)+(COEF_INTERP-2)] 25#define d coefs[(COEF_INTERP+1)*(N*phase+j)+(COEF_INTERP-3)] 26 27#define BEGINNING v4_t X = vLds(x), sum = vZero(); \ 28 v4_t const * const __restrict coefs = (v4_t *)COEFS 29#define END vStorSum(output+i, sum) 30#define cc (n ) case n: core(n); break 31#define CORE (n ) switch (n) {cc(2); cc(3); cc(4); cc(5); cc(6); default: core(n);} 32#else 33#define N FIR_LENGTH 34 35#if COEF_INTERP == 1 36#define _ sum += (b*x + a)*in[j], ++j; 37#elif COEF_INTERP == 2 38#define _ sum += ((c*x + b)*x + a)*in[j], ++j; 39#else 40#define _ sum += (((d*x + c)*x + b)*x + a)*in[j], ++j; 41#endif 42 43#define a (coef(COEFS, COEF_INTERP, N, phase, 0,j)) 44#define b (coef(COEFS, COEF_INTERP, N, phase, 1,j)) 45#define c (coef(COEFS, COEF_INTERP, N, phase, 2,j)) 46#define d (coef(COEFS, COEF_INTERP, N, phase, 3,j)) 47 48#define BEGINNING sample_t sum = 0 49#define END output[i] = sum 50#define CORE (n ) core(n) 51#endif 52 53 54 55#define floatPrecCore (n ) { \ 56 float_step_t at = p->at.flt; \ 57 for (i = 0; (int)at < num_in; ++i, at += p->step.flt) { \ 58 sample_t const * const __restrict in = input + (int)at; \ 59 float_step_t frac = at - (int)at; \ 60 int phase = (int)(frac * (1 << PHASE_BITS)); \ 61 sample_t x = (sample_t)(frac * (1 << PHASE_BITS) - phase); \ 62 int j = 0; \ 63 BEGINNING; CONVOLVE(n); END; \ 64 } \ 65 fifo_read(&p->fifo, (int)at, NULL); \ 66 p->at.flt = at - (int)at; }/* Could round to 1 in some cirmcumstances. */ 67 68 69 70#define highPrecCore (n ) { \ 71 step_t at; at.fix = p->at.fix; \ 72 for (i = 0; at.integer < num_in; ++i, \ 73 at.fix.ls.all += p->step.fix.ls.all, \ 74 at.whole += p->step.whole + (at.fix.ls.all < p->step.fix.ls.all)) { \ 75 sample_t const * const __restrict in = input + at.integer; \ 76 uint32_t frac = at.fraction; \ 77 int phase = (int)(frac >> (32 - PHASE_BITS));/* High-order bits */ \ 78/* Low-order bits, scaled to [0,1): */ \ 79sample_t x = (sample_t )((frac <<PHASE_BITS )* (1 /MULT32 )); \ 80int j = 0 ; \ 81BEGINNING ;CONVOLVE (n );END ; \ 82 } \ 83fifo_read (& p -> fifo ,at .integer ,NULL ); \ 84p -> at .whole = at .fraction ; \ 85p -> at .fix .ls = at .fix .ls ; } 86 87 88 89#define stdPrecCore (n ) { \ 90 int64p_t at; at.all = p->at.whole; \ 91 for (i = 0; at.parts.ms < num_in; ++i, at.all += p->step.whole) { \ 92 sample_t const * const __restrict in = input + at.parts.ms; \ 93 uint32_t const frac = at.parts.ls; \ 94 int phase = (int)(frac >> (32 - PHASE_BITS));/* high-order bits */ \ 95/* Low-order bits, scaled to [0,1): */ \ 96sample_t x = (sample_t )((frac <<PHASE_BITS )* (1 /MULT32 )); \ 97int j = 0 ; \ 98BEGINNING ;CONVOLVE (n );END ; \ 99 } \ 100fifo_read (& p -> fifo ,at .parts .ms ,NULL ); \ 101p -> at .whole = at .parts .ls ; } 102 103 104 105#if WITH_FLOAT_STD_PREC_CLOCK 106#define SPCORE floatPrecCore 107#else 108#define SPCORE stdPrecCore 109#endif 110 111 112 113#if WITH_HI_PREC_CLOCK 114#define core (n ) if (p->use_hi_prec_clock) highPrecCore(n) else SPCORE(n) 115#else 116#define core (n ) SPCORE(n) 117#endif 118 119 120 121static void FUNCTION (stage_t * p ,fifo_t * output_fifo ) 122{ 123sample_t const * input = stage_read_p (p ); 124int num_in = min (stage_occupancy (p ),p -> input_size ); 125int i ,max_num_out = 1 + (int )(num_in * p -> out_in_ratio ); 126sample_t * const __restrict output = fifo_reserve (output_fifo ,max_num_out ); 127 128CORE (N ); 129assert (max_num_out - i >=0 ); 130fifo_trim_by (output_fifo ,max_num_out - i ); 131} 132 133 134 135#undef _ 136#undef a 137#undef b 138#undef c 139#undef d 140#undef CORE 141#undef cc 142#undef core 143#undef COEF_INTERP 144#undef N 145#undef BEGINNING 146#undef END 147#undef CONVOLVE 148#undef FIR_LENGTH 149#undef FUNCTION 150#undef PHASE_BITS