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.

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