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.

385 lines
11KB

  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 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_COPY_STRUCT(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. double tick;
  129. bool needsReset;
  130. uint64_t nextFrame;
  131. #ifndef BUILD_BRIDGE
  132. struct Hylia {
  133. bool enabled;
  134. hylia_t* instance;
  135. hylia_time_info_t timeInfo;
  136. Hylia();
  137. ~Hylia();
  138. CARLA_DECLARE_NON_COPY_STRUCT(Hylia)
  139. } hylia;
  140. #endif
  141. EngineTimeInfo& timeInfo;
  142. const EngineTransportMode& transportMode;
  143. friend class PendingRtEventsRunner;
  144. void preProcess(uint32_t numFrames);
  145. void fillEngineTimeInfo(uint32_t newFrames) noexcept;
  146. friend class CarlaEngineJack;
  147. void fillJackTimeInfo(jack_position_t* pos, uint32_t newFrames) noexcept;
  148. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  149. };
  150. // -----------------------------------------------------------------------
  151. // EngineNextAction
  152. enum EnginePostAction {
  153. kEnginePostActionNull = 0,
  154. kEnginePostActionZeroCount, // set curPluginCount to 0
  155. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  156. kEnginePostActionRemovePlugin, // remove a plugin
  157. kEnginePostActionSwitchPlugins // switch between 2 plugins
  158. #endif
  159. };
  160. struct EngineNextAction {
  161. EnginePostAction opcode;
  162. uint pluginId;
  163. uint value;
  164. CarlaMutex mutex;
  165. bool needsPost;
  166. volatile bool postDone;
  167. carla_sem_t* sem;
  168. EngineNextAction() noexcept;
  169. ~EngineNextAction() noexcept;
  170. void clearAndReset() noexcept;
  171. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  172. };
  173. // -----------------------------------------------------------------------
  174. // EnginePluginData
  175. struct EnginePluginData {
  176. CarlaPluginPtr plugin;
  177. float peaks[4];
  178. EnginePluginData()
  179. : plugin(nullptr),
  180. #ifdef CARLA_PROPER_CPP11_SUPPORT
  181. peaks{0.0f, 0.0f, 0.0f, 0.0f} {}
  182. #else
  183. peaks()
  184. {
  185. carla_zeroStruct(peaks);
  186. }
  187. #endif
  188. };
  189. // -----------------------------------------------------------------------
  190. // CarlaEngineProtectedData
  191. struct CarlaEngine::ProtectedData {
  192. CarlaEngineThread thread;
  193. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  194. CarlaEngineOsc osc;
  195. #endif
  196. EngineCallbackFunc callback;
  197. void* callbackPtr;
  198. FileCallbackFunc fileCallback;
  199. void* fileCallbackPtr;
  200. bool actionCanceled;
  201. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  202. bool loadingProject;
  203. bool ignoreClientPrefix; // backwards compat only
  204. CarlaString currentProjectFilename;
  205. CarlaString currentProjectFolder;
  206. #endif
  207. uint32_t bufferSize;
  208. double sampleRate;
  209. bool aboutToClose; // don't re-activate thread if true
  210. int isIdling; // don't allow any operations while idling
  211. uint curPluginCount; // number of plugins loaded (0...max)
  212. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  213. uint nextPluginId; // invalid if == maxPluginNumber
  214. CarlaMutex envMutex;
  215. CarlaString lastError;
  216. CarlaString name;
  217. EngineOptions options;
  218. EngineTimeInfo timeInfo;
  219. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  220. EnginePluginData plugins[1];
  221. #else
  222. EnginePluginData* plugins;
  223. uint32_t xruns;
  224. float dspLoad;
  225. #endif
  226. float peaks[4];
  227. std::vector<CarlaPluginPtr> pluginsToDelete;
  228. EngineInternalEvents events;
  229. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  230. EngineInternalGraph graph;
  231. #endif
  232. EngineInternalTime time;
  233. EngineNextAction nextAction;
  234. // -------------------------------------------------------------------
  235. ProtectedData(CarlaEngine* engine);
  236. ~ProtectedData();
  237. // -------------------------------------------------------------------
  238. bool init(const char* clientName);
  239. void close();
  240. void initTime(const char* features);
  241. // -------------------------------------------------------------------
  242. void deletePluginsAsNeeded();
  243. // -------------------------------------------------------------------
  244. void doPluginRemove(uint pluginId) noexcept;
  245. void doPluginsSwitch(uint idA, uint idB) noexcept;
  246. void doNextPluginAction() noexcept;
  247. // -------------------------------------------------------------------
  248. #ifdef CARLA_PROPER_CPP11_SUPPORT
  249. ProtectedData() = delete;
  250. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  251. #endif
  252. };
  253. // -----------------------------------------------------------------------
  254. class PendingRtEventsRunner
  255. {
  256. public:
  257. PendingRtEventsRunner(CarlaEngine* engine,
  258. uint32_t numFrames,
  259. bool calcDSPLoad = false) noexcept;
  260. ~PendingRtEventsRunner() noexcept;
  261. private:
  262. CarlaEngine::ProtectedData* const pData;
  263. int64_t prevTime;
  264. CARLA_PREVENT_HEAP_ALLOCATION
  265. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  266. };
  267. // -----------------------------------------------------------------------
  268. class ScopedActionLock
  269. {
  270. public:
  271. ScopedActionLock(CarlaEngine* engine, EnginePostAction action, uint pluginId, uint value) noexcept;
  272. ~ScopedActionLock() noexcept;
  273. private:
  274. CarlaEngine::ProtectedData* const pData;
  275. CARLA_PREVENT_HEAP_ALLOCATION
  276. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  277. };
  278. // -----------------------------------------------------------------------
  279. class ScopedThreadStopper
  280. {
  281. public:
  282. ScopedThreadStopper(CarlaEngine* engine) noexcept;
  283. ~ScopedThreadStopper() noexcept;
  284. private:
  285. CarlaEngine* const engine;
  286. CarlaEngine::ProtectedData* const pData;
  287. CARLA_PREVENT_HEAP_ALLOCATION
  288. CARLA_DECLARE_NON_COPY_CLASS(ScopedThreadStopper)
  289. };
  290. // -----------------------------------------------------------------------
  291. CARLA_BACKEND_END_NAMESPACE
  292. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED