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.

345 lines
9.8KB

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