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.

249 lines
7.2KB

  1. /*
  2. * Carla VST3 utils
  3. * Copyright (C) 2021-2023 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_VST3_UTILS_HPP_INCLUDED
  18. #define CARLA_VST3_UTILS_HPP_INCLUDED
  19. #include "CarlaBackend.h"
  20. #include "CarlaUtils.hpp"
  21. #include "travesty/audio_processor.h"
  22. #include "travesty/component.h"
  23. #include "travesty/edit_controller.h"
  24. #include "travesty/factory.h"
  25. #include "travesty/host.h"
  26. // --------------------------------------------------------------------------------------------------------------------
  27. #if !(defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  28. #if defined(__aarch64__) || defined(__arm64__)
  29. #define V3_ARCHITECTURE "aarch64"
  30. #elif defined(__TARGET_ARCH_ARM)
  31. #if __TARGET_ARCH_ARM == 9
  32. #define V3_ARCHITECTURE "armv9l"
  33. #elif __TARGET_ARCH_ARM == 8
  34. #define V3_ARCHITECTURE "armv8l"
  35. #elif __TARGET_ARCH_ARM == 7
  36. #define V3_ARCHITECTURE "armv7l"
  37. #elif _TARGET_ARCH_ARM == 6
  38. #define V3_ARCHITECTURE "armv6l"
  39. #elif __TARGET_ARCH_ARM == 5
  40. #define V3_ARCHITECTURE "armv5l"
  41. #else
  42. #define V3_ARCHITECTURE "arm"
  43. #endif
  44. #elif defined(__arm__)
  45. #define V3_ARCHITECTURE "arm"
  46. #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64)
  47. #define V3_ARCHITECTURE "x86_64"
  48. #elif defined(__i386) || defined(__i386__)
  49. #define V3_ARCHITECTURE "i386"
  50. #elif defined(__ia64) || defined(__ia64__)
  51. #define V3_ARCHITECTURE "ia64"
  52. #elif defined(__mips64)
  53. #define V3_ARCHITECTURE "mips64"
  54. #elif defined(__mips) || defined(__mips__)
  55. #define V3_ARCHITECTURE "mips"
  56. #elif defined(__ppc64) || defined(__ppc64__) || defined(__powerpc64__)
  57. #ifdef __LITTLE_ENDIAN__
  58. #define V3_ARCHITECTURE "ppc64le"
  59. #else
  60. #define V3_ARCHITECTURE "ppc64"
  61. #endif
  62. #elif defined(__ppc) || defined(__ppc__) || defined(__powerpc__)
  63. #define V3_ARCHITECTURE "ppc"
  64. #elif defined(__riscv)
  65. #if __riscv_xlen == 64
  66. #define V3_ARCHITECTURE "riscv64"
  67. #else
  68. #define V3_ARCHITECTURE "riscv"
  69. #endif
  70. #else
  71. #define V3_ARCHITECTURE "unknown"
  72. #endif
  73. #if defined(__HAIKU__)
  74. #define V3_PLATFORM "haiku"
  75. #elif defined(__linux__) || defined(__linux)
  76. #define V3_PLATFORM "linux"
  77. #elif defined(__FreeBSD__)
  78. #define V3_PLATFORM "freebsd"
  79. #elif defined(__NetBSD__)
  80. #define V3_PLATFORM "netbsd"
  81. #elif defined(__OpenBSD__)
  82. #define V3_PLATFORM "openbsd"
  83. #elif defined(__GNU__)
  84. #define V3_PLATFORM "hurd"
  85. #else
  86. #define V3_PLATFORM "unknown"
  87. #endif
  88. #endif
  89. #if defined(CARLA_OS_MAC)
  90. #define V3_CONTENT_DIR "MacOS"
  91. #elif defined(_M_ARM64)
  92. #define V3_CONTENT_DIR "aarch64-win" /* TBD */
  93. #elif defined(_M_ARM)
  94. #define V3_CONTENT_DIR "arm-win" /* TBD */
  95. #elif defined(CARLA_OS_WIN64)
  96. #define V3_CONTENT_DIR "x86_64-win"
  97. #elif defined(CARLA_OS_WIN32)
  98. #define V3_CONTENT_DIR "x86-win"
  99. #else
  100. #define V3_CONTENT_DIR V3_ARCHITECTURE "-" V3_PLATFORM
  101. #endif
  102. #if defined(CARLA_OS_MAC)
  103. # define V3_ENTRYFNNAME "bundleEntry"
  104. # define V3_EXITFNNAME "bundleExit"
  105. typedef void (*V3_ENTRYFN)(void*);
  106. typedef void (*V3_EXITFN)(void);
  107. #elif defined(CARLA_OS_WIN)
  108. # define V3_ENTRYFNNAME "InitDll"
  109. # define V3_EXITFNNAME "ExitDll"
  110. typedef void (*V3_ENTRYFN)(void);
  111. typedef void (*V3_EXITFN)(void);
  112. #else
  113. # define V3_ENTRYFNNAME "ModuleEntry"
  114. # define V3_EXITFNNAME "ModuleExit"
  115. typedef void (*V3_ENTRYFN)(void*);
  116. typedef void (*V3_EXITFN)(void);
  117. #endif
  118. #define V3_GETFNNAME "GetPluginFactory"
  119. typedef v3_plugin_factory** (*V3_GETFN)(void);
  120. // --------------------------------------------------------------------------------------------------------------------
  121. CARLA_BACKEND_START_NAMESPACE
  122. // --------------------------------------------------------------------------------------------------------------------
  123. static inline
  124. PluginCategory getPluginCategoryFromV3SubCategories(const char* const subcategories) noexcept
  125. {
  126. if (std::strstr(subcategories, "Instrument") != nullptr)
  127. return PLUGIN_CATEGORY_SYNTH;
  128. return subcategories[0] != '\0' ? PLUGIN_CATEGORY_OTHER : PLUGIN_CATEGORY_NONE;
  129. }
  130. static inline constexpr
  131. uint32_t v3_cconst(const uint8_t a, const uint8_t b, const uint8_t c, const uint8_t d) noexcept
  132. {
  133. return static_cast<uint32_t>((a << 24) | (b << 16) | (c << 8) | (d << 0));
  134. }
  135. static inline
  136. const char* tuid2str(const v3_tuid iid) noexcept
  137. {
  138. static char buf[44];
  139. std::snprintf(buf, sizeof(buf), "0x%08X,0x%08X,0x%08X,0x%08X",
  140. v3_cconst(iid[ 0], iid[ 1], iid[ 2], iid[ 3]),
  141. v3_cconst(iid[ 4], iid[ 5], iid[ 6], iid[ 7]),
  142. v3_cconst(iid[ 8], iid[ 9], iid[10], iid[11]),
  143. v3_cconst(iid[12], iid[13], iid[14], iid[15]));
  144. return buf;
  145. }
  146. // --------------------------------------------------------------------------------------------------------------------
  147. struct v3_bus_mini_info {
  148. uint16_t offset;
  149. int32_t bus_type;
  150. uint32_t flags;
  151. };
  152. // --------------------------------------------------------------------------------------------------------------------
  153. template<class T>
  154. T** v3_create_class_ptr()
  155. {
  156. T** const clsptr = new T*;
  157. *clsptr = new T;
  158. return clsptr;
  159. }
  160. // --------------------------------------------------------------------------------------------------------------------
  161. template<class v3_class, const v3_tuid cid>
  162. static inline
  163. v3_result V3_API v3_query_interface(void* const self, const v3_tuid iid, void** const iface)
  164. {
  165. v3_class* const cls = *static_cast<v3_class**>(self);
  166. if (v3_tuid_match(iid, v3_funknown_iid) || v3_tuid_match(iid, cid))
  167. {
  168. ++cls->refcounter;
  169. *iface = self;
  170. return V3_OK;
  171. }
  172. *iface = nullptr;
  173. return V3_NO_INTERFACE;
  174. }
  175. template<class v3_class>
  176. static inline
  177. uint32_t V3_API v3_ref(void* const self)
  178. {
  179. v3_class* const cls = *static_cast<v3_class**>(self);
  180. return ++cls->refcounter;
  181. }
  182. template<class v3_class>
  183. static inline
  184. uint32_t V3_API v3_unref(void* const self)
  185. {
  186. v3_class** const clsptr = static_cast<v3_class**>(self);
  187. v3_class* const cls = *clsptr;
  188. if (const int refcount = --cls->refcounter)
  189. return refcount;
  190. delete cls;
  191. delete clsptr;
  192. return 0;
  193. }
  194. // --------------------------------------------------------------------------------------------------------------------
  195. template<const v3_tuid cid>
  196. static inline
  197. v3_result V3_API v3_query_interface_static(void* const self, const v3_tuid iid, void** const iface)
  198. {
  199. if (v3_tuid_match(iid, v3_funknown_iid) || v3_tuid_match(iid, cid))
  200. {
  201. *iface = self;
  202. return V3_OK;
  203. }
  204. *iface = nullptr;
  205. return V3_NO_INTERFACE;
  206. }
  207. static inline
  208. uint32_t V3_API v3_ref_static(void*) { return 1; }
  209. static inline
  210. uint32_t V3_API v3_unref_static(void*) { return 0; }
  211. // --------------------------------------------------------------------------------------------------------------------
  212. CARLA_BACKEND_END_NAMESPACE
  213. #endif // CARLA_VST3_UTILS_HPP_INCLUDED