1 /* Effect: fir filter from coefs Copyright (c) 2009 robs@users.sourceforge.net
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.
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.
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
19 #include "dft_filter.h"
22 dft_filter_priv_t base
;
23 char const * filename
;
28 static int create(sox_effect_t
* effp
, int argc
, char * * argv
)
30 priv_t
* p
= (priv_t
*)effp
->priv
;
31 dft_filter_priv_t
* b
= &p
->base
;
35 b
->filter_ptr
= &b
->filter
;
38 p
->filename
= "-"; /* default to stdin */
40 p
->filename
= argv
[0], --argc
;
41 else for (; argc
&& sscanf(*argv
, "%lf%c", &d
, &c
) == 1; --argc
, ++argv
) {
43 p
->h
= lsx_realloc(p
->h
, p
->n
* sizeof(*p
->h
));
46 return argc
? lsx_usage(effp
) : SOX_SUCCESS
;
49 static int start(sox_effect_t
* effp
)
51 priv_t
* p
= (priv_t
*)effp
->priv
;
52 dft_filter_t
* f
= p
->base
.filter_ptr
;
58 if (!p
->n
&& p
->filename
) {
59 FILE * file
= lsx_open_input_file(effp
, p
->filename
, sox_true
);
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 */
67 p
->h
= lsx_realloc(p
->h
, p
->n
* sizeof(*p
->h
));
69 } else break; /* either EOF, or something went wrong
70 (read or syntax error) */
73 lsx_fail("error reading coefficient file");
74 if (file
!= stdin
) fclose(file
);
77 if (file
!= stdin
) fclose(file
);
79 lsx_report("%i coefficients", p
->n
);
82 if (effp
->global_info
->plot
!= sox_plot_off
) {
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.);
90 lsx_set_dft_filter(f
, p
->h
, p
->n
, p
->n
>> 1);
92 return lsx_dft_filter_effect_fn()->start(effp
);
95 sox_effect_handler_t
const * lsx_fir_effect_fn(void)
97 static sox_effect_handler_t handler
;
98 handler
= *lsx_dft_filter_effect_fn();
100 handler
.usage
= "[coef-file|coefs]";
101 handler
.getopts
= create
;
102 handler
.start
= start
;
103 handler
.priv_size
= sizeof(priv_t
);