source: roaraudio/libroardsp/filter.c @ 5177:49fc5113e053

Last change on this file since 5177:49fc5113e053 was 5177:49fc5113e053, checked in by phi, 13 years ago

added 8 and 32 bit support

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