fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
2e4d385
livekit/livekit-ffi@0.13.0
1/* SoX Resampler Library Copyright (c) 2013 robs@users.sourceforge.net 2* Licence for this file: LGPL v2.1 See LICENCE for details. */ 3 4/* Generate the filter coefficients for variable-rate resampling. */ 5 6#include <stdio.h> 7#include <stdlib.h> 8#include <math.h> 9#define PI 3.14159265358979323846/* Since M_PI can't be relied on */ 10 11static void double * h ,int m ,double l ,char const * name ) 12{/* Print out a filter: */ 13int i ,N = l ? (int )(l * m )- (l > 1 ) :m ,R = (N + 1 )/2 ; 14int a = !l || l > 1 ?0 :N - R ,b = l > 1 ?R :N ; 15printf ("static float const %s[] = {\n" ,name ); 16if (l > 1 )printf (" 0.f," );else if (!l )l = 1 ; 17for (i = a ;h && i < b ;++ i ,printf ("% .9gf,%c" ,l * h [i - 1 ],"\n " [(i - a )& 3 && i < b ])); 18puts ("};\n" ); 19free (h ); 20} 21/* Parks McClellan FIR LPF: */ 22#define even_adj (f ) ((N&1)? 1 : cos(PI*.5*(f))) 23#define W (f ) (((f) < Fp+1e-9? weight : 1) * even_adj(f))/* Weighting fn */ 24#define D (f ) (((f) < Fp+1e-9) / even_adj(f))/* Desired response fn */ 25#define F (i ) ((i) <= end[0]? (i)*inc[0] : 1-(end[1]-(i))*inc[1]) 26#define EE (x ,z ) (_1 != x 1 && x E[i] > 0 && x E[i] >= x E[i z 1]) 27#define PEAK do {if (k<NP+1) peak[k]=i; ++k,_1=(E[i]>0)-(E[i]<0);} while (0) 28 29typedef struct {double x ,beta ,gamma ;}coef_t ; 30 31static double amp_response (coef_t * coef ,int R ,double f ,int i ) 32{ 33double n = 0 ,d = 0 ,x = cos (PI * f ),t ; 34for (;i < R ;d += t = coef [i ].beta /t ,n += coef [i ].gamma * t ,++ i ) 35if (fabs (t = x - coef [i ].x )< 1e-9 )return coef [i ].gamma ; 36return n /d ; 37} 38 39static void fir (int m ,double l ,double Fp0 ,double Fs0 , 40double weight0 ,int density ,char const * name ) 41{ 42double Fp = Fp0 /l ,Fs = Fs0 /l ,weight = 1 /weight0 ,inc [2 ],Ws = 1 - Fs ; 43int N = (int )(l * m )- (l > 1 ),R = (N + 1 )/2 ,NP = R + 1 ,grid_size = 1 + density * R + 1 ,pass = 0 ; 44int n1 = Ws >=(2 * R - 1 )* Fp ?1 :(int )(R * Fp /(Fp + Ws )+ .5 ),n2 = NP - n1 ,_1 ,i ,j ,k ; 45int * peak = calloc (sizeof (* peak ), (size_t )(NP + 1 )),* P = peak ,end [2 ]; 46coef_t * coef = calloc (sizeof (* coef ), (size_t )(NP )); 47float * E = calloc (sizeof (* E ), (size_t )(grid_size )); 48double d ,n ,e ,f ,mult ,delta ,sum ,hi ,lo ,* A = (double * )E ,* h = 0 ; 49 50if (!P || !coef || !E ) gotoEND ; 51end [0 ]= n1 * density ,end [1 ]= grid_size - 1 ;/* Create prototype peaks: */ 52inc [0 ]= Fp /end [0 ],inc [1 ]= n2 == 1 ?0 :Ws / ((n2 - 1 )* density ); 53for (i = 0 ;i < n1 ;P [n1 - 1 - i ]= end [0 ]- i * density ,++ i ); 54for (i = 0 ;i < n2 ;P [n1 + i ]= 1 + end [0 ]+ i * density ,++ i ); 55 56do {/* Coefs for amp. resp.: */ 57for (i = 0 ;i < NP ;coef [i ].x = cos (PI * F (P [i ])),++ i ); 58for (_1 = -1 ,n = d = i = 0 ;i < NP ;++ i ) { 59for (mult = 1 ,j = 0 ;j < R ;++ j )if (j != i )mult *=coef [i ].x - coef [j ].x ; 60if (mult )coef [i ].beta = 1 /mult ;else gotoEND ; 61if (i != R )mult *=coef [i ].x - coef [R ].x ; 62f = F (P [i ]),n += D (f )/mult ,d += (_1 =- _1 )/(W (f )* mult ); 63 } 64for (delta = n /d ,_1 = -1 ,i = 0 ;i < R ;++ i ) 65f = F (P [i ]),coef [i ].gamma = D (f )- (_1 =- _1 )* delta /W (f ); 66for (i = 0 ;i <=end [1 ];++ i )/* Amplitude response and error: */ 67f = F (i ),E [i ]= (float )(W (f )* (D (f )- amp_response (coef ,R ,f ,0 ))); 68 69i = k = _1 = 0 ;/* Find new peaks: */ 70if (end [0 ])if (EE (+ ,+ ) || EE ( - , + )) PEAK ; /* At F=0 */ 71for ( ++ i, j = 0 ; j < 2 ; ++ j) { /* In band j: */ 72for (; i < end[j]; ++ i) 73if (( EE ( + , - ) && E [i] > E [i + 1 ]) || ( EE ( - , - ) && E [i] < E [i + 1 ])) PEAK ; 74if (!j) { PEAK ; ++ i; PEAK ; ++ i;} /* At Fp & Fs */ 75} 76if (i == end[ 1 ]) if ( EE ( + , - ) || EE ( - , - )) PEAK ; /* At F=1 */ 77if (( unsigned )(k = k - NP ) > 1 ) goto END ; /* Too many/few? */ 78P = peak + k * ( fabs ( E [peak[ 0 ]]) < fabs ( E [peak[ NP ]])); /* rm 1st? */ 79 80for (lo = hi = fabs ( E [ P [ 0 ]]), i = 1 ; i < NP ; ++ i) /* Converged?: */ 81e = fabs ( E [ P [i]]), lo = e < lo? e:lo, hi = e > hi? e:hi; 82} while ((hi - lo)/hi > .001 && ++ pass < 20 ); 83/* Create impulse response from final amp. resp. coefs: */ 84if (!(h = malloc ( sizeof ( * h) * ( size_t ) N ))) goto END ; 85for (i = 0 ; i < R ; f = 2. * i/ N , A [i ++ ] = amp_response (coef, R ,f, 0 ) * even_adj (f)); 86for (i = 0 ; i < R ; h[ N - 1 - i] = h[i] = sum/ N , ++ i) 87for (sum =* A , j = 1 ; j < R ; sum += 2 * cos ( 2 * PI * (i - ( N - 1 )/ 2. )/ N * j) * A [j], ++ j); 88END : free (coef), free ( E ), free (peak); 89(h, m, l, name); 90} 91/* Half-band IIR LPF (Mitra DSP 3/e, 13_9): */ 92static void iir ( int N , double Fp, char const * name) 93{ 94double d = tan ( PI * .5 * Fp), r = d * d, t = sqrt ( 1 - r * r), n = ( 1 - sqrt (t))/( 1 + sqrt (t)) * .5 ; 95double x = (n * n) * (n * n), Q = ((( 150 * x + 15 ) * x + 2 ) * x + 1 ) * n, q = pow ( Q , .25 ), * h; 96int i = 0 , j, _1; 97if (!(h = malloc ( sizeof ( * h) * ( size_t ) N ))) goto END ; 98for (; i < N ; t = n * q/d, t = t * t, t = sqrt (( 1 - t * r) * ( 1 - t/r))/( 1 + t), h[i ++ ] = ( 1 - t)/( 1 + t)) 99for (_1 = 1 , d = -.5 , n = j = 0 , x = (i + 1 ) * PI /( N + .5 ); j < 7 ; ++ j, _1 =- _1) 100n += _1 * pow ( Q ,j * (j + 1 )) * sin (x * (j + .5 )), d += _1 * pow ( Q ,j * j) * cos (x * j); 101END : (h, N , 0 , name); 102} 103 104int main ( int argc, char ** argv) 105{ 106puts ( "/* SoX Resampler Library Copyright (c) 2007-16 robs@users.sourceforge.net" ); 107puts ( " * Licence for this file: LGPL v2.1 See LICENCE for details. */\n" ); 108 109fir ( 241 , 1 , .45 , .5 , 160 , 32 , "half_fir_coefs" ); 110fir ( 24 , .5 , .25 , .5 , 1 , 31 , "fast_half_fir_coefs" ); 111fir ( 20 , 12 , .9 , 1.5 , 160 , 58 , "coefs0_d" ); 112fir ( 12 , 6 , .45 , 1.5 , 80 , 29 , "coefs0_u" ); 113iir ( 15 , .492 , "iir_coefs" ); 114return 0 * argc * !argv ; 115}