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

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