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.

297 lines
8.4KB

  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. // Float operations
  30. #ifdef HAVE_JUCE
  31. # define FLOAT_ADD(bufDst, bufSrc, frames) FloatVectorOperations::add(bufDst, bufSrc, frames)
  32. # define FLOAT_COPY(bufDst, bufSrc, frames) FloatVectorOperations::copy(bufDst, bufSrc, frames)
  33. # define FLOAT_CLEAR(buf, frames) FloatVectorOperations::clear(buf, frames)
  34. #else
  35. # define FLOAT_ADD(bufDst, bufSrc, frames) carla_addFloat(bufDst, bufSrc, frames)
  36. # define FLOAT_COPY(bufDst, bufSrc, frames) carla_copyFloat(bufDst, bufSrc, frames)
  37. # define FLOAT_CLEAR(buf, frames) carla_zeroFloat(buf, frames)
  38. #endif
  39. // -----------------------------------------------------------------------
  40. CARLA_BACKEND_START_NAMESPACE
  41. #if 0
  42. } // Fix editor indentation
  43. #endif
  44. // -----------------------------------------------------------------------
  45. // Maximum pre-allocated events for rack and bridge modes
  46. const unsigned short kMaxEngineEventInternalCount = 512;
  47. // -----------------------------------------------------------------------
  48. // Rack Patchbay stuff
  49. enum RackPatchbayGroupIds {
  50. RACK_PATCHBAY_GROUP_CARLA = -1,
  51. RACK_PATCHBAY_GROUP_AUDIO_IN = 0,
  52. RACK_PATCHBAY_GROUP_AUDIO_OUT = 1,
  53. RACK_PATCHBAY_GROUP_MIDI_IN = 2,
  54. RACK_PATCHBAY_GROUP_MIDI_OUT = 3,
  55. RACK_PATCHBAY_GROUP_MAX = 4
  56. };
  57. enum RackPatchbayPortIds {
  58. RACK_PATCHBAY_PORT_AUDIO_IN1 = -1,
  59. RACK_PATCHBAY_PORT_AUDIO_IN2 = -2,
  60. RACK_PATCHBAY_PORT_AUDIO_OUT1 = -3,
  61. RACK_PATCHBAY_PORT_AUDIO_OUT2 = -4,
  62. RACK_PATCHBAY_PORT_MIDI_IN = -5,
  63. RACK_PATCHBAY_PORT_MIDI_OUT = -6,
  64. RACK_PATCHBAY_PORT_MAX = -7
  65. };
  66. struct PortNameToId {
  67. int portId;
  68. char name[STR_MAX+1];
  69. };
  70. struct ConnectionToId {
  71. int id;
  72. int portOut;
  73. int portIn;
  74. };
  75. // -----------------------------------------------------------------------
  76. // EngineRackBuffers
  77. struct EngineRackBuffers {
  78. float* in[2];
  79. float* out[2];
  80. // connections stuff
  81. LinkedList<uint> connectedIns[2];
  82. LinkedList<uint> connectedOuts[2];
  83. CarlaMutex connectLock;
  84. int lastConnectionId;
  85. LinkedList<ConnectionToId> usedConnections;
  86. EngineRackBuffers(const uint32_t bufferSize);
  87. ~EngineRackBuffers();
  88. void clear();
  89. void resize(const uint32_t bufferSize);
  90. CARLA_DECLARE_NON_COPY_STRUCT(EngineRackBuffers)
  91. };
  92. // -----------------------------------------------------------------------
  93. // EnginePatchbayBuffers
  94. struct EnginePatchbayBuffers {
  95. // TODO
  96. EnginePatchbayBuffers(const uint32_t bufferSize);
  97. ~EnginePatchbayBuffers();
  98. void clear();
  99. void resize(const uint32_t bufferSize);
  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. unsigned int hints;
  173. uint32_t bufferSize;
  174. double sampleRate;
  175. bool aboutToClose; // don't re-activate thread if true
  176. unsigned int curPluginCount; // number of plugins loaded (0...max)
  177. unsigned int maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  178. unsigned int nextPluginId; // invalid if == maxPluginNumber
  179. CarlaString lastError;
  180. CarlaString name;
  181. EngineOptions options;
  182. EngineTimeInfo timeInfo;
  183. EnginePluginData* plugins;
  184. #ifndef BUILD_BRIDGE
  185. EngineInternalAudio bufAudio;
  186. #endif
  187. EngineInternalEvents bufEvents;
  188. EngineInternalTime time;
  189. EngineNextAction nextAction;
  190. // -------------------------------------------------------------------
  191. CarlaEngineProtectedData(CarlaEngine* const engine);
  192. ~CarlaEngineProtectedData() noexcept;
  193. // -------------------------------------------------------------------
  194. void doPluginRemove() noexcept;
  195. void doPluginsSwitch() noexcept;
  196. void doNextPluginAction(const bool unlock) noexcept;
  197. // -------------------------------------------------------------------
  198. #ifndef BUILD_BRIDGE
  199. // the base, where plugins run
  200. void processRack(float* inBufReal[2], float* outBuf[2], const uint32_t nframes, const bool isOffline);
  201. // extended, will call processRack() in the middle
  202. void processRackFull(float** const inBuf, const uint32_t inCount, float** const outBuf, const uint32_t outCount, const uint32_t nframes, const bool isOffline);
  203. #endif
  204. // -------------------------------------------------------------------
  205. class ScopedActionLock
  206. {
  207. public:
  208. ScopedActionLock(CarlaEngineProtectedData* const data, const EnginePostAction action, const unsigned int pluginId, const unsigned int value, const bool lockWait) noexcept;
  209. ~ScopedActionLock() noexcept;
  210. private:
  211. CarlaEngineProtectedData* const fData;
  212. CARLA_PREVENT_HEAP_ALLOCATION
  213. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  214. };
  215. // -------------------------------------------------------------------
  216. #ifdef CARLA_PROPER_CPP11_SUPPORT
  217. CarlaEngineProtectedData() = delete;
  218. CARLA_DECLARE_NON_COPY_STRUCT(CarlaEngineProtectedData)
  219. #endif
  220. };
  221. // -----------------------------------------------------------------------
  222. CARLA_BACKEND_END_NAMESPACE
  223. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED