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.

265 lines
7.3KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 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. // FIXME only use CARLA_PREVENT_HEAP_ALLOCATION for structs
  23. // maybe separate macro
  24. CARLA_BACKEND_START_NAMESPACE
  25. // -----------------------------------------------------------------------
  26. // Engine helper macro, sets lastError and returns false/NULL
  27. #define CARLA_SAFE_ASSERT_RETURN_ERR(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return false; }
  28. #define CARLA_SAFE_ASSERT_RETURN_ERRN(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return nullptr; }
  29. // -----------------------------------------------------------------------
  30. // InternalEvents
  31. struct EngineInternalEvents {
  32. EngineEvent* in;
  33. EngineEvent* out;
  34. EngineInternalEvents() noexcept;
  35. ~EngineInternalEvents() noexcept;
  36. void clear() noexcept;
  37. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalEvents)
  38. };
  39. #ifndef BUILD_BRIDGE
  40. // -----------------------------------------------------------------------
  41. // InternalGraph
  42. struct RackGraph;
  43. struct PatchbayGraph;
  44. class EngineInternalGraph
  45. {
  46. public:
  47. EngineInternalGraph() noexcept;
  48. ~EngineInternalGraph() noexcept;
  49. void create(const bool isRack, const double sampleRate, const uint32_t bufferSize, const uint32_t inputs, const uint32_t outputs);
  50. void destroy() noexcept;
  51. void setBufferSize(const uint32_t bufferSize);
  52. void setSampleRate(const double sampleRate);
  53. void setOffline(const bool offline);
  54. bool isReady() const noexcept;
  55. RackGraph* getRackGraph() const noexcept;
  56. PatchbayGraph* getPatchbayGraph() const noexcept;
  57. void process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames);
  58. // special direct process with connections already handled, used in JACK and Plugin
  59. void processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames);
  60. // used for internal patchbay mode
  61. void addPlugin(CarlaPlugin* const plugin);
  62. void replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin);
  63. void removePlugin(CarlaPlugin* const plugin);
  64. void removeAllPlugins(CarlaEngine* const engine);
  65. void setIgnorePatchbay(const bool ignore) noexcept;
  66. private:
  67. bool fIsRack;
  68. bool fIsReady;
  69. union {
  70. RackGraph* fRack;
  71. PatchbayGraph* fPatchbay;
  72. };
  73. CARLA_PREVENT_HEAP_ALLOCATION
  74. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  75. };
  76. #endif
  77. // -----------------------------------------------------------------------
  78. // InternalTime
  79. struct EngineInternalTime {
  80. bool playing;
  81. uint64_t frame;
  82. EngineInternalTime() noexcept;
  83. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  84. };
  85. // -----------------------------------------------------------------------
  86. // EngineNextAction
  87. enum EnginePostAction {
  88. kEnginePostActionNull = 0,
  89. kEnginePostActionZeroCount, // set curPluginCount to 0
  90. #ifndef BUILD_BRIDGE
  91. kEnginePostActionRemovePlugin, // remove a plugin
  92. kEnginePostActionSwitchPlugins // switch between 2 plugins
  93. #endif
  94. };
  95. struct EngineNextAction {
  96. EnginePostAction opcode;
  97. uint pluginId;
  98. uint value;
  99. CarlaMutex mutex;
  100. EngineNextAction() noexcept;
  101. ~EngineNextAction() noexcept;
  102. void ready() const noexcept;
  103. void clearAndReset() noexcept;
  104. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  105. };
  106. // -----------------------------------------------------------------------
  107. // EnginePluginData
  108. struct EnginePluginData {
  109. CarlaPlugin* plugin;
  110. float insPeak[2];
  111. float outsPeak[2];
  112. };
  113. // -----------------------------------------------------------------------
  114. // CarlaEngineProtectedData
  115. struct CarlaEngine::ProtectedData {
  116. CarlaEngineThread thread;
  117. #ifdef HAVE_LIBLO
  118. CarlaEngineOsc osc;
  119. # ifdef BUILD_BRIDGE
  120. CarlaOscData* oscData;
  121. # else
  122. const CarlaOscData* oscData;
  123. # endif
  124. #endif
  125. EngineCallbackFunc callback;
  126. void* callbackPtr;
  127. FileCallbackFunc fileCallback;
  128. void* fileCallbackPtr;
  129. uint hints;
  130. uint32_t bufferSize;
  131. double sampleRate;
  132. bool aboutToClose; // don't re-activate thread if true
  133. int isIdling; // don't allow any operations while idling
  134. uint curPluginCount; // number of plugins loaded (0...max)
  135. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  136. uint nextPluginId; // invalid if == maxPluginNumber
  137. CarlaMutex envMutex;
  138. CarlaString lastError;
  139. CarlaString name;
  140. EngineOptions options;
  141. EngineTimeInfo timeInfo;
  142. #ifdef BUILD_BRIDGE
  143. EnginePluginData plugins[1];
  144. #else
  145. EnginePluginData* plugins;
  146. #endif
  147. EngineInternalEvents events;
  148. #ifndef BUILD_BRIDGE
  149. EngineInternalGraph graph;
  150. #endif
  151. EngineInternalTime time;
  152. EngineNextAction nextAction;
  153. // -------------------------------------------------------------------
  154. ProtectedData(CarlaEngine* const engine) noexcept;
  155. ~ProtectedData() noexcept;
  156. // -------------------------------------------------------------------
  157. bool init(const char* const clientName);
  158. void close();
  159. // -------------------------------------------------------------------
  160. void doPluginRemove() noexcept;
  161. void doPluginsSwitch() noexcept;
  162. void doNextPluginAction(const bool unlock) noexcept;
  163. // -------------------------------------------------------------------
  164. //friend class ScopedActionLock;
  165. #ifdef CARLA_PROPER_CPP11_SUPPORT
  166. ProtectedData() = delete;
  167. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  168. #endif
  169. };
  170. // -----------------------------------------------------------------------
  171. class PendingRtEventsRunner
  172. {
  173. public:
  174. PendingRtEventsRunner(CarlaEngine* const engine) noexcept
  175. : fEngine(engine) {}
  176. ~PendingRtEventsRunner() noexcept
  177. {
  178. fEngine->runPendingRtEvents();
  179. }
  180. private:
  181. CarlaEngine* const fEngine;
  182. CARLA_PREVENT_HEAP_ALLOCATION
  183. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  184. };
  185. // -----------------------------------------------------------------------
  186. class ScopedActionLock
  187. {
  188. public:
  189. ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  190. ~ScopedActionLock() noexcept;
  191. private:
  192. CarlaEngine::ProtectedData* const fData;
  193. CARLA_PREVENT_HEAP_ALLOCATION
  194. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  195. };
  196. // -----------------------------------------------------------------------
  197. CARLA_BACKEND_END_NAMESPACE
  198. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED