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.

260 lines
7.2KB

  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. private:
  66. bool fIsRack;
  67. bool fIsReady;
  68. union {
  69. RackGraph* fRack;
  70. PatchbayGraph* fPatchbay;
  71. };
  72. CARLA_PREVENT_HEAP_ALLOCATION
  73. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  74. };
  75. #endif
  76. // -----------------------------------------------------------------------
  77. // InternalTime
  78. struct EngineInternalTime {
  79. bool playing;
  80. uint64_t frame;
  81. EngineInternalTime() noexcept;
  82. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  83. };
  84. // -----------------------------------------------------------------------
  85. // EngineNextAction
  86. enum EnginePostAction {
  87. kEnginePostActionNull = 0,
  88. kEnginePostActionZeroCount, // set curPluginCount to 0
  89. #ifndef BUILD_BRIDGE
  90. kEnginePostActionRemovePlugin, // remove a plugin
  91. kEnginePostActionSwitchPlugins // switch between 2 plugins
  92. #endif
  93. };
  94. struct EngineNextAction {
  95. EnginePostAction opcode;
  96. uint pluginId;
  97. uint value;
  98. CarlaMutex mutex;
  99. EngineNextAction() noexcept;
  100. ~EngineNextAction() noexcept;
  101. void ready() const noexcept;
  102. void clearAndReset() noexcept;
  103. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  104. };
  105. // -----------------------------------------------------------------------
  106. // EnginePluginData
  107. struct EnginePluginData {
  108. CarlaPlugin* plugin;
  109. float insPeak[2];
  110. float outsPeak[2];
  111. };
  112. // -----------------------------------------------------------------------
  113. // CarlaEngineProtectedData
  114. struct CarlaEngine::ProtectedData {
  115. CarlaEngineOsc osc;
  116. CarlaEngineThread thread;
  117. #ifdef BUILD_BRIDGE
  118. CarlaOscData* oscData;
  119. #else
  120. const CarlaOscData* oscData;
  121. #endif
  122. EngineCallbackFunc callback;
  123. void* callbackPtr;
  124. FileCallbackFunc fileCallback;
  125. void* fileCallbackPtr;
  126. uint hints;
  127. uint32_t bufferSize;
  128. double sampleRate;
  129. bool aboutToClose; // don't re-activate thread if true
  130. int isIdling; // don't allow any operations while idling
  131. uint curPluginCount; // number of plugins loaded (0...max)
  132. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  133. uint nextPluginId; // invalid if == maxPluginNumber
  134. CarlaString lastError;
  135. CarlaString name;
  136. EngineOptions options;
  137. EngineTimeInfo timeInfo;
  138. #ifdef BUILD_BRIDGE
  139. EnginePluginData plugins[1];
  140. #else
  141. EnginePluginData* plugins;
  142. #endif
  143. EngineInternalEvents events;
  144. #ifndef BUILD_BRIDGE
  145. EngineInternalGraph graph;
  146. #endif
  147. EngineInternalTime time;
  148. EngineNextAction nextAction;
  149. // -------------------------------------------------------------------
  150. ProtectedData(CarlaEngine* const engine) noexcept;
  151. ~ProtectedData() noexcept;
  152. // -------------------------------------------------------------------
  153. bool init(const char* const clientName);
  154. void close();
  155. // -------------------------------------------------------------------
  156. void doPluginRemove() noexcept;
  157. void doPluginsSwitch() noexcept;
  158. void doNextPluginAction(const bool unlock) noexcept;
  159. // -------------------------------------------------------------------
  160. //friend class ScopedActionLock;
  161. #ifdef CARLA_PROPER_CPP11_SUPPORT
  162. ProtectedData() = delete;
  163. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  164. #endif
  165. };
  166. // -----------------------------------------------------------------------
  167. class PendingRtEventsRunner
  168. {
  169. public:
  170. PendingRtEventsRunner(CarlaEngine* const engine) noexcept
  171. : fEngine(engine) {}
  172. ~PendingRtEventsRunner() noexcept
  173. {
  174. fEngine->runPendingRtEvents();
  175. }
  176. private:
  177. CarlaEngine* const fEngine;
  178. CARLA_PREVENT_HEAP_ALLOCATION
  179. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  180. };
  181. // -----------------------------------------------------------------------
  182. class ScopedActionLock
  183. {
  184. public:
  185. ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  186. ~ScopedActionLock() noexcept;
  187. private:
  188. CarlaEngine::ProtectedData* const fData;
  189. CARLA_PREVENT_HEAP_ALLOCATION
  190. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  191. };
  192. // -----------------------------------------------------------------------
  193. CARLA_BACKEND_END_NAMESPACE
  194. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED