source: roaraudio/libroardsp/filter.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

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