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

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
12 years ago
12 years ago
12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 GPL.txt file
  16. */
  17. #ifndef __CARLA_UTILS_HPP__
  18. #define __CARLA_UTILS_HPP__
  19. #include "CarlaDefines.hpp"
  20. #include <cassert>
  21. #include <cstdarg>
  22. #include <cstdio>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #if defined(CARLA_OS_HAIKU)
  26. # include <kernel/OS.h>
  27. #elif defined(CARLA_OS_LINUX)
  28. # include <sys/prctl.h>
  29. # include <linux/prctl.h>
  30. #endif
  31. // -------------------------------------------------
  32. // misc functions
  33. static inline
  34. const char* bool2str(const bool yesNo)
  35. {
  36. return yesNo ? "true" : "false";
  37. }
  38. static inline
  39. void pass() {}
  40. // -------------------------------------------------
  41. // string print functions
  42. #ifndef DEBUG
  43. # define carla_debug(...)
  44. #else
  45. static inline
  46. void carla_debug(const char* const fmt, ...)
  47. {
  48. va_list args;
  49. va_start(args, fmt);
  50. # ifndef CARLA_OS_WIN
  51. std::fprintf(stdout, "\x1b[30;1m");
  52. # endif
  53. std::vfprintf(stdout, fmt, args);
  54. # ifndef CARLA_OS_WIN
  55. std::fprintf(stdout, "\x1b[0m\n");
  56. # else
  57. std::fprintf(stdout, "\n");
  58. # endif
  59. va_end(args);
  60. }
  61. #endif
  62. static inline
  63. void carla_stdout(const char* const fmt, ...)
  64. {
  65. va_list args;
  66. va_start(args, fmt);
  67. std::vfprintf(stdout, fmt, args);
  68. std::fprintf(stdout, "\n");
  69. va_end(args);
  70. }
  71. static inline
  72. void carla_stderr(const char* const fmt, ...)
  73. {
  74. va_list args;
  75. va_start(args, fmt);
  76. std::vfprintf(stderr, fmt, args);
  77. std::fprintf(stderr, "\n");
  78. va_end(args);
  79. }
  80. static inline
  81. void carla_stderr2(const char* const fmt, ...)
  82. {
  83. va_list args;
  84. va_start(args, fmt);
  85. #ifndef CARLA_OS_WIN
  86. std::fprintf(stderr, "\x1b[31m");
  87. #endif
  88. std::vfprintf(stderr, fmt, args);
  89. #ifndef CARLA_OS_WIN
  90. std::fprintf(stderr, "\x1b[0m\n");
  91. #else
  92. std::fprintf(stderr, "\n");
  93. #endif
  94. va_end(args);
  95. }
  96. // -------------------------------------------------
  97. // carla_assert*
  98. static inline
  99. void carla_assert(const char* const assertion, const char* const file, const int line)
  100. {
  101. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  102. }
  103. static inline
  104. void carla_assert_int(const char* const assertion, const char* const file, const int line, const int value)
  105. {
  106. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  107. }
  108. static inline
  109. void carla_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2)
  110. {
  111. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  112. }
  113. // -------------------------------------------------
  114. // carla_*sleep
  115. static inline
  116. void carla_sleep(const unsigned int secs)
  117. {
  118. CARLA_ASSERT(secs > 0);
  119. #ifdef CARLA_OS_WIN
  120. Sleep(secs * 1000);
  121. #else
  122. sleep(secs);
  123. #endif
  124. }
  125. static inline
  126. void carla_msleep(const unsigned int msecs)
  127. {
  128. CARLA_ASSERT(msecs > 0);
  129. #ifdef CARLA_OS_WIN
  130. Sleep(msecs);
  131. #else
  132. usleep(msecs * 1000);
  133. #endif
  134. }
  135. static inline
  136. void carla_usleep(const unsigned int usecs)
  137. {
  138. CARLA_ASSERT(usecs > 0);
  139. #ifdef CARLA_OS_WIN
  140. Sleep(usecs / 1000);
  141. #else
  142. usleep(usecs);
  143. #endif
  144. }
  145. // -------------------------------------------------
  146. // carla_setenv
  147. static inline
  148. void carla_setenv(const char* const key, const char* const value)
  149. {
  150. CARLA_ASSERT(key != nullptr);
  151. CARLA_ASSERT(value != nullptr);
  152. #ifdef CARLA_OS_WIN
  153. SetEnvironmentVariableA(key, value);
  154. #else
  155. setenv(key, value, 1);
  156. #endif
  157. }
  158. // -------------------------------------------------
  159. // carla_setprocname (not available on all platforms)
  160. static inline
  161. void carla_setprocname(const char* const name)
  162. {
  163. CARLA_ASSERT(name != nullptr);
  164. #if defined(CARLA_OS_HAIKU)
  165. if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
  166. rename_thread(this_thread, name);
  167. #elif defined(CARLA_OS_LINUX)
  168. prctl(PR_SET_NAME, name);
  169. #else
  170. carla_stderr("carla_setprocname(\"%s\") - unsupported on this platform", name);
  171. #endif
  172. }
  173. // -------------------------------------------------
  174. // carla_strdup
  175. static inline
  176. const char* carla_strdup(const char* const strBuf)
  177. {
  178. CARLA_ASSERT(strBuf != nullptr);
  179. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  180. char* const buffer = new char[bufferLen+1];
  181. std::strcpy(buffer, strBuf);
  182. buffer[bufferLen] = '\0';
  183. return buffer;
  184. }
  185. static inline
  186. const char* carla_strdup_free(char* const strBuf)
  187. {
  188. const char* const buffer = carla_strdup(strBuf);
  189. std::free(strBuf);
  190. return buffer;
  191. }
  192. // -------------------------------------------------
  193. // math functions
  194. template<typename T>
  195. static inline
  196. const T& carla_min(const T& v1, const T& v2, const T& min)
  197. {
  198. return ((v1 < min || v2 < min) ? min : (v1 < v2 ? v1 : v2));
  199. }
  200. template<typename T>
  201. static inline
  202. const T& carla_max(const T& v1, const T& v2, const T& max)
  203. {
  204. return ((v1 > max || v2 > max) ? max : (v1 > v2 ? v1 : v2));
  205. }
  206. template<typename T>
  207. static inline
  208. const T& carla_fixValue(const T& min, const T& max, const T& value)
  209. {
  210. if (value < min)
  211. return min;
  212. if (value > max)
  213. return max;
  214. return value;
  215. }
  216. template<typename T>
  217. static inline
  218. void carla_copy(T* dataDst, T* dataSrc, const size_t size)
  219. {
  220. CARLA_ASSERT(dataDst != nullptr);
  221. CARLA_ASSERT(dataSrc != nullptr);
  222. CARLA_ASSERT(size > 0);
  223. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  224. return;
  225. for (size_t i=0; i < size; ++i)
  226. *dataDst++ = *dataSrc++;
  227. }
  228. template<typename T>
  229. static inline
  230. void carla_copy(T* dataDst, const T* dataSrc, const size_t size)
  231. {
  232. CARLA_ASSERT(dataDst != nullptr);
  233. CARLA_ASSERT(dataSrc != nullptr);
  234. CARLA_ASSERT(size > 0);
  235. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  236. return;
  237. for (size_t i=0; i < size; ++i)
  238. *dataDst++ = *dataSrc++;
  239. }
  240. template<typename T>
  241. static inline
  242. void carla_fill(T* data, const size_t size, const T v)
  243. {
  244. CARLA_ASSERT(data != nullptr);
  245. CARLA_ASSERT(size > 0);
  246. if (data == nullptr || size == 0)
  247. return;
  248. for (size_t i=0; i < size; ++i)
  249. *data++ = v;
  250. }
  251. static inline
  252. void carla_copyDouble(double* const dataDst, double* const dataSrc, const size_t size)
  253. {
  254. carla_copy<double>(dataDst, dataSrc, size);
  255. }
  256. static inline
  257. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t size)
  258. {
  259. carla_copy<float>(dataDst, dataSrc, size);
  260. }
  261. static inline
  262. void carla_zeroDouble(double* const data, const size_t size)
  263. {
  264. carla_fill<double>(data, size, 0.0);
  265. }
  266. static inline
  267. void carla_zeroFloat(float* const data, const size_t size)
  268. {
  269. carla_fill<float>(data, size, 0.0f);
  270. }
  271. #ifdef CARLA_OS_MAC
  272. namespace std {
  273. inline float
  274. fmin(float __x, float __y)
  275. { return __builtin_fminf(__x, __y); }
  276. inline float
  277. fmax(float __x, float __y)
  278. { return __builtin_fmaxf(__x, __y); }
  279. inline float
  280. rint(float __x)
  281. { return __builtin_rintf(__x); }
  282. }
  283. #endif
  284. // -------------------------------------------------
  285. // memory functions
  286. static inline
  287. void carla_zeroMem(void* const memory, const size_t numBytes)
  288. {
  289. CARLA_ASSERT(memory != nullptr);
  290. CARLA_ASSERT(numBytes > 0);
  291. if (memory == nullptr || numBytes == 0)
  292. return;
  293. std::memset(memory, 0, numBytes);
  294. }
  295. template <typename T>
  296. static inline
  297. void carla_zeroStruct(T& structure)
  298. {
  299. std::memset(&structure, 0, sizeof(T));
  300. }
  301. template <typename T>
  302. static inline
  303. void carla_zeroStruct(T* const structure, const size_t count)
  304. {
  305. CARLA_ASSERT(structure != nullptr);
  306. CARLA_ASSERT(count >= 1);
  307. std::memset(structure, 0, sizeof(T)*count);
  308. }
  309. // -------------------------------------------------
  310. #endif // __CARLA_UTILS_HPP__