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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. // -----------------------------------------------------------------------
  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 (base)
  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. * Copy array values to another array.
  263. */
  264. template<typename T>
  265. static inline
  266. void carla_copy(T* dataDst, T* dataSrc, const size_t size)
  267. {
  268. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  269. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  270. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  271. for (size_t i=0; i < size; ++i)
  272. *dataDst++ = *dataSrc++;
  273. }
  274. /*
  275. * Copy array values to another array.
  276. */
  277. template<typename T>
  278. static inline
  279. void carla_copy(T* dataDst, const T* dataSrc, const size_t size)
  280. {
  281. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  282. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  283. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  284. for (size_t i=0; i < size; ++i)
  285. *dataDst++ = *dataSrc++;
  286. }
  287. /*
  288. * Fill an array with a fixed value.
  289. */
  290. template<typename T>
  291. static inline
  292. void carla_fill(T* data, const size_t size, const T v)
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  295. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  296. for (size_t i=0; i < size; ++i)
  297. *data++ = v;
  298. }
  299. // -----------------------------------------------------------------------
  300. // math functions (extended)
  301. /*
  302. * Add float array values to another float array.
  303. */
  304. static inline
  305. void carla_addFloat(float* dataDst, float* dataSrc, const size_t numSamples)
  306. {
  307. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  308. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  309. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  310. for (size_t i=0; i < numSamples; ++i)
  311. *dataDst++ += *dataSrc++;
  312. }
  313. /*
  314. * Copy float array values to another float array.
  315. */
  316. static inline
  317. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t numSamples)
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(dataDst != nullptr,);
  320. CARLA_SAFE_ASSERT_RETURN(dataSrc != nullptr,);
  321. CARLA_SAFE_ASSERT_RETURN(numSamples > 0,);
  322. std::memcpy(dataDst, dataSrc, numSamples*sizeof(float));
  323. }
  324. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  325. /*
  326. * Missing functions in OSX.
  327. */
  328. namespace std {
  329. inline float
  330. fmin(float __x, float __y)
  331. { return __builtin_fminf(__x, __y); }
  332. inline float
  333. fmax(float __x, float __y)
  334. { return __builtin_fmaxf(__x, __y); }
  335. inline float
  336. rint(float __x)
  337. { return __builtin_rintf(__x); }
  338. }
  339. #endif
  340. // -----------------------------------------------------------------------
  341. // memory functions
  342. /*
  343. * Clear a char array.
  344. */
  345. template<typename C = char>
  346. static inline
  347. void carla_zeroChar(C* const data, const size_t numChars)
  348. {
  349. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  350. CARLA_SAFE_ASSERT_RETURN(numChars > 0,);
  351. std::memset(data, 0, numChars*sizeof(C));
  352. }
  353. /*
  354. * Clear a float array.
  355. */
  356. static inline
  357. void carla_zeroFloat(float* const data, const size_t numSamples)
  358. {
  359. std::memset(data, 0, numSamples*sizeof(float));
  360. }
  361. /*
  362. * Clear a memory location.
  363. */
  364. static inline
  365. void carla_zeroMem(void* const memory, const size_t numBytes)
  366. {
  367. CARLA_SAFE_ASSERT_RETURN(memory != nullptr,);
  368. CARLA_SAFE_ASSERT_RETURN(numBytes > 0,);
  369. std::memset(memory, 0, numBytes);
  370. }
  371. /*
  372. * Clear a single struct/class.
  373. */
  374. template <typename T>
  375. static inline
  376. void carla_zeroStruct(T& structure)
  377. {
  378. std::memset(&structure, 0, sizeof(T));
  379. }
  380. /*
  381. * Clear an array of struct/class.
  382. */
  383. template <typename T>
  384. static inline
  385. void carla_zeroStruct(T* const structure, const size_t count)
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(structure != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  389. std::memset(structure, 0, count*sizeof(T));
  390. }
  391. /*
  392. * Copy a single struct/class.
  393. */
  394. template <typename T>
  395. static inline
  396. void carla_copyStruct(T& struct1, T& struct2)
  397. {
  398. std::memcpy(&struct1, &struct2, sizeof(T));
  399. }
  400. /*
  401. * Copy an array of struct/class.
  402. */
  403. template <typename T>
  404. static inline
  405. void carla_copyStruct(T* const struct1, T* const struct2, const size_t count)
  406. {
  407. CARLA_SAFE_ASSERT_RETURN(struct1 != nullptr,);
  408. CARLA_SAFE_ASSERT_RETURN(struct2 != nullptr,);
  409. CARLA_SAFE_ASSERT_RETURN(count > 0,);
  410. std::memcpy(struct1, struct2, count*sizeof(T));
  411. }
  412. // -----------------------------------------------------------------------
  413. #endif // CARLA_UTILS_HPP_INCLUDED