source: roaraudio/libroardsp/filter.c @ 2990:24fadf8a2fd3

Last change on this file since 2990:24fadf8a2fd3 was 2990:24fadf8a2fd3, checked in by phi, 15 years ago

added support for roardsp_filter_new() and roardsp_filter_free()

File size: 6.8 KB
Line 
1//filter.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - August 2008
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
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "libroardsp.h"
26
27struct _roardsp_filterlist {
28 int id;
29 char * name;
30 int (*  init      )(struct roardsp_filter * filter, struct roar_stream * stream, int id);
31 int (*uninit      )(struct roardsp_filter * filter);
32 int (*ctl         )(struct roardsp_filter * filter, int cmd, void * data);
33 int (*reset       )(struct roardsp_filter * filter, int what);
34 int (*calc  [5][3])(struct roardsp_filter * filter, void * data, size_t samples);
35} _roardsp_filterlist[] = {
36 {ROARDSP_FILTER_AMP, "AMP", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_amp_reset, {
37           {NULL, NULL, NULL},{roardsp_amp_calc8, NULL, NULL},{roardsp_amp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
38 {ROARDSP_FILTER_ADD, "Add", roardsp_add_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_add_reset, {
39           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_add_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
40#ifdef ROAR_HAVE_LIBM
41 {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, {
42           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
43 {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, {
44           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
45#endif
46 {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, {
47           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
48 {ROARDSP_FILTER_CLIP, "Clip", roardsp_quantify_init, NULL, roardsp_clip_ctl, roardsp_clip_reset, {
49           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
50 {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, {
51           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
52 {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, {
53           {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
54 {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, {
55           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_swap_calc162, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}},
56 {-1, NULL, NULL, NULL, NULL, NULL, {
57      // ?                  8Bit               16Bit              24Bit              32Bit
58      // 0B:n     1     2   1B:n     1     2   2B:n     1     2   3B:n     1    2    4B:n     1     2
59           {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}
60};
61
62int    roardsp_filter_str2id(char * str) {
63 struct _roardsp_filterlist * l = _roardsp_filterlist;
64
65 while ( l->id != -1 ) {
66  if ( strcasecmp(l->name, str) == 0 )
67   return l->id;
68  l++;
69 }
70
71 return -1;
72}
73
74char * roardsp_filter_id2str(int id) {
75 struct _roardsp_filterlist * l = _roardsp_filterlist;
76
77 while ( l->id != -1 ) {
78  if ( l->id == id )
79   return l->name;
80  l++;
81 }
82
83 return NULL;
84}
85
86int    roardsp_filter_new   (struct roardsp_filter ** filter, struct roar_stream * stream, int id) {
87 struct roardsp_filter * n;
88 int ret;
89
90 if ( filter == NULL || stream == NULL )
91  return -1;
92
93 *filter = NULL; // just to be sure
94
95 n = roar_mm_malloc(sizeof(struct roardsp_filter));
96
97 if ( n == NULL )
98  return -1;
99
100 if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) {
101  roar_mm_free(n);
102  return -1;
103 }
104
105 n->flags |= ROARDSP_FFLAG_FREE;
106
107 *filter = n;
108
109 return ret;
110}
111
112int roardsp_filter_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
113 struct _roardsp_filterlist * l = _roardsp_filterlist;
114 int bytes;
115 int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL;
116
117 if ( filter == NULL || stream == NULL ) {
118  ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL");
119  return -1;
120 }
121
122 ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id);
123
124 memset(filter, 0, sizeof(struct roardsp_filter));
125
126 filter->channels = stream->info.channels;
127 filter->bits     = stream->info.bits;
128 filter->rate     = stream->info.rate;
129
130 bytes            = stream->info.bits / 8;
131
132 while ( l->id != id ) {
133  if ( l->id == -1 )
134   return -1;
135  l++;
136 }
137
138 filter->uninit = l->uninit;
139 filter->ctl    = l->ctl;
140 filter->reset  = l->reset;
141
142 if ( filter->channels < 3 )
143  calc = l->calc[bytes][filter->channels];
144
145 if ( calc == NULL )
146  calc = l->calc[bytes][0]; // for n channels
147
148 if ( calc == NULL ) {
149  ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code");
150  return -1;
151 }
152
153 filter->calc = calc;
154
155 if ( l->init ) {
156  ROAR_DBG("roardsp_filter_init(*) = ? // execing init");
157  return l->init(filter, stream, id);
158 }
159
160 ROAR_DBG("roardsp_filter_init(*) = 0 // no init");
161 return 0;
162}
163
164int roardsp_filter_uninit(struct roardsp_filter * filter) {
165 int ret = 0;
166
167 if ( filter == NULL )
168  return -1;
169
170 if ( filter->uninit )
171  ret = filter->uninit(filter);
172
173 if ( filter->flags & ROARDSP_FFLAG_FREE ) {
174  roar_mm_free(filter);
175 } else  {
176  memset(filter, 0, sizeof(struct roardsp_filter));
177 }
178
179 return ret;
180}
181
182int roardsp_filter_calc  (struct roardsp_filter * filter, void * data, size_t len) {
183 int ret = 0;
184
185 if ( filter == NULL )
186  return -1;
187
188 if ( filter->calc )
189  ret = filter->calc(filter, data, len);
190
191 return ret;
192}
193
194int    roardsp_filter_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
195 if ( filter == NULL )
196  return -1;
197
198 if ( filter->ctl )
199  return filter->ctl(filter, cmd, data);
200
201 return -1;
202}
203
204int    roardsp_filter_reset (struct roardsp_filter * filter, int what) {
205 if ( filter == NULL )
206  return -1;
207
208 if ( filter->reset )
209  return filter->reset(filter, what);
210
211 return -1;
212}
213
214//ll
Note: See TracBrowser for help on using the repository browser.