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
28 typedef float cbox_sample_t
;
30 static inline float hz2w(float hz
, float sr
)
32 return M_PI
* hz
/ (2 * sr
);
35 static inline float cerp_naive(float v0
, float v1
, float v2
, float v3
, float f
)
42 float l0
= ((f
- x1
) * (f
- x2
) * (f
- x3
)) / ( (x0
- x1
) * (x0
- x2
) * (x0
- x3
));
43 float l1
= ((f
- x0
) * (f
- x2
) * (f
- x3
)) / ((x1
- x0
) * (x1
- x2
) * (x1
- x3
));
44 float l2
= ((f
- x0
) * (f
- x1
) * (f
- x3
)) / ((x2
- x0
) * (x2
- x1
) * (x2
- x3
));
45 float l3
= ((f
- x0
) * (f
- x1
) * (f
- x2
)) / ((x3
- x0
) * (x3
- x1
) * (x3
- x2
) );
47 return v0
* l0
+ v1
* l1
+ v2
* l2
+ v3
* l3
;
50 static inline float cerp(float v0
, float v1
, float v2
, float v3
, float f
)
59 float d03
= (d0
* d3
) * (1.0 / 2.0);
60 float d12
= (d03
+ 1) * (1.0 / 3.0);
67 float y
= v0
* l0
+ v1
* l1
+ v2
* l2
+ v3
* l3
;
68 // printf("%f\n", y - cerp_naive(v0, v1, v2, v3, f - 1));
72 static inline float sanef(float v
)
74 if (fabs(v
) < (1.0 / (65536.0 * 65536.0)))
79 static inline void sanebf(float *buf
)
82 for (i
= 0; i
< CBOX_BLOCK_SIZE
; i
++)
83 buf
[i
] = sanef(buf
[i
]);
86 static inline void copybf(float *to
, float *from
)
88 memcpy(to
, from
, sizeof(float) * CBOX_BLOCK_SIZE
);
91 static inline float cent2factor(float cent
)
93 return pow(2.0, cent
/ 1200.0); // I think this may be optimised using exp()
96 static inline float dB2gain(float dB
)
98 return pow(2.0, dB
/ 6.0);
101 static inline float dB2gain_simple(float dB
)
105 return pow(2.0, dB
/ 6.0);
108 static inline float gain2dB_simple(float gain
)
110 if (gain
< pow(2.0, -96.0 / 6.0))
112 return 6.0 * log(gain
) / log(2.0);
115 static inline float deg2rad(float deg
)
117 return deg
* M_PI
/ 180;
120 static inline float rad2deg(float rad
)
122 return rad
* 180 / M_PI
;
125 // Do a butterfly operation:
126 // dst1 = src1 + e^iw_1*src2
127 // dst2 = src1 + e^iw_2*src2 (w = phase * 2pi / ANALYSIS_BUFFER_SIZE)
128 static inline void butterfly(complex float *dst1
, complex float *dst2
, complex float src1
, complex float src2
, complex float eiw1
, complex float eiw2
)
130 *dst1
= src1
+ eiw1
* src2
;
131 *dst2
= src1
+ eiw2
* src2
;