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.

349 lines
9.8KB

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