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.

272 lines
6.8KB

  1. /*
  2. * Carla math utils
  3. * Copyright (C) 2011-2017 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, constrain).
  26. */
  27. template<typename T>
  28. static inline
  29. const T& carla_minConstrained(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. * Return a value between 'min' and 'max'.
  80. */
  81. template<typename T>
  82. static inline
  83. const T& carla_fixedValue(const T& min, const T& max, const T& value) noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(max > min, min);
  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. CARLA_SAFE_ASSERT_RETURN(size > 0, 0);
  99. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  100. --size;
  101. size |= size >> 1;
  102. size |= size >> 2;
  103. size |= size >> 4;
  104. size |= size >> 8;
  105. size |= size >> 16;
  106. return ++size;
  107. }
  108. /*
  109. * Safely check if 2 numbers are equal.
  110. */
  111. template<typename T>
  112. static inline
  113. bool carla_isEqual(const T& v1, const T& v2)
  114. {
  115. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  116. }
  117. /*
  118. * Safely check if 2 numbers are not equal.
  119. */
  120. template<typename T>
  121. static inline
  122. bool carla_isNotEqual(const T& v1, const T& v2)
  123. {
  124. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  125. }
  126. /*
  127. * Safely check if a number is zero.
  128. */
  129. template<typename T>
  130. static inline
  131. bool carla_isZero(const T& value)
  132. {
  133. return std::abs(value) < std::numeric_limits<T>::epsilon();
  134. }
  135. /*
  136. * Safely check if a number is not zero.
  137. */
  138. template<typename T>
  139. static inline
  140. bool carla_isNotZero(const T& value)
  141. {
  142. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  143. }
  144. // --------------------------------------------------------------------------------------------------------------------
  145. // math functions (extended)
  146. /*
  147. * Add float array values to another float array.
  148. */
  149. static inline
  150. void carla_addFloats(float dest[], const float src[], const std::size_t count) noexcept
  151. {
  152. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  153. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  154. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  155. for (std::size_t i=0; i<count; ++i)
  156. *dest++ += *src++;
  157. }
  158. /*
  159. * Copy float array values to another float array.
  160. */
  161. static inline
  162. void carla_copyFloats(float dest[], const float src[], const std::size_t count) noexcept
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  165. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  166. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  167. std::memcpy(dest, src, count*sizeof(float));
  168. }
  169. /*
  170. * Clear a float array.
  171. */
  172. static inline
  173. void carla_zeroFloats(float floats[], const std::size_t count) noexcept
  174. {
  175. CARLA_SAFE_ASSERT_RETURN(floats != nullptr,);
  176. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  177. std::memset(floats, 0, count*sizeof(float));
  178. }
  179. /*
  180. * Find the highest absolute and normalized value within a float array.
  181. */
  182. static inline
  183. float carla_findMaxNormalizedFloat(float floats[], const std::size_t count)
  184. {
  185. CARLA_SAFE_ASSERT_RETURN(floats != nullptr, 0.0f);
  186. CARLA_SAFE_ASSERT_RETURN(count > 0, 0.0f);
  187. static const float kEmptyFloats[8192] = { 0.0f };
  188. if (count <= 8192 && std::memcmp(floats, kEmptyFloats, count) == 0)
  189. return 0.0f;
  190. float tmp, maxf2 = std::abs(floats[0]);
  191. for (std::size_t i=1; i<count; ++i)
  192. {
  193. tmp = std::abs(*floats++);
  194. if (tmp > maxf2)
  195. maxf2 = tmp;
  196. }
  197. if (maxf2 > 1.0f)
  198. maxf2 = 1.0f;
  199. return maxf2;
  200. }
  201. /*
  202. * Multiply an array with a fixed value, float-specific version.
  203. */
  204. static inline
  205. void carla_multiply(float data[], const float& multiplier, const std::size_t count) noexcept
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  208. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  209. if (carla_isZero(multiplier))
  210. {
  211. std::memset(data, 0, count*sizeof(float));
  212. }
  213. else
  214. {
  215. for (std::size_t i=0; i<count; ++i)
  216. *data++ *= multiplier;
  217. }
  218. }
  219. // --------------------------------------------------------------------------------------------------------------------
  220. // Missing functions in old OSX versions.
  221. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC) && ! defined(CARLA_PROPER_CPP11_SUPPORT)
  222. namespace std {
  223. inline float fmin(float __x, float __y)
  224. { return __builtin_fminf(__x, __y); }
  225. inline float fmax(float __x, float __y)
  226. { return __builtin_fmaxf(__x, __y); }
  227. inline float rint(float __x)
  228. { return __builtin_rintf(__x); }
  229. inline float round(float __x)
  230. { return __builtin_roundf(__x); }
  231. }
  232. #endif
  233. // --------------------------------------------------------------------------------------------------------------------
  234. #endif // CARLA_MATH_UTILS_HPP_INCLUDED