source: roaraudio/libroardsp/filter_highp.c @ 5823:f9f70dbaa376

Last change on this file since 5823:f9f70dbaa376 was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

File size: 3.8 KB
Line 
1//filter_highp.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
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
33 (void)stream, (void)id;
34
35 if ( self == NULL )
36  return -1;
37
38 memset(self, 0, sizeof(struct roardsp_highp));
39
40 filter->inst = (void*) self;
41
42 return roardsp_filter_reset(filter, ROARDSP_RESET_FULL);
43}
44
45int roardsp_highp_uninit(struct roardsp_filter * filter) {
46
47 roar_mm_free(filter->inst);
48 return 0;
49}
50
51#define _calcX(bits,twobits) \
52int roardsp_highp_calc##bits (struct roardsp_filter * filter, void * data, size_t samples) { \
53 struct roardsp_highp * self = (struct roardsp_highp *) filter->inst; \
54 int##bits##_t * samp = (int##bits##_t *) data; \
55 register int##twobits##_t s, h; \
56 size_t i, c; \
57 size_t 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 = (int##twobits##_t)samp[i*channels + c] * self->a + (int##twobits##_t)self->oldin[c] * self->b + (int##twobits##_t)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
85_calcX(8,32) /* we need 32 bit not 16 bit here as we multiplay with 65536 internally */
86_calcX(16,32)
87_calcX(32,64)
88
89int roardsp_highp_ctl   (struct roardsp_filter * filter, int cmd, void * data) {
90 struct roardsp_highp * self = (struct roardsp_highp *) filter->inst;
91 float lp;
92 float oldfreq;
93 float newfreq;
94
95 if ( cmd != ROARDSP_FCTL_FREQ )
96  return -1;
97
98 newfreq = *(float*)data;
99
100 lp = exp(-2.0 * M_PI * newfreq / (float) filter->rate) * 65536.;
101
102 self->a =  (65536 + lp)/2.0;
103 self->b = -(65536 + lp)/2.0;
104 self->c =           lp;
105
106
107 oldfreq = self->freq / 1000;
108 self->freq = newfreq * 1000;
109
110 *(float*)data = oldfreq;
111
112 ROAR_DBG("roardsp_highp_ctl(*): oldfreq=%f, newfreq=%f", oldfreq, newfreq);
113 ROAR_DBG("roardsp_highp_ctl(*): a=%i, b=%i, c=%i", self->a, self->b, self->c);
114
115 return 0;
116}
117
118int roardsp_highp_reset (struct roardsp_filter * filter, int what) {
119 struct roardsp_highp * self;
120 float freq = 25.;
121
122 if ( filter == NULL )
123  return -1;
124
125 if ( filter->inst == NULL )
126  return -1;
127
128 self = filter->inst;
129
130 switch (what) {
131  case ROARDSP_RESET_NONE:
132    return  0;
133   break;
134  case ROARDSP_RESET_FULL:
135    roardsp_highp_ctl(filter, ROARDSP_FCTL_FREQ, &freq);
136  case ROARDSP_RESET_STATE:
137    memset(self->oldin,  0, sizeof(int32_t)*ROAR_MAX_CHANNELS);
138    memset(self->oldout, 0, sizeof(int32_t)*ROAR_MAX_CHANNELS);
139    return  0;
140   break;
141  default:
142    return -1;
143 }
144
145 return -1;
146}
147
148#endif
149
150//ll
Note: See TracBrowser for help on using the repository browser.