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.

183 lines
5.0KB

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