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.

280 lines
7.9KB

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