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.

CarlaEngineInternal.hpp 11KB

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