[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; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | return -1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | char * roardsp_filter_id2str(int id) { |
---|
| 59 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 60 | |
---|
| 61 | while ( l->id != -1 ) { |
---|
| 62 | if ( l->id == id ) |
---|
| 63 | return l->name; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | return NULL; |
---|
| 67 | } |
---|
| 68 | |
---|
[663] | 69 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
[671] | 70 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
| 71 | int bytes; |
---|
| 72 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
| 73 | |
---|
[663] | 74 | if ( filter == NULL ) |
---|
| 75 | return -1; |
---|
| 76 | |
---|
| 77 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 78 | |
---|
| 79 | filter->channels = stream->info.channels; |
---|
| 80 | filter->bits = stream->info.bits; |
---|
[673] | 81 | filter->rate = stream->info.rate; |
---|
[663] | 82 | |
---|
[671] | 83 | bytes = stream->info.bits / 8; |
---|
| 84 | |
---|
| 85 | while ( l->id != id ) |
---|
| 86 | if ( l->id == -1 ) |
---|
| 87 | return -1; |
---|
| 88 | |
---|
| 89 | filter->uninit = l->uninit; |
---|
| 90 | filter->ctl = l->ctl; |
---|
| 91 | |
---|
| 92 | if ( filter->channels < 3 ) |
---|
| 93 | calc = l->calc[bytes][filter->channels]; |
---|
| 94 | |
---|
| 95 | if ( calc == NULL ) |
---|
| 96 | calc = l->calc[bytes][0]; // for n channels |
---|
| 97 | |
---|
| 98 | if ( calc == NULL ) |
---|
| 99 | return -1; |
---|
| 100 | |
---|
| 101 | if ( l->init ) |
---|
| 102 | return l->init(filter, stream, id); |
---|
| 103 | |
---|
| 104 | return 0; |
---|
[663] | 105 | } |
---|
| 106 | |
---|
| 107 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
| 108 | int ret = 0; |
---|
| 109 | |
---|
| 110 | if ( filter == NULL ) |
---|
| 111 | return -1; |
---|
| 112 | |
---|
| 113 | if ( filter->uninit ) |
---|
| 114 | ret = filter->uninit(filter); |
---|
| 115 | |
---|
| 116 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
| 117 | |
---|
| 118 | return ret; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
| 122 | int ret = 0; |
---|
| 123 | |
---|
| 124 | if ( filter == NULL ) |
---|
| 125 | return -1; |
---|
| 126 | |
---|
| 127 | if ( filter->calc ) |
---|
| 128 | ret = filter->calc(filter, data, len); |
---|
| 129 | |
---|
| 130 | return ret; |
---|
| 131 | } |
---|
[661] | 132 | |
---|
[671] | 133 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 134 | if ( filter == NULL ) |
---|
| 135 | return -1; |
---|
| 136 | |
---|
| 137 | if ( filter->ctl ) |
---|
| 138 | return filter->ctl(filter, cmd, data); |
---|
| 139 | |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
[661] | 143 | //ll |
---|