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.

284 lines
7.8KB

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