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.

360 lines
10KB

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