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.5KB

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