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.

182 lines
4.9KB

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