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.

502 lines
11KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2014 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.h"
  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. #ifdef CARLA_OS_WIN
  31. # include <winsock2.h>
  32. # include <windows.h>
  33. #else
  34. # include <unistd.h>
  35. #endif
  36. // -----------------------------------------------------------------------
  37. // misc functions
  38. /*
  39. * Return "true" or "false" according to yesNo.
  40. */
  41. static inline
  42. const char* bool2str(const bool yesNo) noexcept
  43. {
  44. return yesNo ? "true" : "false";
  45. }
  46. /*
  47. * Dummy function.
  48. */
  49. static inline
  50. void pass() noexcept {}
  51. // -----------------------------------------------------------------------
  52. // string print functions
  53. /*
  54. * Print a string to stdout with newline (gray color).
  55. * Does nothing if DEBUG is not defined.
  56. */
  57. #ifndef DEBUG
  58. # define carla_debug(...)
  59. #else
  60. static inline
  61. void carla_debug(const char* const fmt, ...) noexcept
  62. {
  63. try {
  64. va_list args;
  65. va_start(args, fmt);
  66. std::fprintf(stdout, "\x1b[30;1m");
  67. std::vfprintf(stdout, fmt, args);
  68. std::fprintf(stdout, "\x1b[0m\n");
  69. va_end(args);
  70. } catch (...) {}
  71. }
  72. #endif
  73. /*
  74. * Print a string to stdout with newline.
  75. */
  76. static inline
  77. void carla_stdout(const char* const fmt, ...) noexcept
  78. {
  79. try {
  80. va_list args;
  81. va_start(args, fmt);
  82. std::vfprintf(stdout, fmt, args);
  83. std::fprintf(stdout, "\n");
  84. va_end(args);
  85. } catch (...) {}
  86. }
  87. /*
  88. * Print a string to stderr with newline.
  89. */
  90. static inline
  91. void carla_stderr(const char* const fmt, ...) noexcept
  92. {
  93. try {
  94. va_list args;
  95. va_start(args, fmt);
  96. std::vfprintf(stderr, fmt, args);
  97. std::fprintf(stderr, "\n");
  98. va_end(args);
  99. } catch (...) {}
  100. }
  101. /*
  102. * Print a string to stderr with newline (red color).
  103. */
  104. static inline
  105. void carla_stderr2(const char* const fmt, ...) noexcept
  106. {
  107. try {
  108. va_list args;
  109. va_start(args, fmt);
  110. std::fprintf(stderr, "\x1b[31m");
  111. std::vfprintf(stderr, fmt, args);
  112. std::fprintf(stderr, "\x1b[0m\n");
  113. va_end(args);
  114. } catch (...) {}
  115. }
  116. // -----------------------------------------------------------------------
  117. // carla_safe_assert*
  118. /*
  119. * Print a safe assertion error message.
  120. */
  121. static inline
  122. void carla_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  123. {
  124. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  125. }
  126. /*
  127. * Print a safe assertion error message, with 1 extra integer value.
  128. */
  129. static inline
  130. void carla_safe_assert_int(const char* const assertion, const char* const file, const int line, const int value) noexcept
  131. {
  132. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  133. }
  134. /*
  135. * Print a safe assertion error message, with 2 extra integer values.
  136. */
  137. static inline
  138. void carla_safe_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2) noexcept
  139. {
  140. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  141. }
  142. // -----------------------------------------------------------------------
  143. // carla_*sleep
  144. /*
  145. * Sleep for 'secs' seconds.
  146. */
  147. static inline
  148. void carla_sleep(const unsigned int secs)
  149. {
  150. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  151. #ifdef CARLA_OS_WIN
  152. Sleep(secs * 1000);
  153. #else
  154. sleep(secs);
  155. #endif
  156. }
  157. /*
  158. * Sleep for 'msecs' milliseconds.
  159. */
  160. static inline
  161. void carla_msleep(const unsigned int msecs)
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  164. #ifdef CARLA_OS_WIN
  165. Sleep(msecs);
  166. #else
  167. usleep(msecs * 1000);
  168. #endif
  169. }
  170. // -----------------------------------------------------------------------
  171. // carla_setenv
  172. /*
  173. * Set environment variable 'key' to 'value'.
  174. */
  175. static inline
  176. void carla_setenv(const char* const key, const char* const value) noexcept
  177. {
  178. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  179. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  180. #ifdef CARLA_OS_WIN
  181. SetEnvironmentVariableA(key, value);
  182. #else
  183. setenv(key, value, 1);
  184. #endif
  185. }
  186. // -----------------------------------------------------------------------
  187. // carla_strdup
  188. /*
  189. * Custom 'strdup' function.
  190. * Return value is always valid, and must be freed with "delete[] var".
  191. */
  192. static inline
  193. const char* carla_strdup(const char* const strBuf)
  194. {
  195. CARLA_SAFE_ASSERT(strBuf != nullptr);
  196. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  197. char* const buffer = new char[bufferLen+1];
  198. if (strBuf != nullptr && bufferLen > 0)
  199. std::strncpy(buffer, strBuf, bufferLen);
  200. buffer[bufferLen] = '\0';
  201. return buffer;
  202. }
  203. /*
  204. * Custom 'strdup' function.
  205. * Calls "std::free(strBuf)".
  206. * Return value is always valid, and must be freed with "delete[] var".
  207. */
  208. static inline
  209. const char* carla_strdup_free(char* const strBuf)
  210. {
  211. const char* const buffer(carla_strdup(strBuf));
  212. std::free(strBuf);
  213. return buffer;
  214. }
  215. // -----------------------------------------------------------------------
  216. // math functions (base)
  217. /*
  218. * Return the lower of 2 values, with 'min' as the minimum possible value.
  219. */
  220. template<typename T>
  221. static inline
  222. const T& carla_min(const T& v1, const T& v2, const T& min) noexcept
  223. {
  224. return ((v1 <= min || v2 <= min) ? min : (v1 < v2 ? v1 : v2));
  225. }
  226. /*
  227. * Return the higher of 2 values, with 'max' as the maximum possible value.
  228. */
  229. template<typename T>
  230. static inline
  231. const T& carla_max(const T& v1, const T& v2, const T& max) noexcept
  232. {
  233. return ((v1 >= max || v2 >= max) ? max : (v1 > v2 ? v1 : v2));
  234. }
  235. /*
  236. * Fix bounds of 'value', between 'min' and 'max'.
  237. */
  238. template<typename T>
  239. static inline
  240. const T& carla_fixValue(const T& min, const T& max, const T& value) noexcept
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(max > min, max);
  243. if (value <= min)
  244. return min;
  245. if (value >= max)
  246. return max;
  247. return value;
  248. }
  249. /*
  250. * Add array values to another array.
  251. */
  252. template<typename T>
  253. static inline
  254. void carla_add(T* dataDst, T* dataSrc, const size_t size) noexcept
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  257. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  258. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  259. for (size_t i=0; i < size; ++i)
  260. *dataDst++ += *dataSrc++;
  261. }
  262. /*
  263. * Add array values to another array.
  264. */
  265. template<typename T>
  266. static inline
  267. void carla_add(T* dataDst, const T* dataSrc, const size_t size) noexcept
  268. {
  269. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  270. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  271. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  272. for (size_t i=0; i < size; ++i)
  273. *dataDst++ += *dataSrc++;
  274. }
  275. /*
  276. * Copy array values to another array.
  277. */
  278. template<typename T>
  279. static inline
  280. void carla_copy(T* dataDst, T* dataSrc, const size_t size) noexcept
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  283. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  284. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  285. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  286. }
  287. /*
  288. * Copy array values to another array.
  289. */
  290. template<typename T>
  291. static inline
  292. void carla_copy(T* dataDst, const T* dataSrc, const size_t size) noexcept
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  295. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  296. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  297. std::memcpy(dataDst, dataSrc, size*sizeof(T));
  298. }
  299. /*
  300. * Fill an array with a fixed value.
  301. */
  302. template<typename T>
  303. static inline
  304. void carla_fill(T* data, const size_t size, const T v) noexcept
  305. {
  306. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  307. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  308. if (v == 0)
  309. {
  310. std::memset(data, 0, size*sizeof(T));
  311. }
  312. else
  313. {
  314. for (size_t i=0; i < size; ++i)
  315. *data++ = v;
  316. }
  317. }
  318. // -----------------------------------------------------------------------
  319. // math functions (extended)
  320. /*
  321. * Add float array values to another float array.
  322. */
  323. static inline
  324. void carla_addFloat(float* dataDst, float* dataSrc, const size_t numSamples) noexcept
  325. {
  326. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  327. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  328. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  329. for (size_t i=0; i < numSamples; ++i)
  330. *dataDst++ += *dataSrc++;
  331. }
  332. /*
  333. * Copy float array values to another float array.
  334. */
  335. static inline
  336. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t numSamples) noexcept
  337. {
  338. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  339. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  340. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  341. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  342. }
  343. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  344. /*
  345. * Missing functions in OSX.
  346. */
  347. namespace std {
  348. inline float
  349. fmin(float __x, float __y)
  350. { return __builtin_fminf(__x, __y); }
  351. inline float
  352. fmax(float __x, float __y)
  353. { return __builtin_fmaxf(__x, __y); }
  354. inline float
  355. rint(float __x)
  356. { return __builtin_rintf(__x); }
  357. }
  358. #endif
  359. // -----------------------------------------------------------------------
  360. // memory functions
  361. /*
  362. * Clear a char array.
  363. */
  364. static inline
  365. void carla_zeroChar(char* const data, const size_t numChars) noexcept
  366. {
  367. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  368. CARLA_SAFE_ASSERT_RETURN(numChars > 0,);
  369. std::memset(data, 0, numChars*sizeof(char));
  370. }
  371. /*
  372. * Clear a float array.
  373. */
  374. static inline
  375. void carla_zeroFloat(float* const data, const size_t numSamples) noexcept
  376. {
  377. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  378. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  379. std::memset(data, 0, numSamples*sizeof(float));
  380. }
  381. /*
  382. * Clear a memory location.
  383. */
  384. static inline
  385. void carla_zeroMem(void* const memory, const size_t numBytes) noexcept
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(memory != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(numBytes > 0,);
  389. std::memset(memory, 0, numBytes);
  390. }
  391. /*
  392. * Clear a single struct/class.
  393. */
  394. template <typename T>
  395. static inline
  396. void carla_zeroStruct(T& structure) noexcept
  397. {
  398. std::memset(&structure, 0, sizeof(T));
  399. }
  400. /*
  401. * Clear an array of struct/class.
  402. */
  403. template <typename T>
  404. static inline
  405. void carla_zeroStruct(T* const structure, const size_t count) noexcept
  406. {
  407. CARLA_SAFE_ASSERT_RETURN(structure != nullptr,);
  408. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  409. std::memset(structure, 0, count*sizeof(T));
  410. }
  411. /*
  412. * Copy a single struct/class.
  413. */
  414. template <typename T>
  415. static inline
  416. void carla_copyStruct(T& struct1, T& struct2) noexcept
  417. {
  418. std::memcpy(&struct1, &struct2, sizeof(T));
  419. }
  420. /*
  421. * Copy an array of struct/class.
  422. */
  423. template <typename T>
  424. static inline
  425. void carla_copyStruct(T* const struct1, T* const struct2, const size_t count) noexcept
  426. {
  427. CARLA_SAFE_ASSERT_RETURN(struct1 != nullptr,);
  428. CARLA_SAFE_ASSERT_RETURN(struct2 != nullptr,);
  429. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  430. std::memcpy(struct1, struct2, count*sizeof(T));
  431. }
  432. // -----------------------------------------------------------------------
  433. #endif // CARLA_UTILS_HPP_INCLUDED