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.

370 lines
8.0KB

  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. #endif
  57. va_end(args);
  58. }
  59. #endif
  60. static inline
  61. void carla_stdout(const char* const fmt, ...)
  62. {
  63. va_list args;
  64. va_start(args, fmt);
  65. std::vfprintf(stdout, fmt, args);
  66. std::fprintf(stdout, "\n");
  67. va_end(args);
  68. }
  69. static inline
  70. void carla_stderr(const char* const fmt, ...)
  71. {
  72. va_list args;
  73. va_start(args, fmt);
  74. std::vfprintf(stderr, fmt, args);
  75. std::fprintf(stderr, "\n");
  76. va_end(args);
  77. }
  78. static inline
  79. void carla_stderr2(const char* const fmt, ...)
  80. {
  81. va_list args;
  82. va_start(args, fmt);
  83. #ifndef CARLA_OS_WIN
  84. std::fprintf(stderr, "\x1b[31m");
  85. #endif
  86. std::vfprintf(stderr, fmt, args);
  87. #ifndef CARLA_OS_WIN
  88. std::fprintf(stderr, "\x1b[0m\n");
  89. #endif
  90. va_end(args);
  91. }
  92. // -------------------------------------------------
  93. // carla_assert*
  94. static inline
  95. void carla_assert(const char* const assertion, const char* const file, const int line)
  96. {
  97. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  98. }
  99. static inline
  100. void carla_assert_int(const char* const assertion, const char* const file, const int line, const int value)
  101. {
  102. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  103. }
  104. static inline
  105. void carla_assert_int2(const char* const assertion, const char* const file, const int line, const int v1, const int v2)
  106. {
  107. carla_stderr2("Carla assertion failure: \"%s\" in file %s, line %i, v1 %i, v2 %i", assertion, file, line, v1, v2);
  108. }
  109. // -------------------------------------------------
  110. // carla_*sleep
  111. static inline
  112. void carla_sleep(const unsigned int secs)
  113. {
  114. CARLA_ASSERT(secs > 0);
  115. #ifdef CARLA_OS_WIN
  116. Sleep(secs * 1000);
  117. #else
  118. sleep(secs);
  119. #endif
  120. }
  121. static inline
  122. void carla_msleep(const unsigned int msecs)
  123. {
  124. CARLA_ASSERT(msecs > 0);
  125. #ifdef CARLA_OS_WIN
  126. Sleep(msecs);
  127. #else
  128. usleep(msecs * 1000);
  129. #endif
  130. }
  131. static inline
  132. void carla_usleep(const unsigned int usecs)
  133. {
  134. CARLA_ASSERT(usecs > 0);
  135. #ifdef CARLA_OS_WIN
  136. Sleep(usecs / 1000);
  137. #else
  138. usleep(usecs);
  139. #endif
  140. }
  141. // -------------------------------------------------
  142. // carla_setenv
  143. static inline
  144. void carla_setenv(const char* const key, const char* const value)
  145. {
  146. CARLA_ASSERT(key != nullptr);
  147. CARLA_ASSERT(value != nullptr);
  148. #ifdef CARLA_OS_WIN
  149. SetEnvironmentVariableA(key, value);
  150. #else
  151. setenv(key, value, 1);
  152. #endif
  153. }
  154. // -------------------------------------------------
  155. // carla_setprocname (not available on all platforms)
  156. static inline
  157. void carla_setprocname(const char* const name)
  158. {
  159. CARLA_ASSERT(name != nullptr);
  160. #if defined(CARLA_OS_HAIKU)
  161. if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
  162. rename_thread(this_thread, name);
  163. #elif defined(CARLA_OS_LINUX)
  164. prctl(PR_SET_NAME, name);
  165. #else
  166. carla_stderr("carla_setprocname(\"%s\") - unsupported on this platform", name);
  167. #endif
  168. }
  169. // -------------------------------------------------
  170. // carla_strdup
  171. static inline
  172. const char* carla_strdup(const char* const strBuf)
  173. {
  174. CARLA_ASSERT(strBuf != nullptr);
  175. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  176. char* const buffer = new char[bufferLen+1];
  177. std::strcpy(buffer, strBuf);
  178. buffer[bufferLen] = '\0';
  179. return buffer;
  180. }
  181. static inline
  182. const char* carla_strdup_free(char* const strBuf)
  183. {
  184. const char* const buffer = carla_strdup(strBuf);
  185. std::free(strBuf);
  186. return buffer;
  187. }
  188. // -------------------------------------------------
  189. // math functions
  190. template<typename T>
  191. static inline
  192. const T& carla_min(const T& v1, const T& v2, const T& min)
  193. {
  194. return ((v1 < min || v2 < min) ? min : (v1 < v2 ? v1 : v2));
  195. }
  196. template<typename T>
  197. static inline
  198. const T& carla_max(const T& v1, const T& v2, const T& max)
  199. {
  200. return ((v1 > max || v2 > max) ? max : (v1 > v2 ? v1 : v2));
  201. }
  202. template<typename T>
  203. static inline
  204. const T& carla_fixValue(const T& min, const T& max, const T& value)
  205. {
  206. if (value < min)
  207. return min;
  208. if (value > max)
  209. return max;
  210. return value;
  211. }
  212. template<typename T>
  213. static inline
  214. void carla_copy(T* dataDst, T* dataSrc, const size_t size)
  215. {
  216. CARLA_ASSERT(dataDst != nullptr);
  217. CARLA_ASSERT(dataSrc != nullptr);
  218. CARLA_ASSERT(size > 0);
  219. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  220. return;
  221. for (size_t i=0; i < size; ++i)
  222. *dataDst++ = *dataSrc++;
  223. }
  224. template<typename T>
  225. static inline
  226. void carla_copy(T* dataDst, const T* dataSrc, const size_t size)
  227. {
  228. CARLA_ASSERT(dataDst != nullptr);
  229. CARLA_ASSERT(dataSrc != nullptr);
  230. CARLA_ASSERT(size > 0);
  231. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  232. return;
  233. for (size_t i=0; i < size; ++i)
  234. *dataDst++ = *dataSrc++;
  235. }
  236. template<typename T>
  237. static inline
  238. void carla_fill(T* data, const size_t size, const T v)
  239. {
  240. CARLA_ASSERT(data != nullptr);
  241. CARLA_ASSERT(size > 0);
  242. if (data == nullptr || size == 0)
  243. return;
  244. for (size_t i=0; i < size; ++i)
  245. *data++ = v;
  246. }
  247. static inline
  248. void carla_copyDouble(double* const dataDst, double* const dataSrc, const size_t size)
  249. {
  250. carla_copy<double>(dataDst, dataSrc, size);
  251. }
  252. static inline
  253. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t size)
  254. {
  255. carla_copy<float>(dataDst, dataSrc, size);
  256. }
  257. static inline
  258. void carla_zeroDouble(double* const data, const size_t size)
  259. {
  260. carla_fill<double>(data, size, 0.0);
  261. }
  262. static inline
  263. void carla_zeroFloat(float* const data, const size_t size)
  264. {
  265. carla_fill<float>(data, size, 0.0f);
  266. }
  267. #ifdef CARLA_OS_MAC
  268. namespace std {
  269. inline float
  270. fmin(float __x, float __y)
  271. { return __builtin_fminf(__x, __y); }
  272. inline float
  273. fmax(float __x, float __y)
  274. { return __builtin_fmaxf(__x, __y); }
  275. inline float
  276. rint(float __x)
  277. { return __builtin_rintf(__x); }
  278. }
  279. #endif
  280. // -------------------------------------------------
  281. // memory functions
  282. static inline
  283. void carla_zeroMem(void* const memory, const size_t numBytes)
  284. {
  285. CARLA_ASSERT(memory != nullptr);
  286. CARLA_ASSERT(numBytes > 0);
  287. if (memory == nullptr || numBytes == 0)
  288. return;
  289. std::memset(memory, 0, numBytes);
  290. }
  291. template <typename T>
  292. static inline
  293. void carla_zeroStruct(T& structure)
  294. {
  295. std::memset(&structure, 0, sizeof(T));
  296. }
  297. template <typename T>
  298. static inline
  299. void carla_zeroStruct(T* const structure, const size_t count)
  300. {
  301. CARLA_ASSERT(structure != nullptr);
  302. CARLA_ASSERT(count > 1);
  303. std::memset(structure, 0, sizeof(T)*count);
  304. }
  305. // -------------------------------------------------
  306. #endif // __CARLA_UTILS_HPP__