1 /*
2 basics.h
3
4 Copyright 2004-9 Tim Goetze <tim@quitte.de>
5
6 http://quitte.de/dsp/
7
8 common constants, typedefs, utility functions
9 and simplified LADSPA #defines.
10
11 */
12 /*
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2
16 of the License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA or point your web browser to http://www.gnu.org.
27 */
28
29 #ifndef _BASICS_H_
30 #define _BASICS_H_
31
32 #define _GNU_SOURCE 1
33 #define _USE_GNU 1
34
35 /* unlocking some standard math calls. */
36 #define __USE_ISOC99 1
37 #define __USE_ISOC9X 1
38 #define _ISOC99_SOURCE 1
39 #define _ISOC9X_SOURCE 1
40
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <math.h>
45
46 #include <assert.h>
47 #include <stdio.h>
48
49 #include "ladspa.h"
50
51 /* reducing LADSPA_DEFINES_WITH_LOTS_OF_CHARACTERS_REALLY verbosity */
52 #define BOUNDED (LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE)
53 #define INTEGER LADSPA_HINT_INTEGER
54 #define LOG LADSPA_HINT_LOGARITHMIC
55 #define TOGGLE LADSPA_HINT_TOGGLED
56
57 #define DEFAULT_0 LADSPA_HINT_DEFAULT_0
58 #define DEFAULT_1 LADSPA_HINT_DEFAULT_1
59 #define DEFAULT_100 LADSPA_HINT_DEFAULT_100
60 #define DEFAULT_440 LADSPA_HINT_DEFAULT_440
61 #define DEFAULT_MIN LADSPA_HINT_DEFAULT_MINIMUM
62 #define DEFAULT_LOW LADSPA_HINT_DEFAULT_LOW
63 #define DEFAULT_MID LADSPA_HINT_DEFAULT_MIDDLE
64 #define DEFAULT_HIGH LADSPA_HINT_DEFAULT_HIGH
65 #define DEFAULT_MAX LADSPA_HINT_DEFAULT_MAXIMUM
66
67 #define INPUT LADSPA_PORT_INPUT
68 #define OUTPUT LADSPA_PORT_OUTPUT
69 #define AUDIO LADSPA_PORT_AUDIO
70 #define CONTROL LADSPA_PORT_CONTROL
71
72 #define HARD_RT LADSPA_PROPERTY_HARD_RT_CAPABLE
73
74 #define TEN_TO_THE_SIXTH 1000000
75
76 #define MIN_GAIN .000001 /* -120 dB */
77
78 /* smallest non-denormal 32 bit IEEE float is 1.18�10-38 */
79 #define NOISE_FLOOR .00000000000005 /* -266 dB */
80
81 typedef __int8_t int8;
82 typedef __uint8_t uint8;
83 typedef __int16_t int16;
84 typedef __uint16_t uint16;
85 typedef __int32_t int32;
86 typedef __uint32_t uint32;
87 typedef __int64_t int64;
88 typedef __uint64_t uint64;
89
90 typedef struct {
91 const char * name;
92 LADSPA_PortDescriptor descriptor;
93 LADSPA_PortRangeHint range;
94 } PortInfo;
95
96 typedef LADSPA_Data sample_t;
97 typedef unsigned long ulong;
98
99 /* flavours for sample store functions run() and run_adding() */
100 typedef void (*sample_func_t) (sample_t *, int, sample_t, sample_t);
101
102 inline void
store_func(sample_t * s,int i,sample_t x,sample_t gain)103 store_func (sample_t * s, int i, sample_t x, sample_t gain)
104 {
105 s[i] = x;
106 }
107
108 inline void
adding_func(sample_t * s,int i,sample_t x,sample_t gain)109 adding_func (sample_t * s, int i, sample_t x, sample_t gain)
110 {
111 s[i] += gain * x;
112 }
113
114 #ifndef max
115
116 template <class X, class Y>
min(X x,Y y)117 X min (X x, Y y)
118 {
119 return x < y ? x : (X) y;
120 }
121
122 template <class X, class Y>
max(X x,Y y)123 X max (X x, Y y)
124 {
125 return x > y ? x : (X) y;
126 }
127
128 #endif /* ! max */
129
130 template <class T>
clamp(T value,T lower,T upper)131 T clamp (T value, T lower, T upper)
132 {
133 if (value < lower) return lower;
134 if (value > upper) return upper;
135 return value;
136 }
137
138 static inline float
frandom()139 frandom()
140 {
141 return (float) random() / (float) RAND_MAX;
142 }
143
144 /* NB: also true if 0 */
145 inline bool
is_denormal(float & f)146 is_denormal (float & f)
147 {
148 int32 i = *((int32 *) &f);
149 return ((i & 0x7f800000) == 0);
150 }
151
152 /* todo: not sure if this double version is correct, actually ... */
153 inline bool
is_denormal(double & f)154 is_denormal (double & f)
155 {
156 int64 i = *((int64 *) &f);
157 return ((i & 0x7fe0000000000000ll) == 0);
158 }
159
160 #ifdef __i386__
161 #define TRAP asm ("int $3;")
162 #else
163 #define TRAP
164 #endif
165
166 /* //////////////////////////////////////////////////////////////////////// */
167
168 #define CAPS "C* "
169
170 class Plugin {
171 public:
172 double fs; /* sample rate */
173 double adding_gain; /* for run_adding() */
174
175 int first_run; /* 1st block after activate(), do no parameter smoothing */
176 sample_t normal; /* renormal constant */
177
178 sample_t ** ports;
179 LADSPA_PortRangeHint * ranges; /* for getport() below */
180
181 public:
182 /* get port value, mapping inf or nan to 0 */
getport_unclamped(int i)183 inline sample_t getport_unclamped (int i)
184 {
185 sample_t v = *ports[i];
186 return (isinf (v) || isnan(v)) ? 0 : v;
187 }
188
189 /* get port value and clamp to port range */
getport(int i)190 inline sample_t getport (int i)
191 {
192 LADSPA_PortRangeHint & r = ranges[i];
193 sample_t v = getport_unclamped (i);
194 return clamp (v, r.LowerBound, r.UpperBound);
195 }
196 };
197
198 #endif /* _BASICS_H_ */
199