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 4.4KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.
  26. */
  27. template<typename T>
  28. static inline
  29. const T& carla_min(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.
  53. */
  54. template<typename T>
  55. static inline
  56. const T& carla_max(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. * Fix bounds of 'value' between 'min' and 'max'.
  62. */
  63. template<typename T>
  64. static inline
  65. const T& carla_fixValue(const T& min, const T& max, const T& value) noexcept
  66. {
  67. CARLA_SAFE_ASSERT_RETURN(max > min, max);
  68. if (value <= min)
  69. return min;
  70. if (value >= max)
  71. return max;
  72. return value;
  73. }
  74. /*
  75. * Get next power of 2.
  76. */
  77. static inline
  78. uint32_t carla_nextPowerOf2(uint32_t size) noexcept
  79. {
  80. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  81. --size;
  82. size |= size >> 1;
  83. size |= size >> 2;
  84. size |= size >> 4;
  85. size |= size >> 8;
  86. size |= size >> 16;
  87. return ++size;
  88. }
  89. /*
  90. * Safely compare two floating point numbers.
  91. */
  92. template<typename T>
  93. static inline
  94. bool carla_compareFloats(const T& v1, const T& v2)
  95. {
  96. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  97. }
  98. #if 0
  99. // -----------------------------------------------------------------------
  100. // math functions (extended)
  101. /*
  102. * Add float array values to another float array.
  103. */
  104. static inline
  105. void carla_addFloat(float* dataDst, const float* dataSrc, const std::size_t numSamples) noexcept
  106. {
  107. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  108. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  109. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  110. for (std::size_t i=0; i < numSamples; ++i)
  111. *dataDst++ += *dataSrc++;
  112. }
  113. /*
  114. * Copy float array values to another float array.
  115. */
  116. static inline
  117. void carla_copyFloat(float* const dataDst, const float* const dataSrc, const std::size_t numSamples) noexcept
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  120. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  121. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  122. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  123. }
  124. /*
  125. * Clear a float array.
  126. */
  127. static inline
  128. void carla_zeroFloat(float* const data, const std::size_t numSamples) noexcept
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  131. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  132. std::memset(data, 0, numSamples*sizeof(float));
  133. }
  134. #endif
  135. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  136. // -----------------------------------------------------------------------
  137. // Missing functions in OSX.
  138. namespace std {
  139. inline float
  140. fmin(float __x, float __y)
  141. { return __builtin_fminf(__x, __y); }
  142. inline float
  143. fmax(float __x, float __y)
  144. { return __builtin_fmaxf(__x, __y); }
  145. inline float
  146. rint(float __x)
  147. { return __builtin_rintf(__x); }
  148. inline float
  149. round(float __x)
  150. { return __builtin_roundf(__x); }
  151. }
  152. #endif
  153. // -----------------------------------------------------------------------
  154. #endif // CARLA_MATH_UTILS_HPP_INCLUDED