[661] | 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 | |
---|
[665] | 27 | struct _roardsp_filterlist { |
---|
| 28 | int id; |
---|
| 29 | char * name; |
---|
[667] | 30 | int (* init )(struct roardsp_filter * filter, struct roar_stream * stream, int id); |
---|
| 31 | int (*uninit )(struct roardsp_filter * filter); |
---|
[671] | 32 | int (*ctl )(struct roardsp_filter * filter, int cmd, void * data); |
---|
[1131] | 33 | int (*reset )(struct roardsp_filter * filter, int what); |
---|
[667] | 34 | int (*calc [5][3])(struct roardsp_filter * filter, void * data, size_t samples); |
---|
[665] | 35 | } _roardsp_filterlist[] = { |
---|
[1141] | 36 | {ROARDSP_FILTER_AMP, "AMP", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_amp_reset, { |
---|
[882] | 37 | {NULL, NULL, NULL},{roardsp_amp_calc8, NULL, NULL},{roardsp_amp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1141] | 38 | {ROARDSP_FILTER_ADD, "Add", roardsp_add_init, roardsp_amp_uninit, roardsp_amp_ctl, roardsp_add_reset, { |
---|
[979] | 39 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_add_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1104] | 40 | #ifdef ROAR_HAVE_LIBM |
---|
[1141] | 41 | {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, roardsp_lowp_reset, { |
---|
[677] | 42 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1141] | 43 | {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, roardsp_highp_reset, { |
---|
[683] | 44 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1104] | 45 | #endif |
---|
[1141] | 46 | {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, roardsp_quantify_reset, { |
---|
[979] | 47 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1141] | 48 | {ROARDSP_FILTER_CLIP, "Clip", roardsp_quantify_init, NULL, roardsp_clip_ctl, roardsp_clip_reset, { |
---|
[979] | 49 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1141] | 50 | {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_quantify_init, NULL, roardsp_downmix_ctl, roardsp_downmix_reset, { |
---|
[1004] | 51 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1131] | 52 | {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, roardsp_dcblock_reset, { |
---|
[1100] | 53 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1587] | 54 | {ROARDSP_FILTER_SWAP, "Swap", roardsp_swap_init, roardsp_swap_uninit, roardsp_swap_ctl, roardsp_swap_reset, { |
---|
[2999] | 55 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_swap_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[3010] | 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}}}, |
---|
[3029] | 58 | #ifdef ROAR_HAVE_SPEEX_FILTER |
---|
[2992] | 59 | {ROARDSP_FILTER_SPEEX_PREP, "SpeexPrep", roardsp_speex_prep_init, roardsp_speex_prep_uninit, |
---|
| 60 | roardsp_speex_prep_ctl, roardsp_speex_prep_reset, { |
---|
[2998] | 61 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, roardsp_speex_prep_calc161, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[3029] | 62 | #endif |
---|
[1131] | 63 | {-1, NULL, NULL, NULL, NULL, NULL, { |
---|
[677] | 64 | // ? 8Bit 16Bit 24Bit 32Bit |
---|
| 65 | // 0B:n 1 2 1B:n 1 2 2B:n 1 2 3B:n 1 2 4B:n 1 2 |
---|
[671] | 66 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}} |
---|
[665] | 67 | }; |
---|
| 68 | |
---|
| 69 | int roardsp_filter_str2id(char * str) { |
---|
| 70 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 71 | |
---|
| 72 | while ( l->id != -1 ) { |
---|
| 73 | if ( strcasecmp(l->name, str) == 0 ) |
---|
| 74 | return l->id; |
---|
[678] | 75 | l++; |
---|
[665] | 76 | } |
---|
| 77 | |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | char * roardsp_filter_id2str(int id) { |
---|
| 82 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 83 | |
---|
| 84 | while ( l->id != -1 ) { |
---|
| 85 | if ( l->id == id ) |
---|
| 86 | return l->name; |
---|
[678] | 87 | l++; |
---|
[665] | 88 | } |
---|
| 89 | |
---|
| 90 | return NULL; |
---|
| 91 | } |
---|
| 92 | |
---|
[2990] | 93 | int roardsp_filter_new (struct roardsp_filter ** filter, struct roar_stream * stream, int id) { |
---|
| 94 | struct roardsp_filter * n; |
---|
| 95 | int ret; |
---|
| 96 | |
---|
| 97 | if ( filter == NULL || stream == NULL ) |
---|
| 98 | return -1; |
---|
| 99 | |
---|
| 100 | *filter = NULL; // just to be sure |
---|
| 101 | |
---|
| 102 | n = roar_mm_malloc(sizeof(struct roardsp_filter)); |
---|
| 103 | |
---|
| 104 | if ( n == NULL ) |
---|
| 105 | return -1; |
---|
| 106 | |
---|
| 107 | if ( (ret = roardsp_filter_init(n, stream, id)) == -1 ) { |
---|
| 108 | roar_mm_free(n); |
---|
| 109 | return -1; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | n->flags |= ROARDSP_FFLAG_FREE; |
---|
| 113 | |
---|
| 114 | *filter = n; |
---|
| 115 | |
---|
| 116 | return ret; |
---|
| 117 | } |
---|
| 118 | |
---|
[663] | 119 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[671] | 120 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 121 | int bytes; |
---|
| 122 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
| 123 | |
---|
[681] | 124 | if ( filter == NULL || stream == NULL ) { |
---|
| 125 | ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL"); |
---|
[663] | 126 | return -1; |
---|
[681] | 127 | } |
---|
| 128 | |
---|
| 129 | ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id); |
---|
[663] | 130 | |
---|
| 131 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 132 | |
---|
| 133 | filter->channels = stream->info.channels; |
---|
| 134 | filter->bits = stream->info.bits; |
---|
[673] | 135 | filter->rate = stream->info.rate; |
---|
[663] | 136 | |
---|
[671] | 137 | bytes = stream->info.bits / 8; |
---|
| 138 | |
---|
[678] | 139 | while ( l->id != id ) { |
---|
[671] | 140 | if ( l->id == -1 ) |
---|
| 141 | return -1; |
---|
[678] | 142 | l++; |
---|
| 143 | } |
---|
[671] | 144 | |
---|
| 145 | filter->uninit = l->uninit; |
---|
| 146 | filter->ctl = l->ctl; |
---|
[1134] | 147 | filter->reset = l->reset; |
---|
[671] | 148 | |
---|
| 149 | if ( filter->channels < 3 ) |
---|
| 150 | calc = l->calc[bytes][filter->channels]; |
---|
| 151 | |
---|
| 152 | if ( calc == NULL ) |
---|
| 153 | calc = l->calc[bytes][0]; // for n channels |
---|
| 154 | |
---|
[681] | 155 | if ( calc == NULL ) { |
---|
| 156 | ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code"); |
---|
[671] | 157 | return -1; |
---|
[681] | 158 | } |
---|
| 159 | |
---|
| 160 | filter->calc = calc; |
---|
[671] | 161 | |
---|
[681] | 162 | if ( l->init ) { |
---|
| 163 | ROAR_DBG("roardsp_filter_init(*) = ? // execing init"); |
---|
[671] | 164 | return l->init(filter, stream, id); |
---|
[681] | 165 | } |
---|
[671] | 166 | |
---|
[681] | 167 | ROAR_DBG("roardsp_filter_init(*) = 0 // no init"); |
---|
[671] | 168 | return 0; |
---|
[663] | 169 | } |
---|
| 170 | |
---|
| 171 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
| 172 | int ret = 0; |
---|
| 173 | |
---|
| 174 | if ( filter == NULL ) |
---|
| 175 | return -1; |
---|
| 176 | |
---|
| 177 | if ( filter->uninit ) |
---|
| 178 | ret = filter->uninit(filter); |
---|
| 179 | |
---|
[2990] | 180 | if ( filter->flags & ROARDSP_FFLAG_FREE ) { |
---|
| 181 | roar_mm_free(filter); |
---|
| 182 | } else { |
---|
| 183 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 184 | } |
---|
[663] | 185 | |
---|
| 186 | return ret; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
| 190 | int ret = 0; |
---|
| 191 | |
---|
| 192 | if ( filter == NULL ) |
---|
| 193 | return -1; |
---|
| 194 | |
---|
[3002] | 195 | if ( data == NULL && len != 0 ) |
---|
| 196 | return -1; |
---|
| 197 | |
---|
[663] | 198 | if ( filter->calc ) |
---|
| 199 | ret = filter->calc(filter, data, len); |
---|
| 200 | |
---|
| 201 | return ret; |
---|
| 202 | } |
---|
[661] | 203 | |
---|
[671] | 204 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 205 | if ( filter == NULL ) |
---|
| 206 | return -1; |
---|
| 207 | |
---|
| 208 | if ( filter->ctl ) |
---|
| 209 | return filter->ctl(filter, cmd, data); |
---|
| 210 | |
---|
| 211 | return -1; |
---|
| 212 | } |
---|
| 213 | |
---|
[1131] | 214 | int roardsp_filter_reset (struct roardsp_filter * filter, int what) { |
---|
| 215 | if ( filter == NULL ) |
---|
| 216 | return -1; |
---|
| 217 | |
---|
| 218 | if ( filter->reset ) |
---|
| 219 | return filter->reset(filter, what); |
---|
| 220 | |
---|
| 221 | return -1; |
---|
| 222 | } |
---|
| 223 | |
---|
[661] | 224 | //ll |
---|