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.

329 lines
9.3KB

  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. struct 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 setNeedsReset() noexcept;
  93. void relocate(const uint64_t frame) noexcept;
  94. private:
  95. double beatsPerBar;
  96. double beatsPerMinute;
  97. double bufferSize;
  98. double sampleRate;
  99. double tick;
  100. bool needsReset;
  101. uint64_t nextFrame;
  102. #ifndef BUILD_BRIDGE
  103. struct Hylia {
  104. bool enabled;
  105. hylia_t* instance;
  106. hylia_time_info_t timeInfo;
  107. Hylia();
  108. ~Hylia();
  109. } hylia;
  110. #endif
  111. EngineTimeInfo& timeInfo;
  112. const EngineTransportMode& transportMode;
  113. friend class PendingRtEventsRunner;
  114. void preProcess(const uint32_t numFrames);
  115. void fillEngineTimeInfo(const uint32_t newFrames) noexcept;
  116. friend class CarlaEngineJack;
  117. void fillJackTimeInfo(jack_position_t* const pos, const uint32_t newFrames) noexcept;
  118. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  119. };
  120. // -----------------------------------------------------------------------
  121. // EngineNextAction
  122. enum EnginePostAction {
  123. kEnginePostActionNull = 0,
  124. kEnginePostActionZeroCount, // set curPluginCount to 0
  125. #ifndef BUILD_BRIDGE
  126. kEnginePostActionRemovePlugin, // remove a plugin
  127. kEnginePostActionSwitchPlugins // switch between 2 plugins
  128. #endif
  129. };
  130. struct EngineNextAction {
  131. EnginePostAction opcode;
  132. uint pluginId;
  133. uint value;
  134. CarlaMutex mutex;
  135. EngineNextAction() noexcept;
  136. ~EngineNextAction() noexcept;
  137. void ready() const noexcept;
  138. void clearAndReset() noexcept;
  139. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  140. };
  141. // -----------------------------------------------------------------------
  142. // EnginePluginData
  143. struct EnginePluginData {
  144. CarlaPlugin* plugin;
  145. float insPeak[2];
  146. float outsPeak[2];
  147. };
  148. // -----------------------------------------------------------------------
  149. // CarlaEngineProtectedData
  150. struct CarlaEngine::ProtectedData {
  151. CarlaEngineThread thread;
  152. #ifdef HAVE_LIBLO
  153. CarlaEngineOsc osc;
  154. # ifdef BUILD_BRIDGE
  155. CarlaOscData* oscData;
  156. # else
  157. const CarlaOscData* oscData;
  158. # endif
  159. #endif
  160. EngineCallbackFunc callback;
  161. void* callbackPtr;
  162. FileCallbackFunc fileCallback;
  163. void* fileCallbackPtr;
  164. #ifndef BUILD_BRIDGE
  165. // special hack for linuxsampler
  166. bool firstLinuxSamplerInstance;
  167. bool loadingProject;
  168. #endif
  169. uint hints;
  170. uint32_t bufferSize;
  171. double sampleRate;
  172. bool aboutToClose; // don't re-activate thread if true
  173. int isIdling; // don't allow any operations while idling
  174. uint curPluginCount; // number of plugins loaded (0...max)
  175. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  176. uint nextPluginId; // invalid if == maxPluginNumber
  177. CarlaMutex envMutex;
  178. CarlaString lastError;
  179. CarlaString name;
  180. EngineOptions options;
  181. EngineTimeInfo timeInfo;
  182. #ifdef BUILD_BRIDGE
  183. EnginePluginData plugins[1];
  184. #else
  185. EnginePluginData* plugins;
  186. #endif
  187. EngineInternalEvents events;
  188. #ifndef BUILD_BRIDGE
  189. EngineInternalGraph graph;
  190. #endif
  191. EngineInternalTime time;
  192. EngineNextAction nextAction;
  193. // -------------------------------------------------------------------
  194. ProtectedData(CarlaEngine* const engine) noexcept;
  195. ~ProtectedData() noexcept;
  196. // -------------------------------------------------------------------
  197. bool init(const char* const clientName);
  198. void close();
  199. void initTime(const char* const features);
  200. // -------------------------------------------------------------------
  201. void doPluginRemove() noexcept;
  202. void doPluginsSwitch() noexcept;
  203. void doNextPluginAction(const bool unlock) noexcept;
  204. // -------------------------------------------------------------------
  205. #ifdef CARLA_PROPER_CPP11_SUPPORT
  206. ProtectedData() = delete;
  207. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  208. #endif
  209. };
  210. // -----------------------------------------------------------------------
  211. class PendingRtEventsRunner
  212. {
  213. public:
  214. PendingRtEventsRunner(CarlaEngine* const engine, const uint32_t numFrames) noexcept;
  215. ~PendingRtEventsRunner() noexcept;
  216. private:
  217. CarlaEngine::ProtectedData* const pData;
  218. const uint32_t numFrames;
  219. CARLA_PREVENT_HEAP_ALLOCATION
  220. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  221. };
  222. // -----------------------------------------------------------------------
  223. class ScopedActionLock
  224. {
  225. public:
  226. ScopedActionLock(CarlaEngine* const engine, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  227. ~ScopedActionLock() noexcept;
  228. private:
  229. CarlaEngine::ProtectedData* const pData;
  230. CARLA_PREVENT_HEAP_ALLOCATION
  231. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  232. };
  233. // -----------------------------------------------------------------------
  234. class ScopedThreadStopper
  235. {
  236. public:
  237. ScopedThreadStopper(CarlaEngine* const engine) noexcept;
  238. ~ScopedThreadStopper() noexcept;
  239. private:
  240. CarlaEngine* const engine;
  241. CarlaEngine::ProtectedData* const pData;
  242. CARLA_PREVENT_HEAP_ALLOCATION
  243. CARLA_DECLARE_NON_COPY_CLASS(ScopedThreadStopper)
  244. };
  245. // -----------------------------------------------------------------------
  246. CARLA_BACKEND_END_NAMESPACE
  247. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED