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.

277 lines
7.6KB

  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 "CarlaEngine.hpp"
  20. #include "CarlaEngineOsc.hpp"
  21. #include "CarlaEngineThread.hpp"
  22. #include "CarlaMutex.hpp"
  23. #include "LinkedList.hpp"
  24. // -----------------------------------------------------------------------
  25. // Engine helper macro, sets lastError and returns false/NULL
  26. #define CARLA_SAFE_ASSERT_RETURN_ERR(cond, err) if (cond) pass(); else { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return false; }
  27. #define CARLA_SAFE_ASSERT_RETURN_ERRN(cond, err) if (cond) pass(); else { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return nullptr; }
  28. // -----------------------------------------------------------------------
  29. CARLA_BACKEND_START_NAMESPACE
  30. #if 0
  31. } // Fix editor indentation
  32. #endif
  33. // -----------------------------------------------------------------------
  34. // Maximum pre-allocated events for rack and bridge modes
  35. const unsigned short kMaxEngineEventInternalCount = 512;
  36. // -----------------------------------------------------------------------
  37. // AbstractEngineBuffer
  38. struct AbstractEngineBuffer {
  39. AbstractEngineBuffer(const uint32_t /*bufferSize*/) {}
  40. virtual ~AbstractEngineBuffer() noexcept {}
  41. virtual void clear() noexcept = 0;
  42. virtual void resize(const uint32_t bufferSize) = 0;
  43. virtual bool connect(CarlaEngine* const engine, const int groupA, const int portA, const int groupB, const int port) noexcept = 0;
  44. virtual bool disconnect(const uint connectionId) noexcept = 0;
  45. virtual const char* const* getConnections() const = 0;
  46. };
  47. // -----------------------------------------------------------------------
  48. // InternalAudio
  49. struct EngineInternalAudio {
  50. bool isPatchbay; // can only be patchbay or rack mode
  51. bool isReady;
  52. uint inCount;
  53. uint outCount;
  54. AbstractEngineBuffer* buffer;
  55. EngineInternalAudio() noexcept
  56. : isPatchbay(false),
  57. isReady(false),
  58. inCount(0),
  59. outCount(0),
  60. buffer(nullptr) {}
  61. ~EngineInternalAudio() noexcept
  62. {
  63. CARLA_SAFE_ASSERT(! isReady);
  64. CARLA_SAFE_ASSERT(buffer == nullptr);
  65. }
  66. void clear() noexcept
  67. {
  68. isReady = false;
  69. inCount = 0;
  70. outCount = 0;
  71. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  72. delete buffer;
  73. buffer = nullptr;
  74. }
  75. void create(const uint32_t bufferSize);
  76. void initPatchbay() noexcept;
  77. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalAudio)
  78. };
  79. // -----------------------------------------------------------------------
  80. // InternalEvents
  81. struct EngineInternalEvents {
  82. EngineEvent* in;
  83. EngineEvent* out;
  84. EngineInternalEvents() noexcept
  85. : in(nullptr),
  86. out(nullptr) {}
  87. ~EngineInternalEvents() noexcept
  88. {
  89. CARLA_SAFE_ASSERT(in == nullptr);
  90. CARLA_SAFE_ASSERT(out == nullptr);
  91. }
  92. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalEvents)
  93. };
  94. // -----------------------------------------------------------------------
  95. // InternalTime
  96. struct EngineInternalTime {
  97. bool playing;
  98. uint64_t frame;
  99. EngineInternalTime() noexcept
  100. : playing(false),
  101. frame(0) {}
  102. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  103. };
  104. // -----------------------------------------------------------------------
  105. // NextAction
  106. enum EnginePostAction {
  107. kEnginePostActionNull,
  108. kEnginePostActionZeroCount,
  109. kEnginePostActionRemovePlugin,
  110. kEnginePostActionSwitchPlugins
  111. };
  112. struct EngineNextAction {
  113. EnginePostAction opcode;
  114. unsigned int pluginId;
  115. unsigned int value;
  116. CarlaMutex mutex;
  117. EngineNextAction() noexcept
  118. : opcode(kEnginePostActionNull),
  119. pluginId(0),
  120. value(0) {}
  121. ~EngineNextAction() noexcept
  122. {
  123. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  124. }
  125. void ready() const noexcept
  126. {
  127. mutex.lock();
  128. mutex.unlock();
  129. }
  130. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  131. };
  132. // -----------------------------------------------------------------------
  133. // EnginePluginData
  134. struct EnginePluginData {
  135. CarlaPlugin* plugin;
  136. float insPeak[2];
  137. float outsPeak[2];
  138. void clear() noexcept
  139. {
  140. plugin = nullptr;
  141. insPeak[0] = insPeak[1] = 0.0f;
  142. outsPeak[0] = outsPeak[1] = 0.0f;
  143. }
  144. };
  145. // -----------------------------------------------------------------------
  146. // CarlaEngineProtectedData
  147. struct CarlaEngineProtectedData {
  148. CarlaEngineOsc osc;
  149. CarlaEngineThread thread;
  150. const CarlaOscData* oscData;
  151. EngineCallbackFunc callback;
  152. void* callbackPtr;
  153. FileCallbackFunc fileCallback;
  154. void* fileCallbackPtr;
  155. unsigned int hints;
  156. uint32_t bufferSize;
  157. double sampleRate;
  158. bool aboutToClose; // don't re-activate thread if true
  159. unsigned int curPluginCount; // number of plugins loaded (0...max)
  160. unsigned int maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  161. unsigned int nextPluginId; // invalid if == maxPluginNumber
  162. CarlaString lastError;
  163. CarlaString name;
  164. EngineOptions options;
  165. EngineTimeInfo timeInfo;
  166. EnginePluginData* plugins;
  167. #ifndef BUILD_BRIDGE
  168. EngineInternalAudio bufAudio;
  169. #endif
  170. EngineInternalEvents bufEvents;
  171. EngineInternalTime time;
  172. EngineNextAction nextAction;
  173. // -------------------------------------------------------------------
  174. CarlaEngineProtectedData(CarlaEngine* const engine);
  175. ~CarlaEngineProtectedData() noexcept;
  176. // -------------------------------------------------------------------
  177. void doPluginRemove() noexcept;
  178. void doPluginsSwitch() noexcept;
  179. void doNextPluginAction(const bool unlock) noexcept;
  180. // -------------------------------------------------------------------
  181. #ifndef BUILD_BRIDGE
  182. // the base, where plugins run
  183. void processRack(float* inBufReal[2], float* outBuf[2], const uint32_t nframes, const bool isOffline);
  184. // extended, will call processRack() in the middle
  185. void processRackFull(float** const inBuf, const uint32_t inCount, float** const outBuf, const uint32_t outCount, const uint32_t nframes, const bool isOffline);
  186. #endif
  187. // -------------------------------------------------------------------
  188. class ScopedActionLock
  189. {
  190. public:
  191. ScopedActionLock(CarlaEngineProtectedData* const data, const EnginePostAction action, const unsigned int pluginId, const unsigned int value, const bool lockWait) noexcept;
  192. ~ScopedActionLock() noexcept;
  193. private:
  194. CarlaEngineProtectedData* const fData;
  195. CARLA_PREVENT_HEAP_ALLOCATION
  196. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  197. };
  198. // -------------------------------------------------------------------
  199. #ifdef CARLA_PROPER_CPP11_SUPPORT
  200. CarlaEngineProtectedData() = delete;
  201. CARLA_DECLARE_NON_COPY_STRUCT(CarlaEngineProtectedData)
  202. #endif
  203. };
  204. // -----------------------------------------------------------------------
  205. CARLA_BACKEND_END_NAMESPACE
  206. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED