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 8.1KB

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