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