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.

337 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. // special hack for linuxsampler
  172. bool firstLinuxSamplerInstance;
  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