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.

435 lines
10KB

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