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 10KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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 "CarlaEngineThread.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_COPY_STRUCT(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 isReady() const noexcept;
  70. RackGraph* getRackGraph() const noexcept;
  71. PatchbayGraph* getPatchbayGraph() const noexcept;
  72. PatchbayGraph* getPatchbayGraphOrNull() const noexcept;
  73. void process(CarlaEngine::ProtectedData* data, const float* const* inBuf, float* const* outBuf, uint32_t frames);
  74. // special direct process with connections already handled, used in JACK and Plugin
  75. void processRack(CarlaEngine::ProtectedData* data, const float* inBuf[2], float* outBuf[2], uint32_t frames);
  76. // used for internal patchbay mode
  77. void addPlugin(CarlaPluginPtr plugin);
  78. void replacePlugin(CarlaPluginPtr oldPlugin, CarlaPluginPtr newPlugin);
  79. void renamePlugin(CarlaPluginPtr plugin, const char* newName);
  80. void removePlugin(CarlaPluginPtr plugin);
  81. void removeAllPlugins();
  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. volatile bool fIsReady;
  89. union {
  90. RackGraph* fRack;
  91. PatchbayGraph* fPatchbay;
  92. };
  93. CarlaEngine* const kEngine;
  94. CARLA_PREVENT_HEAP_ALLOCATION
  95. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  96. };
  97. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  98. // -----------------------------------------------------------------------
  99. // InternalTime
  100. class EngineInternalTime {
  101. public:
  102. EngineInternalTime(EngineTimeInfo& timeInfo, const EngineTransportMode& transportMode) noexcept;
  103. void init(uint32_t bufferSize, double sampleRate);
  104. void updateAudioValues(uint32_t bufferSize, double sampleRate);
  105. void enableLink(bool enable);
  106. void setBPM(double bpm);
  107. void setNeedsReset() noexcept;
  108. void pause() noexcept;
  109. void relocate(uint64_t frame) noexcept;
  110. private:
  111. double beatsPerBar;
  112. double beatsPerMinute;
  113. double bufferSize;
  114. double sampleRate;
  115. double tick;
  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_COPY_STRUCT(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_COPY_STRUCT(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. struct EngineNextAction {
  148. EnginePostAction opcode;
  149. uint pluginId;
  150. uint value;
  151. CarlaMutex mutex;
  152. bool needsPost;
  153. volatile bool postDone;
  154. carla_sem_t* sem;
  155. EngineNextAction() noexcept;
  156. ~EngineNextAction() noexcept;
  157. void clearAndReset() noexcept;
  158. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  159. };
  160. // -----------------------------------------------------------------------
  161. // EnginePluginData
  162. struct EnginePluginData {
  163. CarlaPluginPtr plugin;
  164. float peaks[4];
  165. EnginePluginData()
  166. : plugin(nullptr),
  167. #ifdef CARLA_PROPER_CPP11_SUPPORT
  168. peaks{0.0f, 0.0f, 0.0f, 0.0f} {}
  169. #else
  170. peaks()
  171. {
  172. carla_zeroStruct(peaks);
  173. }
  174. #endif
  175. };
  176. // -----------------------------------------------------------------------
  177. // CarlaEngineProtectedData
  178. struct CarlaEngine::ProtectedData {
  179. CarlaEngineThread thread;
  180. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  181. CarlaEngineOsc osc;
  182. #endif
  183. EngineCallbackFunc callback;
  184. void* callbackPtr;
  185. FileCallbackFunc fileCallback;
  186. void* fileCallbackPtr;
  187. bool actionCanceled;
  188. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  189. bool loadingProject;
  190. bool ignoreClientPrefix; // backwards compat only
  191. CarlaString currentProjectFilename;
  192. CarlaString currentProjectFolder;
  193. #endif
  194. uint32_t bufferSize;
  195. double sampleRate;
  196. bool aboutToClose; // don't re-activate thread if true
  197. int isIdling; // don't allow any operations while idling
  198. uint curPluginCount; // number of plugins loaded (0...max)
  199. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  200. uint nextPluginId; // invalid if == maxPluginNumber
  201. CarlaMutex envMutex;
  202. CarlaString lastError;
  203. CarlaString name;
  204. EngineOptions options;
  205. EngineTimeInfo timeInfo;
  206. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  207. EnginePluginData plugins[1];
  208. #else
  209. EnginePluginData* plugins;
  210. uint32_t xruns;
  211. float dspLoad;
  212. #endif
  213. float peaks[4];
  214. std::vector<CarlaPluginPtr> pluginsToDelete;
  215. EngineInternalEvents events;
  216. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  217. EngineInternalGraph graph;
  218. #endif
  219. EngineInternalTime time;
  220. EngineNextAction nextAction;
  221. // -------------------------------------------------------------------
  222. ProtectedData(CarlaEngine* engine);
  223. ~ProtectedData();
  224. // -------------------------------------------------------------------
  225. bool init(const char* clientName);
  226. void close();
  227. void initTime(const char* features);
  228. // -------------------------------------------------------------------
  229. void deletePluginsAsNeeded();
  230. // -------------------------------------------------------------------
  231. void doPluginRemove(uint pluginId) noexcept;
  232. void doPluginsSwitch(uint idA, uint idB) noexcept;
  233. void doNextPluginAction() noexcept;
  234. // -------------------------------------------------------------------
  235. #ifdef CARLA_PROPER_CPP11_SUPPORT
  236. ProtectedData() = delete;
  237. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  238. #endif
  239. };
  240. // -----------------------------------------------------------------------
  241. class PendingRtEventsRunner
  242. {
  243. public:
  244. PendingRtEventsRunner(CarlaEngine* engine,
  245. uint32_t numFrames,
  246. bool calcDSPLoad = false) noexcept;
  247. ~PendingRtEventsRunner() noexcept;
  248. private:
  249. CarlaEngine::ProtectedData* const pData;
  250. int64_t prevTime;
  251. CARLA_PREVENT_HEAP_ALLOCATION
  252. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  253. };
  254. // -----------------------------------------------------------------------
  255. class ScopedActionLock
  256. {
  257. public:
  258. ScopedActionLock(CarlaEngine* engine, EnginePostAction action, uint pluginId, uint value) noexcept;
  259. ~ScopedActionLock() noexcept;
  260. private:
  261. CarlaEngine::ProtectedData* const pData;
  262. CARLA_PREVENT_HEAP_ALLOCATION
  263. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  264. };
  265. // -----------------------------------------------------------------------
  266. class ScopedThreadStopper
  267. {
  268. public:
  269. ScopedThreadStopper(CarlaEngine* engine) noexcept;
  270. ~ScopedThreadStopper() noexcept;
  271. private:
  272. CarlaEngine* const engine;
  273. CarlaEngine::ProtectedData* const pData;
  274. CARLA_PREVENT_HEAP_ALLOCATION
  275. CARLA_DECLARE_NON_COPY_CLASS(ScopedThreadStopper)
  276. };
  277. // -----------------------------------------------------------------------
  278. CARLA_BACKEND_END_NAMESPACE
  279. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED