Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaMathUtils.hpp 5.5KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Carla math utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  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.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_MATH_UTILS_HPP_INCLUDED
  18. #define CARLA_MATH_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #include <cmath>
  21. #include <limits>
  22. // -----------------------------------------------------------------------
  23. // math functions (base)
  24. /*
  25. * Return the lower of 2 values, with 'min' as the minimum possible value (ie, base).
  26. */
  27. template<typename T>
  28. static inline
  29. const T& carla_minWithBase(const T& v1, const T& v2, const T& min) noexcept
  30. {
  31. return ((v1 <= min || v2 <= min) ? min : (v1 < v2 ? v1 : v2));
  32. }
  33. /*
  34. * Return the lower positive of 2 values.
  35. * If one of the values is zero, returns zero.
  36. * If one value is negative but the other positive, returns the positive.
  37. * Returned value is guaranteed to be >= 0.
  38. */
  39. template<typename T>
  40. static inline
  41. T carla_minPositive(const T& v1, const T& v2) noexcept
  42. {
  43. if (v1 == 0 || v2 == 0)
  44. return 0;
  45. if (v1 < 0)
  46. return (v2 > 0) ? v2 : 0;
  47. if (v2 < 0)
  48. return (v1 > 0) ? v1 : 0;
  49. return (v1 < v2) ? v1 : v2;
  50. }
  51. /*
  52. * Return the higher of 2 values, with 'max' as the maximum possible value (ie, limit).
  53. */
  54. template<typename T>
  55. static inline
  56. const T& carla_maxLimited(const T& v1, const T& v2, const T& max) noexcept
  57. {
  58. return ((v1 >= max || v2 >= max) ? max : (v1 > v2 ? v1 : v2));
  59. }
  60. /*
  61. * Return the higher negative of 2 values.
  62. * If one of the values is zero, returns zero.
  63. * If one value is positive but the other negative, returns the negative.
  64. * Returned value is guaranteed to be <= 0.
  65. */
  66. template<typename T>
  67. static inline
  68. T carla_maxNegative(const T& v1, const T& v2) noexcept
  69. {
  70. if (v1 == 0 || v2 == 0)
  71. return 0;
  72. if (v1 > 0)
  73. return (v2 < 0) ? v2 : 0;
  74. if (v2 > 0)
  75. return (v1 < 0) ? v1 : 0;
  76. return (v1 > v2) ? v1 : v2;
  77. }
  78. /*
  79. * Fix bounds of 'value' between 'min' and 'max'.
  80. */
  81. template<typename T>
  82. static inline
  83. const T& carla_fixValue(const T& min, const T& max, const T& value) noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(max > min, max);
  86. if (value <= min)
  87. return min;
  88. if (value >= max)
  89. return max;
  90. return value;
  91. }
  92. /*
  93. * Get next power of 2.
  94. */
  95. static inline
  96. uint32_t carla_nextPowerOf2(uint32_t size) noexcept
  97. {
  98. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  99. --size;
  100. size |= size >> 1;
  101. size |= size >> 2;
  102. size |= size >> 4;
  103. size |= size >> 8;
  104. size |= size >> 16;
  105. return ++size;
  106. }
  107. // -----------------------------------------------------------------------
  108. // math functions (floating numbers)
  109. /*
  110. * Safely compare two floating point numbers.
  111. * Returns true if they match.
  112. */
  113. template<typename T>
  114. static inline
  115. bool carla_compareFloats(const T& v1, const T& v2)
  116. {
  117. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  118. }
  119. /*
  120. * Safely check if a floating point number is zero.
  121. */
  122. template<typename T>
  123. static inline
  124. bool carla_isZero(const T& value)
  125. {
  126. return std::abs(value) < std::numeric_limits<T>::epsilon();
  127. }
  128. /*
  129. * Safely check if a floating point number is not zero.
  130. */
  131. template<typename T>
  132. static inline
  133. bool carla_isNotZero(const T& value)
  134. {
  135. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  136. }
  137. // -----------------------------------------------------------------------
  138. // math functions (extended)
  139. /*
  140. * Add float array values to another float array.
  141. */
  142. static inline
  143. void carla_addFloat(float* dataDst, const float* dataSrc, const std::size_t numSamples) noexcept
  144. {
  145. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  146. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  147. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  148. for (std::size_t i=0; i < numSamples; ++i)
  149. *dataDst++ += *dataSrc++;
  150. }
  151. /*
  152. * Copy float array values to another float array.
  153. */
  154. static inline
  155. void carla_copyFloat(float* const dataDst, const float* const dataSrc, const std::size_t numSamples) noexcept
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  158. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  159. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  160. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  161. }
  162. /*
  163. * Clear a float array.
  164. */
  165. static inline
  166. void carla_zeroFloat(float* const data, const std::size_t numSamples) noexcept
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  169. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  170. std::memset(data, 0, numSamples*sizeof(float));
  171. }
  172. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  173. // -----------------------------------------------------------------------
  174. // Missing functions in OSX.
  175. namespace std {
  176. inline float fmin(float __x, float __y)
  177. { return __builtin_fminf(__x, __y); }
  178. inline float fmax(float __x, float __y)
  179. { return __builtin_fmaxf(__x, __y); }
  180. inline float rint(float __x)
  181. { return __builtin_rintf(__x); }
  182. inline float round(float __x)
  183. { return __builtin_roundf(__x); }
  184. }
  185. #endif
  186. // -----------------------------------------------------------------------
  187. #endif // CARLA_MATH_UTILS_HPP_INCLUDED