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.

335 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. 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. class 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. CARLA_DECLARE_NON_COPY_STRUCT(Hylia)
  111. } hylia;
  112. #endif
  113. EngineTimeInfo& timeInfo;
  114. const EngineTransportMode& transportMode;
  115. friend class PendingRtEventsRunner;
  116. void preProcess(const uint32_t numFrames);
  117. void fillEngineTimeInfo(const uint32_t newFrames) noexcept;
  118. friend class CarlaEngineJack;
  119. void fillJackTimeInfo(jack_position_t* const pos, const uint32_t newFrames) noexcept;
  120. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  121. };
  122. // -----------------------------------------------------------------------
  123. // EngineNextAction
  124. enum EnginePostAction {
  125. kEnginePostActionNull = 0,
  126. kEnginePostActionZeroCount, // set curPluginCount to 0
  127. #ifndef BUILD_BRIDGE
  128. kEnginePostActionRemovePlugin, // remove a plugin
  129. kEnginePostActionSwitchPlugins // switch between 2 plugins
  130. #endif
  131. };
  132. struct EngineNextAction {
  133. EnginePostAction opcode;
  134. uint pluginId;
  135. uint value;
  136. // reused from CarlaSignal class
  137. pthread_cond_t condition;
  138. pthread_mutex_t mutex;
  139. volatile bool triggered;
  140. EngineNextAction() noexcept;
  141. ~EngineNextAction() noexcept;
  142. // void ready() const 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() noexcept;
  207. void doPluginsSwitch() noexcept;
  208. void doNextPluginAction(const bool unlock) 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