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.

348 lines
9.7KB

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