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.

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