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.

144 lines
4.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. #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 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. // math functions (extended)
  72. /*
  73. * Add float array values to another float array.
  74. */
  75. static inline
  76. void carla_addFloat(float* dataDst, const float* dataSrc, const size_t numSamples) noexcept
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  79. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  80. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  81. for (size_t i=0; i < numSamples; ++i)
  82. *dataDst++ += *dataSrc++;
  83. }
  84. /*
  85. * Copy float array values to another float array.
  86. */
  87. static inline
  88. void carla_copyFloat(float* const dataDst, const float* const dataSrc, const size_t numSamples) noexcept
  89. {
  90. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  91. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  92. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  93. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  94. }
  95. /*
  96. * Clear a float array.
  97. */
  98. static inline
  99. void carla_zeroFloat(float* const data, const size_t numSamples) noexcept
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  102. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  103. std::memset(data, 0, numSamples*sizeof(float));
  104. }
  105. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  106. /*
  107. * Missing functions in OSX.
  108. */
  109. namespace std {
  110. inline float
  111. fmin(float __x, float __y)
  112. { return __builtin_fminf(__x, __y); }
  113. inline float
  114. fmax(float __x, float __y)
  115. { return __builtin_fmaxf(__x, __y); }
  116. inline float
  117. rint(float __x)
  118. { return __builtin_rintf(__x); }
  119. }
  120. #endif
  121. // -----------------------------------------------------------------------
  122. #endif // CARLA_MATH_UTILS_HPP_INCLUDED