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.

CarlaUtils.hpp 14KB

8 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
11 years ago
11 years ago
9 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
8 years ago
10 years ago
10 years ago
8 years ago
9 years ago
11 years ago
8 years ago
8 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
8 years ago
9 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2016 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. # define WIN32_LEAN_AND_MEAN 1
  32. # include <winsock2.h>
  33. # include <windows.h>
  34. #else
  35. # include <unistd.h>
  36. #endif
  37. // --------------------------------------------------------------------------------------------------------------------
  38. // misc functions
  39. /*
  40. * Return "true" or "false" according to yesNo.
  41. */
  42. static inline
  43. const char* bool2str(const bool yesNo) noexcept
  44. {
  45. return yesNo ? "true" : "false";
  46. }
  47. /*
  48. * Set a string as empty/null.
  49. */
  50. static inline
  51. void nullStrBuf(char* const strBuf) noexcept
  52. {
  53. strBuf[0] = '\0';
  54. }
  55. /*
  56. * Dummy function.
  57. */
  58. static inline
  59. void pass() noexcept {}
  60. // --------------------------------------------------------------------------------------------------------------------
  61. // string print functions
  62. /*
  63. * Internal noexcept-safe fopen function.
  64. */
  65. static inline
  66. FILE* __carla_fopen(const char* const filename, FILE* const fallback) noexcept
  67. {
  68. #ifdef CARLA_OS_LINUX
  69. if (std::getenv("CARLA_CAPTURE_CONSOLE_OUTPUT") == nullptr)
  70. return fallback;
  71. FILE* ret = nullptr;
  72. try {
  73. ret = std::fopen(filename, "a+");
  74. } catch (...) {}
  75. if (ret == nullptr)
  76. ret = fallback;
  77. return ret;
  78. #else
  79. return fallback;
  80. // unused
  81. (void)filename;
  82. #endif
  83. }
  84. /*
  85. * Print a string to stdout with newline (gray color).
  86. * Does nothing if DEBUG is not defined.
  87. */
  88. #ifndef DEBUG
  89. # define carla_debug(...)
  90. #else
  91. static inline
  92. void carla_debug(const char* const fmt, ...) noexcept
  93. {
  94. static FILE* const output = __carla_fopen("/tmp/carla.debug.log", stdout);
  95. try {
  96. ::va_list args;
  97. ::va_start(args, fmt);
  98. if (output == stdout)
  99. {
  100. std::fprintf(output, "\x1b[30;1m");
  101. std::vfprintf(output, fmt, args);
  102. std::fprintf(output, "\x1b[0m\n");
  103. }
  104. else
  105. {
  106. std::vfprintf(output, fmt, args);
  107. std::fprintf(output, "\n");
  108. std::fflush(output);
  109. }
  110. ::va_end(args);
  111. } catch (...) {}
  112. }
  113. #endif
  114. /*
  115. * Print a string to stdout with newline.
  116. */
  117. static inline
  118. void carla_stdout(const char* const fmt, ...) noexcept
  119. {
  120. static FILE* const output = __carla_fopen("/tmp/carla.stdout.log", stdout);
  121. try {
  122. ::va_list args;
  123. ::va_start(args, fmt);
  124. std::vfprintf(output, fmt, args);
  125. std::fprintf(output, "\n");
  126. if (output != stdout)
  127. std::fflush(output);
  128. ::va_end(args);
  129. } catch (...) {}
  130. }
  131. /*
  132. * Print a string to stderr with newline.
  133. */
  134. static inline
  135. void carla_stderr(const char* const fmt, ...) noexcept
  136. {
  137. static FILE* const output = __carla_fopen("/tmp/carla.stderr.log", stderr);
  138. try {
  139. ::va_list args;
  140. ::va_start(args, fmt);
  141. std::vfprintf(output, fmt, args);
  142. std::fprintf(output, "\n");
  143. if (output != stderr)
  144. std::fflush(output);
  145. ::va_end(args);
  146. } catch (...) {}
  147. }
  148. /*
  149. * Print a string to stderr with newline (red color).
  150. */
  151. static inline
  152. void carla_stderr2(const char* const fmt, ...) noexcept
  153. {
  154. static FILE* const output = __carla_fopen("/tmp/carla.stderr2.log", stderr);
  155. try {
  156. ::va_list args;
  157. ::va_start(args, fmt);
  158. if (output == stderr)
  159. {
  160. std::fprintf(output, "\x1b[31m");
  161. std::vfprintf(output, fmt, args);
  162. std::fprintf(output, "\x1b[0m\n");
  163. }
  164. else
  165. {
  166. std::vfprintf(output, fmt, args);
  167. std::fprintf(output, "\n");
  168. std::fflush(output);
  169. }
  170. ::va_end(args);
  171. } catch (...) {}
  172. }
  173. // --------------------------------------------------------------------------------------------------------------------
  174. // carla_safe_assert*
  175. /*
  176. * Print a safe assertion error message.
  177. */
  178. static inline
  179. void carla_safe_assert(const char* const assertion, const char* const file, const int line) noexcept
  180. {
  181. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  182. }
  183. /*
  184. * Print a safe assertion error message, with 1 extra integer value.
  185. */
  186. static inline
  187. void carla_safe_assert_int(const char* const assertion, const char* const file, const int line,
  188. const int value) noexcept
  189. {
  190. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  191. }
  192. static inline
  193. void carla_safe_assert_uint(const char* const assertion, const char* const file, const int line,
  194. const uint value) noexcept
  195. {
  196. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %u", assertion, file, line, value);
  197. }
  198. /*
  199. * Print a safe assertion error message, with 2 extra integer values.
  200. */
  201. static inline
  202. void carla_safe_assert_int2(const char* const assertion, const char* const file, const int line,
  203. const int v1, const int v2) noexcept
  204. {
  205. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  206. }
  207. static inline
  208. void carla_safe_assert_uint2(const char* const assertion, const char* const file, const int line,
  209. const uint v1, const uint v2) noexcept
  210. {
  211. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %u, v2 %u", assertion, file, line, v1, v2);
  212. }
  213. // --------------------------------------------------------------------------------------------------------------------
  214. // carla_safe_exception*
  215. /*
  216. * Print a safe exception error message.
  217. */
  218. static inline
  219. void carla_safe_exception(const char* const exception, const char* const file, const int line) noexcept
  220. {
  221. carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line);
  222. }
  223. // --------------------------------------------------------------------------------------------------------------------
  224. // carla_*sleep
  225. /*
  226. * Sleep for 'secs' seconds.
  227. */
  228. static inline
  229. void carla_sleep(const uint secs) noexcept
  230. {
  231. CARLA_SAFE_ASSERT_RETURN(secs > 0,);
  232. try {
  233. #ifdef CARLA_OS_WIN
  234. ::Sleep(secs * 1000);
  235. #else
  236. ::sleep(secs);
  237. #endif
  238. } CARLA_SAFE_EXCEPTION("carla_sleep");
  239. }
  240. /*
  241. * Sleep for 'msecs' milliseconds.
  242. */
  243. static inline
  244. void carla_msleep(const uint msecs) noexcept
  245. {
  246. CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
  247. try {
  248. #ifdef CARLA_OS_WIN
  249. ::Sleep(msecs);
  250. #else
  251. ::usleep(msecs * 1000);
  252. #endif
  253. } CARLA_SAFE_EXCEPTION("carla_msleep");
  254. }
  255. // --------------------------------------------------------------------------------------------------------------------
  256. // carla_setenv
  257. /*
  258. * Set environment variable 'key' to 'value'.
  259. */
  260. static inline
  261. void carla_setenv(const char* const key, const char* const value) noexcept
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  264. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  265. #ifdef CARLA_OS_WIN
  266. try {
  267. ::SetEnvironmentVariableA(key, value);
  268. } CARLA_SAFE_EXCEPTION("carla_setenv");
  269. #else
  270. ::setenv(key, value, 1);
  271. #endif
  272. }
  273. /*
  274. * Unset environment variable 'key'.
  275. */
  276. static inline
  277. void carla_unsetenv(const char* const key) noexcept
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  280. #ifdef CARLA_OS_WIN
  281. try {
  282. ::SetEnvironmentVariableA(key, nullptr);
  283. } CARLA_SAFE_EXCEPTION("carla_unsetenv");
  284. #else
  285. ::unsetenv(key);
  286. #endif
  287. }
  288. // --------------------------------------------------------------------------------------------------------------------
  289. // carla_strdup
  290. /*
  291. * Custom 'strdup' function.
  292. * Returned value is always valid, and must be freed with "delete[] var".
  293. * May throw.
  294. */
  295. static inline
  296. const char* carla_strdup(const char* const strBuf)
  297. {
  298. CARLA_SAFE_ASSERT(strBuf != nullptr);
  299. const std::size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  300. char* const buffer = new char[bufferLen+1];
  301. if (bufferLen > 0)
  302. std::memcpy(buffer, strBuf, bufferLen);
  303. buffer[bufferLen] = '\0';
  304. return buffer;
  305. }
  306. /*
  307. * Custom 'strdup' function.
  308. * Calls "std::free(strBuf)".
  309. * Returned value is always valid, and must be freed with "delete[] var".
  310. * May throw.
  311. */
  312. static inline
  313. const char* carla_strdup_free(char* const strBuf)
  314. {
  315. const char* const buffer(carla_strdup(strBuf));
  316. std::free(strBuf);
  317. return buffer;
  318. }
  319. /*
  320. * Custom 'strdup' function, safe version.
  321. * Returned value may be null. It must be freed with "delete[] var".
  322. */
  323. static inline
  324. const char* carla_strdup_safe(const char* const strBuf) noexcept
  325. {
  326. CARLA_SAFE_ASSERT_RETURN(strBuf != nullptr, nullptr);
  327. const std::size_t bufferLen = std::strlen(strBuf);
  328. char* buffer;
  329. try {
  330. buffer = new char[bufferLen+1];
  331. } CARLA_SAFE_EXCEPTION_RETURN("carla_strdup_safe", nullptr);
  332. if (bufferLen > 0)
  333. std::memcpy(buffer, strBuf, bufferLen);
  334. buffer[bufferLen] = '\0';
  335. return buffer;
  336. }
  337. // --------------------------------------------------------------------------------------------------------------------
  338. // memory functions
  339. /*
  340. * Add array values to another array.
  341. */
  342. template<typename T>
  343. static inline
  344. void carla_add(T dest[], const T src[], const std::size_t count) noexcept
  345. {
  346. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  347. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  348. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  349. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  350. for (std::size_t i=0; i<count; ++i)
  351. *dest++ += *src++;
  352. }
  353. /*
  354. * Add array values to another array, with a multiplication factor.
  355. */
  356. template<typename T>
  357. static inline
  358. void carla_addWithMultiply(T dest[], const T src[], const T& multiplier, const std::size_t count) noexcept
  359. {
  360. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  361. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  362. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  363. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  364. for (std::size_t i=0; i<count; ++i)
  365. *dest++ += *src++ * multiplier;
  366. }
  367. /*
  368. * Copy array values to another array.
  369. */
  370. template<typename T>
  371. static inline
  372. void carla_copy(T dest[], const T src[], const std::size_t count) noexcept
  373. {
  374. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  375. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  376. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  377. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  378. std::memcpy(dest, src, count*sizeof(T));
  379. }
  380. /*
  381. * Copy array values to another array, with a multiplication factor.
  382. */
  383. template<typename T>
  384. static inline
  385. void carla_copyWithMultiply(T dest[], const T src[], const T& multiplier, const std::size_t count) noexcept
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  389. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  390. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  391. for (std::size_t i=0; i<count; ++i)
  392. *dest++ = *src++ * multiplier;
  393. }
  394. /*
  395. * Fill an array with a fixed value.
  396. */
  397. template<typename T>
  398. static inline
  399. void carla_fill(T data[], const T& value, const std::size_t count) noexcept
  400. {
  401. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  402. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  403. if (value == 0)
  404. {
  405. std::memset(data, 0, count*sizeof(T));
  406. }
  407. else
  408. {
  409. for (std::size_t i=0; i<count; ++i)
  410. *data++ = value;
  411. }
  412. }
  413. /*
  414. * Multiply an array with a fixed value.
  415. */
  416. template<typename T>
  417. static inline
  418. void carla_multiply(T data[], const T& multiplier, const std::size_t count) noexcept
  419. {
  420. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  421. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  422. if (multiplier == 0)
  423. {
  424. std::memset(data, 0, count*sizeof(T));
  425. }
  426. else
  427. {
  428. for (std::size_t i=0; i<count; ++i)
  429. *data++ *= multiplier;
  430. }
  431. }
  432. /*
  433. * Clear a byte array.
  434. */
  435. static inline
  436. void carla_zeroBytes(uint8_t bytes[], const std::size_t count) noexcept
  437. {
  438. CARLA_SAFE_ASSERT_RETURN(bytes != nullptr,);
  439. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  440. std::memset(bytes, 0, count*sizeof(uint8_t));
  441. }
  442. /*
  443. * Clear a char array.
  444. */
  445. static inline
  446. void carla_zeroChars(char chars[], const std::size_t count) noexcept
  447. {
  448. CARLA_SAFE_ASSERT_RETURN(chars != nullptr,);
  449. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  450. std::memset(chars, 0, count*sizeof(char));
  451. }
  452. /*
  453. * Clear a pointer array.
  454. */
  455. template<typename T>
  456. static inline
  457. void carla_zeroPointers(T* ptrs[], const std::size_t count) noexcept
  458. {
  459. CARLA_SAFE_ASSERT_RETURN(ptrs != nullptr,);
  460. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  461. std::memset(ptrs, 0, count*sizeof(T*));
  462. }
  463. /*
  464. * Clear a single struct.
  465. */
  466. template <typename T>
  467. static inline
  468. void carla_zeroStruct(T& s) noexcept
  469. {
  470. std::memset(&s, 0, sizeof(T));
  471. }
  472. /*
  473. * Clear a struct array.
  474. */
  475. template <typename T>
  476. static inline
  477. void carla_zeroStructs(T structs[], const std::size_t count) noexcept
  478. {
  479. CARLA_SAFE_ASSERT_RETURN(structs != nullptr,);
  480. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  481. std::memset(structs, 0, count*sizeof(T));
  482. }
  483. /*
  484. * Copy a single struct.
  485. */
  486. template <typename T>
  487. static inline
  488. void carla_copyStruct(T& dest, const T& src) noexcept
  489. {
  490. std::memcpy(&dest, &src, sizeof(T));
  491. }
  492. /*
  493. * Copy a struct array.
  494. */
  495. template <typename T>
  496. static inline
  497. void carla_copyStructs(T dest[], const T src[], const std::size_t count) noexcept
  498. {
  499. CARLA_SAFE_ASSERT_RETURN(dest != nullptr,);
  500. CARLA_SAFE_ASSERT_RETURN(src != nullptr,);
  501. CARLA_SAFE_ASSERT_RETURN(dest != src,);
  502. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  503. std::memcpy(dest, src, count*sizeof(T));
  504. }
  505. // --------------------------------------------------------------------------------------------------------------------
  506. #endif // CARLA_UTILS_HPP_INCLUDED