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.

229 lines
6.4KB

  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. const CarlaOscData* oscData;
  112. EngineCallbackFunc callback;
  113. void* callbackPtr;
  114. FileCallbackFunc fileCallback;
  115. void* fileCallbackPtr;
  116. uint hints;
  117. uint32_t bufferSize;
  118. double sampleRate;
  119. bool aboutToClose; // don't re-activate thread if true
  120. bool isIdling; // don't allow any operations while idling
  121. uint curPluginCount; // number of plugins loaded (0...max)
  122. uint maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  123. uint nextPluginId; // invalid if == maxPluginNumber
  124. CarlaString lastError;
  125. CarlaString name;
  126. EngineOptions options;
  127. EngineTimeInfo timeInfo;
  128. #ifdef BUILD_BRIDGE
  129. EnginePluginData plugins[1];
  130. #else
  131. EnginePluginData* plugins;
  132. #endif
  133. EngineInternalEvents events;
  134. #ifndef BUILD_BRIDGE
  135. EngineInternalGraph graph;
  136. #endif
  137. EngineInternalTime time;
  138. EngineNextAction nextAction;
  139. // -------------------------------------------------------------------
  140. ProtectedData(CarlaEngine* const engine) noexcept;
  141. ~ProtectedData() noexcept;
  142. // -------------------------------------------------------------------
  143. bool init(const char* const clientName);
  144. void close();
  145. // -------------------------------------------------------------------
  146. void doPluginRemove() noexcept;
  147. void doPluginsSwitch() noexcept;
  148. void doNextPluginAction(const bool unlock) noexcept;
  149. // -------------------------------------------------------------------
  150. //friend class ScopedActionLock;
  151. #ifdef CARLA_PROPER_CPP11_SUPPORT
  152. ProtectedData() = delete;
  153. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData)
  154. #endif
  155. };
  156. // -----------------------------------------------------------------------
  157. class ScopedActionLock
  158. {
  159. public:
  160. ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept;
  161. ~ScopedActionLock() noexcept;
  162. private:
  163. CarlaEngine::ProtectedData* const fData;
  164. CARLA_PREVENT_HEAP_ALLOCATION
  165. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  166. };
  167. // -----------------------------------------------------------------------
  168. CARLA_BACKEND_END_NAMESPACE
  169. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED