2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef CBOX_ONEPOLE_FLOAT_H
20 #define CBOX_ONEPOLE_FLOAT_H
24 struct cbox_onepolef_state
30 struct cbox_onepolef_coeffs
37 static inline void cbox_onepolef_reset(struct cbox_onepolef_state
*state
)
39 state
->x1
= state
->y1
= 0.f
;
42 static inline void cbox_onepolef_set_lowpass(struct cbox_onepolef_coeffs
*coeffs
, float w
)
44 float x
= tan (w
* 0.5f
);
45 float q
= 1 / (1 + x
);
54 static inline void cbox_onepolef_set_highpass(struct cbox_onepolef_coeffs
*coeffs
, float w
)
56 float x
= tan (w
* 0.5f
);
57 float q
= 1 / (1 + x
);
66 static inline void cbox_onepolef_set_highshelf_tonectl(struct cbox_onepolef_coeffs
*coeffs
, float w
, float g0
)
68 float x
= tan (w
* 0.5f
);
69 float q
= 1 / (1 + x
);
72 coeffs
->a0
= 0.5 * (1 + b1
+ g0
- b1
* g0
);
73 coeffs
->a1
= 0.5 * (1 + b1
- g0
+ b1
* g0
);
77 static inline void cbox_onepolef_set_highshelf_setgain(struct cbox_onepolef_coeffs
*coeffs
, float g0
)
79 coeffs
->a0
= 0.5 * (1 + coeffs
->b1
+ g0
- coeffs
->b1
* g0
);
80 coeffs
->a1
= 0.5 * (1 + coeffs
->b1
- g0
+ coeffs
->b1
* g0
);
83 static inline void cbox_onepolef_set_allpass(struct cbox_onepolef_coeffs
*coeffs
, float w
)
85 float x
= tan (w
* 0.5f
);
86 float q
= 1 / (1 + x
);
95 static inline float cbox_onepolef_process_sample(struct cbox_onepolef_state
*state
, struct cbox_onepolef_coeffs
*coeffs
, float in
)
97 float out
= sanef(coeffs
->a0
* in
+ coeffs
->a1
* state
->x1
- coeffs
->b1
* state
->y1
);
104 static inline void cbox_onepolef_process(struct cbox_onepolef_state
*state
, struct cbox_onepolef_coeffs
*coeffs
, float *buffer
)
107 float a0
= coeffs
->a0
;
108 float a1
= coeffs
->a1
;
109 float b1
= coeffs
->b1
;
111 for (i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
113 float in
= buffer
[i
];
114 double out
= a0
* in
+ a1
* state
->x1
- b1
* state
->y1
;
120 state
->y1
= sanef(state
->y1
);
123 static inline void cbox_onepolef_process_to(struct cbox_onepolef_state
*state
, struct cbox_onepolef_coeffs
*coeffs
, float *buffer_in
, float *buffer_out
)
126 float a0
= coeffs
->a0
;
127 float a1
= coeffs
->a1
;
128 float b1
= coeffs
->b1
;
130 for (i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
132 float in
= buffer_in
[i
];
133 double out
= a0
* in
+ a1
* state
->x1
- b1
* state
->y1
;
139 state
->y1
= sanef(state
->y1
);