source: roaraudio/libroardsp/filter.c @ 5381:430b1d26e12d

Last change on this file since 5381:430b1d26e12d was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

File size: 8.9 KB
RevLine 
[661]1//filter.c:
2
3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
[661]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[661]23 *
24 */
25
26#include "libroardsp.h"
27
[5270]28static struct _roardsp_filterlist {
[665]29 int id;
30 char * name;
[667]31 int (*  init      )(struct roardsp_filter * filter, struct roar_stream * stream, int id);
32 int (*uninit      )(struct roardsp_filter * filter);
[671]33 int (*ctl         )(struct roardsp_filter * filter, int cmd, void * data);
[1131]34 int (*reset       )(struct roardsp_filter * filter, int what);
[667]35 int (*calc  [5][3])(struct roardsp_filter * filter, void * data, size_t samples);
[665]36} _roardsp_filterlist[] = {
[1141]37 {ROARDSP_FILTER_AMP, "AMP", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_amp_reset, {
[5181]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}}},
[1104]43#ifdef ROAR_HAVE_LIBM
[1141]44 {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, {
[5181]45           {NULL, NULL, NULL},{roardsp_lowp_calc8, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},
46           {NULL, NULL, NULL},{roardsp_lowp_calc32, NULL, NULL}}},
[1141]47 {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, {
[5181]48           {NULL, NULL, NULL},{roardsp_highp_calc8, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},
49           {NULL, NULL, NULL},{roardsp_highp_calc32, NULL, NULL}}},
[1104]50#endif
[1141]51 {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, {
[5181]52           {NULL, NULL, NULL},{roardsp_quantify_calc8, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},
53           {NULL, NULL, NULL},{roardsp_quantify_calc32, NULL, NULL}}},
[5173]54 {ROARDSP_FILTER_CLIP, "Clip", roardsp_clip_init, roardsp_clip_uninit, roardsp_clip_ctl, roardsp_clip_reset, {
[5177]55           {NULL, NULL, NULL},{roardsp_clip_calc8, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},
56           {NULL, NULL, NULL},{roardsp_clip_calc32, NULL, NULL}}},
[1141]57 {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, {
[1004]58           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1131]59 {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, {
[1100]60           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1587]61 {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, {
[5181]62           {NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc82},{NULL, NULL, roardsp_swap_calc162},
63           {NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc322}}},
[3010]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}}},
[3029]66#ifdef ROAR_HAVE_SPEEX_FILTER
[2992]67 {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit,
68                                          roardsp_speex_prep_ctl,  roardsp_speex_prep_reset, {
[2998]69           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[3029]70#endif
[5362]71#ifdef ROAR_HAVE_LIBM
[5269]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 }},
[5362]77#endif
[1131]78 {-1, NULL, NULL, NULL, NULL, NULL, {
[677]79      // ?                  8Bit               16Bit              24Bit              32Bit
80      // 0B:n     1     2   1B:n     1     2   2B:n     1     2   3B:n     1    2    4B:n     1     2
[671]81           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}
[665]82};
83
[5269]84int    roardsp_filter_str2id(const char * str) {
[665]85 struct _roardsp_filterlist * l = _roardsp_filterlist;
86
87 while ( l->id != -1 ) {
88  if ( strcasecmp(l->name, str) == 0 )
89   return l->id;
[678]90  l++;
[665]91 }
92
[5171]93 roar_err_set(ROAR_ERROR_NOENT);
[665]94 return -1;
95}
96
[5269]97const char * roardsp_filter_id2str(const int id) {
[665]98 struct _roardsp_filterlist * l = _roardsp_filterlist;
99
100 while ( l->id != -1 ) {
101  if ( l->id == id )
102   return l->name;
[678]103  l++;
[665]104 }
105
[5171]106 roar_err_set(ROAR_ERROR_NOENT);
[665]107 return NULL;
108}
109
[2990]110int    roardsp_filter_new   (struct roardsp_filter ** filter, struct roar_stream * stream, int id) {
111 struct roardsp_filter * n;
112 int ret;
[5171]113 int error;
[2990]114
[5171]115 if ( filter == NULL || stream == NULL ) {
116  roar_err_set(ROAR_ERROR_FAULT);
[2990]117  return -1;
[5171]118 }
[2990]119
120 *filter = NULL; // just to be sure
121
122 n = roar_mm_malloc(sizeof(struct roardsp_filter));
123
124 if ( n == NULL )
125  return -1;
126
127 if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) {
[5171]128  error = roar_error;
[2990]129  roar_mm_free(n);
[5171]130  roar_err_set(error);
[2990]131  return -1;
132 }
133
134 n->flags |= ROARDSP_FFLAG_FREE;
135
136 *filter = n;
137
138 return ret;
139}
140
[663]141int roardsp_filter_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
[671]142 struct _roardsp_filterlist * l = _roardsp_filterlist;
143 int bytes;
144 int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL;
145
[681]146 if ( filter == NULL || stream == NULL ) {
147  ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL");
[5171]148  roar_err_set(ROAR_ERROR_FAULT);
149  return -1;
150 }
151
152 if ( id < 0 ) {
153  roar_err_set(ROAR_ERROR_INVAL);
[663]154  return -1;
[681]155 }
156
157 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
[663]158
159 memset(filter, 0, sizeof(struct roardsp_filter));
160
161 filter->channels = stream->info.channels;
162 filter->bits     = stream->info.bits;
[673]163 filter->rate     = stream->info.rate;
[663]164
[671]165 bytes            = stream->info.bits / 8;
166
[678]167 while ( l->id != id ) {
[5171]168  if ( l->id == -1 ) {
169   roar_err_set(ROAR_ERROR_NOENT);
[671]170   return -1;
[5171]171  }
[678]172  l++;
173 }
[671]174
175 filter->uninit = l->uninit;
176 filter->ctl    = l->ctl;
[1134]177 filter->reset  = l->reset;
[671]178
[5269]179 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i): bytes=%i", filter, stream, id, bytes);
180
[671]181 if ( filter->channels < 3 )
182  calc = l->calc[bytes][filter->channels];
183
184 if ( calc == NULL )
185  calc = l->calc[bytes][0]; // for n channels
186
[681]187 if ( calc == NULL ) {
188  ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code");
[5171]189  roar_err_set(ROAR_ERROR_NOTSUP);
[671]190  return -1;
[681]191 }
192
193 filter->calc = calc;
[671]194
[4601]195 if ( l->init != NULL ) {
[681]196  ROAR_DBG("roardsp_filter_init(*) = ? // execing init");
[671]197  return l->init(filter, stream, id);
[681]198 }
[671]199
[681]200 ROAR_DBG("roardsp_filter_init(*) = 0 // no init");
[671]201 return 0;
[663]202}
203
204int roardsp_filter_uninit(struct roardsp_filter * filter) {
205 int ret = 0;
206
[5171]207 if ( filter == NULL ) {
208  roar_err_set(ROAR_ERROR_FAULT);
[663]209  return -1;
[5171]210 }
[663]211
[4601]212 if ( filter->uninit != NULL )
[663]213  ret = filter->uninit(filter);
214
[2990]215 if ( filter->flags & ROARDSP_FFLAG_FREE ) {
216  roar_mm_free(filter);
217 } else  {
218  memset(filter, 0, sizeof(struct roardsp_filter));
219 }
[663]220
221 return ret;
222}
223
224int roardsp_filter_calc  (struct roardsp_filter * filter, void * data, size_t len) {
225 int ret = 0;
226
[5171]227 if ( filter == NULL ) {
228  roar_err_set(ROAR_ERROR_FAULT);
[663]229  return -1;
[5171]230 }
[663]231
[5171]232 if ( data == NULL && len != 0 ) {
233  roar_err_set(ROAR_ERROR_FAULT);
[3002]234  return -1;
[5171]235 }
[3002]236
[4601]237 if ( filter->calc != NULL )
[663]238  ret = filter->calc(filter, data, len);
239
240 return ret;
241}
[661]242
[671]243int    roardsp_filter_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
[5171]244 if ( filter == NULL ) {
245  roar_err_set(ROAR_ERROR_FAULT);
[671]246  return -1;
[5171]247 }
[671]248
[4601]249 if ( filter->ctl != NULL )
[671]250  return filter->ctl(filter, cmd, data);
251
[5171]252 roar_err_set(ROAR_ERROR_NOSYS);
[671]253 return -1;
254}
255
[1131]256int    roardsp_filter_reset (struct roardsp_filter * filter, int what) {
[5171]257 if ( filter == NULL ) {
258  roar_err_set(ROAR_ERROR_FAULT);
[1131]259  return -1;
[5171]260 }
[1131]261
[4601]262 if ( filter->reset != NULL )
[1131]263  return filter->reset(filter, what);
264
[5171]265 roar_err_set(ROAR_ERROR_NOSYS);
[1131]266 return -1;
267}
268
[661]269//ll
Note: See TracBrowser for help on using the repository browser.