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.

431 lines
9.5KB

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