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.

384 lines
10KB

  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 removePlugin(CarlaPluginPtr plugin);
  92. void removeAllPlugins();
  93. bool isUsingExternalHost() const noexcept;
  94. bool isUsingExternalOSC() const noexcept;
  95. void setUsingExternalHost(bool usingExternal) noexcept;
  96. void setUsingExternalOSC(bool usingExternal) noexcept;
  97. private:
  98. bool fIsRack;
  99. uint32_t fNumAudioOuts;
  100. volatile bool fIsReady;
  101. union {
  102. RackGraph* fRack;
  103. PatchbayGraph* fPatchbay;
  104. };
  105. CarlaEngine* const kEngine;
  106. CARLA_PREVENT_HEAP_ALLOCATION
  107. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  108. };
  109. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  110. // -----------------------------------------------------------------------
  111. // InternalTime
  112. class EngineInternalTime {
  113. public:
  114. EngineInternalTime(EngineTimeInfo& timeInfo, const EngineTransportMode& transportMode) noexcept;
  115. void init(uint32_t bufferSize, double sampleRate);
  116. void updateAudioValues(uint32_t bufferSize, double sampleRate);
  117. void enableLink(bool enable);
  118. void setBPM(double bpm);
  119. void setNeedsReset() noexcept;
  120. void pause() noexcept;
  121. void relocate(uint64_t frame) noexcept;
  122. private:
  123. double beatsPerBar;
  124. double beatsPerMinute;
  125. double bufferSize;
  126. double sampleRate;
  127. double tick;
  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_COPY_STRUCT(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_COPY_STRUCT(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_COPY_STRUCT(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. CarlaEngineThread thread;
  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 thread 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_COPY_STRUCT(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_COPY_CLASS(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_COPY_CLASS(ScopedActionLock)
  276. };
  277. // -----------------------------------------------------------------------
  278. class ScopedThreadStopper
  279. {
  280. public:
  281. ScopedThreadStopper(CarlaEngine* engine) noexcept;
  282. ~ScopedThreadStopper() noexcept;
  283. private:
  284. CarlaEngine* const engine;
  285. CarlaEngine::ProtectedData* const pData;
  286. CARLA_PREVENT_HEAP_ALLOCATION
  287. CARLA_DECLARE_NON_COPY_CLASS(ScopedThreadStopper)
  288. };
  289. // -----------------------------------------------------------------------
  290. CARLA_BACKEND_END_NAMESPACE
  291. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED