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.

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