source: roaraudio/libroardsp/filter.c @ 5889:d866fb1213d6

Last change on this file since 5889:d866fb1213d6 was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 9.2 KB
Line 
1//filter.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
5 *
6 *  This file is part of libroardsp a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroardsp 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.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "libroardsp.h"
27
28static struct _roardsp_filterlist {
29 int id;
30 char * name;
31 int (*  init      )(struct roardsp_filter * filter, struct roar_stream * stream, int id);
32 int (*uninit      )(struct roardsp_filter * filter);
33 int (*ctl         )(struct roardsp_filter * filter, int cmd, void * data);
34 int (*reset       )(struct roardsp_filter * filter, int what);
35 int (*calc  [5][3])(struct roardsp_filter * filter, void * data, size_t samples);
36} _roardsp_filterlist[] = {
37 {ROARDSP_FILTER_AMP, "AMP", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_amp_reset, {
38           {NULL, NULL, NULL},{roardsp_amp_calc8, NULL, NULL},{roardsp_amp_calc16, NULL, NULL},
39           {NULL, NULL, NULL},{roardsp_amp_calc32, NULL, NULL}}},
40 {ROARDSP_FILTER_ADD, "Add", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_add_reset, {
41           {NULL, NULL, NULL},{roardsp_add_calc8, NULL, NULL},{roardsp_add_calc16, NULL, NULL},
42           {NULL, NULL, NULL},{roardsp_add_calc32, NULL, NULL}}},
43#ifdef ROAR_HAVE_LIBM
44 {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, {
45           {NULL, NULL, NULL},{roardsp_lowp_calc8, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},
46           {NULL, NULL, NULL},{roardsp_lowp_calc32, NULL, NULL}}},
47 {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, {
48           {NULL, NULL, NULL},{roardsp_highp_calc8, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},
49           {NULL, NULL, NULL},{roardsp_highp_calc32, NULL, NULL}}},
50#endif
51 {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, {
52           {NULL, NULL, NULL},{roardsp_quantify_calc8, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},
53           {NULL, NULL, NULL},{roardsp_quantify_calc32, NULL, NULL}}},
54 {ROARDSP_FILTER_CLIP, "Clip", roardsp_clip_init, roardsp_clip_uninit, roardsp_clip_ctl, roardsp_clip_reset, {
55           {NULL, NULL, NULL},{roardsp_clip_calc8, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},
56           {NULL, NULL, NULL},{roardsp_clip_calc32, NULL, NULL}}},
57 {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, {
58           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
59 {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, {
60           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
61 {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, {
62           {NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc82},{NULL, NULL, roardsp_swap_calc162},
63           {NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc322}}},
64 {ROARDSP_FILTER_AGC, "AGC", roardsp_agc_init, roardsp_agc_uninit, roardsp_agc_ctl, roardsp_agc_reset, {
65           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
66#ifdef ROAR_HAVE_SPEEX_FILTER
67 {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit,
68                                          roardsp_speex_prep_ctl,  roardsp_speex_prep_reset, {
69           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
70#endif
71#ifdef ROAR_HAVE_LIBM
72 {ROARDSP_FILTER_RESPONSE_CURVE, "ResponseCurve", roardsp_responsecurve_init, roardsp_responsecurve_uninit,
73                                                  roardsp_responsecurve_ctl, roardsp_responsecurve_reset, {
74           {NULL, NULL, NULL},{roardsp_responsecurve_calc8, NULL, NULL},{roardsp_responsecurve_calc16, NULL, NULL},
75           {NULL, NULL, NULL},{roardsp_responsecurve_calc32, NULL, NULL}
76 }},
77 {ROARDSP_FILTER_GOERTZEL, "Goertzel", roardsp_goertzel_init, roardsp_goertzel_uninit,
78                                       roardsp_goertzel_ctl, roardsp_goertzel_reset, {
79           {NULL, NULL, NULL},{roardsp_goertzel_calc8, NULL, NULL},{roardsp_goertzel_calc16, NULL, NULL},
80           {NULL, NULL, NULL},{roardsp_goertzel_calc32, NULL, NULL}
81 }},
82#endif
83 {-1, NULL, NULL, NULL, NULL, NULL, {
84      // ?                  8Bit               16Bit              24Bit              32Bit
85      // 0B:n     1     2   1B:n     1     2   2B:n     1     2   3B:n     1    2    4B:n     1     2
86           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}
87};
88
89int    roardsp_filter_str2id(const char * str) {
90 struct _roardsp_filterlist * l = _roardsp_filterlist;
91
92 while ( l->id != -1 ) {
93  if ( strcasecmp(l->name, str) == 0 )
94   return l->id;
95  l++;
96 }
97
98 roar_err_set(ROAR_ERROR_NOENT);
99 return -1;
100}
101
102const char * roardsp_filter_id2str(const int id) {
103 struct _roardsp_filterlist * l = _roardsp_filterlist;
104
105 while ( l->id != -1 ) {
106  if ( l->id == id )
107   return l->name;
108  l++;
109 }
110
111 roar_err_set(ROAR_ERROR_NOENT);
112 return NULL;
113}
114
115int    roardsp_filter_new   (struct roardsp_filter ** filter, struct roar_stream * stream, int id) {
116 struct roardsp_filter * n;
117 int ret;
118 int error;
119
120 if ( filter == NULL || stream == NULL ) {
121  roar_err_set(ROAR_ERROR_FAULT);
122  return -1;
123 }
124
125 *filter = NULL; // just to be sure
126
127 n = roar_mm_malloc(sizeof(struct roardsp_filter));
128
129 if ( n == NULL )
130  return -1;
131
132 if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) {
133  error = roar_error;
134  roar_mm_free(n);
135  roar_err_set(error);
136  return -1;
137 }
138
139 n->flags |= ROARDSP_FFLAG_FREE;
140
141 *filter = n;
142
143 return ret;
144}
145
146int roardsp_filter_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
147 struct _roardsp_filterlist * l = _roardsp_filterlist;
148 int bytes;
149 int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL;
150
151 if ( filter == NULL || stream == NULL ) {
152  ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL");
153  roar_err_set(ROAR_ERROR_FAULT);
154  return -1;
155 }
156
157 if ( id < 0 ) {
158  roar_err_set(ROAR_ERROR_INVAL);
159  return -1;
160 }
161
162 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
163
164 memset(filter, 0, sizeof(struct roardsp_filter));
165
166 filter->channels = stream->info.channels;
167 filter->bits     = stream->info.bits;
168 filter->rate     = stream->info.rate;
169
170 bytes            = stream->info.bits / 8;
171
172 while ( l->id != id ) {
173  if ( l->id == -1 ) {
174   roar_err_set(ROAR_ERROR_NOENT);
175   return -1;
176  }
177  l++;
178 }
179
180 filter->uninit = l->uninit;
181 filter->ctl    = l->ctl;
182 filter->reset  = l->reset;
183
184 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i): bytes=%i", filter, stream, id, bytes);
185
186 if ( filter->channels < 3 )
187  calc = l->calc[bytes][filter->channels];
188
189 if ( calc == NULL )
190  calc = l->calc[bytes][0]; // for n channels
191
192 if ( calc == NULL ) {
193  ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code");
194  roar_err_set(ROAR_ERROR_NOTSUP);
195  return -1;
196 }
197
198 filter->calc = calc;
199
200 if ( l->init != NULL ) {
201  ROAR_DBG("roardsp_filter_init(*) = ? // execing init");
202  return l->init(filter, stream, id);
203 }
204
205 ROAR_DBG("roardsp_filter_init(*) = 0 // no init");
206 return 0;
207}
208
209int roardsp_filter_uninit(struct roardsp_filter * filter) {
210 int ret = 0;
211
212 if ( filter == NULL ) {
213  roar_err_set(ROAR_ERROR_FAULT);
214  return -1;
215 }
216
217 if ( filter->uninit != NULL )
218  ret = filter->uninit(filter);
219
220 if ( filter->flags & ROARDSP_FFLAG_FREE ) {
221  roar_mm_free(filter);
222 } else  {
223  memset(filter, 0, sizeof(struct roardsp_filter));
224 }
225
226 return ret;
227}
228
229int roardsp_filter_calc  (struct roardsp_filter * filter, void * data, size_t len) {
230 int ret = 0;
231
232 if ( filter == NULL ) {
233  roar_err_set(ROAR_ERROR_FAULT);
234  return -1;
235 }
236
237 if ( data == NULL && len != 0 ) {
238  roar_err_set(ROAR_ERROR_FAULT);
239  return -1;
240 }
241
242 if ( filter->calc != NULL )
243  ret = filter->calc(filter, data, len);
244
245 return ret;
246}
247
248int    roardsp_filter_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
249 if ( filter == NULL ) {
250  roar_err_set(ROAR_ERROR_FAULT);
251  return -1;
252 }
253
254 if ( filter->ctl != NULL )
255  return filter->ctl(filter, cmd, data);
256
257 roar_err_set(ROAR_ERROR_NOSYS);
258 return -1;
259}
260
261int    roardsp_filter_reset (struct roardsp_filter * filter, int what) {
262 if ( filter == NULL ) {
263  roar_err_set(ROAR_ERROR_FAULT);
264  return -1;
265 }
266
267 if ( filter->reset != NULL )
268  return filter->reset(filter, what);
269
270 roar_err_set(ROAR_ERROR_NOSYS);
271 return -1;
272}
273
274//ll
Note: See TracBrowser for help on using the repository browser.