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.

CarlaEngineInternal.hpp 9.8KB

11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 isUsingExternalHost() const noexcept;
  77. bool isUsingExternalOSC() const noexcept;
  78. void setUsingExternalHost(const bool usingExternal) noexcept;
  79. void setUsingExternalOSC(const 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(const uint32_t bufferSize, double sampleRate);
  98. void updateAudioValues(const uint32_t bufferSize, const double sampleRate);
  99. void enableLink(const bool enable);
  100. void setBPM(const double bpm);
  101. void setNeedsReset() noexcept;
  102. void pause() noexcept;
  103. void relocate(const 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(const uint32_t numFrames);
  126. void fillEngineTimeInfo(const uint32_t newFrames) noexcept;
  127. friend class CarlaEngineJack;
  128. void fillJackTimeInfo(jack_position_t* const pos, const 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. uint hints;
  177. uint32_t bufferSize;
  178. double sampleRate;
  179. bool aboutToClose; // don't re-activate thread if true
  180. int isIdling; // don't allow any operations while idling
  181. uint curPluginCount; // number of plugins loaded (0...max)
  182. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  183. uint nextPluginId; // invalid if == maxPluginNumber
  184. CarlaMutex envMutex;
  185. CarlaString lastError;
  186. CarlaString name;
  187. EngineOptions options;
  188. EngineTimeInfo timeInfo;
  189. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  190. EnginePluginData plugins[1];
  191. #else
  192. EnginePluginData* plugins;
  193. uint32_t xruns;
  194. float dspLoad;
  195. #endif
  196. float peaks[4];
  197. EngineInternalEvents events;
  198. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  199. EngineInternalGraph graph;
  200. #endif
  201. EngineInternalTime time;
  202. EngineNextAction nextAction;
  203. // -------------------------------------------------------------------
  204. ProtectedData(CarlaEngine* const engine) noexcept;
  205. ~ProtectedData() noexcept;
  206. // -------------------------------------------------------------------
  207. bool init(const char* const clientName);
  208. void close();
  209. void initTime(const char* const features);
  210. // -------------------------------------------------------------------
  211. void doPluginRemove(const uint pluginId) noexcept;
  212. void doPluginsSwitch(const uint idA, const uint idB) noexcept;
  213. void doNextPluginAction() noexcept;
  214. // -------------------------------------------------------------------
  215. #ifdef CARLA_PROPER_CPP11_SUPPORT
  216. ProtectedData() = delete;
  217. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  218. #endif
  219. };
  220. // -----------------------------------------------------------------------
  221. class PendingRtEventsRunner
  222. {
  223. public:
  224. PendingRtEventsRunner(CarlaEngine* const engine,
  225. const uint32_t numFrames,
  226. const bool calcDSPLoad = false) noexcept;
  227. ~PendingRtEventsRunner() noexcept;
  228. private:
  229. CarlaEngine::ProtectedData* const pData;
  230. int64_t prevTime;
  231. CARLA_PREVENT_HEAP_ALLOCATION
  232. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  233. };
  234. // -----------------------------------------------------------------------
  235. class ScopedActionLock
  236. {
  237. public:
  238. ScopedActionLock(CarlaEngine* const engine, const EnginePostAction action, const uint pluginId, const uint value) noexcept;
  239. ~ScopedActionLock() noexcept;
  240. private:
  241. CarlaEngine::ProtectedData* const pData;
  242. CARLA_PREVENT_HEAP_ALLOCATION
  243. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  244. };
  245. // -----------------------------------------------------------------------
  246. class ScopedThreadStopper
  247. {
  248. public:
  249. ScopedThreadStopper(CarlaEngine* const engine) noexcept;
  250. ~ScopedThreadStopper() noexcept;
  251. private:
  252. CarlaEngine* const engine;
  253. CarlaEngine::ProtectedData* const pData;
  254. CARLA_PREVENT_HEAP_ALLOCATION
  255. CARLA_DECLARE_NON_COPY_CLASS(ScopedThreadStopper)
  256. };
  257. // -----------------------------------------------------------------------
  258. CARLA_BACKEND_END_NAMESPACE
  259. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED