LMMS
Loading...
Searching...
No Matches
ladspa-util.h
Go to the documentation of this file.
1/* Some misc util functions for audio DSP work, written by Steve Harris,
2 * December 2000
3 *
4 * steve@plugin.org.uk
5 */
6
7#ifndef LADSPA_UTIL_H
8#define LADSPA_UTIL_H
9
10#ifndef _USE_MATH_DEFINES
11#define _USE_MATH_DEFINES
12#endif
13
14#include <math.h>
15#include <stdint.h>
16
17#include "config.h"
18
19// 16.16 fixpoint
20typedef union {
22 struct {
23#ifdef WORDS_BIGENDIAN
24 int16_t in;
26#else
29#endif
30 } part;
31} fixp16;
32
33// 32.32 fixpoint
34typedef union {
35 int64_t all;
36 struct {
37#ifdef WORDS_BIGENDIAN
38 int32_t in;
40#else
43#endif
44 } part;
45} fixp32;
46
47/* 32 bit "pointer cast" union */
48typedef union {
49 float f;
52
53// Sometimes it doesn't get defined, even though it eists and C99 is declared
54long int lrintf (float x);
55
56// 1.0 / ln(2)
57#define LN2R 1.442695041f
58
59/* detet floating point denormal numbers by comparing them to the smallest
60 * normal, crap, but reliable */
61#define DN_CHECK(x, l) if (fabs(x) < 1e-38) printf("DN: "l"\n")
62
63// Denormalise floats, only actually needed for PIII and recent PowerPC
64//#define FLUSH_TO_ZERO(fv) (((*(unsigned int*)&(fv))&0x7f800000)==0)?0.0f:(fv)
65
66static inline float flush_to_zero(float f)
67{
69
70 v.f = f;
71
72 // original: return (v.i & 0x7f800000) == 0 ? 0.0f : f;
73 // version from Tim Blechmann
74 return (v.i & 0x7f800000) < 0x08000000 ? 0.0f : f;
75}
76
77static inline void round_to_zero(volatile float *f)
78{
79 *f += 1e-18;
80 *f -= 1e-18;
81}
82
83/* A set of branchless clipping operations from Laurent de Soras */
84
85static inline float f_max(float x, float a)
86{
87 x -= a;
88 x += fabs(x);
89 x *= 0.5;
90 x += a;
91
92 return x;
93}
94
95static inline float f_min(float x, float b)
96{
97 x = b - x;
98 x += fabs(x);
99 x *= 0.5;
100 x = b - x;
101
102 return x;
103}
104
105static inline float f_clamp(float x, float a, float b)
106{
107 const float x1 = fabs(x - a);
108 const float x2 = fabs(x - b);
109
110 x = x1 + a + b;
111 x -= x2;
112 x *= 0.5;
113
114 return x;
115}
116
117// Limit a value to be l<=v<=u
118#define LIMIT(v,l,u) ((v)<(l)?(l):((v)>(u)?(u):(v)))
119
120// Truncate-to-zero modulo (ANSI C doesn't specify) will only work
121// if -m < v < 2m
122#define MOD(v,m) (v<0?v+m:(v>=m?v-m:v))
123
124// Truncate-to-zero modulo (ANSI C doesn't specify) will only work
125// if v > -m and v < m
126#define NEG_MOD(v,m) ((v)<0?((v)+(m)):(v))
127
128// Convert a value in dB's to a coefficent
129#define DB_CO(g) ((g) > -90.0f ? powf(10.0f, (g) * 0.05f) : 0.0f)
130#define CO_DB(v) (20.0f * log10f(v))
131
132// Linearly interpolate [ = a * (1 - f) + b * f]
133#define LIN_INTERP(f,a,b) ((a) + (f) * ((b) - (a)))
134
135// Cubic interpolation function
136static inline float cube_interp(const float fr, const float inm1, const float
137 in, const float inp1, const float inp2)
138{
139 return in + 0.5f * fr * (inp1 - inm1 +
140 fr * (4.0f * inp1 + 2.0f * inm1 - 5.0f * in - inp2 +
141 fr * (3.0f * (in - inp1) - inm1 + inp2)));
142}
143
144/* fast sin^2 aproxiamtion, adapted from jan AT rpgfan's posting to the
145 * music-dsp list */
146static inline float f_sin_sq(float angle)
147{
148 const float asqr = angle * angle;
149 float result = -2.39e-08f;
150
151 result *= asqr;
152 result += 2.7526e-06f;
153 result *= asqr;
154 result -= 1.98409e-04f;
155 result *= asqr;
156 result += 8.3333315e-03f;
157 result *= asqr;
158 result -= 1.666666664e-01f;
159 result *= asqr;
160 result += 1.0f;
161 result *= angle;
162
163 return result * result;
164}
165
166#ifdef HAVE_LRINTF
167
168#define f_round(f) lrintf(f)
169
170#define f_abs(j) labs(j)
171
172#else
173
174// Round float to int using IEEE int* hack
175static inline int f_round(float f)
176{
178
179 p.f = f;
180 p.f += (3<<22);
181
182 return p.i - 0x4b400000;
183}
184
185#define f_abs(j) abs(j)
186
187#endif
188
189// Truncate float to int
190static inline int f_trunc(float f)
191{
192 return f_round(floorf(f));
193}
194
195/* Andrew Simper's pow(2, x) aproximation from the music-dsp list */
196
197#if 0
198
199/* original */
200static inline float f_pow2(float x)
201{
202 long *px = (long*)(&x); // store address of float as long pointer
203 const float tx = (x-0.5f) + (3<<22); // temporary value for truncation
204 const long lx = *((long*)&tx) - 0x4b400000; // integer power of 2
205 const float dx = x-(float)(lx); // float remainder of power of 2
206
207 x = 1.0f + dx*(0.6960656421638072f + // cubic apporoximation of 2^x
208 dx*(0.224494337302845f + // for x in the range [0, 1]
209 dx*(0.07944023841053369f)));
210 *px += (lx<<23); // add integer power of 2 to exponent
211
212 return x;
213}
214
215#else
216
217/* union version */
218static inline float f_pow2(float x)
219{
220 ls_pcast32 *px, tx, lx;
221 float dx;
222
223 px = (ls_pcast32 *)&x; // store address of float as long pointer
224 tx.f = (x-0.5f) + (3<<22); // temporary value for truncation
225 lx.i = tx.i - 0x4b400000; // integer power of 2
226 dx = x - (float)lx.i; // float remainder of power of 2
227
228 x = 1.0f + dx * (0.6960656421638072f + // cubic apporoximation of 2^x
229 dx * (0.224494337302845f + // for x in the range [0, 1]
230 dx * (0.07944023841053369f)));
231 (*px).i += (lx.i << 23); // add integer power of 2 to exponent
232
233 return (*px).f;
234}
235
236#endif
237
238/* Fast exponentiation function, y = e^x */
239#define f_exp(x) f_pow2(x * LN2R)
240
241#endif
uint8_t a
Definition Spc_Cpu.h:141
* e
Definition inflate.c:1404
unsigned v[N_MAX]
Definition inflate.c:1584
int lx[BMAX+1]
Definition inflate.c:1578
unsigned x[BMAX+1]
Definition inflate.c:1586
unsigned f
Definition inflate.c:1572
static float f_clamp(float x, float a, float b)
Definition ladspa-util.h:105
long int lrintf(float x)
static int f_trunc(float f)
Definition ladspa-util.h:190
static void round_to_zero(volatile float *f)
Definition ladspa-util.h:77
static float f_pow2(float x)
Definition ladspa-util.h:218
static float f_min(float x, float b)
Definition ladspa-util.h:95
static float f_sin_sq(float angle)
Definition ladspa-util.h:146
static float cube_interp(const float fr, const float inm1, const float in, const float inp1, const float inp2)
Definition ladspa-util.h:136
static float f_max(float x, float a)
Definition ladspa-util.h:85
static int f_round(float f)
Definition ladspa-util.h:175
static float flush_to_zero(float f)
Definition ladspa-util.h:66
float in
Definition lilv_test.c:1460
unsigned short uint16_t
Definition mid.cpp:99
int int32_t
Definition mid.cpp:97
unsigned int uint32_t
Definition mid.cpp:100
short int16_t
Definition mid.cpp:96
Definition ladspa-util.h:20
int16_t in
Definition ladspa-util.h:28
uint16_t fr
Definition ladspa-util.h:27
int32_t all
Definition ladspa-util.h:21
Definition ladspa-util.h:34
int64_t all
Definition ladspa-util.h:35
uint32_t fr
Definition ladspa-util.h:41
int32_t in
Definition ladspa-util.h:42
Definition ladspa-util.h:48
float f
Definition ladspa-util.h:49
int32_t i
Definition ladspa-util.h:50
uch * p
Definition crypt.c:594
b
Definition crypt.c:628
int result
Definition process.c:1455