| 1 | /* |
| 2 | * FILE: resample.h |
| 3 | * BY: Julius Smith (at CCRMA, Stanford U) |
| 4 | * C BY: translated from SAIL to C by Christopher Lee Fraley |
| 5 | * ([email protected]) |
| 6 | * DATE: 7-JUN-88 |
| 7 | * VERS: 2.0 (17-JUN-88, 3:00pm) |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * October 29, 1999 |
| 12 | * Various changes, bugfixes(?), increased precision, by Stan Brooks. |
| 13 | * |
| 14 | * This source code is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /* Conversion constants */ |
| 21 | #define Lc 7 |
| 22 | #define Nc (1<<Lc) |
| 23 | #define La 16 |
| 24 | #define Na (1<<La) |
| 25 | #define Lp (Lc+La) |
| 26 | #define Np (1<<Lp) |
| 27 | #define Amask (Na-1) |
| 28 | #define Pmask (Np-1) |
| 29 | |
| 30 | #define MAXNWING (80<<Lc) |
| 31 | /* Description of constants: |
| 32 | * |
| 33 | * Nc - is the number of look-up values available for the lowpass filter |
| 34 | * between the beginning of its impulse response and the "cutoff time" |
| 35 | * of the filter. The cutoff time is defined as the reciprocal of the |
| 36 | * lowpass-filter cut off frequence in Hz. For example, if the |
| 37 | * lowpass filter were a sinc function, Nc would be the index of the |
| 38 | * impulse-response lookup-table corresponding to the first zero- |
| 39 | * crossing of the sinc function. (The inverse first zero-crossing |
| 40 | * time of a sinc function equals its nominal cutoff frequency in Hz.) |
| 41 | * Nc must be a power of 2 due to the details of the current |
| 42 | * implementation. The default value of 128 is sufficiently high that |
| 43 | * using linear interpolation to fill in between the table entries |
| 44 | * gives approximately 16-bit precision, and quadratic interpolation |
| 45 | * gives about 23-bit (float) precision in filter coefficients. |
| 46 | * |
| 47 | * Lc - is log base 2 of Nc. |
| 48 | * |
| 49 | * La - is the number of bits devoted to linear interpolation of the |
| 50 | * filter coefficients. |
| 51 | * |
| 52 | * Lp - is La + Lc, the number of bits to the right of the binary point |
| 53 | * in the integer "time" variable. To the left of the point, it indexes |
| 54 | * the input array (X), and to the right, it is interpreted as a number |
| 55 | * between 0 and 1 sample of the input X. The default value of 23 is |
| 56 | * about right. There is a constraint that the filter window must be |
| 57 | * "addressable" in a int32_t, more precisely, if Nmult is the number |
| 58 | * of sinc zero-crossings in the right wing of the filter window, then |
| 59 | * (Nwing<<Lp) must be expressible in 31 bits. |
| 60 | * |
| 61 | */ |
| 62 | |
| 63 | /* this Float MUST match that in filter.c */ |
| 64 | #define Float double/*float*/ |
| 65 | #define ISCALE 0x10000 |
| 66 | |
| 67 | /* largest factor for which exact-coefficients upsampling will be used |
| 68 | * */ |
| 69 | #define NQMAX 511 |
| 70 | |
| 71 | #define BUFFSIZE 8192 /*16384*/ /* Total I/O buffer size */ |
| 72 | |
| 73 | typedef short st_sample_t; |
| 74 | typedef unsigned long st_size_t; |
| 75 | typedef int st_ssize_t; |
| 76 | |
| 77 | #define ST_SAMPLE_MAX 0x7fff |
| 78 | #define ST_SAMPLE_MIN (-ST_SAMPLE_MAX - 1) |
| 79 | |
| 80 | #define ST_SUCCESS 0 |
| 81 | #define ST_EOF 1 |
| 82 | |
| 83 | #define ST_SAMPLE_RATE 8000 |
| 84 | #define ST_CHANNELS 1 |
| 85 | |
| 86 | |
| 87 | /* Private data for Lerp via LCM file */ |
| 88 | typedef struct resamplestuff { |
| 89 | double Factor; /* Factor = Fout/Fin sample rates */ |
| 90 | double rolloff; /* roll-off frequency */ |
| 91 | double beta; /* passband/stopband tuning magic */ |
| 92 | int quadr; /* non-zero to use qprodUD quadratic interpolation */ |
| 93 | long Nmult; |
| 94 | long Nwing; |
| 95 | long Nq; |
| 96 | Float *Imp; /* impulse [Nwing+1] Filter coefficients */ |
| 97 | |
| 98 | double Time; /* Current time/pos in input sample */ |
| 99 | long dhb; |
| 100 | |
| 101 | long a,b; /* gcd-reduced input,output rates */ |
| 102 | long t; /* Current time/pos for exact-coeff's method */ |
| 103 | |
| 104 | long Xh; /* number of past/future samples needed by filter */ |
| 105 | long Xoff; /* Xh plus some room for creep */ |
| 106 | long Xread; /* X[Xread] is start-position to enter new samples */ |
| 107 | long Xp; /* X[Xp] is position to start filter application */ |
| 108 | long Xsize,Ysize; /* size (Floats) of X[],Y[] */ |
| 109 | Float *X, *Y; /* I/O buffers */ |
| 110 | } *resample_t; |
| 111 | |
| 112 | |
| 113 | typedef struct compand { |
| 114 | int expectedChannels; /* Also flags that channels aren't to be treated |
| 115 | individually when = 1 and input not mono */ |
| 116 | int transferPoints; /* Number of points specified on the transfer |
| 117 | function */ |
| 118 | double *attackRate; /* An array of attack rates */ |
| 119 | double *decayRate; /* ... and of decay rates */ |
| 120 | double *transferIns; /* ... and points on the transfer function */ |
| 121 | double *transferOuts; |
| 122 | double *volume; /* Current "volume" of each channel */ |
| 123 | double outgain; /* Post processor gain */ |
| 124 | double delay; /* Delay to apply before companding */ |
| 125 | st_sample_t *delay_buf; /* Old samples, used for delay processing */ |
| 126 | st_ssize_t delay_buf_size;/* Size of delay_buf in samples */ |
| 127 | st_ssize_t delay_buf_ptr; /* Index into delay_buf */ |
| 128 | st_ssize_t delay_buf_cnt; /* No. of active entries in delay_buf */ |
| 129 | short int delay_buf_full; /* Shows buffer situation (important for st_compand_drain) */ |
| 130 | } *compand_t; |
| 131 | |
| 132 | |
| 133 | int st_resample_start(resample_t *rH, int inrate, int outrate); |
| 134 | int st_resample_flow(resample_t *rH, st_sample_t *ibuf, st_sample_t *obuf, |
| 135 | st_size_t *isamp, st_size_t *osamp); |
| 136 | int st_resample_drain(resample_t *rH, st_sample_t *obuf, st_size_t *osamp); |
| 137 | int st_resample_stop(resample_t *rH); |
| 138 | |
| 139 | |
| 140 | int st_compand_getopts(compand_t l, int n, char **argv); |
| 141 | int st_compand_start(compand_t *lH, char **opts, int nopts); |
| 142 | int st_compand_flow(compand_t l, st_sample_t *ibuf, st_sample_t *obuf, |
| 143 | st_size_t *isamp, st_size_t *osamp); |
| 144 | int st_compand_drain(compand_t l, st_sample_t *obuf, st_size_t *osamp); |
| 145 | int st_compand_stop(compand_t l); |
| 146 | |
| 147 | void st_report(const char *fmt, ...); |
| 148 | void st_fail(const char *fmt, ...); |
| 149 | void st_warn(const char *fmt, ...); |
| 150 | long st_gcd(long a, long b); |