1 | //filter.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "libroardsp.h" |
---|
27 | |
---|
28 | struct _roardsp_filterlist { |
---|
29 | int id; |
---|
30 | char * name; |
---|
31 | int (* init )(struct roardsp_filter * filter, struct roar_stream * stream, int id); |
---|
32 | int (*uninit )(struct roardsp_filter * filter); |
---|
33 | int (*ctl )(struct roardsp_filter * filter, int cmd, void * data); |
---|
34 | int (*reset )(struct roardsp_filter * filter, int what); |
---|
35 | int (*calc [5][3])(struct roardsp_filter * filter, void * data, size_t samples); |
---|
36 | } _roardsp_filterlist[] = { |
---|
37 | {ROARDSP_FILTER_AMP, "AMP", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_amp_reset, { |
---|
38 | {NULL, NULL, NULL},{roardsp_amp_calc8, NULL, NULL},{roardsp_amp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
39 | {ROARDSP_FILTER_ADD, "Add", roardsp_add_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_add_reset, { |
---|
40 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_add_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
41 | #ifdef ROAR_HAVE_LIBM |
---|
42 | {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, { |
---|
43 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
44 | {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, { |
---|
45 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
46 | #endif |
---|
47 | {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, { |
---|
48 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
49 | {ROARDSP_FILTER_CLIP, "Clip", roardsp_quantify_init, NULL, roardsp_clip_ctl, roardsp_clip_reset, { |
---|
50 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
51 | {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, { |
---|
52 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
53 | {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, { |
---|
54 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
55 | {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, { |
---|
56 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
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}}}, |
---|
59 | #ifdef ROAR_HAVE_SPEEX_FILTER |
---|
60 | {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit, |
---|
61 | roardsp_speex_prep_ctl, roardsp_speex_prep_reset, { |
---|
62 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
63 | #endif |
---|
64 | {-1, NULL, NULL, NULL, NULL, NULL, { |
---|
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 |
---|
67 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}} |
---|
68 | }; |
---|
69 | |
---|
70 | int 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; |
---|
76 | l++; |
---|
77 | } |
---|
78 | |
---|
79 | return -1; |
---|
80 | } |
---|
81 | |
---|
82 | char * 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; |
---|
88 | l++; |
---|
89 | } |
---|
90 | |
---|
91 | return NULL; |
---|
92 | } |
---|
93 | |
---|
94 | int 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 | |
---|
120 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
121 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
122 | int bytes; |
---|
123 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
124 | |
---|
125 | if ( filter == NULL || stream == NULL ) { |
---|
126 | ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL"); |
---|
127 | return -1; |
---|
128 | } |
---|
129 | |
---|
130 | ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id); |
---|
131 | |
---|
132 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
133 | |
---|
134 | filter->channels = stream->info.channels; |
---|
135 | filter->bits = stream->info.bits; |
---|
136 | filter->rate = stream->info.rate; |
---|
137 | |
---|
138 | bytes = stream->info.bits / 8; |
---|
139 | |
---|
140 | while ( l->id != id ) { |
---|
141 | if ( l->id == -1 ) |
---|
142 | return -1; |
---|
143 | l++; |
---|
144 | } |
---|
145 | |
---|
146 | filter->uninit = l->uninit; |
---|
147 | filter->ctl = l->ctl; |
---|
148 | filter->reset = l->reset; |
---|
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 | |
---|
156 | if ( calc == NULL ) { |
---|
157 | ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code"); |
---|
158 | return -1; |
---|
159 | } |
---|
160 | |
---|
161 | filter->calc = calc; |
---|
162 | |
---|
163 | if ( l->init ) { |
---|
164 | ROAR_DBG("roardsp_filter_init(*) = ? // execing init"); |
---|
165 | return l->init(filter, stream, id); |
---|
166 | } |
---|
167 | |
---|
168 | ROAR_DBG("roardsp_filter_init(*) = 0 // no init"); |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
173 | int ret = 0; |
---|
174 | |
---|
175 | if ( filter == NULL ) |
---|
176 | return -1; |
---|
177 | |
---|
178 | if ( filter->uninit ) |
---|
179 | ret = filter->uninit(filter); |
---|
180 | |
---|
181 | if ( filter->flags & ROARDSP_FFLAG_FREE ) { |
---|
182 | roar_mm_free(filter); |
---|
183 | } else { |
---|
184 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
185 | } |
---|
186 | |
---|
187 | return ret; |
---|
188 | } |
---|
189 | |
---|
190 | int 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 | |
---|
196 | if ( data == NULL && len != 0 ) |
---|
197 | return -1; |
---|
198 | |
---|
199 | if ( filter->calc ) |
---|
200 | ret = filter->calc(filter, data, len); |
---|
201 | |
---|
202 | return ret; |
---|
203 | } |
---|
204 | |
---|
205 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
206 | if ( filter == NULL ) |
---|
207 | return -1; |
---|
208 | |
---|
209 | if ( filter->ctl ) |
---|
210 | return filter->ctl(filter, cmd, data); |
---|
211 | |
---|
212 | return -1; |
---|
213 | } |
---|
214 | |
---|
215 | int roardsp_filter_reset (struct roardsp_filter * filter, int what) { |
---|
216 | if ( filter == NULL ) |
---|
217 | return -1; |
---|
218 | |
---|
219 | if ( filter->reset ) |
---|
220 | return filter->reset(filter, what); |
---|
221 | |
---|
222 | return -1; |
---|
223 | } |
---|
224 | |
---|
225 | //ll |
---|