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.

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