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