source: roaraudio/libroardsp/filter.c @ 5171:7c5491b0c607

Last change on this file since 5171:7c5491b0c607 was 5171:7c5491b0c607, checked in by phi, 13 years ago

improved error handling

File size: 8.0 KB
Line 
1//filter.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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
28struct _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},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
39 {ROARDSP_FILTER_ADD, "Add", roardsp_add_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_add_reset, {
40           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_add_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
41#ifdef ROAR_HAVE_LIBM
42 {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, {
43           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
44 {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, {
45           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
46#endif
47 {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, {
48           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
49 {ROARDSP_FILTER_CLIP, "Clip", roardsp_quantify_init, NULL, roardsp_clip_ctl, roardsp_clip_reset, {
50           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
51 {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, {
52           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
53 {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, {
54           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
55 {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, {
56           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
57 {ROARDSP_FILTER_AGC, "AGC", roardsp_agc_init, roardsp_agc_uninit, roardsp_agc_ctl, roardsp_agc_reset, {
58           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
59#ifdef ROAR_HAVE_SPEEX_FILTER
60 {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit,
61                                          roardsp_speex_prep_ctl,  roardsp_speex_prep_reset, {
62           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
63#endif
64 {-1, NULL, NULL, NULL, NULL, NULL, {
65      // ?                  8Bit               16Bit              24Bit              32Bit
66      // 0B:n     1     2   1B:n     1     2   2B:n     1     2   3B:n     1    2    4B:n     1     2
67           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}
68};
69
70int    roardsp_filter_str2id(char * str) {
71 struct _roardsp_filterlist * l = _roardsp_filterlist;
72
73 while ( l->id != -1 ) {
74  if ( strcasecmp(l->name, str) == 0 )
75   return l->id;
76  l++;
77 }
78
79 roar_err_set(ROAR_ERROR_NOENT);
80 return -1;
81}
82
83char * roardsp_filter_id2str(int id) {
84 struct _roardsp_filterlist * l = _roardsp_filterlist;
85
86 while ( l->id != -1 ) {
87  if ( l->id == id )
88   return l->name;
89  l++;
90 }
91
92 roar_err_set(ROAR_ERROR_NOENT);
93 return NULL;
94}
95
96int    roardsp_filter_new   (struct roardsp_filter ** filter, struct roar_stream * stream, int id) {
97 struct roardsp_filter * n;
98 int ret;
99 int error;
100
101 if ( filter == NULL || stream == NULL ) {
102  roar_err_set(ROAR_ERROR_FAULT);
103  return -1;
104 }
105
106 *filter = NULL; // just to be sure
107
108 n = roar_mm_malloc(sizeof(struct roardsp_filter));
109
110 if ( n == NULL )
111  return -1;
112
113 if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) {
114  error = roar_error;
115  roar_mm_free(n);
116  roar_err_set(error);
117  return -1;
118 }
119
120 n->flags |= ROARDSP_FFLAG_FREE;
121
122 *filter = n;
123
124 return ret;
125}
126
127int roardsp_filter_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
128 struct _roardsp_filterlist * l = _roardsp_filterlist;
129 int bytes;
130 int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL;
131
132 if ( filter == NULL || stream == NULL ) {
133  ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL");
134  roar_err_set(ROAR_ERROR_FAULT);
135  return -1;
136 }
137
138 if ( id < 0 ) {
139  roar_err_set(ROAR_ERROR_INVAL);
140  return -1;
141 }
142
143 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
144
145 memset(filter, 0, sizeof(struct roardsp_filter));
146
147 filter->channels = stream->info.channels;
148 filter->bits     = stream->info.bits;
149 filter->rate     = stream->info.rate;
150
151 bytes            = stream->info.bits / 8;
152
153 while ( l->id != id ) {
154  if ( l->id == -1 ) {
155   roar_err_set(ROAR_ERROR_NOENT);
156   return -1;
157  }
158  l++;
159 }
160
161 filter->uninit = l->uninit;
162 filter->ctl    = l->ctl;
163 filter->reset  = l->reset;
164
165 if ( filter->channels < 3 )
166  calc = l->calc[bytes][filter->channels];
167
168 if ( calc == NULL )
169  calc = l->calc[bytes][0]; // for n channels
170
171 if ( calc == NULL ) {
172  ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code");
173  roar_err_set(ROAR_ERROR_NOTSUP);
174  return -1;
175 }
176
177 filter->calc = calc;
178
179 if ( l->init != NULL ) {
180  ROAR_DBG("roardsp_filter_init(*) = ? // execing init");
181  return l->init(filter, stream, id);
182 }
183
184 ROAR_DBG("roardsp_filter_init(*) = 0 // no init");
185 return 0;
186}
187
188int roardsp_filter_uninit(struct roardsp_filter * filter) {
189 int ret = 0;
190
191 if ( filter == NULL ) {
192  roar_err_set(ROAR_ERROR_FAULT);
193  return -1;
194 }
195
196 if ( filter->uninit != NULL )
197  ret = filter->uninit(filter);
198
199 if ( filter->flags & ROARDSP_FFLAG_FREE ) {
200  roar_mm_free(filter);
201 } else  {
202  memset(filter, 0, sizeof(struct roardsp_filter));
203 }
204
205 return ret;
206}
207
208int roardsp_filter_calc  (struct roardsp_filter * filter, void * data, size_t len) {
209 int ret = 0;
210
211 if ( filter == NULL ) {
212  roar_err_set(ROAR_ERROR_FAULT);
213  return -1;
214 }
215
216 if ( data == NULL && len != 0 ) {
217  roar_err_set(ROAR_ERROR_FAULT);
218  return -1;
219 }
220
221 if ( filter->calc != NULL )
222  ret = filter->calc(filter, data, len);
223
224 return ret;
225}
226
227int    roardsp_filter_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
228 if ( filter == NULL ) {
229  roar_err_set(ROAR_ERROR_FAULT);
230  return -1;
231 }
232
233 if ( filter->ctl != NULL )
234  return filter->ctl(filter, cmd, data);
235
236 roar_err_set(ROAR_ERROR_NOSYS);
237 return -1;
238}
239
240int    roardsp_filter_reset (struct roardsp_filter * filter, int what) {
241 if ( filter == NULL ) {
242  roar_err_set(ROAR_ERROR_FAULT);
243  return -1;
244 }
245
246 if ( filter->reset != NULL )
247  return filter->reset(filter, what);
248
249 roar_err_set(ROAR_ERROR_NOSYS);
250 return -1;
251}
252
253//ll
Note: See TracBrowser for help on using the repository browser.