Collection of tools useful for audio production
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.

419 lines
9.3KB

  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 modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING 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. // -------------------------------------------------
  141. // carla_setenv
  142. static inline
  143. void carla_setenv(const char* const key, const char* const value)
  144. {
  145. CARLA_ASSERT(key != nullptr);
  146. CARLA_ASSERT(value != nullptr);
  147. #ifdef CARLA_OS_WIN
  148. SetEnvironmentVariableA(key, value);
  149. #else
  150. setenv(key, value, 1);
  151. #endif
  152. }
  153. // -------------------------------------------------
  154. // carla_setprocname (not available on all platforms)
  155. static inline
  156. void carla_setprocname(const char* const name)
  157. {
  158. CARLA_ASSERT(name != nullptr);
  159. #if defined(CARLA_OS_HAIKU)
  160. if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
  161. rename_thread(this_thread, name);
  162. #elif defined(CARLA_OS_LINUX)
  163. prctl(PR_SET_NAME, name);
  164. #else
  165. carla_stderr("carla_setprocname(\"%s\") - unsupported on this platform", name);
  166. #endif
  167. }
  168. // -------------------------------------------------
  169. // carla_strdup
  170. static inline
  171. const char* carla_strdup(const char* const strBuf)
  172. {
  173. CARLA_ASSERT(strBuf != nullptr);
  174. const size_t bufferLen = (strBuf != nullptr) ? std::strlen(strBuf) : 0;
  175. char* const buffer = new char[bufferLen+1];
  176. if (strBuf != nullptr && bufferLen > 0)
  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_add(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_add(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_copy(T* dataDst, T* dataSrc, const size_t size)
  239. {
  240. CARLA_ASSERT(dataDst != nullptr);
  241. CARLA_ASSERT(dataSrc != nullptr);
  242. CARLA_ASSERT(size != 0);
  243. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  244. return;
  245. for (size_t i=0; i < size; ++i)
  246. *dataDst++ = *dataSrc++;
  247. }
  248. template<typename T>
  249. static inline
  250. void carla_copy(T* dataDst, const T* dataSrc, const size_t size)
  251. {
  252. CARLA_ASSERT(dataDst != nullptr);
  253. CARLA_ASSERT(dataSrc != nullptr);
  254. CARLA_ASSERT(size != 0);
  255. if (dataDst == nullptr || dataSrc == nullptr || size == 0)
  256. return;
  257. for (size_t i=0; i < size; ++i)
  258. *dataDst++ = *dataSrc++;
  259. }
  260. template<typename T>
  261. static inline
  262. void carla_fill(T* data, const size_t size, const T v)
  263. {
  264. CARLA_ASSERT(data != nullptr);
  265. CARLA_ASSERT(size != 0);
  266. if (data == nullptr || size == 0)
  267. return;
  268. for (size_t i=0; i < size; ++i)
  269. *data++ = v;
  270. }
  271. static inline
  272. void carla_addDouble(double* const dataDst, double* const dataSrc, const size_t size)
  273. {
  274. carla_add<double>(dataDst, dataSrc, size);
  275. }
  276. static inline
  277. void carla_addFloat(float* const dataDst, float* const dataSrc, const size_t size)
  278. {
  279. carla_add<float>(dataDst, dataSrc, size);
  280. }
  281. static inline
  282. void carla_copyDouble(double* const dataDst, double* const dataSrc, const size_t size)
  283. {
  284. CARLA_ASSERT(dataDst != nullptr);
  285. CARLA_ASSERT(dataSrc != nullptr);
  286. CARLA_ASSERT(size > 0);
  287. std::memcpy(dataDst, dataSrc, size*sizeof(double));
  288. }
  289. static inline
  290. void carla_copyFloat(float* const dataDst, float* const dataSrc, const size_t size)
  291. {
  292. CARLA_ASSERT(dataDst != nullptr);
  293. CARLA_ASSERT(dataSrc != nullptr);
  294. CARLA_ASSERT(size > 0);
  295. std::memcpy(dataDst, dataSrc, size*sizeof(float));
  296. }
  297. static inline
  298. void carla_zeroDouble(double* const data, const size_t size)
  299. {
  300. carla_fill<double>(data, size, 0.0);
  301. }
  302. static inline
  303. void carla_zeroFloat(float* const data, const size_t size)
  304. {
  305. carla_fill<float>(data, size, 0.0f);
  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. static inline
  323. void carla_zeroMem(void* const memory, const size_t numBytes)
  324. {
  325. CARLA_ASSERT(memory != nullptr);
  326. CARLA_ASSERT(numBytes != 0);
  327. if (memory == nullptr || numBytes == 0)
  328. return;
  329. std::memset(memory, 0, numBytes);
  330. }
  331. template <typename T>
  332. static inline
  333. void carla_zeroStruct(T& structure)
  334. {
  335. std::memset(&structure, 0, sizeof(T));
  336. }
  337. template <typename T>
  338. static inline
  339. void carla_zeroStruct(T* const structure, const size_t count)
  340. {
  341. CARLA_ASSERT(structure != nullptr);
  342. CARLA_ASSERT(count >= 1);
  343. std::memset(structure, 0, sizeof(T)*count);
  344. }
  345. // -------------------------------------------------
  346. #endif // __CARLA_UTILS_HPP__