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.6KB

  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* engine) noexcept;
  58. ~EngineInternalGraph() noexcept;
  59. void create(uint32_t audioIns, uint32_t audioOuts, uint32_t cvIns, uint32_t cvOuts);
  60. void destroy() noexcept;
  61. void setBufferSize(uint32_t bufferSize);
  62. void setSampleRate(double sampleRate);
  63. void setOffline(bool offline);
  64. bool isReady() const noexcept;
  65. RackGraph* getRackGraph() const noexcept;
  66. PatchbayGraph* getPatchbayGraph() const noexcept;
  67. void process(CarlaEngine::ProtectedData* data, const float* const* inBuf, float* const* outBuf, uint32_t frames);
  68. // special direct process with connections already handled, used in JACK and Plugin
  69. void processRack(CarlaEngine::ProtectedData* data, const float* inBuf[2], float* outBuf[2], uint32_t frames);
  70. // used for internal patchbay mode
  71. void addPlugin(CarlaPlugin* plugin);
  72. void replacePlugin(CarlaPlugin* oldPlugin, CarlaPlugin* newPlugin);
  73. void renamePlugin(CarlaPlugin* plugin, const char* newName);
  74. void removePlugin(CarlaPlugin* plugin);
  75. void removeAllPlugins();
  76. bool isUsingExternalHost() const noexcept;
  77. bool isUsingExternalOSC() const noexcept;
  78. void setUsingExternalHost(bool usingExternal) noexcept;
  79. void setUsingExternalOSC(bool usingExternal) noexcept;
  80. private:
  81. bool fIsRack;
  82. bool fIsReady;
  83. union {
  84. RackGraph* fRack;
  85. PatchbayGraph* fPatchbay;
  86. };
  87. CarlaEngine* const kEngine;
  88. CARLA_PREVENT_HEAP_ALLOCATION
  89. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  90. };
  91. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  92. // -----------------------------------------------------------------------
  93. // InternalTime
  94. class EngineInternalTime {
  95. public:
  96. EngineInternalTime(EngineTimeInfo& timeInfo, const EngineTransportMode& transportMode) noexcept;
  97. void init(uint32_t bufferSize, double sampleRate);
  98. void updateAudioValues(uint32_t bufferSize, double sampleRate);
  99. void enableLink(bool enable);
  100. void setBPM(double bpm);
  101. void setNeedsReset() noexcept;
  102. void pause() noexcept;
  103. void relocate(uint64_t frame) noexcept;
  104. private:
  105. double beatsPerBar;
  106. double beatsPerMinute;
  107. double bufferSize;
  108. double sampleRate;
  109. double tick;
  110. bool needsReset;
  111. uint64_t nextFrame;
  112. #ifndef BUILD_BRIDGE
  113. struct Hylia {
  114. bool enabled;
  115. hylia_t* instance;
  116. hylia_time_info_t timeInfo;
  117. Hylia();
  118. ~Hylia();
  119. CARLA_DECLARE_NON_COPY_STRUCT(Hylia)
  120. } hylia;
  121. #endif
  122. EngineTimeInfo& timeInfo;
  123. const EngineTransportMode& transportMode;
  124. friend class PendingRtEventsRunner;
  125. void preProcess(uint32_t numFrames);
  126. void fillEngineTimeInfo(uint32_t newFrames) noexcept;
  127. friend class CarlaEngineJack;
  128. void fillJackTimeInfo(jack_position_t* pos, uint32_t newFrames) noexcept;
  129. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  130. };
  131. // -----------------------------------------------------------------------
  132. // EngineNextAction
  133. enum EnginePostAction {
  134. kEnginePostActionNull = 0,
  135. kEnginePostActionZeroCount, // set curPluginCount to 0
  136. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  137. kEnginePostActionRemovePlugin, // remove a plugin
  138. kEnginePostActionSwitchPlugins // switch between 2 plugins
  139. #endif
  140. };
  141. struct EngineNextAction {
  142. EnginePostAction opcode;
  143. uint pluginId;
  144. uint value;
  145. CarlaMutex mutex;
  146. bool needsPost;
  147. volatile bool postDone;
  148. carla_sem_t* sem;
  149. EngineNextAction() noexcept;
  150. ~EngineNextAction() noexcept;
  151. void clearAndReset() noexcept;
  152. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  153. };
  154. // -----------------------------------------------------------------------
  155. // EnginePluginData
  156. struct EnginePluginData {
  157. CarlaPlugin* plugin;
  158. float peaks[4];
  159. };
  160. // -----------------------------------------------------------------------
  161. // CarlaEngineProtectedData
  162. struct CarlaEngine::ProtectedData {
  163. CarlaEngineThread thread;
  164. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  165. CarlaEngineOsc osc;
  166. #endif
  167. EngineCallbackFunc callback;
  168. void* callbackPtr;
  169. FileCallbackFunc fileCallback;
  170. void* fileCallbackPtr;
  171. bool actionCanceled;
  172. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  173. bool loadingProject;
  174. CarlaString currentProjectFilename;
  175. #endif
  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* engine) noexcept;
  204. ~ProtectedData() noexcept;
  205. // -------------------------------------------------------------------
  206. bool init(const char* clientName);
  207. void close();
  208. void initTime(const char* features);
  209. // -------------------------------------------------------------------
  210. void doPluginRemove(uint pluginId) noexcept;
  211. void doPluginsSwitch(uint idA, 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* engine,
  224. uint32_t numFrames,
  225. 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* engine, EnginePostAction action, uint pluginId, 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* 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