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.

335 lines
9.4KB

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