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

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