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.

313 lines
8.7KB

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