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.

CarlaEngineInternal.hpp 8.4KB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. // Global action lock for UI operations, used for osc only
  30. #ifndef BUILD_BRIDGE
  31. # define CARLA_ENGINE_THREAD_SAFE_SECTION const CarlaCriticalSection::Scope _ccsl(pData->_cs);
  32. #else
  33. # define CARLA_ENGINE_THREAD_SAFE_SECTION
  34. #endif
  35. // -----------------------------------------------------------------------
  36. CARLA_BACKEND_START_NAMESPACE
  37. #if 0
  38. } // Fix editor indentation
  39. #endif
  40. // -----------------------------------------------------------------------
  41. // Maximum pre-allocated events for rack and bridge modes
  42. const unsigned short kMaxEngineEventInternalCount = 512;
  43. // -----------------------------------------------------------------------
  44. // Rack Patchbay stuff
  45. enum RackPatchbayGroupIds {
  46. RACK_PATCHBAY_GROUP_CARLA = 0,
  47. RACK_PATCHBAY_GROUP_AUDIO_IN = 1,
  48. RACK_PATCHBAY_GROUP_AUDIO_OUT = 2,
  49. RACK_PATCHBAY_GROUP_MIDI_IN = 3,
  50. RACK_PATCHBAY_GROUP_MIDI_OUT = 4,
  51. RACK_PATCHBAY_GROUP_MAX = 5
  52. };
  53. enum RackPatchbayPortIds {
  54. RACK_PATCHBAY_PORT_AUDIO_IN1 = -1,
  55. RACK_PATCHBAY_PORT_AUDIO_IN2 = -2,
  56. RACK_PATCHBAY_PORT_AUDIO_OUT1 = -3,
  57. RACK_PATCHBAY_PORT_AUDIO_OUT2 = -4,
  58. RACK_PATCHBAY_PORT_MIDI_IN = -5,
  59. RACK_PATCHBAY_PORT_MIDI_OUT = -6,
  60. RACK_PATCHBAY_PORT_MAX = -7
  61. };
  62. struct PortNameToId {
  63. int portId;
  64. char name[STR_MAX+1];
  65. };
  66. struct ConnectionToId {
  67. uint id;
  68. int portOut;
  69. int portIn;
  70. };
  71. // -----------------------------------------------------------------------
  72. // EngineRackBuffers
  73. struct EngineRackBuffers {
  74. float* in[2];
  75. float* out[2];
  76. // connections stuff
  77. LinkedList<int> connectedIns[2];
  78. LinkedList<int> connectedOuts[2];
  79. CarlaMutex connectLock;
  80. uint lastConnectionId;
  81. LinkedList<ConnectionToId> usedConnections;
  82. EngineRackBuffers(const uint32_t bufferSize);
  83. ~EngineRackBuffers();
  84. void clear();
  85. void resize(const uint32_t bufferSize);
  86. const char* const* getConnections() const;
  87. CARLA_DECLARE_NON_COPY_STRUCT(EngineRackBuffers)
  88. };
  89. // -----------------------------------------------------------------------
  90. // EnginePatchbayBuffers
  91. struct EnginePatchbayBuffers {
  92. // TODO
  93. EnginePatchbayBuffers(const uint32_t bufferSize);
  94. ~EnginePatchbayBuffers();
  95. void clear();
  96. void resize(const uint32_t bufferSize);
  97. const char* const* getConnections() const;
  98. CARLA_DECLARE_NON_COPY_STRUCT(EnginePatchbayBuffers)
  99. };
  100. // -----------------------------------------------------------------------
  101. // InternalAudio
  102. struct EngineInternalAudio {
  103. bool isReady;
  104. bool usePatchbay;
  105. uint inCount;
  106. uint outCount;
  107. union {
  108. EngineRackBuffers* rack;
  109. EnginePatchbayBuffers* patchbay;
  110. };
  111. EngineInternalAudio() noexcept;
  112. ~EngineInternalAudio() noexcept;
  113. void initPatchbay() noexcept;
  114. void clear();
  115. void create(const uint32_t bufferSize);
  116. void resize(const uint32_t bufferSize);
  117. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalAudio)
  118. };
  119. // -----------------------------------------------------------------------
  120. // InternalEvents
  121. struct EngineInternalEvents {
  122. EngineEvent* in;
  123. EngineEvent* out;
  124. EngineInternalEvents() noexcept;
  125. ~EngineInternalEvents() noexcept;
  126. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalEvents)
  127. };
  128. // -----------------------------------------------------------------------
  129. // InternalTime
  130. struct EngineInternalTime {
  131. bool playing;
  132. uint64_t frame;
  133. EngineInternalTime() noexcept;
  134. CARLA_DECLARE_NON_COPY_STRUCT(EngineInternalTime)
  135. };
  136. // -----------------------------------------------------------------------
  137. // NextAction
  138. enum EnginePostAction {
  139. kEnginePostActionNull,
  140. kEnginePostActionZeroCount,
  141. kEnginePostActionRemovePlugin,
  142. kEnginePostActionSwitchPlugins
  143. };
  144. struct EngineNextAction {
  145. EnginePostAction opcode;
  146. unsigned int pluginId;
  147. unsigned int value;
  148. CarlaMutex mutex;
  149. EngineNextAction() noexcept;
  150. ~EngineNextAction() noexcept;
  151. void ready() noexcept;
  152. CARLA_DECLARE_NON_COPY_STRUCT(EngineNextAction)
  153. };
  154. // -----------------------------------------------------------------------
  155. // EnginePluginData
  156. struct EnginePluginData {
  157. CarlaPlugin* plugin;
  158. float insPeak[2];
  159. float outsPeak[2];
  160. void clear() noexcept;
  161. };
  162. // -----------------------------------------------------------------------
  163. // CarlaEngineProtectedData
  164. struct CarlaEngineProtectedData {
  165. CarlaEngineOsc osc;
  166. CarlaEngineThread thread;
  167. const CarlaOscData* oscData;
  168. EngineCallbackFunc callback;
  169. void* callbackPtr;
  170. FileCallbackFunc fileCallback;
  171. void* fileCallbackPtr;
  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. #ifndef BUILD_BRIDGE
  191. CarlaCriticalSection _cs; // for handling requests from multiple threads
  192. #endif
  193. // -------------------------------------------------------------------
  194. CarlaEngineProtectedData(CarlaEngine* const engine);
  195. ~CarlaEngineProtectedData() noexcept;
  196. // -------------------------------------------------------------------
  197. void doPluginRemove() noexcept;
  198. void doPluginsSwitch() noexcept;
  199. void doNextPluginAction(const bool unlock) noexcept;
  200. // -------------------------------------------------------------------
  201. #ifndef BUILD_BRIDGE
  202. // the base, where plugins run
  203. void processRack(float* inBufReal[2], float* outBuf[2], const uint32_t nframes, const bool isOffline);
  204. // extended, will call processRack() in the middle
  205. void processRackFull(float** const inBuf, const uint32_t inCount, float** const outBuf, const uint32_t outCount, const uint32_t nframes, const bool isOffline);
  206. #endif
  207. // -------------------------------------------------------------------
  208. class ScopedActionLock
  209. {
  210. public:
  211. ScopedActionLock(CarlaEngineProtectedData* const data, const EnginePostAction action, const unsigned int pluginId, const unsigned int value, const bool lockWait) noexcept;
  212. ~ScopedActionLock() noexcept;
  213. private:
  214. CarlaEngineProtectedData* const fData;
  215. CARLA_PREVENT_HEAP_ALLOCATION
  216. CARLA_DECLARE_NON_COPY_CLASS(ScopedActionLock)
  217. };
  218. // -------------------------------------------------------------------
  219. #ifdef CARLA_PROPER_CPP11_SUPPORT
  220. CarlaEngineProtectedData() = delete;
  221. CARLA_DECLARE_NON_COPY_STRUCT(CarlaEngineProtectedData)
  222. #endif
  223. };
  224. // -----------------------------------------------------------------------
  225. CARLA_BACKEND_END_NAMESPACE
  226. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED