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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, frames)
  29. # define FLOAT_COPY(bufDst, bufSrc, frames) FloatVectorOperations::copy(bufDst, bufSrc, frames)
  30. # define FLOAT_CLEAR(buf, frames) FloatVectorOperations::clear(buf, 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 higher of 2 values, with 'max' as the maximum possible value.
  49. */
  50. template<typename T>
  51. static inline
  52. const T& carla_max(const T& v1, const T& v2, const T& max) noexcept
  53. {
  54. return ((v1 >= max || v2 >= max) ? max : (v1 > v2 ? v1 : v2));
  55. }
  56. /*
  57. * Fix bounds of 'value', between 'min' and 'max'.
  58. */
  59. template<typename T>
  60. static inline
  61. const T& carla_fixValue(const T& min, const T& max, const T& value) noexcept
  62. {
  63. CARLA_SAFE_ASSERT_RETURN(max > min, max);
  64. if (value <= min)
  65. return min;
  66. if (value >= max)
  67. return max;
  68. return value;
  69. }
  70. /*
  71. * Add array values to another array.
  72. */
  73. template<typename T>
  74. static inline
  75. void carla_add(T* dataDst, T* dataSrc, const size_t size) noexcept
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  78. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  79. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  80. for (size_t i=0; i < size; ++i)
  81. *dataDst++ += *dataSrc++;
  82. }
  83. /*
  84. * Add array values to another array.
  85. */
  86. template<typename T>
  87. static inline
  88. void carla_add(T* dataDst, const T* dataSrc, const size_t size) noexcept
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  91. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  92. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  93. for (size_t i=0; i < size; ++i)
  94. *dataDst++ += *dataSrc++;
  95. }
  96. /*
  97. * Copy array values to another array.
  98. */
  99. template<typename T>
  100. static inline
  101. void carla_copy(T* dataDst, T* dataSrc, const size_t size) noexcept
  102. {
  103. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  104. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  105. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  106. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  107. }
  108. /*
  109. * Copy array values to another array.
  110. */
  111. template<typename T>
  112. static inline
  113. void carla_copy(T* dataDst, const T* dataSrc, const size_t size) noexcept
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  116. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  117. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  118. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  119. }
  120. /*
  121. * Fill an array with a fixed value.
  122. */
  123. template<typename T>
  124. static inline
  125. void carla_fill(T* data, const size_t size, const T v) noexcept
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  128. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  129. if (v == 0)
  130. {
  131. std::memset(data, 0, size*sizeof(T));
  132. }
  133. else
  134. {
  135. for (size_t i=0; i < size; ++i)
  136. *data++ = v;
  137. }
  138. }
  139. // -----------------------------------------------------------------------
  140. // math functions (extended)
  141. /*
  142. * Add float array values to another float array.
  143. */
  144. static inline
  145. void carla_addFloat(float* dataDst, float* dataSrc, const size_t numSamples) noexcept
  146. {
  147. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  148. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  149. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  150. for (size_t i=0; i < numSamples; ++i)
  151. *dataDst++ += *dataSrc++;
  152. }
  153. /*
  154. * Copy float array values to another float array.
  155. */
  156. static inline
  157. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t numSamples) noexcept
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  160. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  161. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  162. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  163. }
  164. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  165. /*
  166. * Missing functions in OSX.
  167. */
  168. namespace std {
  169. inline float
  170. fmin(float __x, float __y)
  171. { return __builtin_fminf(__x, __y); }
  172. inline float
  173. fmax(float __x, float __y)
  174. { return __builtin_fmaxf(__x, __y); }
  175. inline float
  176. rint(float __x)
  177. { return __builtin_rintf(__x); }
  178. }
  179. #endif
  180. // -----------------------------------------------------------------------
  181. #endif // CARLA_MATH_UTILS_HPP_INCLUDED