[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}}}, |
---|
[671] | 39 | {ROARDSP_FILTER_HIGHP, "Highpass", NULL, NULL, NULL, { |
---|
[667] | 40 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, 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 | |
---|
[663] | 76 | if ( filter == NULL ) |
---|
| 77 | return -1; |
---|
| 78 | |
---|
| 79 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 80 | |
---|
| 81 | filter->channels = stream->info.channels; |
---|
| 82 | filter->bits = stream->info.bits; |
---|
[673] | 83 | filter->rate = stream->info.rate; |
---|
[663] | 84 | |
---|
[671] | 85 | bytes = stream->info.bits / 8; |
---|
| 86 | |
---|
[678] | 87 | while ( l->id != id ) { |
---|
[671] | 88 | if ( l->id == -1 ) |
---|
| 89 | return -1; |
---|
[678] | 90 | l++; |
---|
| 91 | } |
---|
[671] | 92 | |
---|
| 93 | filter->uninit = l->uninit; |
---|
| 94 | filter->ctl = l->ctl; |
---|
| 95 | |
---|
| 96 | if ( filter->channels < 3 ) |
---|
| 97 | calc = l->calc[bytes][filter->channels]; |
---|
| 98 | |
---|
| 99 | if ( calc == NULL ) |
---|
| 100 | calc = l->calc[bytes][0]; // for n channels |
---|
| 101 | |
---|
| 102 | if ( calc == NULL ) |
---|
| 103 | return -1; |
---|
| 104 | |
---|
| 105 | if ( l->init ) |
---|
| 106 | return l->init(filter, stream, id); |
---|
| 107 | |
---|
| 108 | return 0; |
---|
[663] | 109 | } |
---|
| 110 | |
---|
| 111 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
| 112 | int ret = 0; |
---|
| 113 | |
---|
| 114 | if ( filter == NULL ) |
---|
| 115 | return -1; |
---|
| 116 | |
---|
| 117 | if ( filter->uninit ) |
---|
| 118 | ret = filter->uninit(filter); |
---|
| 119 | |
---|
| 120 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 121 | |
---|
| 122 | return ret; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
| 126 | int ret = 0; |
---|
| 127 | |
---|
| 128 | if ( filter == NULL ) |
---|
| 129 | return -1; |
---|
| 130 | |
---|
| 131 | if ( filter->calc ) |
---|
| 132 | ret = filter->calc(filter, data, len); |
---|
| 133 | |
---|
| 134 | return ret; |
---|
| 135 | } |
---|
[661] | 136 | |
---|
[671] | 137 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 138 | if ( filter == NULL ) |
---|
| 139 | return -1; |
---|
| 140 | |
---|
| 141 | if ( filter->ctl ) |
---|
| 142 | return filter->ctl(filter, cmd, data); |
---|
| 143 | |
---|
| 144 | return -1; |
---|
| 145 | } |
---|
| 146 | |
---|
[661] | 147 | //ll |
---|