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.

296 lines
8.3KB

  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. // Global action lock for UI operations, used for osc only
  30. #define CARLA_ENGINE_THREAD_SAFE_SECTION const CarlaCriticalSection::Scope _ccsl(pData->_cs);
  31. // -----------------------------------------------------------------------
  32. CARLA_BACKEND_START_NAMESPACE
  33. #if 0
  34. } // Fix editor indentation
  35. #endif
  36. // -----------------------------------------------------------------------
  37. // Maximum pre-allocated events for rack and bridge modes
  38. const unsigned short kMaxEngineEventInternalCount = 512;
  39. // -----------------------------------------------------------------------
  40. // Rack Patchbay stuff
  41. enum RackPatchbayGroupIds {
  42. RACK_PATCHBAY_GROUP_CARLA = 0,
  43. RACK_PATCHBAY_GROUP_AUDIO_IN = 1,
  44. RACK_PATCHBAY_GROUP_AUDIO_OUT = 2,
  45. RACK_PATCHBAY_GROUP_MIDI_IN = 3,
  46. RACK_PATCHBAY_GROUP_MIDI_OUT = 4,
  47. RACK_PATCHBAY_GROUP_MAX = 5
  48. };
  49. enum RackPatchbayPortIds {
  50. RACK_PATCHBAY_PORT_AUDIO_IN1 = -1,
  51. RACK_PATCHBAY_PORT_AUDIO_IN2 = -2,
  52. RACK_PATCHBAY_PORT_AUDIO_OUT1 = -3,
  53. RACK_PATCHBAY_PORT_AUDIO_OUT2 = -4,
  54. RACK_PATCHBAY_PORT_MIDI_IN = -5,
  55. RACK_PATCHBAY_PORT_MIDI_OUT = -6,
  56. RACK_PATCHBAY_PORT_MAX = -7
  57. };
  58. struct PortNameToId {
  59. int portId;
  60. char name[STR_MAX+1];
  61. };
  62. struct ConnectionToId {
  63. uint id;
  64. int portOut;
  65. int portIn;
  66. };
  67. // -----------------------------------------------------------------------
  68. // EngineRackBuffers
  69. struct EngineRackBuffers {
  70. float* in[2];
  71. float* out[2];
  72. // connections stuff
  73. LinkedList<int> connectedIns[2];
  74. LinkedList<int> connectedOuts[2];
  75. CarlaMutex connectLock;
  76. uint lastConnectionId;
  77. LinkedList<ConnectionToId> usedConnections;
  78. EngineRackBuffers(const uint32_t bufferSize);
  79. ~EngineRackBuffers();
  80. void clear();
  81. void resize(const uint32_t bufferSize);
  82. const char* const* getConnections() const;
  83. CARLA_DECLARE_NON_COPY_STRUCT(EngineRackBuffers)
  84. };
  85. // -----------------------------------------------------------------------
  86. // EnginePatchbayBuffers
  87. struct EnginePatchbayBuffers {
  88. // TODO
  89. EnginePatchbayBuffers(const uint32_t bufferSize);
  90. ~EnginePatchbayBuffers();
  91. void clear();
  92. void resize(const uint32_t bufferSize);
  93. const char* const* getConnections() const;
  94. CARLA_DECLARE_NON_COPY_STRUCT(EnginePatchbayBuffers)
  95. };
  96. // -----------------------------------------------------------------------
  97. // InternalAudio
  98. struct EngineInternalAudio {
  99. bool isReady;
  100. bool usePatchbay;
  101. uint inCount;
  102. uint outCount;
  103. union {
  104. EngineRackBuffers* rack;
  105. EnginePatchbayBuffers* patchbay;
  106. };
  107. EngineInternalAudio() noexcept;
  108. ~EngineInternalAudio() noexcept;
  109. void initPatchbay() noexcept;
  110. void clear();
  111. void create(const uint32_t bufferSize);
  112. void resize(const uint32_t bufferSize);
  113. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalAudio)
  114. };
  115. // -----------------------------------------------------------------------
  116. // InternalEvents
  117. struct EngineInternalEvents {
  118. EngineEvent* in;
  119. EngineEvent* out;
  120. EngineInternalEvents() noexcept;
  121. ~EngineInternalEvents() noexcept;
  122. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalEvents)
  123. };
  124. // -----------------------------------------------------------------------
  125. // InternalTime
  126. struct EngineInternalTime {
  127. bool playing;
  128. uint64_t frame;
  129. EngineInternalTime() noexcept;
  130. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  131. };
  132. // -----------------------------------------------------------------------
  133. // NextAction
  134. enum EnginePostAction {
  135. kEnginePostActionNull,
  136. kEnginePostActionZeroCount,
  137. kEnginePostActionRemovePlugin,
  138. kEnginePostActionSwitchPlugins
  139. };
  140. struct EngineNextAction {
  141. EnginePostAction opcode;
  142. unsigned int pluginId;
  143. unsigned int value;
  144. CarlaMutex mutex;
  145. EngineNextAction() noexcept;
  146. ~EngineNextAction() noexcept;
  147. void ready() noexcept;
  148. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  149. };
  150. // -----------------------------------------------------------------------
  151. // EnginePluginData
  152. struct EnginePluginData {
  153. CarlaPlugin* plugin;
  154. float insPeak[2];
  155. float outsPeak[2];
  156. void clear() noexcept;
  157. };
  158. // -----------------------------------------------------------------------
  159. // CarlaEngineProtectedData
  160. struct CarlaEngineProtectedData {
  161. CarlaEngineOsc osc;
  162. CarlaEngineThread thread;
  163. const CarlaOscData* oscData;
  164. EngineCallbackFunc callback;
  165. void* callbackPtr;
  166. FileCallbackFunc fileCallback;
  167. void* fileCallbackPtr;
  168. unsigned int hints;
  169. uint32_t bufferSize;
  170. double sampleRate;
  171. bool aboutToClose; // don't re-activate thread if true
  172. unsigned int curPluginCount; // number of plugins loaded (0...max)
  173. unsigned int maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  174. unsigned int nextPluginId; // invalid if == maxPluginNumber
  175. CarlaString lastError;
  176. CarlaString name;
  177. EngineOptions options;
  178. EngineTimeInfo timeInfo;
  179. EnginePluginData* plugins;
  180. #ifndef BUILD_BRIDGE
  181. EngineInternalAudio bufAudio;
  182. #endif
  183. EngineInternalEvents bufEvents;
  184. EngineInternalTime time;
  185. EngineNextAction nextAction;
  186. CarlaCriticalSection _cs; // for handling requests from multiple threads
  187. // -------------------------------------------------------------------
  188. CarlaEngineProtectedData(CarlaEngine* const engine);
  189. ~CarlaEngineProtectedData() noexcept;
  190. // -------------------------------------------------------------------
  191. void doPluginRemove() noexcept;
  192. void doPluginsSwitch() noexcept;
  193. void doNextPluginAction(const bool unlock) noexcept;
  194. // -------------------------------------------------------------------
  195. #ifndef BUILD_BRIDGE
  196. // the base, where plugins run
  197. void processRack(float* inBufReal[2], float* outBuf[2], const uint32_t nframes, const bool isOffline);
  198. // extended, will call processRack() in the middle
  199. void processRackFull(float** const inBuf, const uint32_t inCount, float** const outBuf, const uint32_t outCount, const uint32_t nframes, const bool isOffline);
  200. #endif
  201. // -------------------------------------------------------------------
  202. class ScopedActionLock
  203. {
  204. public:
  205. ScopedActionLock(CarlaEngineProtectedData* const data, const EnginePostAction action, const unsigned int pluginId, const unsigned int value, const bool lockWait) noexcept;
  206. ~ScopedActionLock() noexcept;
  207. private:
  208. CarlaEngineProtectedData* const fData;
  209. CARLA_PREVENT_HEAP_ALLOCATION
  210. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  211. };
  212. // -------------------------------------------------------------------
  213. #ifdef CARLA_PROPER_CPP11_SUPPORT
  214. CarlaEngineProtectedData() = delete;
  215. CARLA_DECLARE_NON_COPY_STRUCT(CarlaEngineProtectedData)
  216. #endif
  217. };
  218. // -----------------------------------------------------------------------
  219. CARLA_BACKEND_END_NAMESPACE
  220. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED