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

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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_MAC) && ! defined(CARLA_OS_MAC) && ! defined(DISTRHO_PROPER_CPP11_SUPPORT)
  31. namespace std {
  32. inline float fmin(float __x, float __y)
  33. { return __builtin_fminf(__x, __y); }
  34. inline float fmax(float __x, float __y)
  35. { return __builtin_fmaxf(__x, __y); }
  36. inline float rint(float __x)
  37. { return __builtin_rintf(__x); }
  38. inline float round(float __x)
  39. { return __builtin_roundf(__x); }
  40. }
  41. #endif
  42. #ifndef M_PI
  43. # define M_PI 3.14159265358979323846
  44. #endif
  45. // -----------------------------------------------------------------------
  46. // misc functions
  47. /*
  48. * Return a 64-bit number from 4 8-bit numbers.
  49. */
  50. static inline
  51. int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  52. {
  53. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  54. }
  55. /*
  56. * Return an hexadecimal representation of a MAJ.MIN.MICRO version number.
  57. */
  58. static inline
  59. uint32_t d_version(const uint8_t major, const uint8_t minor, const uint8_t micro) noexcept
  60. {
  61. return uint32_t(major << 16) | uint32_t(minor << 8) | (micro << 0);
  62. }
  63. /*
  64. * Dummy function.
  65. */
  66. static inline
  67. void d_pass() noexcept {}
  68. // -----------------------------------------------------------------------
  69. // string print functions
  70. /*
  71. * Print a string to stdout with newline (gray color).
  72. * Does nothing if DEBUG is not defined.
  73. */
  74. #ifndef DEBUG
  75. # define d_debug(...)
  76. #else
  77. static inline
  78. void d_debug(const char* const fmt, ...) noexcept
  79. {
  80. try {
  81. ::va_list args;
  82. ::va_start(args, fmt);
  83. std::fprintf(stdout, "\x1b[30;1m");
  84. std::vfprintf(stdout, fmt, args);
  85. std::fprintf(stdout, "\x1b[0m\n");
  86. ::va_end(args);
  87. } catch (...) {}
  88. }
  89. #endif
  90. /*
  91. * Print a string to stdout with newline.
  92. */
  93. static inline
  94. void d_stdout(const char* const fmt, ...) noexcept
  95. {
  96. try {
  97. ::va_list args;
  98. ::va_start(args, fmt);
  99. std::vfprintf(stdout, fmt, args);
  100. std::fprintf(stdout, "\n");
  101. ::va_end(args);
  102. } catch (...) {}
  103. }
  104. /*
  105. * Print a string to stderr with newline.
  106. */
  107. static inline
  108. void d_stderr(const char* const fmt, ...) noexcept
  109. {
  110. try {
  111. ::va_list args;
  112. ::va_start(args, fmt);
  113. std::vfprintf(stderr, fmt, args);
  114. std::fprintf(stderr, "\n");
  115. ::va_end(args);
  116. } catch (...) {}
  117. }
  118. /*
  119. * Print a string to stderr with newline (red color).
  120. */
  121. static inline
  122. void d_stderr2(const char* const fmt, ...) noexcept
  123. {
  124. try {
  125. ::va_list args;
  126. ::va_start(args, fmt);
  127. std::fprintf(stderr, "\x1b[31m");
  128. std::vfprintf(stderr, fmt, args);
  129. std::fprintf(stderr, "\x1b[0m\n");
  130. ::va_end(args);
  131. } catch (...) {}
  132. }
  133. /*
  134. * Print a safe assertion error message.
  135. */
  136. static inline
  137. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  138. {
  139. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  140. }
  141. /*
  142. * Print a safe exception error message.
  143. */
  144. static inline
  145. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  146. {
  147. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  148. }
  149. // -----------------------------------------------------------------------
  150. // math functions
  151. /*
  152. * Safely compare two floating point numbers.
  153. * Returns true if they match.
  154. */
  155. template<typename T>
  156. static inline
  157. bool d_isEqual(const T& v1, const T& v2)
  158. {
  159. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  160. }
  161. /*
  162. * Safely compare two floating point numbers.
  163. * Returns true if they don't match.
  164. */
  165. template<typename T>
  166. static inline
  167. bool d_isNotEqual(const T& v1, const T& v2)
  168. {
  169. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  170. }
  171. /*
  172. * Safely check if a floating point number is zero.
  173. */
  174. template<typename T>
  175. static inline
  176. bool d_isZero(const T& value)
  177. {
  178. return std::abs(value) < std::numeric_limits<T>::epsilon();
  179. }
  180. /*
  181. * Safely check if a floating point number is not zero.
  182. */
  183. template<typename T>
  184. static inline
  185. bool d_isNotZero(const T& value)
  186. {
  187. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  188. }
  189. /*
  190. * Get next power of 2.
  191. */
  192. static inline
  193. uint32_t d_nextPowerOf2(uint32_t size) noexcept
  194. {
  195. DISTRHO_SAFE_ASSERT_RETURN(size > 0, 0);
  196. // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  197. --size;
  198. size |= size >> 1;
  199. size |= size >> 2;
  200. size |= size >> 4;
  201. size |= size >> 8;
  202. size |= size >> 16;
  203. return ++size;
  204. }
  205. // -----------------------------------------------------------------------
  206. #ifndef DONT_SET_USING_DISTRHO_NAMESPACE
  207. // If your code uses a lot of DISTRHO classes, then this will obviously save you
  208. // a lot of typing, but can be disabled by setting DONT_SET_USING_DISTRHO_NAMESPACE.
  209. namespace DISTRHO_NAMESPACE {}
  210. using namespace DISTRHO_NAMESPACE;
  211. #endif
  212. // -----------------------------------------------------------------------
  213. #endif // DISTRHO_UTILS_HPP_INCLUDED