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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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();
  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. struct EngineNextAction {
  160. EnginePostAction opcode;
  161. uint pluginId;
  162. uint value;
  163. CarlaMutex mutex;
  164. bool needsPost;
  165. volatile bool postDone;
  166. carla_sem_t* sem;
  167. EngineNextAction() noexcept;
  168. ~EngineNextAction() noexcept;
  169. void clearAndReset() noexcept;
  170. CARLA_DECLARE_NON_COPYABLE(EngineNextAction)
  171. };
  172. // -----------------------------------------------------------------------
  173. // EnginePluginData
  174. struct EnginePluginData {
  175. CarlaPluginPtr plugin;
  176. float peaks[4];
  177. EnginePluginData()
  178. : plugin(nullptr),
  179. #ifdef CARLA_PROPER_CPP11_SUPPORT
  180. peaks{0.0f, 0.0f, 0.0f, 0.0f} {}
  181. #else
  182. peaks()
  183. {
  184. carla_zeroStruct(peaks);
  185. }
  186. #endif
  187. };
  188. // -----------------------------------------------------------------------
  189. // CarlaEngineProtectedData
  190. struct CarlaEngine::ProtectedData {
  191. CarlaEngineRunner runner;
  192. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  193. CarlaEngineOsc osc;
  194. #endif
  195. EngineCallbackFunc callback;
  196. void* callbackPtr;
  197. FileCallbackFunc fileCallback;
  198. void* fileCallbackPtr;
  199. bool actionCanceled;
  200. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  201. bool loadingProject;
  202. bool ignoreClientPrefix; // backwards compat only
  203. CarlaString currentProjectFilename;
  204. CarlaString currentProjectFolder;
  205. #endif
  206. uint32_t bufferSize;
  207. double sampleRate;
  208. bool aboutToClose; // don't re-activate runner if true
  209. int isIdling; // don't allow any operations while idling
  210. uint curPluginCount; // number of plugins loaded (0...max)
  211. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  212. uint nextPluginId; // invalid if == maxPluginNumber
  213. CarlaMutex envMutex;
  214. CarlaString lastError;
  215. CarlaString name;
  216. EngineOptions options;
  217. EngineTimeInfo timeInfo;
  218. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  219. EnginePluginData plugins[1];
  220. #else
  221. EnginePluginData* plugins;
  222. uint32_t xruns;
  223. float dspLoad;
  224. #endif
  225. float peaks[4];
  226. std::vector<CarlaPluginPtr> pluginsToDelete;
  227. EngineInternalEvents events;
  228. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  229. EngineInternalGraph graph;
  230. #endif
  231. EngineInternalTime time;
  232. EngineNextAction nextAction;
  233. // -------------------------------------------------------------------
  234. ProtectedData(CarlaEngine* engine);
  235. ~ProtectedData();
  236. // -------------------------------------------------------------------
  237. bool init(const char* clientName);
  238. void close();
  239. void initTime(const char* features);
  240. // -------------------------------------------------------------------
  241. void deletePluginsAsNeeded();
  242. // -------------------------------------------------------------------
  243. void doPluginRemove(uint pluginId) noexcept;
  244. void doPluginsSwitch(uint idA, uint idB) noexcept;
  245. void doNextPluginAction() noexcept;
  246. // -------------------------------------------------------------------
  247. #ifdef CARLA_PROPER_CPP11_SUPPORT
  248. ProtectedData() = delete;
  249. CARLA_DECLARE_NON_COPYABLE(ProtectedData)
  250. #endif
  251. };
  252. // -----------------------------------------------------------------------
  253. class PendingRtEventsRunner
  254. {
  255. public:
  256. PendingRtEventsRunner(CarlaEngine* engine,
  257. uint32_t numFrames,
  258. bool calcDSPLoad = false) noexcept;
  259. ~PendingRtEventsRunner() noexcept;
  260. private:
  261. CarlaEngine::ProtectedData* const pData;
  262. int64_t prevTime;
  263. CARLA_PREVENT_HEAP_ALLOCATION
  264. CARLA_DECLARE_NON_COPYABLE(PendingRtEventsRunner)
  265. };
  266. // -----------------------------------------------------------------------
  267. class ScopedActionLock
  268. {
  269. public:
  270. ScopedActionLock(CarlaEngine* engine, EnginePostAction action, uint pluginId, uint value) noexcept;
  271. ~ScopedActionLock() noexcept;
  272. private:
  273. CarlaEngine::ProtectedData* const pData;
  274. CARLA_PREVENT_HEAP_ALLOCATION
  275. CARLA_DECLARE_NON_COPYABLE(ScopedActionLock)
  276. };
  277. // -----------------------------------------------------------------------
  278. class ScopedRunnerStopper
  279. {
  280. public:
  281. ScopedRunnerStopper(CarlaEngine* engine) noexcept;
  282. ~ScopedRunnerStopper() noexcept;
  283. private:
  284. CarlaEngine* const engine;
  285. CarlaEngine::ProtectedData* const pData;
  286. CARLA_PREVENT_HEAP_ALLOCATION
  287. CARLA_DECLARE_NON_COPYABLE(ScopedRunnerStopper)
  288. };
  289. // -----------------------------------------------------------------------
  290. CARLA_BACKEND_END_NAMESPACE
  291. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED