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.

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