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.

333 lines
9.5KB

  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
  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_ALTERNATIVE_ARCH
  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 // BUILD_BRIDGE_ALTERNATIVE_ARCH
  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_ALTERNATIVE_ARCH
  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)
  161. CarlaEngineOsc osc;
  162. const CarlaOscData* oscData;
  163. #endif
  164. EngineCallbackFunc callback;
  165. void* callbackPtr;
  166. FileCallbackFunc fileCallback;
  167. void* fileCallbackPtr;
  168. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  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_ALTERNATIVE_ARCH
  185. EnginePluginData plugins[1];
  186. #else
  187. EnginePluginData* plugins;
  188. #endif
  189. EngineInternalEvents events;
  190. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  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(const uint pluginId) noexcept;
  204. void doPluginsSwitch(const uint idA, const uint idB) noexcept;
  205. void doNextPluginAction() 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) 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