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