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.

311 lines
8.7KB

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