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

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