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.6KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. //friend class ScopedActionLock;
  169. #ifdef CARLA_PROPER_CPP11_SUPPORT
  170. ProtectedData() = delete;
  171. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  172. #endif
  173. };
  174. // -----------------------------------------------------------------------
  175. class PendingRtEventsRunner
  176. {
  177. public:
  178. PendingRtEventsRunner(CarlaEngine* const engine) noexcept;
  179. ~PendingRtEventsRunner() noexcept;
  180. private:
  181. CarlaEngine::ProtectedData* const pData;
  182. CARLA_PREVENT_HEAP_ALLOCATION
  183. CARLA_DECLARE_NON_COPY_CLASS(PendingRtEventsRunner)
  184. };
  185. // -----------------------------------------------------------------------
  186. class ScopedActionLock
  187. {
  188. public:
  189. ScopedActionLock(CarlaEngine* const engine, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  190. ~ScopedActionLock() noexcept;
  191. private:
  192. CarlaEngine::ProtectedData* const pData;
  193. CARLA_PREVENT_HEAP_ALLOCATION
  194. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  195. };
  196. // -----------------------------------------------------------------------
  197. CARLA_BACKEND_END_NAMESPACE
  198. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED