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/>.
18 #ifndef CBOX_DSPMATH_H
19 #define CBOX_DSPMATH_H
21 #define CBOX_BLOCK_SIZE 16
33 typedef float cbox_sample_t
;
35 static inline float hz2w(float hz
, float sr
)
37 return M_PI
* hz
/ (2 * sr
);
40 static inline float cerp_naive(float v0
, float v1
, float v2
, float v3
, float f
)
47 float l0
= ((f
- x1
) * (f
- x2
) * (f
- x3
)) / ( (x0
- x1
) * (x0
- x2
) * (x0
- x3
));
48 float l1
= ((f
- x0
) * (f
- x2
) * (f
- x3
)) / ((x1
- x0
) * (x1
- x2
) * (x1
- x3
));
49 float l2
= ((f
- x0
) * (f
- x1
) * (f
- x3
)) / ((x2
- x0
) * (x2
- x1
) * (x2
- x3
));
50 float l3
= ((f
- x0
) * (f
- x1
) * (f
- x2
)) / ((x3
- x0
) * (x3
- x1
) * (x3
- x2
) );
52 return v0
* l0
+ v1
* l1
+ v2
* l2
+ v3
* l3
;
55 static inline float cerp(float v0
, float v1
, float v2
, float v3
, float f
)
64 float d03
= (d0
* d3
) * (1.0 / 2.0);
65 float d12
= (d03
+ 1) * (1.0 / 3.0);
72 float y
= v0
* l0
+ v1
* l1
+ v2
* l2
+ v3
* l3
;
73 // printf("%f\n", y - cerp_naive(v0, v1, v2, v3, f - 1));
77 static inline float sanef(float v
)
79 if (fabs(v
) < (1.0 / (65536.0 * 65536.0)))
84 static inline void sanebf(float *buf
)
87 for (i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
88 buf
[i
] = sanef(buf
[i
]);
91 static inline void copybf(float *to
, float *from
)
93 memcpy(to
, from
, sizeof(float) * CBOX_BLOCK_SIZE
);
96 static inline float cent2factor(float cent
)
98 return pow(2.0, cent
/ 1200.0); // I think this may be optimised using exp()
101 static inline float dB2gain(float dB
)
103 return pow(2.0, dB
/ 6.0);
106 static inline float dB2gain_simple(float dB
)
110 return pow(2.0, dB
/ 6.0);
113 static inline float gain2dB_simple(float gain
)
115 if (gain
< pow(2.0, -96.0 / 6.0))
117 return 6.0 * log(gain
) / log(2.0);
120 static inline float deg2rad(float deg
)
122 return deg
* M_PI
/ 180;
125 static inline float rad2deg(float rad
)
127 return rad
* 180 / M_PI
;
130 // Do a butterfly operation:
131 // dst1 = src1 + e^iw_1*src2
132 // dst2 = src1 + e^iw_2*src2 (w = phase * 2pi / ANALYSIS_BUFFER_SIZE)
133 static inline void butterfly(complex float *dst1
, complex float *dst2
, complex float src1
, complex float src2
, complex float eiw1
, complex float eiw2
)
135 *dst1
= src1
+ eiw1
* src2
;
136 *dst2
= src1
+ eiw2
* src2
;