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.

425 lines
9.3KB

  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. if (strBuf != nullptr && bufferLen > 0)
  182. std::strcpy(buffer, strBuf);
  183. buffer[bufferLen] = '\0';
  184. return buffer;
  185. }
  186. static inline
  187. const char* carla_strdup_free(char* const strBuf)
  188. {
  189. const char* const buffer = carla_strdup(strBuf);
  190. std::free(strBuf);
  191. return buffer;
  192. }
  193. // -------------------------------------------------
  194. // math functions
  195. template<typename T>
  196. static inline
  197. const T& carla_min(const T& v1, const T& v2, const T& min)
  198. {
  199. return ((v1 < min || v2 < min) ? min : (v1 < v2 ? v1 : v2));
  200. }
  201. template<typename T>
  202. static inline
  203. const T& carla_max(const T& v1, const T& v2, const T& max)
  204. {
  205. return ((v1 > max || v2 > max) ? max : (v1 > v2 ? v1 : v2));
  206. }
  207. template<typename T>
  208. static inline
  209. const T& carla_fixValue(const T& min, const T& max, const T& value)
  210. {
  211. if (value < min)
  212. return min;
  213. if (value > max)
  214. return max;
  215. return value;
  216. }
  217. template<typename T>
  218. static inline
  219. void carla_add(T* dataDst, T* dataSrc, const size_t size)
  220. {
  221. CARLA_ASSERT(dataDst != nullptr);
  222. CARLA_ASSERT(dataSrc != nullptr);
  223. CARLA_ASSERT(size != 0);
  224. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  225. return;
  226. for (size_t i=0; i < size; ++i)
  227. *dataDst++ += *dataSrc++;
  228. }
  229. template<typename T>
  230. static inline
  231. void carla_add(T* dataDst, const T* dataSrc, const size_t size)
  232. {
  233. CARLA_ASSERT(dataDst != nullptr);
  234. CARLA_ASSERT(dataSrc != nullptr);
  235. CARLA_ASSERT(size != 0);
  236. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  237. return;
  238. for (size_t i=0; i < size; ++i)
  239. *dataDst++ += *dataSrc++;
  240. }
  241. template<typename T>
  242. static inline
  243. void carla_copy(T* dataDst, T* dataSrc, const size_t size)
  244. {
  245. CARLA_ASSERT(dataDst != nullptr);
  246. CARLA_ASSERT(dataSrc != nullptr);
  247. CARLA_ASSERT(size != 0);
  248. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  249. return;
  250. for (size_t i=0; i < size; ++i)
  251. *dataDst++ = *dataSrc++;
  252. }
  253. template<typename T>
  254. static inline
  255. void carla_copy(T* dataDst, const T* dataSrc, const size_t size)
  256. {
  257. CARLA_ASSERT(dataDst != nullptr);
  258. CARLA_ASSERT(dataSrc != nullptr);
  259. CARLA_ASSERT(size != 0);
  260. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  261. return;
  262. for (size_t i=0; i < size; ++i)
  263. *dataDst++ = *dataSrc++;
  264. }
  265. template<typename T>
  266. static inline
  267. void carla_fill(T* data, const size_t size, const T v)
  268. {
  269. CARLA_ASSERT(data != nullptr);
  270. CARLA_ASSERT(size != 0);
  271. if (data == nullptr || size == 0)
  272. return;
  273. for (size_t i=0; i < size; ++i)
  274. *data++ = v;
  275. }
  276. static inline
  277. void carla_addDouble(double* const dataDst, double* const dataSrc, const size_t size)
  278. {
  279. carla_add<double>(dataDst, dataSrc, size);
  280. }
  281. static inline
  282. void carla_addFloat(float* const dataDst, float* const dataSrc, const size_t size)
  283. {
  284. carla_add<float>(dataDst, dataSrc, size);
  285. }
  286. static inline
  287. void carla_copyDouble(double* const dataDst, double* const dataSrc, const size_t size)
  288. {
  289. CARLA_ASSERT(dataDst != nullptr);
  290. CARLA_ASSERT(dataSrc != nullptr);
  291. CARLA_ASSERT(size > 0);
  292. std::memcpy(dataDst, dataSrc, size*sizeof(double));
  293. }
  294. static inline
  295. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t size)
  296. {
  297. CARLA_ASSERT(dataDst != nullptr);
  298. CARLA_ASSERT(dataSrc != nullptr);
  299. CARLA_ASSERT(size > 0);
  300. std::memcpy(dataDst, dataSrc, size*sizeof(float));
  301. }
  302. static inline
  303. void carla_zeroDouble(double* const data, const size_t size)
  304. {
  305. carla_fill<double>(data, size, 0.0);
  306. }
  307. static inline
  308. void carla_zeroFloat(float* const data, const size_t size)
  309. {
  310. carla_fill<float>(data, size, 0.0f);
  311. }
  312. #ifdef CARLA_OS_MAC
  313. namespace std {
  314. inline float
  315. fmin(float __x, float __y)
  316. { return __builtin_fminf(__x, __y); }
  317. inline float
  318. fmax(float __x, float __y)
  319. { return __builtin_fmaxf(__x, __y); }
  320. inline float
  321. rint(float __x)
  322. { return __builtin_rintf(__x); }
  323. }
  324. #endif
  325. // -------------------------------------------------
  326. // memory functions
  327. static inline
  328. void carla_zeroMem(void* const memory, const size_t numBytes)
  329. {
  330. CARLA_ASSERT(memory != nullptr);
  331. CARLA_ASSERT(numBytes != 0);
  332. if (memory == nullptr || numBytes == 0)
  333. return;
  334. std::memset(memory, 0, numBytes);
  335. }
  336. template <typename T>
  337. static inline
  338. void carla_zeroStruct(T& structure)
  339. {
  340. std::memset(&structure, 0, sizeof(T));
  341. }
  342. template <typename T>
  343. static inline
  344. void carla_zeroStruct(T* const structure, const size_t count)
  345. {
  346. CARLA_ASSERT(structure != nullptr);
  347. CARLA_ASSERT(count >= 1);
  348. std::memset(structure, 0, sizeof(T)*count);
  349. }
  350. // -------------------------------------------------
  351. #endif // __CARLA_UTILS_HPP__