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.

234 lines
5.7KB

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