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 | |
---|
27 | struct _roardsp_filterlist { |
---|
28 | int id; |
---|
29 | char * name; |
---|
30 | int (* init )(struct roardsp_filter * filter, struct roar_stream * stream, int id); |
---|
31 | int (*uninit )(struct roardsp_filter * filter); |
---|
32 | int (*ctl )(struct roardsp_filter * filter, int cmd, void * data); |
---|
33 | int (*calc [5][3])(struct roardsp_filter * filter, void * data, size_t samples); |
---|
34 | } _roardsp_filterlist[] = { |
---|
35 | {ROARDSP_FILTER_AMP, "AMP", NULL, NULL, NULL, { |
---|
36 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
37 | {ROARDSP_FILTER_LOWP, "Lowpass", NULL, NULL, NULL, { |
---|
38 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
39 | {ROARDSP_FILTER_HIGHP, "Highpass", NULL, NULL, NULL, { |
---|
40 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}}, |
---|
41 | {-1, NULL, NULL, NULL, NULL, { |
---|
42 | {NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL},{NULL, NULL, NULL}}} |
---|
43 | }; |
---|
44 | |
---|
45 | int roardsp_filter_str2id(char * str) { |
---|
46 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
47 | |
---|
48 | while ( l->id != -1 ) { |
---|
49 | if ( strcasecmp(l->name, str) == 0 ) |
---|
50 | return l->id; |
---|
51 | } |
---|
52 | |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | char * roardsp_filter_id2str(int id) { |
---|
57 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
58 | |
---|
59 | while ( l->id != -1 ) { |
---|
60 | if ( l->id == id ) |
---|
61 | return l->name; |
---|
62 | } |
---|
63 | |
---|
64 | return NULL; |
---|
65 | } |
---|
66 | |
---|
67 | int roardsp_filter_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
68 | struct _roardsp_filterlist * l = _roardsp_filterlist; |
---|
69 | int bytes; |
---|
70 | int (*calc)(struct roardsp_filter * filter, void * data, size_t samples) = NULL; |
---|
71 | |
---|
72 | if ( filter == NULL ) |
---|
73 | return -1; |
---|
74 | |
---|
75 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
76 | |
---|
77 | filter->channels = stream->info.channels; |
---|
78 | filter->bits = stream->info.bits; |
---|
79 | filter->rate = stream->info.rate; |
---|
80 | |
---|
81 | bytes = stream->info.bits / 8; |
---|
82 | |
---|
83 | while ( l->id != id ) |
---|
84 | if ( l->id == -1 ) |
---|
85 | return -1; |
---|
86 | |
---|
87 | filter->uninit = l->uninit; |
---|
88 | filter->ctl = l->ctl; |
---|
89 | |
---|
90 | if ( filter->channels < 3 ) |
---|
91 | calc = l->calc[bytes][filter->channels]; |
---|
92 | |
---|
93 | if ( calc == NULL ) |
---|
94 | calc = l->calc[bytes][0]; // for n channels |
---|
95 | |
---|
96 | if ( calc == NULL ) |
---|
97 | return -1; |
---|
98 | |
---|
99 | if ( l->init ) |
---|
100 | return l->init(filter, stream, id); |
---|
101 | |
---|
102 | return 0; |
---|
103 | } |
---|
104 | |
---|
105 | int roardsp_filter_uninit(struct roardsp_filter * filter) { |
---|
106 | int ret = 0; |
---|
107 | |
---|
108 | if ( filter == NULL ) |
---|
109 | return -1; |
---|
110 | |
---|
111 | if ( filter->uninit ) |
---|
112 | ret = filter->uninit(filter); |
---|
113 | |
---|
114 | memset(filter, 0, sizeof(struct roardsp_filter)); |
---|
115 | |
---|
116 | return ret; |
---|
117 | } |
---|
118 | |
---|
119 | int roardsp_filter_calc (struct roardsp_filter * filter, void * data, size_t len) { |
---|
120 | int ret = 0; |
---|
121 | |
---|
122 | if ( filter == NULL ) |
---|
123 | return -1; |
---|
124 | |
---|
125 | if ( filter->calc ) |
---|
126 | ret = filter->calc(filter, data, len); |
---|
127 | |
---|
128 | return ret; |
---|
129 | } |
---|
130 | |
---|
131 | int roardsp_filter_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
132 | if ( filter == NULL ) |
---|
133 | return -1; |
---|
134 | |
---|
135 | if ( filter->ctl ) |
---|
136 | return filter->ctl(filter, cmd, data); |
---|
137 | |
---|
138 | return -1; |
---|
139 | } |
---|
140 | |
---|
141 | //ll |
---|