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.

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