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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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. #include <limits>
  25. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  26. # include <cstdint>
  27. #else
  28. # include <stdint.h>
  29. #endif
  30. #if defined(DISTRHO_OS_WINDOWS) && defined(_MSC_VER)
  31. #include <basetsd.h>
  32. typedef SSIZE_T ssize_t;
  33. #endif
  34. #if ! defined(CARLA_MATH_UTILS_HPP_INCLUDED) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
  35. namespace std {
  36. inline float fmin(float __x, float __y)
  37. { return __builtin_fminf(__x, __y); }
  38. inline float fmax(float __x, float __y)
  39. { return __builtin_fmaxf(__x, __y); }
  40. inline float rint(float __x)
  41. { return __builtin_rintf(__x); }
  42. inline float round(float __x)
  43. { return __builtin_roundf(__x); }
  44. }
  45. #endif
  46. #ifndef M_PI
  47. # define M_PI 3.14159265358979323846
  48. #endif
  49. #define DISTRHO_MACRO_AS_STRING_VALUE(MACRO) #MACRO
  50. #define DISTRHO_MACRO_AS_STRING(MACRO) DISTRHO_MACRO_AS_STRING_VALUE(MACRO)
  51. /* ------------------------------------------------------------------------------------------------------------
  52. * misc functions */
  53. /**
  54. @defgroup MiscellaneousFunctions Miscellaneous functions
  55. @{
  56. */
  57. /**
  58. Return a 32-bit number from 4 8-bit numbers.@n
  59. The return type is a int64_t for better compatibility with plugin formats that use such numbers.
  60. */
  61. static inline constexpr
  62. int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  63. {
  64. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  65. }
  66. /**
  67. Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
  68. */
  69. static inline constexpr
  70. uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
  71. {
  72. return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
  73. }
  74. /**
  75. Dummy, no-op function.
  76. */
  77. static inline
  78. void d_pass() noexcept {}
  79. /** @} */
  80. /* ------------------------------------------------------------------------------------------------------------
  81. * string print functions */
  82. /**
  83. @defgroup StringPrintFunctions String print functions
  84. @{
  85. */
  86. /**
  87. Print a string to stdout with newline (gray color).
  88. Does nothing if DEBUG is not defined.
  89. */
  90. #ifndef DEBUG
  91. # define d_debug(...)
  92. #else
  93. static inline
  94. void d_debug(const char* const fmt, ...) noexcept
  95. {
  96. try {
  97. va_list args;
  98. va_start(args, fmt);
  99. std::fprintf(stdout, "\x1b[30;1m");
  100. std::vfprintf(stdout, fmt, args);
  101. std::fprintf(stdout, "\x1b[0m\n");
  102. va_end(args);
  103. } catch (...) {}
  104. }
  105. #endif
  106. /**
  107. Print a string to stdout with newline.
  108. */
  109. static inline
  110. void d_stdout(const char* const fmt, ...) noexcept
  111. {
  112. try {
  113. va_list args;
  114. va_start(args, fmt);
  115. std::vfprintf(stdout, fmt, args);
  116. std::fprintf(stdout, "\n");
  117. va_end(args);
  118. } catch (...) {}
  119. }
  120. /**
  121. Print a string to stderr with newline.
  122. */
  123. static inline
  124. void d_stderr(const char* const fmt, ...) noexcept
  125. {
  126. try {
  127. va_list args;
  128. va_start(args, fmt);
  129. std::vfprintf(stderr, fmt, args);
  130. std::fprintf(stderr, "\n");
  131. va_end(args);
  132. } catch (...) {}
  133. }
  134. /**
  135. Print a string to stderr with newline (red color).
  136. */
  137. static inline
  138. void d_stderr2(const char* const fmt, ...) noexcept
  139. {
  140. try {
  141. va_list args;
  142. va_start(args, fmt);
  143. std::fprintf(stderr, "\x1b[31m");
  144. std::vfprintf(stderr, fmt, args);
  145. std::fprintf(stderr, "\x1b[0m\n");
  146. va_end(args);
  147. } catch (...) {}
  148. }
  149. /**
  150. Print a safe assertion error message.
  151. */
  152. static inline
  153. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  154. {
  155. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  156. }
  157. /**
  158. Print a safe assertion error message, with 1 extra signed integer value.
  159. */
  160. static inline
  161. void d_safe_assert_int(const char* const assertion, const char* const file,
  162. const int line, const int value) noexcept
  163. {
  164. d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  165. }
  166. /**
  167. Print a safe assertion error message, with 1 extra unsigned integer value.
  168. */
  169. static inline
  170. void d_safe_assert_uint(const char* const assertion, const char* const file,
  171. const int line, const uint value) noexcept
  172. {
  173. d_stderr2("assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  174. }
  175. /**
  176. Print a safe assertion error message, with 2 extra signed integer values.
  177. */
  178. static inline
  179. void d_safe_assert_int2(const char* const assertion, const char* const file,
  180. const int line, const int v1, const int v2) noexcept
  181. {
  182. d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  183. }
  184. /**
  185. Print a safe assertion error message, with 2 extra unsigned integer values.
  186. */
  187. static inline
  188. void d_safe_assert_uint2(const char* const assertion, const char* const file,
  189. const int line, const uint v1, const uint v2) noexcept
  190. {
  191. d_stderr2("assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  192. }
  193. /**
  194. Print a safe assertion error message, with a custom error message.
  195. */
  196. static inline
  197. void d_custom_safe_assert(const char* const message, const char* const assertion, const char* const file,
  198. const int line) noexcept
  199. {
  200. d_stderr2("assertion failure: %s, condition \"%s\" in file %s, line %i", message, assertion, file, line);
  201. }
  202. /**
  203. Print a safe exception error message.
  204. */
  205. static inline
  206. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  207. {
  208. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  209. }
  210. /** @} */
  211. /* ------------------------------------------------------------------------------------------------------------
  212. * math functions */
  213. /**
  214. @defgroup MathFunctions Math related functions
  215. @{
  216. */
  217. /**
  218. Safely compare two floating point numbers.
  219. Returns true if they match.
  220. */
  221. template<typename T>
  222. static inline
  223. bool d_isEqual(const T& v1, const T& v2)
  224. {
  225. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  226. }
  227. /**
  228. Safely compare two floating point numbers.
  229. Returns true if they don't match.
  230. */
  231. template<typename T>
  232. static inline
  233. bool d_isNotEqual(const T& v1, const T& v2)
  234. {
  235. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  236. }
  237. /**
  238. Safely check if a floating point number is zero.
  239. */
  240. template<typename T>
  241. static inline
  242. bool d_isZero(const T& value)
  243. {
  244. return std::abs(value) < std::numeric_limits<T>::epsilon();
  245. }
  246. /**
  247. Safely check if a floating point number is not zero.
  248. */
  249. template<typename T>
  250. static inline
  251. bool d_isNotZero(const T& value)
  252. {
  253. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  254. }
  255. /**
  256. Get next power of 2.
  257. */
  258. static inline
  259. uint32_t d_nextPowerOf2(uint32_t size) noexcept
  260. {
  261. DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
  262. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  263. --size;
  264. size |= size >> 1;
  265. size |= size >> 2;
  266. size |= size >> 4;
  267. size |= size >> 8;
  268. size |= size >> 16;
  269. return ++size;
  270. }
  271. /** @} */
  272. // -----------------------------------------------------------------------
  273. #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
  274. // If your code uses a lot of DISTRHO classes, then this will obviously save you
  275. // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
  276. namespace DISTRHO_NAMESPACE {}
  277. using namespace DISTRHO_NAMESPACE;
  278. #endif
  279. // -----------------------------------------------------------------------
  280. #endif // DISTRHO_UTILS_HPP_INCLUDED