1 /* Effect: fir filter from coefs   Copyright (c) 2009 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 "sox_i.h"
19 #include "dft_filter.h"
20 
21 typedef struct {
22   dft_filter_priv_t  base;
23   char const         * filename;
24   double             * h;
25   int                n;
26 } priv_t;
27 
create(sox_effect_t * effp,int argc,char ** argv)28 static int create(sox_effect_t * effp, int argc, char * * argv)
29 {
30   priv_t             * p = (priv_t *)effp->priv;
31   dft_filter_priv_t  * b = &p->base;
32   double             d;
33   char               c;
34 
35   b->filter_ptr = &b->filter;
36   --argc, ++argv;
37   if (!argc)
38     p->filename = "-"; /* default to stdin */
39   else if (argc == 1)
40     p->filename = argv[0], --argc;
41   else for (; argc && sscanf(*argv, "%lf%c", &d, &c) == 1; --argc, ++argv) {
42     p->n++;
43     p->h = lsx_realloc(p->h, p->n * sizeof(*p->h));
44     p->h[p->n - 1] = d;
45   }
46   return argc? lsx_usage(effp) : SOX_SUCCESS;
47 }
48 
start(sox_effect_t * effp)49 static int start(sox_effect_t * effp)
50 {
51   priv_t        * p = (priv_t *)effp->priv;
52   dft_filter_t  * f = p->base.filter_ptr;
53   double        d;
54   char          c;
55   int           i;
56 
57   if (!f->num_taps) {
58     if (!p->n && p->filename) {
59       FILE * file = lsx_open_input_file(effp, p->filename, sox_true);
60       if (!file)
61         return SOX_EOF;
62       while ((i = fscanf(file, " #%*[^\n]%c", &c)) >= 0) {
63         if (i >= 1) continue; /* found and skipped a comment */
64         if ((i = fscanf(file, "%lf", &d)) > 0) {
65           /* found a coefficient value */
66           p->n++;
67           p->h = lsx_realloc(p->h, p->n * sizeof(*p->h));
68           p->h[p->n - 1] = d;
69         } else break; /* either EOF, or something went wrong
70                          (read or syntax error) */
71       }
72       if (!feof(file)) {
73         lsx_fail("error reading coefficient file");
74         if (file != stdin) fclose(file);
75         return SOX_EOF;
76       }
77       if (file != stdin) fclose(file);
78     }
79     lsx_report("%i coefficients", p->n);
80     if (!p->n)
81       return SOX_EFF_NULL;
82     if (effp->global_info->plot != sox_plot_off) {
83       char title[100];
84       sprintf(title, "SoX effect: fir (%d coefficients)", p->n);
85       lsx_plot_fir(p->h, p->n, effp->in_signal.rate,
86           effp->global_info->plot, title, -30., 30.);
87       free(p->h);
88       return SOX_EOF;
89     }
90     lsx_set_dft_filter(f, p->h, p->n, p->n >> 1);
91   }
92   return lsx_dft_filter_effect_fn()->start(effp);
93 }
94 
lsx_fir_effect_fn(void)95 sox_effect_handler_t const * lsx_fir_effect_fn(void)
96 {
97   static sox_effect_handler_t handler;
98   handler = *lsx_dft_filter_effect_fn();
99   handler.name = "fir";
100   handler.usage = "[coef-file|coefs]";
101   handler.getopts = create;
102   handler.start = start;
103   handler.priv_size = sizeof(priv_t);
104   return &handler;
105 }
106