source: roaraudio/libroardsp/filter.c @ 5242:97239101cee9

Last change on this file since 5242:97239101cee9 was 5181:ca57e34b2a35, checked in by phi, 13 years ago

updated filters to support 8 and 32 bit as well as 16 bit

File size: 8.3 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, {
[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
[1131]71 {-1, NULL, NULL, NULL, NULL, NULL, {
[677]72      // ?                  8Bit               16Bit              24Bit              32Bit
73      // 0B:n     1     2   1B:n     1     2   2B:n     1     2   3B:n     1    2    4B:n     1     2
[671]74           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}
[665]75};
76
77int    roardsp_filter_str2id(char * str) {
78 struct _roardsp_filterlist * l = _roardsp_filterlist;
79
80 while ( l->id != -1 ) {
81  if ( strcasecmp(l->name, str) == 0 )
82   return l->id;
[678]83  l++;
[665]84 }
85
[5171]86 roar_err_set(ROAR_ERROR_NOENT);
[665]87 return -1;
88}
89
90char * roardsp_filter_id2str(int id) {
91 struct _roardsp_filterlist * l = _roardsp_filterlist;
92
93 while ( l->id != -1 ) {
94  if ( l->id == id )
95   return l->name;
[678]96  l++;
[665]97 }
98
[5171]99 roar_err_set(ROAR_ERROR_NOENT);
[665]100 return NULL;
101}
102
[2990]103int    roardsp_filter_new   (struct roardsp_filter ** filter, struct roar_stream * stream, int id) {
104 struct roardsp_filter * n;
105 int ret;
[5171]106 int error;
[2990]107
[5171]108 if ( filter == NULL || stream == NULL ) {
109  roar_err_set(ROAR_ERROR_FAULT);
[2990]110  return -1;
[5171]111 }
[2990]112
113 *filter = NULL; // just to be sure
114
115 n = roar_mm_malloc(sizeof(struct roardsp_filter));
116
117 if ( n == NULL )
118  return -1;
119
120 if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) {
[5171]121  error = roar_error;
[2990]122  roar_mm_free(n);
[5171]123  roar_err_set(error);
[2990]124  return -1;
125 }
126
127 n->flags |= ROARDSP_FFLAG_FREE;
128
129 *filter = n;
130
131 return ret;
132}
133
[663]134int roardsp_filter_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
[671]135 struct _roardsp_filterlist * l = _roardsp_filterlist;
136 int bytes;
137 int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL;
138
[681]139 if ( filter == NULL || stream == NULL ) {
140  ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL");
[5171]141  roar_err_set(ROAR_ERROR_FAULT);
142  return -1;
143 }
144
145 if ( id < 0 ) {
146  roar_err_set(ROAR_ERROR_INVAL);
[663]147  return -1;
[681]148 }
149
150 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
[663]151
152 memset(filter, 0, sizeof(struct roardsp_filter));
153
154 filter->channels = stream->info.channels;
155 filter->bits     = stream->info.bits;
[673]156 filter->rate     = stream->info.rate;
[663]157
[671]158 bytes            = stream->info.bits / 8;
159
[678]160 while ( l->id != id ) {
[5171]161  if ( l->id == -1 ) {
162   roar_err_set(ROAR_ERROR_NOENT);
[671]163   return -1;
[5171]164  }
[678]165  l++;
166 }
[671]167
168 filter->uninit = l->uninit;
169 filter->ctl    = l->ctl;
[1134]170 filter->reset  = l->reset;
[671]171
172 if ( filter->channels < 3 )
173  calc = l->calc[bytes][filter->channels];
174
175 if ( calc == NULL )
176  calc = l->calc[bytes][0]; // for n channels
177
[681]178 if ( calc == NULL ) {
179  ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code");
[5171]180  roar_err_set(ROAR_ERROR_NOTSUP);
[671]181  return -1;
[681]182 }
183
184 filter->calc = calc;
[671]185
[4601]186 if ( l->init != NULL ) {
[681]187  ROAR_DBG("roardsp_filter_init(*) = ? // execing init");
[671]188  return l->init(filter, stream, id);
[681]189 }
[671]190
[681]191 ROAR_DBG("roardsp_filter_init(*) = 0 // no init");
[671]192 return 0;
[663]193}
194
195int roardsp_filter_uninit(struct roardsp_filter * filter) {
196 int ret = 0;
197
[5171]198 if ( filter == NULL ) {
199  roar_err_set(ROAR_ERROR_FAULT);
[663]200  return -1;
[5171]201 }
[663]202
[4601]203 if ( filter->uninit != NULL )
[663]204  ret = filter->uninit(filter);
205
[2990]206 if ( filter->flags & ROARDSP_FFLAG_FREE ) {
207  roar_mm_free(filter);
208 } else  {
209  memset(filter, 0, sizeof(struct roardsp_filter));
210 }
[663]211
212 return ret;
213}
214
215int roardsp_filter_calc  (struct roardsp_filter * filter, void * data, size_t len) {
216 int ret = 0;
217
[5171]218 if ( filter == NULL ) {
219  roar_err_set(ROAR_ERROR_FAULT);
[663]220  return -1;
[5171]221 }
[663]222
[5171]223 if ( data == NULL && len != 0 ) {
224  roar_err_set(ROAR_ERROR_FAULT);
[3002]225  return -1;
[5171]226 }
[3002]227
[4601]228 if ( filter->calc != NULL )
[663]229  ret = filter->calc(filter, data, len);
230
231 return ret;
232}
[661]233
[671]234int    roardsp_filter_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
[5171]235 if ( filter == NULL ) {
236  roar_err_set(ROAR_ERROR_FAULT);
[671]237  return -1;
[5171]238 }
[671]239
[4601]240 if ( filter->ctl != NULL )
[671]241  return filter->ctl(filter, cmd, data);
242
[5171]243 roar_err_set(ROAR_ERROR_NOSYS);
[671]244 return -1;
245}
246
[1131]247int    roardsp_filter_reset (struct roardsp_filter * filter, int what) {
[5171]248 if ( filter == NULL ) {
249  roar_err_set(ROAR_ERROR_FAULT);
[1131]250  return -1;
[5171]251 }
[1131]252
[4601]253 if ( filter->reset != NULL )
[1131]254  return filter->reset(filter, what);
255
[5171]256 roar_err_set(ROAR_ERROR_NOSYS);
[1131]257 return -1;
258}
259
[661]260//ll
Note: See TracBrowser for help on using the repository browser.