source: roaraudio/libroardsp/filter_highp.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

File size: 3.5 KB
Line 
1//filter_highp.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "libroardsp.h"
27
28#ifdef ROAR_HAVE_LIBM
29
30int roardsp_highp_init  (struct roardsp_filter * filter, struct roar_stream * stream, int id) {
31 struct roardsp_highp * self = roar_mm_malloc(sizeof(struct roardsp_highp));
32 float freq = 25;
33
34 if ( self == NULL )
35  return -1;
36
37 memset(self, 0, sizeof(struct roardsp_highp));
38
39 filter->inst = (void*) self;
40
41 roardsp_highp_ctl(filter, ROARDSP_FCTL_FREQ, &freq);
42
43 return 0;
44}
45
46int roardsp_highp_uninit(struct roardsp_filter * filter) {
47
48 roar_mm_free(filter->inst);
49 return 0;
50}
51
52int roardsp_highp_calc16(struct roardsp_filter * filter, void * data, size_t samples) {
53 struct roardsp_highp * self = (struct roardsp_highp *) filter->inst;
54 int16_t * samp = (int16_t *) data;
55 register int32_t s, h;
56 int i, c;
57 int channels = filter->channels;
58
59 if ( channels > ROAR_MAX_CHANNELS )
60  return -1;
61
62 samples /= channels;
63
64 ROAR_DBG("roardsp_highp_calc16(*): filtering %i frames of %i channels...", samples, channels);
65
66// *      output[N] = A0 * input[N] + A1 * input[N-1] + B1 * output[N-1]
67
68
69 for (i = 0; i < samples; i++) {
70  for (c = 0; c < channels; c++) {
71   h = samp[i*channels + c];
72   s = samp[i*channels + c] * self->a + self->oldin[c] * self->b + self->oldout[c] * self->c;
73
74   s /= 65536;
75
76   self->oldin[      c] = h;
77   samp[i*channels + c] = s;
78   self->oldout[     c] = s;
79  }
80 }
81
82 return 0;
83}
84
85int roardsp_highp_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
86 struct roardsp_highp * self = (struct roardsp_highp *) filter->inst;
87 float lp;
88 float oldfreq;
89 float newfreq;
90
91 if ( cmd != ROARDSP_FCTL_FREQ )
92  return -1;
93
94 newfreq = *(float*)data;
95
96 lp = exp(-2.0 * M_PI * newfreq / (float) filter->rate) * 65536;
97
98 self->a =  (65536 + lp)/2.0;
99 self->b = -(65536 + lp)/2.0;
100 self->c =           lp;
101
102
103 oldfreq = self->freq / 1000;
104 self->freq = newfreq * 1000;
105
106 *(float*)data = oldfreq;
107
108 ROAR_DBG("roardsp_highp_ctl(*): oldfreq=%f, newfreq=%f", oldfreq, newfreq);
109 ROAR_DBG("roardsp_highp_ctl(*): a=%i, b=%i, c=%i", self->a, self->b, self->c);
110
111 return 0;
112}
113
114int roardsp_highp_reset (struct roardsp_filter * filter, int what) {
115 struct roardsp_highp * self;
116 float freq = 25;
117
118 if ( filter == NULL )
119  return -1;
120
121 if ( filter->inst == NULL )
122  return -1;
123
124 self = filter->inst;
125
126 switch (what) {
127  case ROARDSP_RESET_NONE:
128    return  0;
129   break;
130  case ROARDSP_RESET_FULL:
131    roardsp_highp_ctl(filter, ROARDSP_FCTL_FREQ, &freq);
132  case ROARDSP_RESET_STATE:
133    memset(self->oldin,  0, sizeof(int32_t)*ROAR_MAX_CHANNELS);
134    memset(self->oldout, 0, sizeof(int32_t)*ROAR_MAX_CHANNELS);
135    return  0;
136   break;
137  default:
138    return -1;
139 }
140
141 return -1;
142}
143
144#endif
145
146//ll
Note: See TracBrowser for help on using the repository browser.