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 #include <stdint.h>
49 
50 #include <ladspa.h>
51 
52 /* reducing LADSPA_DEFINES_WITH_LOTS_OF_CHARACTERS_REALLY verbosity */
53 #define BOUNDED (LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE)
54 #define INTEGER LADSPA_HINT_INTEGER
55 #define LOG LADSPA_HINT_LOGARITHMIC
56 #define TOGGLE LADSPA_HINT_TOGGLED
57 
58 #define DEFAULT_0 LADSPA_HINT_DEFAULT_0
59 #define DEFAULT_1 LADSPA_HINT_DEFAULT_1
60 #define DEFAULT_100 LADSPA_HINT_DEFAULT_100
61 #define DEFAULT_440 LADSPA_HINT_DEFAULT_440
62 #define DEFAULT_MIN LADSPA_HINT_DEFAULT_MINIMUM
63 #define DEFAULT_LOW LADSPA_HINT_DEFAULT_LOW
64 #define DEFAULT_MID LADSPA_HINT_DEFAULT_MIDDLE
65 #define DEFAULT_HIGH LADSPA_HINT_DEFAULT_HIGH
66 #define DEFAULT_MAX LADSPA_HINT_DEFAULT_MAXIMUM
67 
68 #define INPUT LADSPA_PORT_INPUT
69 #define OUTPUT LADSPA_PORT_OUTPUT
70 #define AUDIO LADSPA_PORT_AUDIO
71 #define CONTROL LADSPA_PORT_CONTROL
72 
73 #define HARD_RT LADSPA_PROPERTY_HARD_RT_CAPABLE
74 
75 #define TEN_TO_THE_SIXTH 1000000
76 
77 #define MIN_GAIN .000001 /* -120 dB */
78 
79 /* smallest non-denormal 32 bit IEEE float is 1.18�10-38 */
80 #define NOISE_FLOOR .00000000000005 /* -266 dB */
81 
82 typedef int8_t			int8;
83 typedef uint8_t			uint8;
84 typedef int16_t			int16;
85 typedef uint16_t		uint16;
86 typedef int32_t			int32;
87 typedef uint32_t		uint32;
88 typedef int64_t			int64;
89 typedef uint64_t		uint64;
90 
91 typedef struct {
92 	const char * name;
93 	LADSPA_PortDescriptor descriptor;
94 	LADSPA_PortRangeHint range;
95 } PortInfo;
96 
97 typedef LADSPA_Data sample_t;
98 typedef unsigned long ulong;
99 
100 /* flavours for sample store functions run() and run_adding() */
101 typedef void (*sample_func_t) (sample_t *, int, sample_t, sample_t);
102 
103 inline void
store_func(sample_t * s,int i,sample_t x,sample_t gain)104 store_func (sample_t * s, int i, sample_t x, sample_t gain)
105 {
106 	s[i] = x;
107 }
108 
109 inline void
adding_func(sample_t * s,int i,sample_t x,sample_t gain)110 adding_func (sample_t * s, int i, sample_t x, sample_t gain)
111 {
112 	s[i] += gain * x;
113 }
114 
115 #ifndef max
116 
117 template <class X, class Y>
min(X x,Y y)118 X min (X x, Y y)
119 {
120 	return x < y ? x : (X) y;
121 }
122 
123 template <class X, class Y>
max(X x,Y y)124 X max (X x, Y y)
125 {
126 	return x > y ? x : (X) y;
127 }
128 
129 #endif /* ! max */
130 
131 template <class T>
clamp(T value,T lower,T upper)132 T clamp (T value, T lower, T upper)
133 {
134 	if (value < lower) return lower;
135 	if (value > upper) return upper;
136 	return value;
137 }
138 
139 static inline float
frandom()140 frandom()
141 {
142 	return (float) rand() / (float) RAND_MAX;
143 }
144 
145 /* NB: also true if 0  */
146 inline bool
is_denormal(float & f)147 is_denormal (float & f)
148 {
149 	int32 i = *((int32 *) &f);
150 	return ((i & 0x7f800000) == 0);
151 }
152 
153 /* todo: not sure if this double version is correct, actually ... */
154 inline bool
is_denormal(double & f)155 is_denormal (double & f)
156 {
157 	int64 i = *((int64 *) &f);
158 	return ((i & 0x7fe0000000000000ll) == 0);
159 }
160 
161 #ifdef __i386__
162 	#define TRAP asm ("int $3;")
163 #else
164 	#define TRAP
165 #endif
166 
167 /* //////////////////////////////////////////////////////////////////////// */
168 
169 #define CAPS "C* "
170 
171 class Plugin {
172 	public:
173 		double fs; /* sample rate */
174 		double adding_gain; /* for run_adding() */
175 
176 		int first_run; /* 1st block after activate(), do no parameter smoothing */
177 		sample_t normal; /* renormal constant */
178 
179 		sample_t ** ports;
180 		LADSPA_PortRangeHint * ranges; /* for getport() below */
181 
182 	public:
183 		/* get port value, mapping inf or nan to 0 */
getport_unclamped(int i)184 		inline sample_t getport_unclamped (int i)
185 			{
186 				sample_t v = *ports[i];
187 				return (isinf (v) || isnan(v)) ? 0 : v;
188 			}
189 
190 		/* get port value and clamp to port range */
getport(int i)191 		inline sample_t getport (int i)
192 			{
193 				LADSPA_PortRangeHint & r = ranges[i];
194 				sample_t v = getport_unclamped (i);
195 				return clamp (v, r.LowerBound, r.UpperBound);
196 			}
197 };
198 
199 #endif /* _BASICS_H_ */
200