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 | |
---|
27 | struct _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, NULL, roardsp_swap_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
56 | {ROARDSP_FILTER_AGC, "AGC", roardsp_agc_init, roardsp_agc_uninit, roardsp_agc_ctl, roardsp_agc_reset, { |
---|
57 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
58 | {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit, |
---|
59 | roardsp_speex_prep_ctl, roardsp_speex_prep_reset, { |
---|
60 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
61 | {-1, NULL, NULL, NULL, NULL, NULL, { |
---|
62 | // ? 8Bit 16Bit 24Bit 32Bit |
---|
63 | // 0B:n 1 2 1B:n 1 2 2B:n 1 2 3B:n 1 2 4B:n 1 2 |
---|
64 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}} |
---|
65 | }; |
---|
66 | |
---|
67 | int roardsp_filter_str2id(char * str) { |
---|
68 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
69 | |
---|
70 | while ( l->id != -1 ) { |
---|
71 | if ( strcasecmp(l->name, str) == 0 ) |
---|
72 | return l->id; |
---|
73 | l++; |
---|
74 | } |
---|
75 | |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | char * roardsp_filter_id2str(int id) { |
---|
80 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
81 | |
---|
82 | while ( l->id != -1 ) { |
---|
83 | if ( l->id == id ) |
---|
84 | return l->name; |
---|
85 | l++; |
---|
86 | } |
---|
87 | |
---|
88 | return NULL; |
---|
89 | } |
---|
90 | |
---|
91 | int roardsp_filter_new (struct roardsp_filter ** filter, struct roar_stream * stream, int id) { |
---|
92 | struct roardsp_filter * n; |
---|
93 | int ret; |
---|
94 | |
---|
95 | if ( filter == NULL || stream == NULL ) |
---|
96 | return -1; |
---|
97 | |
---|
98 | *filter = NULL; // just to be sure |
---|
99 | |
---|
100 | n = roar_mm_malloc(sizeof(struct roardsp_filter)); |
---|
101 | |
---|
102 | if ( n == NULL ) |
---|
103 | return -1; |
---|
104 | |
---|
105 | if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) { |
---|
106 | roar_mm_free(n); |
---|
107 | return -1; |
---|
108 | } |
---|
109 | |
---|
110 | n->flags |= ROARDSP_FFLAG_FREE; |
---|
111 | |
---|
112 | *filter = n; |
---|
113 | |
---|
114 | return ret; |
---|
115 | } |
---|
116 | |
---|
117 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
118 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
119 | int bytes; |
---|
120 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
121 | |
---|
122 | if ( filter == NULL || stream == NULL ) { |
---|
123 | ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL"); |
---|
124 | return -1; |
---|
125 | } |
---|
126 | |
---|
127 | ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id); |
---|
128 | |
---|
129 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
130 | |
---|
131 | filter->channels = stream->info.channels; |
---|
132 | filter->bits = stream->info.bits; |
---|
133 | filter->rate = stream->info.rate; |
---|
134 | |
---|
135 | bytes = stream->info.bits / 8; |
---|
136 | |
---|
137 | while ( l->id != id ) { |
---|
138 | if ( l->id == -1 ) |
---|
139 | return -1; |
---|
140 | l++; |
---|
141 | } |
---|
142 | |
---|
143 | filter->uninit = l->uninit; |
---|
144 | filter->ctl = l->ctl; |
---|
145 | filter->reset = l->reset; |
---|
146 | |
---|
147 | if ( filter->channels < 3 ) |
---|
148 | calc = l->calc[bytes][filter->channels]; |
---|
149 | |
---|
150 | if ( calc == NULL ) |
---|
151 | calc = l->calc[bytes][0]; // for n channels |
---|
152 | |
---|
153 | if ( calc == NULL ) { |
---|
154 | ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code"); |
---|
155 | return -1; |
---|
156 | } |
---|
157 | |
---|
158 | filter->calc = calc; |
---|
159 | |
---|
160 | if ( l->init ) { |
---|
161 | ROAR_DBG("roardsp_filter_init(*) = ? // execing init"); |
---|
162 | return l->init(filter, stream, id); |
---|
163 | } |
---|
164 | |
---|
165 | ROAR_DBG("roardsp_filter_init(*) = 0 // no init"); |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
170 | int ret = 0; |
---|
171 | |
---|
172 | if ( filter == NULL ) |
---|
173 | return -1; |
---|
174 | |
---|
175 | if ( filter->uninit ) |
---|
176 | ret = filter->uninit(filter); |
---|
177 | |
---|
178 | if ( filter->flags & ROARDSP_FFLAG_FREE ) { |
---|
179 | roar_mm_free(filter); |
---|
180 | } else { |
---|
181 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
182 | } |
---|
183 | |
---|
184 | return ret; |
---|
185 | } |
---|
186 | |
---|
187 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
188 | int ret = 0; |
---|
189 | |
---|
190 | if ( filter == NULL ) |
---|
191 | return -1; |
---|
192 | |
---|
193 | if ( data == NULL && len != 0 ) |
---|
194 | return -1; |
---|
195 | |
---|
196 | if ( filter->calc ) |
---|
197 | ret = filter->calc(filter, data, len); |
---|
198 | |
---|
199 | return ret; |
---|
200 | } |
---|
201 | |
---|
202 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
203 | if ( filter == NULL ) |
---|
204 | return -1; |
---|
205 | |
---|
206 | if ( filter->ctl ) |
---|
207 | return filter->ctl(filter, cmd, data); |
---|
208 | |
---|
209 | return -1; |
---|
210 | } |
---|
211 | |
---|
212 | int roardsp_filter_reset (struct roardsp_filter * filter, int what) { |
---|
213 | if ( filter == NULL ) |
---|
214 | return -1; |
---|
215 | |
---|
216 | if ( filter->reset ) |
---|
217 | return filter->reset(filter, what); |
---|
218 | |
---|
219 | return -1; |
---|
220 | } |
---|
221 | |
---|
222 | //ll |
---|