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.

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