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.

411 lines
9.2KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_UTILS_HPP_INCLUDED
  18. #define CARLA_UTILS_HPP_INCLUDED
  19. #include "CarlaDefines.hpp"
  20. #include <cassert>
  21. #include <cstdarg>
  22. #include <cstdio>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #ifdef CARLA_PROPER_CPP11_SUPPORT
  26. # include <cstdint>
  27. #else
  28. # include <stdint.h>
  29. #endif
  30. // -----------------------------------------------------------------------
  31. // misc functions
  32. /*
  33. * Return "true" or "false" according to yesNo.
  34. */
  35. static inline
  36. const char* bool2str(const bool yesNo) noexcept
  37. {
  38. return yesNo ? "true" : "false";
  39. }
  40. /*
  41. * Dummy function.
  42. */
  43. static inline
  44. void pass() noexcept {}
  45. // -----------------------------------------------------------------------
  46. // string print functions
  47. /*
  48. * Print a string to stdout with newline (gray color).
  49. * Does nothing if DEBUG is not defined.
  50. */
  51. #ifndef DEBUG
  52. # define carla_debug(...)
  53. #else
  54. static inline
  55. void carla_debug(const char* const fmt, ...)
  56. {
  57. va_list args;
  58. va_start(args, fmt);
  59. std::fprintf(stdout, "\x1b[30;1m");
  60. std::vfprintf(stdout, fmt, args);
  61. std::fprintf(stdout, "\x1b[0m\n");
  62. va_end(args);
  63. }
  64. #endif
  65. /*
  66. * Print a string to stdout with newline.
  67. */
  68. static inline
  69. void carla_stdout(const char* const fmt, ...)
  70. {
  71. va_list args;
  72. va_start(args, fmt);
  73. std::vfprintf(stdout, fmt, args);
  74. std::fprintf(stdout, "\n");
  75. va_end(args);
  76. }
  77. /*
  78. * Print a string to stderr with newline.
  79. */
  80. static inline
  81. void carla_stderr(const char* const fmt, ...)
  82. {
  83. va_list args;
  84. va_start(args, fmt);
  85. std::vfprintf(stderr, fmt, args);
  86. std::fprintf(stderr, "\n");
  87. va_end(args);
  88. }
  89. /*
  90. * Print a string to stderr with newline (red color).
  91. */
  92. static inline
  93. void carla_stderr2(const char* const fmt, ...)
  94. {
  95. va_list args;
  96. va_start(args, fmt);
  97. std::fprintf(stderr, "\x1b[31m");
  98. std::vfprintf(stderr, fmt, args);
  99. std::fprintf(stderr, "\x1b[0m\n");
  100. va_end(args);
  101. }
  102. // -----------------------------------------------------------------------
  103. // carla_safe_assert*
  104. /*
  105. * Print a safe assertion error message.
  106. */
  107. static inline
  108. void carla_safe_assert(const char* const assertion, const char* const file, const int line)
  109. {
  110. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  111. }
  112. /*
  113. * Print a safe assertion error message, with 1 extra integer value.
  114. */
  115. static inline
  116. void carla_safe_assert_int(const char* const assertion, const char* const file, const int line, const int value)
  117. {
  118. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  119. }
  120. /*
  121. * Print a safe assertion error message, with 2 extra integer values.
  122. */
  123. static inline
  124. void carla_safe_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2)
  125. {
  126. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  127. }
  128. // -----------------------------------------------------------------------
  129. // carla_*sleep
  130. /*
  131. * Sleep for 'secs' seconds.
  132. */
  133. static inline
  134. void carla_sleep(const unsigned int secs)
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  137. #ifdef CARLA_OS_WIN
  138. Sleep(secs * 1000);
  139. #else
  140. sleep(secs);
  141. #endif
  142. }
  143. /*
  144. * Sleep for 'msecs' milliseconds.
  145. */
  146. static inline
  147. void carla_msleep(const unsigned int msecs)
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  150. #ifdef CARLA_OS_WIN
  151. Sleep(msecs);
  152. #else
  153. usleep(msecs * 1000);
  154. #endif
  155. }
  156. // -----------------------------------------------------------------------
  157. // carla_setenv
  158. /*
  159. * Set environment variable 'key' to 'value'.
  160. */
  161. static inline
  162. void carla_setenv(const char* const key, const char* const value)
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(key != nullptr,);
  165. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  166. #ifdef CARLA_OS_WIN
  167. SetEnvironmentVariableA(key, value);
  168. #else
  169. setenv(key, value, 1);
  170. #endif
  171. }
  172. // -----------------------------------------------------------------------
  173. // carla_strdup
  174. /*
  175. * Custom 'strdup' function.
  176. * Return value is always valid, and must be freed with "delete[] var".
  177. */
  178. static inline
  179. const char* carla_strdup(const char* const strBuf)
  180. {
  181. CARLA_SAFE_ASSERT(strBuf != nullptr);
  182. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  183. char* const buffer = new char[bufferLen+1];
  184. if (strBuf != nullptr && bufferLen > 0)
  185. std::strncpy(buffer, strBuf, bufferLen);
  186. buffer[bufferLen] = '\0';
  187. return buffer;
  188. }
  189. /*
  190. * Custom 'strdup' function.
  191. * Calls "std::free(strBuf)".
  192. * Return value is always valid, and must be freed with "delete[] var".
  193. */
  194. static inline
  195. const char* carla_strdup_free(char* const strBuf)
  196. {
  197. const char* const buffer(carla_strdup(strBuf));
  198. std::free(strBuf);
  199. return buffer;
  200. }
  201. // -----------------------------------------------------------------------
  202. // math functions
  203. /*
  204. * Return the lower of 2 values, with 'min' as the minimum possible value.
  205. */
  206. template<typename T>
  207. static inline
  208. const T& carla_min(const T& v1, const T& v2, const T& min) noexcept
  209. {
  210. return ((v1 <= min || v2 <= min) ? min : (v1 < v2 ? v1 : v2));
  211. }
  212. /*
  213. * Return the higher of 2 values, with 'max' as the maximum possible value.
  214. */
  215. template<typename T>
  216. static inline
  217. const T& carla_max(const T& v1, const T& v2, const T& max) noexcept
  218. {
  219. return ((v1 >= max || v2 >= max) ? max : (v1 > v2 ? v1 : v2));
  220. }
  221. /*
  222. * Fix bounds of 'value', between 'min' and 'max'.
  223. */
  224. template<typename T>
  225. static inline
  226. const T& carla_fixValue(const T& min, const T& max, const T& value)
  227. {
  228. CARLA_SAFE_ASSERT_RETURN(max > min, max);
  229. if (value <= min)
  230. return min;
  231. if (value >= max)
  232. return max;
  233. return value;
  234. }
  235. /*
  236. * Add array values to another array.
  237. */
  238. template<typename T>
  239. static inline
  240. void carla_add(T* dataDst, T* dataSrc, const size_t size)
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  243. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  244. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  245. for (size_t i=0; i < size; ++i)
  246. *dataDst++ += *dataSrc++;
  247. }
  248. /*
  249. * Add array values to another array.
  250. */
  251. template<typename T>
  252. static inline
  253. void carla_add(T* dataDst, const T* dataSrc, const size_t size)
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  256. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  257. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  258. for (size_t i=0; i < size; ++i)
  259. *dataDst++ += *dataSrc++;
  260. }
  261. /*
  262. * Fill an array with a fixed value.
  263. */
  264. template<typename T>
  265. static inline
  266. void carla_fill(T* data, const size_t size, const T v)
  267. {
  268. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  269. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  270. for (size_t i=0; i < size; ++i)
  271. *data++ = v;
  272. }
  273. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  274. /*
  275. * Missing functions in OSX.
  276. */
  277. namespace std {
  278. inline float
  279. fmin(float __x, float __y)
  280. { return __builtin_fminf(__x, __y); }
  281. inline float
  282. fmax(float __x, float __y)
  283. { return __builtin_fmaxf(__x, __y); }
  284. inline float
  285. rint(float __x)
  286. { return __builtin_rintf(__x); }
  287. }
  288. #endif
  289. // -----------------------------------------------------------------------
  290. // memory functions
  291. /*
  292. * Clear a char array.
  293. */
  294. template<typename C = char>
  295. static inline
  296. void carla_zeroChar(C* const data, const size_t numChars)
  297. {
  298. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  299. CARLA_SAFE_ASSERT_RETURN(numChars > 0,);
  300. std::memset(data, 0, numChars*sizeof(C));
  301. }
  302. /*
  303. * Clear a memory location.
  304. */
  305. static inline
  306. void carla_zeroMem(void* const memory, const size_t numBytes)
  307. {
  308. CARLA_SAFE_ASSERT_RETURN(memory != nullptr,);
  309. CARLA_SAFE_ASSERT_RETURN(numBytes > 0,);
  310. std::memset(memory, 0, numBytes);
  311. }
  312. /*
  313. * Clear a single struct/class.
  314. */
  315. template <typename T>
  316. static inline
  317. void carla_zeroStruct(T& structure)
  318. {
  319. std::memset(&structure, 0, sizeof(T));
  320. }
  321. /*
  322. * Clear an array of struct/class.
  323. */
  324. template <typename T>
  325. static inline
  326. void carla_zeroStruct(T* const structure, const size_t count)
  327. {
  328. CARLA_SAFE_ASSERT_RETURN(structure != nullptr,);
  329. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  330. std::memset(structure, 0, count*sizeof(T));
  331. }
  332. /*
  333. * Copy a single struct/class.
  334. */
  335. template <typename T>
  336. static inline
  337. void carla_copyStruct(T& struct1, T& struct2)
  338. {
  339. std::memcpy(&struct1, &struct2, sizeof(T));
  340. }
  341. /*
  342. * Copy an array of struct/class.
  343. */
  344. template <typename T>
  345. static inline
  346. void carla_copyStruct(T* const struct1, T* const struct2, const size_t count)
  347. {
  348. CARLA_SAFE_ASSERT_RETURN(struct1 != nullptr,);
  349. CARLA_SAFE_ASSERT_RETURN(struct2 != nullptr,);
  350. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  351. std::memcpy(struct1, struct2, count*sizeof(T));
  352. }
  353. // -----------------------------------------------------------------------
  354. #endif // CARLA_UTILS_HPP_INCLUDED