source: roaraudio/libroardsp/filter.c @ 1103:bb5ee2384821

Last change on this file since 1103:bb5ee2384821 was 1100:aac17d20aea1, checked in by phi, 15 years ago

added DC block with f_e=~5Hz

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