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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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. // -----------------------------------------------------------------------
  43. // misc functions
  44. /*
  45. * Return a 64-bit number from 4 8-bit numbers.
  46. */
  47. static inline
  48. int64_t d_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  49. {
  50. return (a << 24) | (b << 16) | (c << 8) | (d << 0);
  51. }
  52. /*
  53. * Dummy function.
  54. */
  55. static inline
  56. void d_pass() noexcept {}
  57. // -----------------------------------------------------------------------
  58. // string print functions
  59. /*
  60. * Print a string to stdout with newline (gray color).
  61. * Does nothing if DEBUG is not defined.
  62. */
  63. #ifndef DEBUG
  64. # define d_debug(...)
  65. #else
  66. static inline
  67. void d_debug(const char* const fmt, ...) noexcept
  68. {
  69. try {
  70. ::va_list args;
  71. ::va_start(args, fmt);
  72. std::fprintf(stdout, "\x1b[30;1m");
  73. std::vfprintf(stdout, fmt, args);
  74. std::fprintf(stdout, "\x1b[0m\n");
  75. ::va_end(args);
  76. } catch (...) {}
  77. }
  78. #endif
  79. /*
  80. * Print a string to stdout with newline.
  81. */
  82. static inline
  83. void d_stdout(const char* const fmt, ...) noexcept
  84. {
  85. try {
  86. ::va_list args;
  87. ::va_start(args, fmt);
  88. std::vfprintf(stdout, fmt, args);
  89. std::fprintf(stdout, "\n");
  90. ::va_end(args);
  91. } catch (...) {}
  92. }
  93. /*
  94. * Print a string to stderr with newline.
  95. */
  96. static inline
  97. void d_stderr(const char* const fmt, ...) noexcept
  98. {
  99. try {
  100. ::va_list args;
  101. ::va_start(args, fmt);
  102. std::vfprintf(stderr, fmt, args);
  103. std::fprintf(stderr, "\n");
  104. ::va_end(args);
  105. } catch (...) {}
  106. }
  107. /*
  108. * Print a string to stderr with newline (red color).
  109. */
  110. static inline
  111. void d_stderr2(const char* const fmt, ...) noexcept
  112. {
  113. try {
  114. ::va_list args;
  115. ::va_start(args, fmt);
  116. std::fprintf(stderr, "\x1b[31m");
  117. std::vfprintf(stderr, fmt, args);
  118. std::fprintf(stderr, "\x1b[0m\n");
  119. ::va_end(args);
  120. } catch (...) {}
  121. }
  122. /*
  123. * Print a safe assertion error message.
  124. */
  125. static inline
  126. void d_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  127. {
  128. d_stderr2("assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  129. }
  130. /*
  131. * Print a safe exception error message.
  132. */
  133. static inline
  134. void d_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  135. {
  136. d_stderr2("exception caught: \"%s\" in file %s, line %i", exception, file, line);
  137. }
  138. // -----------------------------------------------------------------------
  139. // math functions
  140. /*
  141. * Safely compare two floating point numbers.
  142. * Returns true if they match.
  143. */
  144. template<typename T>
  145. static inline
  146. bool d_isEqual(const T& v1, const T& v2)
  147. {
  148. return std::abs(v1-v2) < std::numeric_limits<T>::epsilon();
  149. }
  150. /*
  151. * Safely compare two floating point numbers.
  152. * Returns true if they don't match.
  153. */
  154. template<typename T>
  155. static inline
  156. bool d_isNotEqual(const T& v1, const T& v2)
  157. {
  158. return std::abs(v1-v2) >= std::numeric_limits<T>::epsilon();
  159. }
  160. /*
  161. * Safely check if a floating point number is zero.
  162. */
  163. template<typename T>
  164. static inline
  165. bool d_isZero(const T& value)
  166. {
  167. return std::abs(value) < std::numeric_limits<T>::epsilon();
  168. }
  169. /*
  170. * Safely check if a floating point number is not zero.
  171. */
  172. template<typename T>
  173. static inline
  174. bool d_isNotZero(const T& value)
  175. {
  176. return std::abs(value) >= std::numeric_limits<T>::epsilon();
  177. }
  178. // -----------------------------------------------------------------------
  179. #endif // DISTRHO_UTILS_HPP_INCLUDED