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