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.2KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // -----------------------------------------------------------------------
  22. // math functions (base)
  23. /*
  24. * Return the lower of 2 values, with 'min' as the minimum possible value.
  25. */
  26. template<typename T>
  27. static inline
  28. const T& carla_min(const T& v1, const T& v2, const T& min) noexcept
  29. {
  30. return ((v1 <= min || v2 <= min) ? min : (v1 < v2 ? v1 : v2));
  31. }
  32. /*
  33. * Return the lower positive of 2 values.
  34. * If one of the values is zero, returns zero.
  35. * If one value is negative but the other positive, returns the positive.
  36. * Returned value is guaranteed to be >= 0.
  37. */
  38. template<typename T>
  39. static inline
  40. T carla_minPositive(const T& v1, const T& v2) noexcept
  41. {
  42. if (v1 == 0 || v2 == 0)
  43. return 0;
  44. if (v1 < 0)
  45. return (v2 > 0) ? v2 : 0;
  46. if (v2 < 0)
  47. return (v1 > 0) ? v1 : 0;
  48. return (v1 < v2 ? v1 : v2);
  49. }
  50. /*
  51. * Return the higher of 2 values, with 'max' as the maximum possible value.
  52. */
  53. template<typename T>
  54. static inline
  55. const T& carla_max(const T& v1, const T& v2, const T& max) noexcept
  56. {
  57. return ((v1 >= max || v2 >= max) ? max : (v1 > v2 ? v1 : v2));
  58. }
  59. /*
  60. * Fix bounds of 'value' between 'min' and 'max'.
  61. */
  62. template<typename T>
  63. static inline
  64. const T& carla_fixValue(const T& min, const T& max, const T& value) noexcept
  65. {
  66. CARLA_SAFE_ASSERT_RETURN(max > min, max);
  67. if (value <= min)
  68. return min;
  69. if (value >= max)
  70. return max;
  71. return value;
  72. }
  73. /*
  74. * Get next power of 2.
  75. */
  76. static inline
  77. uint32_t carla_nextPowerOf2(uint32_t size) noexcept
  78. {
  79. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  80. --size;
  81. size |= size >> 1;
  82. size |= size >> 2;
  83. size |= size >> 4;
  84. size |= size >> 8;
  85. size |= size >> 16;
  86. return ++size;
  87. }
  88. #if 0
  89. // -----------------------------------------------------------------------
  90. // math functions (extended)
  91. /*
  92. * Add float array values to another float array.
  93. */
  94. static inline
  95. void carla_addFloat(float* dataDst, const float* dataSrc, const std::size_t numSamples) noexcept
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  98. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  99. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  100. for (std::size_t i=0; i < numSamples; ++i)
  101. *dataDst++ += *dataSrc++;
  102. }
  103. /*
  104. * Copy float array values to another float array.
  105. */
  106. static inline
  107. void carla_copyFloat(float* const dataDst, const float* const dataSrc, const std::size_t numSamples) noexcept
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  110. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  111. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  112. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  113. }
  114. /*
  115. * Clear a float array.
  116. */
  117. static inline
  118. void carla_zeroFloat(float* const data, const std::size_t numSamples) noexcept
  119. {
  120. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  121. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  122. std::memset(data, 0, numSamples*sizeof(float));
  123. }
  124. #endif
  125. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  126. // -----------------------------------------------------------------------
  127. // Missing functions in OSX.
  128. namespace std {
  129. inline float
  130. fmin(float __x, float __y)
  131. { return __builtin_fminf(__x, __y); }
  132. inline float
  133. fmax(float __x, float __y)
  134. { return __builtin_fmaxf(__x, __y); }
  135. inline float
  136. rint(float __x)
  137. { return __builtin_rintf(__x); }
  138. inline float
  139. round(float __x)
  140. { return __builtin_roundf(__x); }
  141. }
  142. #endif
  143. // -----------------------------------------------------------------------
  144. #endif // CARLA_MATH_UTILS_HPP_INCLUDED