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.

233 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. #include "juce_core.h"
  23. using juce::Atomic;
  24. using juce::WaitableEvent;
  25. CARLA_BACKEND_START_NAMESPACE
  26. // -----------------------------------------------------------------------
  27. // Engine helper macro, sets lastError and returns false/NULL
  28. #define CARLA_SAFE_ASSERT_RETURN_ERR(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return false; }
  29. #define CARLA_SAFE_ASSERT_RETURN_ERRN(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return nullptr; }
  30. // -----------------------------------------------------------------------
  31. // InternalEvents
  32. struct EngineInternalEvents {
  33. EngineEvent* in;
  34. EngineEvent* out;
  35. EngineInternalEvents() noexcept;
  36. ~EngineInternalEvents() noexcept;
  37. void clear() noexcept;
  38. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalEvents)
  39. };
  40. #ifndef BUILD_BRIDGE
  41. // -----------------------------------------------------------------------
  42. // InternalGraph
  43. struct RackGraph;
  44. struct PatchbayGraph;
  45. class EngineInternalGraph
  46. {
  47. public:
  48. EngineInternalGraph() noexcept;
  49. ~EngineInternalGraph() noexcept;
  50. void create(const bool isRack, const double sampleRate, const uint32_t bufferSize, const uint32_t inputs, const uint32_t outputs);
  51. void destroy() noexcept;
  52. void setBufferSize(const uint32_t bufferSize);
  53. void setSampleRate(const double sampleRate);
  54. void setOffline(const bool offline);
  55. bool isReady() const noexcept;
  56. RackGraph* getRackGraph() const noexcept;
  57. PatchbayGraph* getPatchbayGraph() const noexcept;
  58. void process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames);
  59. // special direct process with connections already handled, used in JACK and Plugin
  60. void processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames);
  61. private:
  62. bool fIsRack;
  63. bool fIsReady;
  64. union {
  65. RackGraph* fRack;
  66. PatchbayGraph* fPatchbay;
  67. };
  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. Atomic<EnginePostAction> opcode;
  91. uint pluginId;
  92. uint value;
  93. WaitableEvent waitEvent;
  94. EngineNextAction() noexcept;
  95. ~EngineNextAction() noexcept;
  96. void ready() const noexcept;
  97. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  98. };
  99. // -----------------------------------------------------------------------
  100. // EnginePluginData
  101. struct EnginePluginData {
  102. CarlaPlugin* plugin;
  103. float insPeak[2];
  104. float outsPeak[2];
  105. };
  106. // -----------------------------------------------------------------------
  107. // CarlaEngineProtectedData
  108. struct CarlaEngine::ProtectedData {
  109. CarlaEngineOsc osc;
  110. CarlaEngineThread thread;
  111. #ifdef BUILD_BRIDGE
  112. CarlaOscData* oscData;
  113. #else
  114. const CarlaOscData* oscData;
  115. #endif
  116. EngineCallbackFunc callback;
  117. void* callbackPtr;
  118. FileCallbackFunc fileCallback;
  119. void* fileCallbackPtr;
  120. uint hints;
  121. uint32_t bufferSize;
  122. double sampleRate;
  123. bool aboutToClose; // don't re-activate thread if true
  124. int isIdling; // don't allow any operations while idling
  125. uint curPluginCount; // number of plugins loaded (0...max)
  126. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  127. uint nextPluginId; // invalid if == maxPluginNumber
  128. CarlaString lastError;
  129. CarlaString name;
  130. EngineOptions options;
  131. EngineTimeInfo timeInfo;
  132. #ifdef BUILD_BRIDGE
  133. EnginePluginData plugins[1];
  134. #else
  135. EnginePluginData* plugins;
  136. #endif
  137. EngineInternalEvents events;
  138. #ifndef BUILD_BRIDGE
  139. EngineInternalGraph graph;
  140. #endif
  141. EngineInternalTime time;
  142. EngineNextAction nextAction;
  143. // -------------------------------------------------------------------
  144. ProtectedData(CarlaEngine* const engine) noexcept;
  145. ~ProtectedData() noexcept;
  146. // -------------------------------------------------------------------
  147. bool init(const char* const clientName);
  148. void close();
  149. // -------------------------------------------------------------------
  150. void doPluginRemove() noexcept;
  151. void doPluginsSwitch() noexcept;
  152. void doNextPluginAction(const bool unlock) noexcept;
  153. // -------------------------------------------------------------------
  154. //friend class ScopedActionLock;
  155. #ifdef CARLA_PROPER_CPP11_SUPPORT
  156. ProtectedData() = delete;
  157. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  158. #endif
  159. };
  160. // -----------------------------------------------------------------------
  161. class ScopedActionLock
  162. {
  163. public:
  164. ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  165. ~ScopedActionLock() noexcept;
  166. private:
  167. CarlaEngine::ProtectedData* const fData;
  168. CARLA_PREVENT_HEAP_ALLOCATION
  169. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  170. };
  171. // -----------------------------------------------------------------------
  172. CARLA_BACKEND_END_NAMESPACE
  173. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED