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.

DistrhoUtils.hpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_UTILS_HPP_INCLUDED
  17. #define DISTRHO_UTILS_HPP_INCLUDED
  18. #include "src/DistrhoDefines.h"
  19. #include <cstdarg>
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <cmath>
  24. #undef max
  25. #undef min
  26. #include <limits>
  27. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  28. # include <cstdint>
  29. #else
  30. # include <stdint.h>
  31. #endif
  32. #if defined(DISTRHO_OS_MAC) && ! defined(CARLA_OS_MAC) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
  33. namespace std {
  34. inline float fmin(float __x, float __y)
  35. { return __builtin_fminf(__x, __y); }
  36. inline float fmax(float __x, float __y)
  37. { return __builtin_fmaxf(__x, __y); }
  38. inline float rint(float __x)
  39. { return __builtin_rintf(__x); }
  40. inline float round(float __x)
  41. { return __builtin_roundf(__x); }
  42. }
  43. #endif
  44. #ifndef M_PI
  45. # define M_PI 3.14159265358979323846
  46. #endif
  47. // -----------------------------------------------------------------------
  48. // misc functions
  49. /*
  50. * Return a 64-bit number from 4 8-bit numbers.
  51. */
  52. static inline
  53. int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  54. {
  55. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  56. }
  57. /*
  58. * Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
  59. */
  60. static inline
  61. uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
  62. {
  63. return uint32_t(major << 16) | uint32_t(minor << 8) | uint32_t(micro << 0);
  64. }
  65. /*
  66. * Dummy function.
  67. */
  68. static inline
  69. void d_pass() noexcept {}
  70. // -----------------------------------------------------------------------
  71. // string print functions
  72. /*
  73. * Print a string to stdout with newline (gray color).
  74. * Does nothing if DEBUG is not defined.
  75. */
  76. #ifndef DEBUG
  77. # define d_debug(...)
  78. #else
  79. static inline
  80. void d_debug(const char* const fmt, ...) noexcept
  81. {
  82. try {
  83. ::va_list args;
  84. ::va_start(args, fmt);
  85. std::fprintf(stdout, "\x1b[30;1m");
  86. std::vfprintf(stdout, fmt, args);
  87. std::fprintf(stdout, "\x1b[0m\n");
  88. ::va_end(args);
  89. } catch (...) {}
  90. }
  91. #endif
  92. /*
  93. * Print a string to stdout with newline.
  94. */
  95. static inline
  96. void d_stdout(const char* const fmt, ...) noexcept
  97. {
  98. try {
  99. ::va_list args;
  100. ::va_start(args, fmt);
  101. std::vfprintf(stdout, fmt, args);
  102. std::fprintf(stdout, "\n");
  103. ::va_end(args);
  104. } catch (...) {}
  105. }
  106. /*
  107. * Print a string to stderr with newline.
  108. */
  109. static inline
  110. void d_stderr(const char* const fmt, ...) noexcept
  111. {
  112. try {
  113. ::va_list args;
  114. ::va_start(args, fmt);
  115. std::vfprintf(stderr, fmt, args);
  116. std::fprintf(stderr, "\n");
  117. ::va_end(args);
  118. } catch (...) {}
  119. }
  120. /*
  121. * Print a string to stderr with newline (red color).
  122. */
  123. static inline
  124. void d_stderr2(const char* const fmt, ...) noexcept
  125. {
  126. try {
  127. ::va_list args;
  128. ::va_start(args, fmt);
  129. std::fprintf(stderr, "\x1b[31m");
  130. std::vfprintf(stderr, fmt, args);
  131. std::fprintf(stderr, "\x1b[0m\n");
  132. ::va_end(args);
  133. } catch (...) {}
  134. }
  135. /*
  136. * Print a safe assertion error message.
  137. */
  138. static inline
  139. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  140. {
  141. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  142. }
  143. /*
  144. * Print a safe exception error message.
  145. */
  146. static inline
  147. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  148. {
  149. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  150. }
  151. // -----------------------------------------------------------------------
  152. // math functions
  153. /*
  154. * Safely compare two floating point numbers.
  155. * Returns true if they match.
  156. */
  157. template<typename T>
  158. static inline
  159. bool d_isEqual(const T& v1, const T& v2)
  160. {
  161. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  162. }
  163. /*
  164. * Safely compare two floating point numbers.
  165. * Returns true if they don't match.
  166. */
  167. template<typename T>
  168. static inline
  169. bool d_isNotEqual(const T& v1, const T& v2)
  170. {
  171. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  172. }
  173. /*
  174. * Safely check if a floating point number is zero.
  175. */
  176. template<typename T>
  177. static inline
  178. bool d_isZero(const T& value)
  179. {
  180. return std::abs(value) < std::numeric_limits<T>::epsilon();
  181. }
  182. /*
  183. * Safely check if a floating point number is not zero.
  184. */
  185. template<typename T>
  186. static inline
  187. bool d_isNotZero(const T& value)
  188. {
  189. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  190. }
  191. /*
  192. * Get next power of 2.
  193. */
  194. static inline
  195. uint32_t d_nextPowerOf2(uint32_t size) noexcept
  196. {
  197. DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
  198. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  199. --size;
  200. size |= size >> 1;
  201. size |= size >> 2;
  202. size |= size >> 4;
  203. size |= size >> 8;
  204. size |= size >> 16;
  205. return ++size;
  206. }
  207. // -----------------------------------------------------------------------
  208. #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
  209. // If your code uses a lot of DISTRHO classes, then this will obviously save you
  210. // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
  211. namespace DISTRHO_NAMESPACE {}
  212. using namespace DISTRHO_NAMESPACE;
  213. #endif
  214. // -----------------------------------------------------------------------
  215. #endif // DISTRHO_UTILS_HPP_INCLUDED