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.

386 lines
11KB

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