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.

336 lines
9.4KB

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