[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); |
---|
[667] | 33 | int (*calc [5][3])(struct roardsp_filter * filter, void * data, size_t samples); |
---|
[665] | 34 | } _roardsp_filterlist[] = { |
---|
[882] | 35 | {ROARDSP_FILTER_AMP, "AMP", roardsp_amp_init, roardsp_amp_uninit, roardsp_amp_ctl, { |
---|
| 36 | {NULL, NULL, NULL},{roardsp_amp_calc8, NULL, NULL},{roardsp_amp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[979] | 37 | {ROARDSP_FILTER_ADD, "Add", roardsp_add_init, roardsp_amp_uninit, roardsp_amp_ctl, { |
---|
| 38 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_add_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1104] | 39 | #ifdef ROAR_HAVE_LIBM |
---|
[677] | 40 | {ROARDSP_FILTER_LOWP, "Lowpass", roardsp_lowp_init, roardsp_lowp_uninit, roardsp_lowp_ctl, { |
---|
| 41 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_lowp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[683] | 42 | {ROARDSP_FILTER_HIGHP, "Highpass", roardsp_highp_init, roardsp_highp_uninit, roardsp_highp_ctl, { |
---|
| 43 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_highp_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1104] | 44 | #endif |
---|
[979] | 45 | {ROARDSP_FILTER_QUANTIFY, "Quantifier", roardsp_quantify_init, NULL, roardsp_quantify_ctl, { |
---|
| 46 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_quantify_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
| 47 | {ROARDSP_FILTER_CLIP, "Clip", roardsp_clip_init, NULL, roardsp_clip_ctl, { |
---|
| 48 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_clip_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1004] | 49 | {ROARDSP_FILTER_DOWNMIX, "downmix", roardsp_downmix_init, NULL, roardsp_downmix_ctl, { |
---|
| 50 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, roardsp_downmix_calc162},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[1100] | 51 | {ROARDSP_FILTER_DCBLOCK, "DCBlock", roardsp_dcblock_init, NULL, NULL, { |
---|
| 52 | {NULL, NULL, NULL},{NULL, NULL, NULL},{roardsp_dcblock_calc16, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
[671] | 53 | {-1, NULL, NULL, NULL, NULL, { |
---|
[677] | 54 | // ? 8Bit 16Bit 24Bit 32Bit |
---|
| 55 | // 0B:n 1 2 1B:n 1 2 2B:n 1 2 3B:n 1 2 4B:n 1 2 |
---|
[671] | 56 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}} |
---|
[665] | 57 | }; |
---|
| 58 | |
---|
| 59 | int roardsp_filter_str2id(char * str) { |
---|
| 60 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 61 | |
---|
| 62 | while ( l->id != -1 ) { |
---|
| 63 | if ( strcasecmp(l->name, str) == 0 ) |
---|
| 64 | return l->id; |
---|
[678] | 65 | l++; |
---|
[665] | 66 | } |
---|
| 67 | |
---|
| 68 | return -1; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | char * roardsp_filter_id2str(int id) { |
---|
| 72 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 73 | |
---|
| 74 | while ( l->id != -1 ) { |
---|
| 75 | if ( l->id == id ) |
---|
| 76 | return l->name; |
---|
[678] | 77 | l++; |
---|
[665] | 78 | } |
---|
| 79 | |
---|
| 80 | return NULL; |
---|
| 81 | } |
---|
| 82 | |
---|
[663] | 83 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[671] | 84 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 85 | int bytes; |
---|
| 86 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
| 87 | |
---|
[681] | 88 | if ( filter == NULL || stream == NULL ) { |
---|
| 89 | ROAR_DBG("roardsp_filter_init(*) = -1 // filter or stream is NULL"); |
---|
[663] | 90 | return -1; |
---|
[681] | 91 | } |
---|
| 92 | |
---|
| 93 | ROAR_DBG("roardsp_filter_init(filter=%p, stream=%p, id=%i) = ?", filter, stream, id); |
---|
[663] | 94 | |
---|
| 95 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 96 | |
---|
| 97 | filter->channels = stream->info.channels; |
---|
| 98 | filter->bits = stream->info.bits; |
---|
[673] | 99 | filter->rate = stream->info.rate; |
---|
[663] | 100 | |
---|
[671] | 101 | bytes = stream->info.bits / 8; |
---|
| 102 | |
---|
[678] | 103 | while ( l->id != id ) { |
---|
[671] | 104 | if ( l->id == -1 ) |
---|
| 105 | return -1; |
---|
[678] | 106 | l++; |
---|
| 107 | } |
---|
[671] | 108 | |
---|
| 109 | filter->uninit = l->uninit; |
---|
| 110 | filter->ctl = l->ctl; |
---|
| 111 | |
---|
| 112 | if ( filter->channels < 3 ) |
---|
| 113 | calc = l->calc[bytes][filter->channels]; |
---|
| 114 | |
---|
| 115 | if ( calc == NULL ) |
---|
| 116 | calc = l->calc[bytes][0]; // for n channels |
---|
| 117 | |
---|
[681] | 118 | if ( calc == NULL ) { |
---|
| 119 | ROAR_DBG("roardsp_filter_init(*) = -1 // no calc code"); |
---|
[671] | 120 | return -1; |
---|
[681] | 121 | } |
---|
| 122 | |
---|
| 123 | filter->calc = calc; |
---|
[671] | 124 | |
---|
[681] | 125 | if ( l->init ) { |
---|
| 126 | ROAR_DBG("roardsp_filter_init(*) = ? // execing init"); |
---|
[671] | 127 | return l->init(filter, stream, id); |
---|
[681] | 128 | } |
---|
[671] | 129 | |
---|
[681] | 130 | ROAR_DBG("roardsp_filter_init(*) = 0 // no init"); |
---|
[671] | 131 | return 0; |
---|
[663] | 132 | } |
---|
| 133 | |
---|
| 134 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
| 135 | int ret = 0; |
---|
| 136 | |
---|
| 137 | if ( filter == NULL ) |
---|
| 138 | return -1; |
---|
| 139 | |
---|
| 140 | if ( filter->uninit ) |
---|
| 141 | ret = filter->uninit(filter); |
---|
| 142 | |
---|
| 143 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 144 | |
---|
| 145 | return ret; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
| 149 | int ret = 0; |
---|
| 150 | |
---|
| 151 | if ( filter == NULL ) |
---|
| 152 | return -1; |
---|
| 153 | |
---|
| 154 | if ( filter->calc ) |
---|
| 155 | ret = filter->calc(filter, data, len); |
---|
| 156 | |
---|
| 157 | return ret; |
---|
| 158 | } |
---|
[661] | 159 | |
---|
[671] | 160 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 161 | if ( filter == NULL ) |
---|
| 162 | return -1; |
---|
| 163 | |
---|
| 164 | if ( filter->ctl ) |
---|
| 165 | return filter->ctl(filter, cmd, data); |
---|
| 166 | |
---|
| 167 | return -1; |
---|
| 168 | } |
---|
| 169 | |
---|
[661] | 170 | //ll |
---|