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.

395 lines
11KB

  1. // SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #ifndef CARLA_ENGINE_INTERNAL_HPP_INCLUDED
  4. #define CARLA_ENGINE_INTERNAL_HPP_INCLUDED
  5. #include "CarlaEngineRunner.hpp"
  6. #include "CarlaEngineUtils.hpp"
  7. #include "CarlaPlugin.hpp"
  8. #include "LinkedList.hpp"
  9. #ifndef BUILD_BRIDGE
  10. # include "CarlaEngineOsc.hpp"
  11. # include "hylia/hylia.h"
  12. #endif
  13. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  14. # include "water/processors/AudioProcessorGraph.h"
  15. # include "water/containers/Array.h"
  16. # include "water/memory/Atomic.h"
  17. #endif
  18. #include <vector>
  19. // FIXME only use CARLA_PREVENT_HEAP_ALLOCATION for structs
  20. // maybe separate macro
  21. typedef struct _jack_position jack_position_t;
  22. struct carla_sem_t;
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. // Engine helper macro, sets lastError and returns false/NULL
  26. #define CARLA_SAFE_ASSERT_RETURN_ERR(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return false; }
  27. #define CARLA_SAFE_ASSERT_RETURN_ERRN(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return nullptr; }
  28. #define CARLA_SAFE_EXCEPTION_RETURN_ERR(excptMsg, errMsg) catch(...) { carla_safe_exception(excptMsg, __FILE__, __LINE__); setLastError(errMsg); return false; }
  29. #define CARLA_SAFE_EXCEPTION_RETURN_ERRN(excptMsg, errMsg) catch(...) { carla_safe_exception(excptMsg, __FILE__, __LINE__); setLastError(errMsg); return nullptr; }
  30. // -----------------------------------------------------------------------
  31. // InternalEvents
  32. struct EngineInternalEvents {
  33. EngineEvent* in;
  34. EngineEvent* out;
  35. EngineInternalEvents() noexcept;
  36. ~EngineInternalEvents() noexcept;
  37. void clear() noexcept;
  38. CARLA_DECLARE_NON_COPYABLE(EngineInternalEvents)
  39. };
  40. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  41. // -----------------------------------------------------------------------
  42. // InternalGraph
  43. struct RackGraph;
  44. class PatchbayGraph;
  45. class EngineInternalGraph
  46. {
  47. public:
  48. EngineInternalGraph(CarlaEngine* engine) noexcept;
  49. ~EngineInternalGraph() noexcept;
  50. void create(uint32_t audioIns, uint32_t audioOuts,
  51. uint32_t cvIns, uint32_t cvOuts,
  52. bool withMidiIn = true, bool withMidiOut = true);
  53. void destroy() noexcept;
  54. void setBufferSize(uint32_t bufferSize);
  55. void setSampleRate(double sampleRate);
  56. void setOffline(bool offline);
  57. bool isRack() const noexcept
  58. {
  59. return fIsRack;
  60. }
  61. bool isReady() const noexcept
  62. {
  63. return fIsReady;
  64. }
  65. uint32_t getNumAudioOuts() const noexcept
  66. {
  67. return fNumAudioOuts;
  68. }
  69. RackGraph* getRackGraph() const noexcept;
  70. PatchbayGraph* getPatchbayGraph() const noexcept;
  71. PatchbayGraph* getPatchbayGraphOrNull() const noexcept;
  72. void process(CarlaEngine::ProtectedData* data, const float* const* inBuf, float* const* outBuf, uint32_t frames);
  73. // special direct process with connections already handled, used in JACK and Plugin
  74. void processRack(CarlaEngine::ProtectedData* data, const float* inBuf[2], float* outBuf[2], uint32_t frames);
  75. // used for internal patchbay mode
  76. void addPlugin(CarlaPluginPtr plugin);
  77. void replacePlugin(CarlaPluginPtr oldPlugin, CarlaPluginPtr newPlugin);
  78. void renamePlugin(CarlaPluginPtr plugin, const char* newName);
  79. void switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB);
  80. void removePlugin(CarlaPluginPtr plugin);
  81. void removeAllPlugins(bool aboutToClose);
  82. bool isUsingExternalHost() const noexcept;
  83. bool isUsingExternalOSC() const noexcept;
  84. void setUsingExternalHost(bool usingExternal) noexcept;
  85. void setUsingExternalOSC(bool usingExternal) noexcept;
  86. private:
  87. bool fIsRack;
  88. uint32_t fNumAudioOuts;
  89. volatile bool fIsReady;
  90. union {
  91. RackGraph* fRack;
  92. PatchbayGraph* fPatchbay;
  93. };
  94. CarlaEngine* const kEngine;
  95. CARLA_PREVENT_HEAP_ALLOCATION
  96. CARLA_DECLARE_NON_COPYABLE(EngineInternalGraph)
  97. };
  98. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  99. // -----------------------------------------------------------------------
  100. // InternalTime
  101. class EngineInternalTime {
  102. public:
  103. EngineInternalTime(EngineTimeInfo& timeInfo, const EngineTransportMode& transportMode) noexcept;
  104. void init(uint32_t bufferSize, double sampleRate);
  105. void updateAudioValues(uint32_t bufferSize, double sampleRate);
  106. void enableLink(bool enable);
  107. void setBPM(double bpm);
  108. void setNeedsReset() noexcept;
  109. void pause() noexcept;
  110. void relocate(uint64_t frame) noexcept;
  111. private:
  112. double beatsPerBar;
  113. double beatsPerMinute;
  114. double bufferSize;
  115. double sampleRate;
  116. bool needsReset;
  117. uint64_t nextFrame;
  118. #ifndef BUILD_BRIDGE
  119. struct Hylia {
  120. bool enabled;
  121. hylia_t* instance;
  122. hylia_time_info_t timeInfo;
  123. Hylia();
  124. ~Hylia();
  125. CARLA_DECLARE_NON_COPYABLE(Hylia)
  126. } hylia;
  127. #endif
  128. EngineTimeInfo& timeInfo;
  129. const EngineTransportMode& transportMode;
  130. friend class PendingRtEventsRunner;
  131. void preProcess(uint32_t numFrames);
  132. void fillEngineTimeInfo(uint32_t newFrames) noexcept;
  133. friend class CarlaEngineJack;
  134. void fillJackTimeInfo(jack_position_t* pos, uint32_t newFrames) noexcept;
  135. CARLA_DECLARE_NON_COPYABLE(EngineInternalTime)
  136. };
  137. // -----------------------------------------------------------------------
  138. // EngineNextAction
  139. enum EnginePostAction {
  140. kEnginePostActionNull = 0,
  141. kEnginePostActionZeroCount, // set curPluginCount to 0
  142. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  143. kEnginePostActionRemovePlugin, // remove a plugin
  144. kEnginePostActionSwitchPlugins // switch between 2 plugins
  145. #endif
  146. };
  147. static inline
  148. const char* EnginePostAction2Str(const EnginePostAction action)
  149. {
  150. switch (action)
  151. {
  152. case kEnginePostActionNull:
  153. return "kEnginePostActionNull";
  154. case kEnginePostActionZeroCount:
  155. return "kEnginePostActionZeroCount";
  156. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  157. case kEnginePostActionRemovePlugin:
  158. return "kEnginePostActionRemovePlugin";
  159. case kEnginePostActionSwitchPlugins:
  160. return "kEnginePostActionSwitchPlugins";
  161. #endif
  162. }
  163. carla_stderr("CarlaBackend::EnginePostAction2Str(%i) - invalid action", action);
  164. return nullptr;
  165. }
  166. struct EngineNextAction {
  167. EnginePostAction opcode;
  168. uint pluginId;
  169. uint value;
  170. CarlaMutex mutex;
  171. bool needsPost;
  172. volatile bool postDone;
  173. carla_sem_t* sem;
  174. EngineNextAction() noexcept;
  175. ~EngineNextAction() noexcept;
  176. void clearAndReset() noexcept;
  177. CARLA_DECLARE_NON_COPYABLE(EngineNextAction)
  178. };
  179. // -----------------------------------------------------------------------
  180. // EnginePluginData
  181. struct EnginePluginData {
  182. CarlaPluginPtr plugin;
  183. float peaks[4];
  184. EnginePluginData()
  185. : plugin(nullptr),
  186. #ifdef CARLA_PROPER_CPP11_SUPPORT
  187. peaks{0.0f, 0.0f, 0.0f, 0.0f} {}
  188. #else
  189. peaks()
  190. {
  191. carla_zeroStruct(peaks);
  192. }
  193. #endif
  194. };
  195. // -----------------------------------------------------------------------
  196. // CarlaEngineProtectedData
  197. struct CarlaEngine::ProtectedData {
  198. CarlaEngineRunner runner;
  199. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  200. CarlaEngineOsc osc;
  201. #endif
  202. EngineCallbackFunc callback;
  203. void* callbackPtr;
  204. FileCallbackFunc fileCallback;
  205. void* fileCallbackPtr;
  206. bool actionCanceled;
  207. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  208. bool loadingProject;
  209. bool ignoreClientPrefix; // backwards compat only
  210. String currentProjectFilename;
  211. String currentProjectFolder;
  212. #endif
  213. uint32_t bufferSize;
  214. double sampleRate;
  215. bool aboutToClose; // don't re-activate runner if true
  216. int isIdling; // don't allow any operations while idling
  217. uint curPluginCount; // number of plugins loaded (0...max)
  218. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  219. uint nextPluginId; // invalid if == maxPluginNumber
  220. CarlaMutex envMutex;
  221. String lastError;
  222. String name;
  223. EngineOptions options;
  224. EngineTimeInfo timeInfo;
  225. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  226. EnginePluginData plugins[1];
  227. #else
  228. EnginePluginData* plugins;
  229. uint32_t xruns;
  230. float dspLoad;
  231. #endif
  232. float peaks[4];
  233. CarlaMutex pluginsToDeleteMutex;
  234. std::vector<CarlaPluginPtr> pluginsToDelete;
  235. EngineInternalEvents events;
  236. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  237. EngineInternalGraph graph;
  238. #endif
  239. EngineInternalTime time;
  240. EngineNextAction nextAction;
  241. // -------------------------------------------------------------------
  242. ProtectedData(CarlaEngine* engine);
  243. ~ProtectedData();
  244. // -------------------------------------------------------------------
  245. bool init(const char* clientName);
  246. void close();
  247. void initTime(const char* features);
  248. // -------------------------------------------------------------------
  249. void deletePluginsAsNeeded();
  250. // -------------------------------------------------------------------
  251. void doPluginRemove(uint pluginId) noexcept;
  252. void doPluginsSwitch(uint idA, uint idB) noexcept;
  253. void doNextPluginAction() noexcept;
  254. // -------------------------------------------------------------------
  255. #ifdef CARLA_PROPER_CPP11_SUPPORT
  256. ProtectedData() = delete;
  257. CARLA_DECLARE_NON_COPYABLE(ProtectedData)
  258. #endif
  259. };
  260. // -----------------------------------------------------------------------
  261. class PendingRtEventsRunner
  262. {
  263. public:
  264. PendingRtEventsRunner(CarlaEngine* engine,
  265. uint32_t numFrames,
  266. bool calcDSPLoad = false) noexcept;
  267. ~PendingRtEventsRunner() noexcept;
  268. private:
  269. CarlaEngine::ProtectedData* const pData;
  270. int64_t prevTime;
  271. CARLA_PREVENT_HEAP_ALLOCATION
  272. CARLA_DECLARE_NON_COPYABLE(PendingRtEventsRunner)
  273. };
  274. // -----------------------------------------------------------------------
  275. class ScopedActionLock
  276. {
  277. public:
  278. ScopedActionLock(CarlaEngine* engine, EnginePostAction action, uint pluginId, uint value) noexcept;
  279. ~ScopedActionLock() noexcept;
  280. private:
  281. CarlaEngine::ProtectedData* const pData;
  282. CARLA_PREVENT_HEAP_ALLOCATION
  283. CARLA_DECLARE_NON_COPYABLE(ScopedActionLock)
  284. };
  285. // -----------------------------------------------------------------------
  286. class ScopedRunnerStopper
  287. {
  288. public:
  289. ScopedRunnerStopper(CarlaEngine* engine) noexcept;
  290. ~ScopedRunnerStopper() noexcept;
  291. private:
  292. CarlaEngine* const engine;
  293. CarlaEngine::ProtectedData* const pData;
  294. CARLA_PREVENT_HEAP_ALLOCATION
  295. CARLA_DECLARE_NON_COPYABLE(ScopedRunnerStopper)
  296. };
  297. // -----------------------------------------------------------------------
  298. CARLA_BACKEND_END_NAMESPACE
  299. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED