1 /* libSoX Compander Transfer Function  (c) 2007 robs@users.sourceforge.net
2  *
3  * This library is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2.1 of the License, or (at
6  * your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 
18 #include <math.h>
19 
20 typedef struct {
21   struct sox_compandt_segment {
22     double x, y;              /* 1st point in segment */
23     double a, b;              /* Quadratic coeffecients for rest of segment */
24   } * segments;
25   double in_min_lin;
26   double out_min_lin;
27   double outgain_dB;        /* Post processor gain */
28   double curve_dB;
29 } sox_compandt_t;
30 
31 sox_bool lsx_compandt_parse(sox_compandt_t * t, char * points, char * gain);
32 sox_bool lsx_compandt_show(sox_compandt_t * t, sox_plot_t plot);
33 void    lsx_compandt_kill(sox_compandt_t * p);
34 
35 /* Place in header to allow in-lining */
lsx_compandt(sox_compandt_t * t,double in_lin)36 static double lsx_compandt(sox_compandt_t * t, double in_lin)
37 {
38   struct sox_compandt_segment * s;
39   double in_log, out_log;
40 
41   if (in_lin <= t->in_min_lin)
42     return t->out_min_lin;
43 
44   in_log = log(in_lin);
45 
46   for (s = t->segments + 1; in_log > s[1].x; ++s);
47 
48   in_log -= s->x;
49   out_log = s->y + in_log * (s->a * in_log + s->b);
50 
51   return exp(out_log);
52 }
53