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.

366 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. const EngineProcessMode processMode;
  171. LinkedList<CarlaEngineEventCV> cvs;
  172. ProtectedData(const EngineProcessMode pm) noexcept;
  173. ~ProtectedData() noexcept;
  174. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  175. };
  176. // -----------------------------------------------------------------------
  177. // CarlaEngineProtectedData
  178. struct CarlaEngine::ProtectedData {
  179. CarlaEngineThread thread;
  180. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  181. CarlaEngineOsc osc;
  182. #endif
  183. EngineCallbackFunc callback;
  184. void* callbackPtr;
  185. FileCallbackFunc fileCallback;
  186. void* fileCallbackPtr;
  187. bool actionCanceled;
  188. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  189. bool loadingProject;
  190. CarlaString currentProjectFilename;
  191. #endif
  192. uint32_t bufferSize;
  193. double sampleRate;
  194. bool aboutToClose; // don't re-activate thread if true
  195. int isIdling; // don't allow any operations while idling
  196. uint curPluginCount; // number of plugins loaded (0...max)
  197. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  198. uint nextPluginId; // invalid if == maxPluginNumber
  199. CarlaMutex envMutex;
  200. CarlaString lastError;
  201. CarlaString name;
  202. EngineOptions options;
  203. EngineTimeInfo timeInfo;
  204. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  205. EnginePluginData plugins[1];
  206. #else
  207. EnginePluginData* plugins;
  208. uint32_t xruns;
  209. float dspLoad;
  210. #endif
  211. float peaks[4];
  212. EngineInternalEvents events;
  213. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  214. EngineInternalGraph graph;
  215. #endif
  216. EngineInternalTime time;
  217. EngineNextAction nextAction;
  218. // -------------------------------------------------------------------
  219. ProtectedData(CarlaEngine* engine) noexcept;
  220. ~ProtectedData() noexcept;
  221. // -------------------------------------------------------------------
  222. bool init(const char* clientName);
  223. void close();
  224. void initTime(const char* features);
  225. // -------------------------------------------------------------------
  226. void doPluginRemove(uint pluginId) noexcept;
  227. void doPluginsSwitch(uint idA, uint idB) noexcept;
  228. void doNextPluginAction() noexcept;
  229. // -------------------------------------------------------------------
  230. #ifdef CARLA_PROPER_CPP11_SUPPORT
  231. ProtectedData() = delete;
  232. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  233. #endif
  234. };
  235. // -----------------------------------------------------------------------
  236. class PendingRtEventsRunner
  237. {
  238. public:
  239. PendingRtEventsRunner(CarlaEngine* engine,
  240. uint32_t numFrames,
  241. bool calcDSPLoad = false) noexcept;
  242. ~PendingRtEventsRunner() noexcept;
  243. private:
  244. CarlaEngine::ProtectedData* const pData;
  245. int64_t prevTime;
  246. CARLA_PREVENT_HEAP_ALLOCATION
  247. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  248. };
  249. // -----------------------------------------------------------------------
  250. class ScopedActionLock
  251. {
  252. public:
  253. ScopedActionLock(CarlaEngine* engine, EnginePostAction action, uint pluginId, uint value) noexcept;
  254. ~ScopedActionLock() noexcept;
  255. private:
  256. CarlaEngine::ProtectedData* const pData;
  257. CARLA_PREVENT_HEAP_ALLOCATION
  258. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  259. };
  260. // -----------------------------------------------------------------------
  261. class ScopedThreadStopper
  262. {
  263. public:
  264. ScopedThreadStopper(CarlaEngine* engine) noexcept;
  265. ~ScopedThreadStopper() noexcept;
  266. private:
  267. CarlaEngine* const engine;
  268. CarlaEngine::ProtectedData* const pData;
  269. CARLA_PREVENT_HEAP_ALLOCATION
  270. CARLA_DECLARE_NON_COPY_CLASS(ScopedThreadStopper)
  271. };
  272. // -----------------------------------------------------------------------
  273. CARLA_BACKEND_END_NAMESPACE
  274. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED