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.

316 lines
7.3KB

  1. /*
  2. * Carla common utils
  3. * Copyright (C) 2011-2012 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_H
  18. #define CARLA_UTILS_H
  19. #include "carla_defines.h"
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #if defined(Q_OS_HAIKU)
  24. # include <kernel/OS.h>
  25. #elif defined(Q_OS_LINUX)
  26. # include <sys/prctl.h>
  27. # include <linux/prctl.h>
  28. #endif
  29. // carla_*sleep
  30. #ifdef Q_OS_WIN
  31. # define carla_sleep(t) Sleep(t * 1000)
  32. # define carla_msleep(t) Sleep(t)
  33. # define carla_usleep(t) Sleep(t / 1000)
  34. #else
  35. # define carla_sleep(t) sleep(t)
  36. # define carla_msleep(t) usleep(t * 1000)
  37. # define carla_usleep(t) usleep(t)
  38. #endif
  39. // carla_setenv
  40. #ifdef Q_OS_WIN
  41. # define carla_setenv(key, value) SetEnvironmentVariableA(key, value)
  42. #else
  43. # define carla_setenv(key, value) setenv(key, value, 1)
  44. #endif
  45. // carla_assert*
  46. static inline
  47. void carla_assert(const char* const assertion, const char* const file, const int line)
  48. {
  49. qCritical("Carla assertion failure: \"%s\" in file %s, line %i", assertion, file, line);
  50. }
  51. static inline
  52. void carla_assert_int(const char* const assertion, const char* const file, const int line, const int value)
  53. {
  54. qCritical("Carla assertion failure: \"%s\" in file %s, line %i, value %i", assertion, file, line, value);
  55. }
  56. // carla_setprocname (not available on all platforms)
  57. static inline
  58. void carla_setprocname(const char* const name)
  59. {
  60. CARLA_ASSERT(name);
  61. #if defined(Q_OS_HAIKU)
  62. if ((thread_id this_thread = find_thread(nullptr)) != B_NAME_NOT_FOUND)
  63. rename_thread(this_thread, name);
  64. #elif defined(Q_OS_LINUX)
  65. prctl(PR_SET_NAME, name);
  66. #else
  67. qWarning("carla_setprocname(\"%s\") - unsupported on this platform");
  68. #endif
  69. }
  70. // math functions
  71. inline
  72. float carla_absF(const float& value)
  73. {
  74. return (value < 0.0f) ? -value : value;
  75. }
  76. inline
  77. float carla_minF(const float& x, const float& y)
  78. {
  79. return (x < y ? x : y);
  80. }
  81. inline
  82. float carla_maxF(const float& x, const float& y)
  83. {
  84. return (x > y ? x : y);
  85. }
  86. static inline
  87. void carla_zeroF(float* data, const unsigned int size)
  88. {
  89. for (unsigned int i=0; i < size; i++)
  90. *data++ = 0.0f;
  91. }
  92. // other misc functions
  93. static inline
  94. const char* bool2str(const bool yesNo)
  95. {
  96. return yesNo ? "true" : "false";
  97. }
  98. static inline
  99. void pass() {}
  100. // carla_string class
  101. class carla_string
  102. {
  103. public:
  104. // ---------------------------------------------
  105. // constructors (no explicit conversions allowed)
  106. explicit carla_string()
  107. {
  108. buffer = ::strdup("");
  109. }
  110. explicit carla_string(char* const strBuf)
  111. {
  112. buffer = ::strdup(strBuf ? strBuf : "");
  113. }
  114. carla_string(const char* const strBuf)
  115. {
  116. buffer = ::strdup(strBuf ? strBuf : "");
  117. }
  118. explicit carla_string(const carla_string& str)
  119. {
  120. buffer = ::strdup(str.buffer);
  121. }
  122. explicit carla_string(const int value)
  123. {
  124. const size_t strBufSize = ::abs(value/10) + 3;
  125. char strBuf[strBufSize];
  126. ::snprintf(strBuf, strBufSize, "%d", value);
  127. buffer = ::strdup(strBuf);
  128. }
  129. explicit carla_string(const unsigned int value, const bool hexadecimal = false)
  130. {
  131. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  132. char strBuf[strBufSize];
  133. ::snprintf(strBuf, strBufSize, hexadecimal ? "%u" : "0x%x", value);
  134. buffer = ::strdup(strBuf);
  135. }
  136. explicit carla_string(const long int value)
  137. {
  138. const size_t strBufSize = ::labs(value/10) + 3;
  139. char strBuf[strBufSize];
  140. ::snprintf(strBuf, strBufSize, "%ld", value);
  141. buffer = ::strdup(strBuf);
  142. }
  143. explicit carla_string(const unsigned long int value, const bool hexadecimal = false)
  144. {
  145. const size_t strBufSize = value/10 + 2 + (hexadecimal ? 2 : 0);
  146. char strBuf[strBufSize];
  147. ::snprintf(strBuf, strBufSize, hexadecimal ? "%lu" : "0x%lx", value);
  148. buffer = ::strdup(strBuf);
  149. }
  150. explicit carla_string(const float value)
  151. {
  152. char strBuf[0xff];
  153. ::snprintf(strBuf, 0xff, "%f", value);
  154. buffer = ::strdup(strBuf);
  155. }
  156. explicit carla_string(const double value)
  157. {
  158. char strBuf[0xff];
  159. ::snprintf(strBuf, 0xff, "%g", value);
  160. buffer = ::strdup(strBuf);
  161. }
  162. // ---------------------------------------------
  163. // deconstructor
  164. ~carla_string()
  165. {
  166. ::free(buffer);
  167. }
  168. // ---------------------------------------------
  169. // public methods
  170. size_t length() const
  171. {
  172. return ::strlen(buffer);
  173. }
  174. bool isEmpty() const
  175. {
  176. return (*buffer == 0);
  177. }
  178. // ---------------------------------------------
  179. // public operators
  180. operator const char*() const
  181. {
  182. return buffer;
  183. }
  184. bool operator==(const char* const strBuf) const
  185. {
  186. return (strBuf && ::strcmp(buffer, strBuf) == 0);
  187. }
  188. bool operator==(const carla_string& str) const
  189. {
  190. return operator==(str.buffer);
  191. }
  192. bool operator!=(const char* const strBuf) const
  193. {
  194. return !operator==(strBuf);
  195. }
  196. bool operator!=(const carla_string& str) const
  197. {
  198. return !operator==(str.buffer);
  199. }
  200. carla_string& operator=(const char* const strBuf)
  201. {
  202. ::free(buffer);
  203. buffer = ::strdup(strBuf ? strBuf : "");
  204. return *this;
  205. }
  206. carla_string& operator=(const carla_string& str)
  207. {
  208. return operator=(str.buffer);
  209. }
  210. carla_string& operator+=(const char* const strBuf)
  211. {
  212. const size_t newBufSize = ::strlen(buffer) + (strBuf ? ::strlen(strBuf) : 0) + 1;
  213. char newBuf[newBufSize];
  214. ::strcpy(newBuf, buffer);
  215. ::strcat(newBuf, strBuf);
  216. ::free(buffer);
  217. buffer = ::strdup(newBuf);
  218. return *this;
  219. }
  220. carla_string& operator+=(const carla_string& str)
  221. {
  222. return operator+=(str.buffer);
  223. }
  224. carla_string operator+(const char* const strBuf)
  225. {
  226. const size_t newBufSize = ::strlen(buffer) + (strBuf ? ::strlen(strBuf) : 0) + 1;
  227. char newBuf[newBufSize];
  228. ::strcpy(newBuf, buffer);
  229. ::strcat(newBuf, strBuf);
  230. return carla_string(newBuf);
  231. }
  232. carla_string operator+(const carla_string& str)
  233. {
  234. return operator+(str.buffer);
  235. }
  236. // ---------------------------------------------
  237. private:
  238. char* buffer;
  239. };
  240. static inline
  241. carla_string operator+(const char* const strBufBefore, const carla_string& strAfter)
  242. {
  243. const char* const strBufAfter = (const char*)strAfter;
  244. const size_t newBufSize = (strBufBefore ? ::strlen(strBufBefore) : 0) + ::strlen(strBufAfter) + 1;
  245. char newBuf[newBufSize];
  246. ::strcpy(newBuf, strBufBefore);
  247. ::strcat(newBuf, strBufAfter);
  248. return carla_string(newBuf);
  249. }
  250. // -------------------------------------------------
  251. #endif // CARLA_UTILS_H