Debian Codegraph @

3rdparty/iaxclient/lib/sox/compand.c

raw symbols SCIP
1 /*
2 * Compander effect
3 *
4 * Written by Nick Bailey ([email protected] or
5 * [email protected])
6 *
7 * Copyright 1999 Chris Bagwell And Nick Bailey
8 * This source code is freely redistributable and may be used for
9 * any purpose. This copyright notice must be maintained.
10 * Chris Bagwell And Nick Bailey are not responsible for
11 * the consequences of using this software.
12 */
13
14 #include <string.h>
15 #include <stdlib.h>
16 #include <math.h>
17 #include "sox.h"
18
19 /*
20 * Compressor/expander effect for dsp.
21 *
22 * Flow diagram for one channel:
23 *
24 * ------------ ---------------
25 * | | | | ---
26 * ibuff ---+---| integrator |--->| transfer func |--->| |
27 * | | | | | | |
28 * | ------------ --------------- | | * gain
29 * | | * |----------->obuff
30 * | ------- | |
31 * | | | | |
32 * +----->| delay |-------------------------->| |
33 * | | ---
34 * -------
35 *
36 * Usage:
37 * compand attack1,decay1[,attack2,decay2...]
38 * in-dB1,out-dB1[,in-dB2,out-dB2...]
39 * [ gain [ initial-volume [ delay ] ] ]
40 *
41 * Note: clipping can occur if the transfer function pushes things too
42 * close to 0 dB. In that case, use a negative gain, or reduce the
43 * output level of the transfer function.
44 */
45
46 /*
47 * Process options
48 *
49 * Don't do initialization now.
50 * The 'info' fields are not yet filled in.
51 */
52 int st_compand_getopts(compand_t l, int n, char **argv)
53 {
54
55 if (n < 2 || n > 5)
56 {
57 st_fail("Wrong number of arguments for the compander effect\n"
58 "Use: {<attack_time>,<decay_time>}+ {<dB_in>,<db_out>}+ "
59 "[<dB_postamp> [<initial-volume> [<delay_time]]]\n"
60 "where {}+ means `one or more in a comma-separated, "
61 "white-space-free list'\n"
62 "and [] indications possible omission. dB values are floating\n"
63 "point or `-inf'; times are in seconds.");
64 return (ST_EOF);
65 }
66 else { /* Right no. of args, but are they well formed? */
67 char *s;
68 int rates, tfers, i, commas;
69
70 /* Start by checking the attack and decay rates */
71
72 for (s = argv[0], commas = 0; *s; ++s)
73 if (*s == ',') ++commas;
74
75 if (commas % 2 == 0) /* There must be an even number of
76 attack/decay parameters */
77 {
78 st_fail("compander: Odd number of attack & decay rate parameters");
79 return (ST_EOF);
80 }
81
82 rates = 1 + commas/2;
83 if ((l->attackRate = malloc(sizeof(double) * rates)) == NULL ||
84 (l->decayRate = malloc(sizeof(double) * rates)) == NULL ||
85 (l->volume = malloc(sizeof(double) * rates)) == NULL)
86 {
87 st_fail("Out of memory");
88 return (ST_EOF);
89 }
90 l->expectedChannels = rates;
91 l->delay_buf = NULL;
92
93 /* Now tokenise the rates string and set up these arrays. Keep
94 them in seconds at the moment: we don't know the sample rate yet. */
95
96 s = strtok(argv[0], ","); i = 0;
97 do {
98 l->attackRate[i] = atof(s); s = strtok(NULL, ",");
99 l->decayRate[i] = atof(s); s = strtok(NULL, ",");
100 ++i;
101 } while (s != NULL);
102
103 /* Same business, but this time for the transfer function */
104
105 for (s = argv[1], commas = 0; *s; ++s)
106 if (*s == ',') ++commas;
107
108 if (commas % 2 == 0) /* There must be an even number of
109 transfer parameters */
110 {
111 st_fail("compander: Odd number of transfer function parameters\n"
112 "Each input value in dB must have a corresponding output value");
113 return (ST_EOF);
114 }
115
116 tfers = 3 + commas/2; /* 0, 0 at start; 1, 1 at end */
117 if ((l->transferIns = malloc(sizeof(double) * tfers)) == NULL ||
118 (l->transferOuts = malloc(sizeof(double) * tfers)) == NULL)
119 {
120 st_fail("Out of memory");
121 return (ST_EOF);
122 }
123 l->transferPoints = tfers;
124 l->transferIns[0] = 0.0; l->transferOuts[0] = 0.0;
125 l->transferIns[tfers-1] = 1.0; l->transferOuts[tfers-1] = 1.0;
126 s = strtok(argv[1], ","); i = 1;
127 do {
128 if (!strcmp(s, "-inf"))
129 {
130 st_fail("Input signals of zero level must always generate zero output");
131 return (ST_EOF);
132 }
133 l->transferIns[i] = pow(10.0, atof(s)/20.0);
134 if (l->transferIns[i] > 1.0)
135 {
136 st_fail("dB values are relative to maximum input, and, ipso facto, "
137 "cannot exceed 0");
138 return (ST_EOF);
139 }
140 if (l->transferIns[i] == 1.0) /* Final point was explicit */
141 --(l->transferPoints);
142 if (i > 0 && l->transferIns[i] <= l->transferIns[i-1])
143 {
144 st_fail("Transfer function points don't have strictly ascending "
145 "input amplitude");
146 return (ST_EOF);
147 }
148 s = strtok(NULL, ",");
149 l->transferOuts[i] = strcmp(s, "-inf") ?
150 pow(10.0, atof(s)/20.0) : 0;
151 s = strtok(NULL, ",");
152 ++i;
153 } while (s != NULL);
154
155 /* If there is a postprocessor gain, store it */
156 if (n >= 3) l->outgain = pow(10.0, atof(argv[2])/20.0);
157 else l->outgain = 1.0;
158
159 /* Set the initial "volume" to be attibuted to the input channels.
160 Unless specified, choose 1.0 (maximum) otherwise clipping will
161 result if the user has seleced a long attack time */
162 for (i = 0; i < l->expectedChannels; ++i) {
163 double v = n>=4 ? pow(10.0, atof(argv[3])/20) : 1.0;
164 l->volume[i] = v;
165
166 /* If there is a delay, store it. */
167 if (n >= 5) l->delay = atof(argv[4]);
168 else l->delay = 0.0;
169 }
170 }
171 return (ST_SUCCESS);
172 }
173
174
175 /*
176 * Prepare processing.
177 * Do all initializations.
178 */
179 int st_compand_start(compand_t *lH, char **opts, int nopts)
180 {
181 int i;
182 compand_t l;
183
184 *lH = malloc(sizeof (struct compand));
185 l = *lH;
186
187 st_compand_getopts(l, nopts, opts);
188
189 # ifdef DEBUG
190 {
191 fprintf(stderr, "Starting compand effect\n");
192 fprintf(stderr, "\nRate %ld, size %d, encoding %d, output gain %g.\n",
193 ST_SAMPLE_RATE, effp->outinfo.size, effp->outinfo.encoding,
194 l->outgain);
195 fprintf(stderr, "%d input channel(s) expected: actually %d\n",
196 l->expectedChannels, ST_CHANNELS);
197 fprintf(stderr, "\nAttack and decay rates\n"
198 "======================\n");
199 for (i = 0; i < l->expectedChannels; ++i)
200 fprintf(stderr, "Channel %d: attack = %-12g decay = %-12g\n",
201 i, l->attackRate[i], l->decayRate[i]);
202 fprintf(stderr, "\nTransfer function (linear values)\n"
203 "================= =============\n");
204 for (i = 0; i < l->transferPoints; ++i)
205 fprintf(stderr, "%12g -> %-12g\n",
206 l->transferIns[i], l->transferOuts[i]);
207 }
208 # endif
209
210 /* Convert attack and decay rates using number of samples */
211
212 for (i = 0; i < l->expectedChannels; ++i) {
213 if (l->attackRate[i] > 1.0/ST_SAMPLE_RATE)
214 l->attackRate[i] = 1.0 -
215 exp(-1.0/(ST_SAMPLE_RATE * l->attackRate[i]));
216 else
217 l->attackRate[i] = 1.0;
218 if (l->decayRate[i] > 1.0/ST_SAMPLE_RATE)
219 l->decayRate[i] = 1.0 -
220 exp(-1.0/(ST_SAMPLE_RATE * l->decayRate[i]));
221 else
222 l->decayRate[i] = 1.0;
223 }
224
225 /* Allocate the delay buffer */
226 l->delay_buf_size = (int) (l->delay * ST_SAMPLE_RATE * ST_CHANNELS);
227 if (l->delay_buf_size > 0
228 && (l->delay_buf = malloc(sizeof(long) * l->delay_buf_size)) == NULL) {
229 st_fail("Out of memory");
230 return (ST_EOF);
231 }
232 for (i = 0; i < l->delay_buf_size; i++)
233 l->delay_buf[i] = 0;
234 l->delay_buf_ptr = 0;
235 l->delay_buf_cnt = 0;
236 l->delay_buf_full= 0;
237
238 return (ST_SUCCESS);
239 }
240
241 /*
242 * Update a volume value using the given sample
243 * value, the attack rate and decay rate
244 */
245
246 static void doVolume(double *v, double samp, compand_t l, int chan)
247 {
248 double s = samp/ST_SAMPLE_MAX;
249 double delta = s - *v;
250
251 if (delta > 0.0) /* increase volume according to attack rate */
252 *v += delta * l->attackRate[chan];
253 else /* reduce volume according to decay rate */
254 *v += delta * l->decayRate[chan];
255 }
256
257 /*
258 * Processed signed long samples from ibuf to obuf.
259 * Return number of samples processed.
260 */
261 int st_compand_flow(compand_t l, st_sample_t *ibuf, st_sample_t *obuf,
262 st_size_t *isamp, st_size_t *osamp)
263 {
264 int len = (*isamp > *osamp) ? *osamp : *isamp;
265 int filechans = ST_CHANNELS;
266 int idone,odone;
267 long checkbuf; //if st_sample_t of type int32_t
268
269 for (idone = 0,odone = 0; idone < len; ibuf += filechans) {
270 int chan;
271
272 /* Maintain the volume fields by simulating a leaky pump circuit */
273
274 for (chan = 0; chan < filechans; ++chan) {
275 if (l->expectedChannels == 1 && filechans > 1) {
276 /* User is expecting same compander for all channels */
277 int i;
278 double maxsamp = 0.0;
279 for (i = 0; i < filechans; ++i) {
280 double rect = fabs(ibuf[i]);
281 if (rect > maxsamp) maxsamp = rect;
282 }
283 doVolume(&l->volume[0], maxsamp, l, 0);
284 break;
285 } else
286 doVolume(&l->volume[chan], fabs(ibuf[chan]), l, chan);
287 }
288
289 /* Volume memory is updated: perform compand */
290
291 for (chan = 0; chan < filechans; ++chan) {
292 double v = l->expectedChannels > 1 ?
293 l->volume[chan] : l->volume[0];
294 double outv;
295 int piece;
296
297 for (piece = 1 /* yes, 1 */;
298 piece < l->transferPoints;
299 ++piece)
300 if (v >= l->transferIns[piece - 1] &&
301 v < l->transferIns[piece])
302 break;
303
304 outv = l->transferOuts[piece-1] +
305 (l->transferOuts[piece] - l->transferOuts[piece-1]) *
306 (v - l->transferIns[piece-1]) /
307 (l->transferIns[piece] - l->transferIns[piece-1]);
308
309 if (l->delay_buf_size <= 0)
310 {
311 checkbuf = (long int) (ibuf[chan]*(outv/v)*l->outgain);
312 if(checkbuf > ST_SAMPLE_MAX)
313 obuf[odone] = ST_SAMPLE_MAX;
314 else if(checkbuf < ST_SAMPLE_MIN)
315 obuf[odone] = ST_SAMPLE_MIN;
316 else
317 obuf[odone] = (st_sample_t) checkbuf;
318
319 idone++;
320 odone++;
321 }
322 else
323 {
324 if (l->delay_buf_cnt >= l->delay_buf_size)
325 {
326 l->delay_buf_full=1; //delay buffer is now definetly full
327 checkbuf = (long int) (l->delay_buf[l->delay_buf_ptr]*(outv/v)*l->outgain);
328 if(checkbuf > ST_SAMPLE_MAX)
329 obuf[odone] = ST_SAMPLE_MAX;
330 else if(checkbuf < ST_SAMPLE_MIN)
331 obuf[odone] = ST_SAMPLE_MIN;
332 else
333 obuf[odone] = (st_sample_t) checkbuf;
334
335 odone++;
336 idone++;
337 }
338 else
339 {
340 l->delay_buf_cnt++;
341 idone++; //no "odone++" because we did not fill obuf[...]
342 }
343 l->delay_buf[l->delay_buf_ptr++] = ibuf[chan];
344 l->delay_buf_ptr %= l->delay_buf_size;
345 }
346 }
347 }
348
349 *isamp = idone; *osamp = odone;
350 return (ST_SUCCESS);
351 }
352
353 /*
354 * Drain out compander delay lines.
355 */
356 int st_compand_drain(compand_t l, st_sample_t *obuf, st_size_t *osamp)
357 {
358 int done;
359
360 /*
361 * Drain out delay samples. Note that this loop does all channels.
362 */
363 if(l->delay_buf_full==0) l->delay_buf_ptr=0;
364 for (done = 0; done < (int) *osamp && l->delay_buf_cnt > 0; done++) {
365 obuf[done] = l->delay_buf[l->delay_buf_ptr++];
366 l->delay_buf_ptr %= l->delay_buf_size;
367 l->delay_buf_cnt--;
368 }
369
370 /* tell caller number of samples played */
371 *osamp = done;
372 return (ST_SUCCESS);
373 }
374
375
376 /*
377 * Clean up compander effect.
378 */
379 int st_compand_stop(compand_t l)
380 {
381
382 free((char *) l->delay_buf);
383 free((char *) l->transferOuts);
384 free((char *) l->transferIns);
385 free((char *) l->volume);
386 free((char *) l->decayRate);
387 free((char *) l->attackRate);
388
389 l->delay_buf = NULL;
390 l->transferOuts = NULL;
391 l->transferIns = NULL;
392 l->volume = NULL;
393 l->decayRate = NULL;
394 l->attackRate = NULL;
395
396 return (ST_SUCCESS);
397 }