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.

234 lines
6.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. // 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. // -----------------------------------------------------------------------
  30. // InternalEvents
  31. struct EngineInternalEvents {
  32. EngineEvent* in;
  33. EngineEvent* out;
  34. EngineInternalEvents() noexcept;
  35. ~EngineInternalEvents() noexcept;
  36. void clear() noexcept;
  37. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalEvents)
  38. };
  39. #ifndef BUILD_BRIDGE
  40. // -----------------------------------------------------------------------
  41. // InternalGraph
  42. struct RackGraph;
  43. struct PatchbayGraph;
  44. class EngineInternalGraph
  45. {
  46. public:
  47. EngineInternalGraph() noexcept;
  48. ~EngineInternalGraph() noexcept;
  49. void create(const bool isRack, const double sampleRate, const uint32_t bufferSize, const uint32_t inputs, const uint32_t outputs);
  50. void destroy() noexcept;
  51. void setBufferSize(const uint32_t bufferSize);
  52. void setSampleRate(const double sampleRate);
  53. void setOffline(const bool offline);
  54. bool isReady() const noexcept;
  55. RackGraph* getRackGraph() const noexcept;
  56. PatchbayGraph* getPatchbayGraph() const noexcept;
  57. void process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames);
  58. // special direct process with connections already handled, used in JACK and Plugin
  59. void processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames);
  60. private:
  61. bool fIsRack;
  62. bool fIsReady;
  63. union {
  64. RackGraph* fRack;
  65. PatchbayGraph* fPatchbay;
  66. };
  67. CARLA_PREVENT_HEAP_ALLOCATION
  68. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalGraph)
  69. };
  70. #endif
  71. // -----------------------------------------------------------------------
  72. // InternalTime
  73. struct EngineInternalTime {
  74. bool playing;
  75. uint64_t frame;
  76. EngineInternalTime() noexcept;
  77. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  78. };
  79. // -----------------------------------------------------------------------
  80. // EngineNextAction
  81. enum EnginePostAction {
  82. kEnginePostActionNull = 0,
  83. kEnginePostActionZeroCount, // set curPluginCount to 0
  84. #ifndef BUILD_BRIDGE
  85. kEnginePostActionRemovePlugin, // remove a plugin
  86. kEnginePostActionSwitchPlugins // switch between 2 plugins
  87. #endif
  88. };
  89. struct EngineNextAction {
  90. EnginePostAction opcode;
  91. uint pluginId;
  92. uint value;
  93. CarlaMutex mutex;
  94. EngineNextAction() noexcept;
  95. ~EngineNextAction() noexcept;
  96. void ready() const noexcept;
  97. void clearAndReset() noexcept;
  98. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  99. };
  100. // -----------------------------------------------------------------------
  101. // EnginePluginData
  102. struct EnginePluginData {
  103. CarlaPlugin* plugin;
  104. float insPeak[2];
  105. float outsPeak[2];
  106. };
  107. // -----------------------------------------------------------------------
  108. // CarlaEngineProtectedData
  109. struct CarlaEngine::ProtectedData {
  110. CarlaEngineOsc osc;
  111. CarlaEngineThread thread;
  112. #ifdef BUILD_BRIDGE
  113. CarlaOscData* oscData;
  114. #else
  115. const CarlaOscData* oscData;
  116. #endif
  117. EngineCallbackFunc callback;
  118. void* callbackPtr;
  119. FileCallbackFunc fileCallback;
  120. void* fileCallbackPtr;
  121. uint hints;
  122. uint32_t bufferSize;
  123. double sampleRate;
  124. bool aboutToClose; // don't re-activate thread if true
  125. int isIdling; // don't allow any operations while idling
  126. uint curPluginCount; // number of plugins loaded (0...max)
  127. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  128. uint nextPluginId; // invalid if == maxPluginNumber
  129. CarlaString lastError;
  130. CarlaString name;
  131. EngineOptions options;
  132. EngineTimeInfo timeInfo;
  133. #ifdef BUILD_BRIDGE
  134. EnginePluginData plugins[1];
  135. #else
  136. EnginePluginData* plugins;
  137. #endif
  138. EngineInternalEvents events;
  139. #ifndef BUILD_BRIDGE
  140. EngineInternalGraph graph;
  141. #endif
  142. EngineInternalTime time;
  143. EngineNextAction nextAction;
  144. // -------------------------------------------------------------------
  145. ProtectedData(CarlaEngine* const engine) noexcept;
  146. ~ProtectedData() noexcept;
  147. // -------------------------------------------------------------------
  148. bool init(const char* const clientName);
  149. void close();
  150. // -------------------------------------------------------------------
  151. void doPluginRemove() noexcept;
  152. void doPluginsSwitch() noexcept;
  153. void doNextPluginAction(const bool unlock) noexcept;
  154. // -------------------------------------------------------------------
  155. //friend class ScopedActionLock;
  156. #ifdef CARLA_PROPER_CPP11_SUPPORT
  157. ProtectedData() = delete;
  158. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  159. #endif
  160. };
  161. // -----------------------------------------------------------------------
  162. class ScopedActionLock
  163. {
  164. public:
  165. ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  166. ~ScopedActionLock() noexcept;
  167. private:
  168. CarlaEngine::ProtectedData* const fData;
  169. CARLA_PREVENT_HEAP_ALLOCATION
  170. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  171. };
  172. // -----------------------------------------------------------------------
  173. CARLA_BACKEND_END_NAMESPACE
  174. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED