basics.h 4.5 KB

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