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.

418 lines
9.6KB

  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_ASSERT(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_ASSERT(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_ASSERT(key != nullptr);
  138. CARLA_ASSERT(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_ASSERT(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_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::strcpy(buffer, strBuf);
  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_ASSERT(max > min);
  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_ASSERT(dataDst != nullptr);
  216. CARLA_ASSERT(dataSrc != nullptr);
  217. CARLA_ASSERT(size != 0);
  218. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  219. return;
  220. for (size_t i=0; i < size; ++i)
  221. *dataDst++ += *dataSrc++;
  222. }
  223. template<typename T>
  224. static inline
  225. void carla_add(T* dataDst, const T* dataSrc, const size_t size)
  226. {
  227. CARLA_ASSERT(dataDst != nullptr);
  228. CARLA_ASSERT(dataSrc != nullptr);
  229. CARLA_ASSERT(size != 0);
  230. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  231. return;
  232. for (size_t i=0; i < size; ++i)
  233. *dataDst++ += *dataSrc++;
  234. }
  235. template<typename T>
  236. static inline
  237. void carla_copy(T* dataDst, T* dataSrc, const size_t size)
  238. {
  239. CARLA_ASSERT(dataDst != nullptr);
  240. CARLA_ASSERT(dataSrc != nullptr);
  241. CARLA_ASSERT(size != 0);
  242. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  243. return;
  244. for (size_t i=0; i < size; ++i)
  245. *dataDst++ = *dataSrc++;
  246. }
  247. template<typename T>
  248. static inline
  249. void carla_copy(T* dataDst, const T* dataSrc, const size_t size)
  250. {
  251. CARLA_ASSERT(dataDst != nullptr);
  252. CARLA_ASSERT(dataSrc != nullptr);
  253. CARLA_ASSERT(size != 0);
  254. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  255. return;
  256. for (size_t i=0; i < size; ++i)
  257. *dataDst++ = *dataSrc++;
  258. }
  259. template<typename T>
  260. static inline
  261. void carla_fill(T* data, const size_t size, const T v)
  262. {
  263. CARLA_ASSERT(data != nullptr);
  264. CARLA_ASSERT(size != 0);
  265. if (data == nullptr || size == 0)
  266. return;
  267. for (size_t i=0; i < size; ++i)
  268. *data++ = v;
  269. }
  270. static inline
  271. void carla_addDouble(double* const dataDst, double* const dataSrc, const size_t size)
  272. {
  273. carla_add<double>(dataDst, dataSrc, size);
  274. }
  275. static inline
  276. void carla_addFloat(float* const dataDst, float* const dataSrc, const size_t size)
  277. {
  278. carla_add<float>(dataDst, dataSrc, size);
  279. }
  280. static inline
  281. void carla_copyDouble(double* const dataDst, double* const dataSrc, const size_t size)
  282. {
  283. CARLA_ASSERT(dataDst != nullptr);
  284. CARLA_ASSERT(dataSrc != nullptr);
  285. CARLA_ASSERT(size > 0);
  286. std::memcpy(dataDst, dataSrc, size*sizeof(double));
  287. }
  288. static inline
  289. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t size)
  290. {
  291. CARLA_ASSERT(dataDst != nullptr);
  292. CARLA_ASSERT(dataSrc != nullptr);
  293. CARLA_ASSERT(size > 0);
  294. std::memcpy(dataDst, dataSrc, size*sizeof(float));
  295. }
  296. static inline
  297. void carla_zeroDouble(double* const data, const size_t size)
  298. {
  299. carla_fill<double>(data, size, 0.0);
  300. }
  301. static inline
  302. void carla_zeroFloat(float* const data, const size_t size)
  303. {
  304. carla_fill<float>(data, size, 0.0f);
  305. }
  306. #if defined(CARLA_OS_MAC) && ! defined(DISTRHO_OS_MAC)
  307. namespace std {
  308. inline float
  309. fmin(float __x, float __y)
  310. { return __builtin_fminf(__x, __y); }
  311. inline float
  312. fmax(float __x, float __y)
  313. { return __builtin_fmaxf(__x, __y); }
  314. inline float
  315. rint(float __x)
  316. { return __builtin_rintf(__x); }
  317. }
  318. #endif
  319. // -----------------------------------------------------------------------
  320. // memory functions
  321. static inline
  322. void carla_zeroMem(void* const memory, const size_t numBytes)
  323. {
  324. CARLA_ASSERT(memory != nullptr);
  325. CARLA_ASSERT(numBytes != 0);
  326. if (memory == nullptr || numBytes == 0)
  327. return;
  328. std::memset(memory, 0, numBytes);
  329. }
  330. template <typename T>
  331. static inline
  332. void carla_zeroStruct(T& structure)
  333. {
  334. std::memset(&structure, 0, sizeof(T));
  335. }
  336. template <typename T>
  337. static inline
  338. void carla_zeroStruct(T* const structure, const size_t count)
  339. {
  340. CARLA_ASSERT(structure != nullptr);
  341. CARLA_ASSERT(count >= 1);
  342. std::memset(structure, 0, sizeof(T)*count);
  343. }
  344. #endif // CARLA_UTILS_HPP_INCLUDED