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
RevLine 
[661]1//filter.c:
2
3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[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
[665]28struct _roardsp_filterlist {
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, {
[882]38           {NULL, NULL, NULL},{roardsp_amp_calc8, NULL, NULL},{roardsp_amp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1141]39 {ROARDSP_FILTER_ADD, "Add", roardsp_add_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_add_reset, {
[979]40           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_add_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1104]41#ifdef ROAR_HAVE_LIBM
[1141]42 {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, {
[677]43           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1141]44 {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, {
[683]45           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1104]46#endif
[1141]47 {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, {
[979]48           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1141]49 {ROARDSP_FILTER_CLIP, "Clip", roardsp_quantify_init, NULL, roardsp_clip_ctl, roardsp_clip_reset, {
[979]50           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1141]51 {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, {
[1004]52           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1131]53 {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, {
[1100]54           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[1587]55 {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, {
[2999]56           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[3010]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}}},
[3029]59#ifdef ROAR_HAVE_SPEEX_FILTER
[2992]60 {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit,
61                                          roardsp_speex_prep_ctl,  roardsp_speex_prep_reset, {
[2998]62           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
[3029]63#endif
[1131]64 {-1, NULL, NULL, NULL, NULL, NULL, {
[677]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
[671]67           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}
[665]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;
[678]76  l++;
[665]77 }
78
[5171]79 roar_err_set(ROAR_ERROR_NOENT);
[665]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;
[678]89  l++;
[665]90 }
91
[5171]92 roar_err_set(ROAR_ERROR_NOENT);
[665]93 return NULL;
94}
95
[2990]96int    roardsp_filter_new   (struct roardsp_filter ** filter, struct roar_stream * stream, int id) {
97 struct roardsp_filter * n;
98 int ret;
[5171]99 int error;
[2990]100
[5171]101 if ( filter == NULL || stream == NULL ) {
102  roar_err_set(ROAR_ERROR_FAULT);
[2990]103  return -1;
[5171]104 }
[2990]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 ) {
[5171]114  error = roar_error;
[2990]115  roar_mm_free(n);
[5171]116  roar_err_set(error);
[2990]117  return -1;
118 }
119
120 n->flags |= ROARDSP_FFLAG_FREE;
121
122 *filter = n;
123
124 return ret;
125}
126
[663]127int roardsp_filter_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
[671]128 struct _roardsp_filterlist * l = _roardsp_filterlist;
129 int bytes;
130 int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL;
131
[681]132 if ( filter == NULL || stream == NULL ) {
133  ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL");
[5171]134  roar_err_set(ROAR_ERROR_FAULT);
135  return -1;
136 }
137
138 if ( id < 0 ) {
139  roar_err_set(ROAR_ERROR_INVAL);
[663]140  return -1;
[681]141 }
142
143 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
[663]144
145 memset(filter, 0, sizeof(struct roardsp_filter));
146
147 filter->channels = stream->info.channels;
148 filter->bits     = stream->info.bits;
[673]149 filter->rate     = stream->info.rate;
[663]150
[671]151 bytes            = stream->info.bits / 8;
152
[678]153 while ( l->id != id ) {
[5171]154  if ( l->id == -1 ) {
155   roar_err_set(ROAR_ERROR_NOENT);
[671]156   return -1;
[5171]157  }
[678]158  l++;
159 }
[671]160
161 filter->uninit = l->uninit;
162 filter->ctl    = l->ctl;
[1134]163 filter->reset  = l->reset;
[671]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
[681]171 if ( calc == NULL ) {
172  ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code");
[5171]173  roar_err_set(ROAR_ERROR_NOTSUP);
[671]174  return -1;
[681]175 }
176
177 filter->calc = calc;
[671]178
[4601]179 if ( l->init != NULL ) {
[681]180  ROAR_DBG("roardsp_filter_init(*) = ? // execing init");
[671]181  return l->init(filter, stream, id);
[681]182 }
[671]183
[681]184 ROAR_DBG("roardsp_filter_init(*) = 0 // no init");
[671]185 return 0;
[663]186}
187
188int roardsp_filter_uninit(struct roardsp_filter * filter) {
189 int ret = 0;
190
[5171]191 if ( filter == NULL ) {
192  roar_err_set(ROAR_ERROR_FAULT);
[663]193  return -1;
[5171]194 }
[663]195
[4601]196 if ( filter->uninit != NULL )
[663]197  ret = filter->uninit(filter);
198
[2990]199 if ( filter->flags & ROARDSP_FFLAG_FREE ) {
200  roar_mm_free(filter);
201 } else  {
202  memset(filter, 0, sizeof(struct roardsp_filter));
203 }
[663]204
205 return ret;
206}
207
208int roardsp_filter_calc  (struct roardsp_filter * filter, void * data, size_t len) {
209 int ret = 0;
210
[5171]211 if ( filter == NULL ) {
212  roar_err_set(ROAR_ERROR_FAULT);
[663]213  return -1;
[5171]214 }
[663]215
[5171]216 if ( data == NULL && len != 0 ) {
217  roar_err_set(ROAR_ERROR_FAULT);
[3002]218  return -1;
[5171]219 }
[3002]220
[4601]221 if ( filter->calc != NULL )
[663]222  ret = filter->calc(filter, data, len);
223
224 return ret;
225}
[661]226
[671]227int    roardsp_filter_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
[5171]228 if ( filter == NULL ) {
229  roar_err_set(ROAR_ERROR_FAULT);
[671]230  return -1;
[5171]231 }
[671]232
[4601]233 if ( filter->ctl != NULL )
[671]234  return filter->ctl(filter, cmd, data);
235
[5171]236 roar_err_set(ROAR_ERROR_NOSYS);
[671]237 return -1;
238}
239
[1131]240int    roardsp_filter_reset (struct roardsp_filter * filter, int what) {
[5171]241 if ( filter == NULL ) {
242  roar_err_set(ROAR_ERROR_FAULT);
[1131]243  return -1;
[5171]244 }
[1131]245
[4601]246 if ( filter->reset != NULL )
[1131]247  return filter->reset(filter, what);
248
[5171]249 roar_err_set(ROAR_ERROR_NOSYS);
[1131]250 return -1;
251}
252
[661]253//ll
Note: See TracBrowser for help on using the repository browser.