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.

291 lines
8.1KB

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