[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}}}, |
---|
[1131] | 54 | {-1, NULL, NULL, NULL, NULL, NULL, { |
---|
[677] | 55 | // ? 8Bit 16Bit 24Bit 32Bit |
---|
| 56 | // 0B:n 1 2 1B:n 1 2 2B:n 1 2 3B:n 1 2 4B:n 1 2 |
---|
[671] | 57 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}} |
---|
[665] | 58 | }; |
---|
| 59 | |
---|
| 60 | int roardsp_filter_str2id(char * str) { |
---|
| 61 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 62 | |
---|
| 63 | while ( l->id != -1 ) { |
---|
| 64 | if ( strcasecmp(l->name, str) == 0 ) |
---|
| 65 | return l->id; |
---|
[678] | 66 | l++; |
---|
[665] | 67 | } |
---|
| 68 | |
---|
| 69 | return -1; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | char * roardsp_filter_id2str(int id) { |
---|
| 73 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 74 | |
---|
| 75 | while ( l->id != -1 ) { |
---|
| 76 | if ( l->id == id ) |
---|
| 77 | return l->name; |
---|
[678] | 78 | l++; |
---|
[665] | 79 | } |
---|
| 80 | |
---|
| 81 | return NULL; |
---|
| 82 | } |
---|
| 83 | |
---|
[663] | 84 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[671] | 85 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 86 | int bytes; |
---|
| 87 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
| 88 | |
---|
[681] | 89 | if ( filter == NULL || stream == NULL ) { |
---|
| 90 | ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL"); |
---|
[663] | 91 | return -1; |
---|
[681] | 92 | } |
---|
| 93 | |
---|
| 94 | ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id); |
---|
[663] | 95 | |
---|
| 96 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 97 | |
---|
| 98 | filter->channels = stream->info.channels; |
---|
| 99 | filter->bits = stream->info.bits; |
---|
[673] | 100 | filter->rate = stream->info.rate; |
---|
[663] | 101 | |
---|
[671] | 102 | bytes = stream->info.bits / 8; |
---|
| 103 | |
---|
[678] | 104 | while ( l->id != id ) { |
---|
[671] | 105 | if ( l->id == -1 ) |
---|
| 106 | return -1; |
---|
[678] | 107 | l++; |
---|
| 108 | } |
---|
[671] | 109 | |
---|
| 110 | filter->uninit = l->uninit; |
---|
| 111 | filter->ctl = l->ctl; |
---|
[1134] | 112 | filter->reset = l->reset; |
---|
[671] | 113 | |
---|
| 114 | if ( filter->channels < 3 ) |
---|
| 115 | calc = l->calc[bytes][filter->channels]; |
---|
| 116 | |
---|
| 117 | if ( calc == NULL ) |
---|
| 118 | calc = l->calc[bytes][0]; // for n channels |
---|
| 119 | |
---|
[681] | 120 | if ( calc == NULL ) { |
---|
| 121 | ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code"); |
---|
[671] | 122 | return -1; |
---|
[681] | 123 | } |
---|
| 124 | |
---|
| 125 | filter->calc = calc; |
---|
[671] | 126 | |
---|
[681] | 127 | if ( l->init ) { |
---|
| 128 | ROAR_DBG("roardsp_filter_init(*) = ? // execing init"); |
---|
[671] | 129 | return l->init(filter, stream, id); |
---|
[681] | 130 | } |
---|
[671] | 131 | |
---|
[681] | 132 | ROAR_DBG("roardsp_filter_init(*) = 0 // no init"); |
---|
[671] | 133 | return 0; |
---|
[663] | 134 | } |
---|
| 135 | |
---|
| 136 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
| 137 | int ret = 0; |
---|
| 138 | |
---|
| 139 | if ( filter == NULL ) |
---|
| 140 | return -1; |
---|
| 141 | |
---|
| 142 | if ( filter->uninit ) |
---|
| 143 | ret = filter->uninit(filter); |
---|
| 144 | |
---|
| 145 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 146 | |
---|
| 147 | return ret; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
| 151 | int ret = 0; |
---|
| 152 | |
---|
| 153 | if ( filter == NULL ) |
---|
| 154 | return -1; |
---|
| 155 | |
---|
| 156 | if ( filter->calc ) |
---|
| 157 | ret = filter->calc(filter, data, len); |
---|
| 158 | |
---|
| 159 | return ret; |
---|
| 160 | } |
---|
[661] | 161 | |
---|
[671] | 162 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 163 | if ( filter == NULL ) |
---|
| 164 | return -1; |
---|
| 165 | |
---|
| 166 | if ( filter->ctl ) |
---|
| 167 | return filter->ctl(filter, cmd, data); |
---|
| 168 | |
---|
| 169 | return -1; |
---|
| 170 | } |
---|
| 171 | |
---|
[1131] | 172 | int roardsp_filter_reset (struct roardsp_filter * filter, int what) { |
---|
| 173 | if ( filter == NULL ) |
---|
| 174 | return -1; |
---|
| 175 | |
---|
| 176 | if ( filter->reset ) |
---|
| 177 | return filter->reset(filter, what); |
---|
| 178 | |
---|
| 179 | return -1; |
---|
| 180 | } |
---|
| 181 | |
---|
[661] | 182 | //ll |
---|