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.

331 lines
7.2KB

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