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.

317 lines
9.2KB

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