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.

CarlaEngineInternal.hpp 7.7KB

11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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() noexcept;
  50. ~EngineInternalGraph() noexcept;
  51. void create(const bool isRack, const double sampleRate, const uint32_t bufferSize, 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(CarlaEngine* const engine);
  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. CARLA_PREVENT_HEAP_ALLOCATION
  76. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  77. };
  78. #endif
  79. // -----------------------------------------------------------------------
  80. // InternalTime
  81. struct EngineInternalTime {
  82. bool playing;
  83. uint64_t frame;
  84. EngineInternalTime() noexcept;
  85. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  86. };
  87. // -----------------------------------------------------------------------
  88. // EngineNextAction
  89. enum EnginePostAction {
  90. kEnginePostActionNull = 0,
  91. kEnginePostActionZeroCount, // set curPluginCount to 0
  92. #ifndef BUILD_BRIDGE
  93. kEnginePostActionRemovePlugin, // remove a plugin
  94. kEnginePostActionSwitchPlugins // switch between 2 plugins
  95. #endif
  96. };
  97. struct EngineNextAction {
  98. EnginePostAction opcode;
  99. uint pluginId;
  100. uint value;
  101. CarlaMutex mutex;
  102. EngineNextAction() noexcept;
  103. ~EngineNextAction() noexcept;
  104. void ready() const noexcept;
  105. void clearAndReset() noexcept;
  106. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  107. };
  108. // -----------------------------------------------------------------------
  109. // EnginePluginData
  110. struct EnginePluginData {
  111. CarlaPlugin* plugin;
  112. float insPeak[2];
  113. float outsPeak[2];
  114. };
  115. // -----------------------------------------------------------------------
  116. // CarlaEngineProtectedData
  117. struct CarlaEngine::ProtectedData {
  118. CarlaEngineThread thread;
  119. #ifdef HAVE_LIBLO
  120. CarlaEngineOsc osc;
  121. # ifdef BUILD_BRIDGE
  122. CarlaOscData* oscData;
  123. # else
  124. const CarlaOscData* oscData;
  125. # endif
  126. #endif
  127. EngineCallbackFunc callback;
  128. void* callbackPtr;
  129. FileCallbackFunc fileCallback;
  130. void* fileCallbackPtr;
  131. uint hints;
  132. uint32_t bufferSize;
  133. double sampleRate;
  134. bool aboutToClose; // don't re-activate thread if true
  135. int isIdling; // don't allow any operations while idling
  136. uint curPluginCount; // number of plugins loaded (0...max)
  137. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  138. uint nextPluginId; // invalid if == maxPluginNumber
  139. CarlaMutex envMutex;
  140. CarlaString lastError;
  141. CarlaString name;
  142. EngineOptions options;
  143. EngineTimeInfo timeInfo;
  144. #ifdef BUILD_BRIDGE
  145. EnginePluginData plugins[1];
  146. #else
  147. EnginePluginData* plugins;
  148. #endif
  149. EngineInternalEvents events;
  150. #ifndef BUILD_BRIDGE
  151. EngineInternalGraph graph;
  152. #endif
  153. EngineInternalTime time;
  154. EngineNextAction nextAction;
  155. // -------------------------------------------------------------------
  156. ProtectedData(CarlaEngine* const engine) noexcept;
  157. ~ProtectedData() noexcept;
  158. // -------------------------------------------------------------------
  159. bool init(const char* const clientName);
  160. void close();
  161. // -------------------------------------------------------------------
  162. void doPluginRemove() noexcept;
  163. void doPluginsSwitch() noexcept;
  164. void doNextPluginAction(const bool unlock) noexcept;
  165. // -------------------------------------------------------------------
  166. //friend class ScopedActionLock;
  167. #ifdef CARLA_PROPER_CPP11_SUPPORT
  168. ProtectedData() = delete;
  169. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  170. #endif
  171. };
  172. // -----------------------------------------------------------------------
  173. class PendingRtEventsRunner
  174. {
  175. public:
  176. PendingRtEventsRunner(CarlaEngine* const engine) noexcept
  177. : fEngine(engine) {}
  178. ~PendingRtEventsRunner() noexcept
  179. {
  180. fEngine->runPendingRtEvents();
  181. }
  182. private:
  183. CarlaEngine* const fEngine;
  184. CARLA_PREVENT_HEAP_ALLOCATION
  185. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  186. };
  187. // -----------------------------------------------------------------------
  188. class ScopedActionLock
  189. {
  190. public:
  191. ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  192. ~ScopedActionLock() noexcept;
  193. private:
  194. CarlaEngine::ProtectedData* const fData;
  195. CARLA_PREVENT_HEAP_ALLOCATION
  196. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  197. };
  198. // -----------------------------------------------------------------------
  199. CARLA_BACKEND_END_NAMESPACE
  200. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED