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.

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