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.

441 lines
12KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2019 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_PLUGIN_INTERNAL_HPP_INCLUDED
  18. #define CARLA_PLUGIN_INTERNAL_HPP_INCLUDED
  19. #include "CarlaPlugin.hpp"
  20. #include "CarlaJuceUtils.hpp"
  21. #include "CarlaLibUtils.hpp"
  22. #include "CarlaStateUtils.hpp"
  23. #include "CarlaMIDI.h"
  24. #include "CarlaMutex.hpp"
  25. #include "CarlaString.hpp"
  26. #include "RtLinkedList.hpp"
  27. CARLA_BACKEND_START_NAMESPACE
  28. // -----------------------------------------------------------------------
  29. // Engine helper macro, sets lastError and returns false/NULL
  30. #define CARLA_SAFE_ASSERT_RETURN_ERR(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); pData->engine->setLastError(err); return false; }
  31. #define CARLA_SAFE_ASSERT_RETURN_ERRN(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); pData->engine->setLastError(err); return nullptr; }
  32. #define CARLA_SAFE_EXCEPTION_RETURN_ERR(excptMsg, errMsg) catch(...) { carla_safe_exception(excptMsg, __FILE__, __LINE__); pData->engine->setLastError(errMsg); return false; }
  33. #define CARLA_SAFE_EXCEPTION_RETURN_ERRN(excptMsg, errMsg) catch(...) { carla_safe_exception(excptMsg, __FILE__, __LINE__); pData->engine->setLastError(errMsg); return nullptr; }
  34. // -----------------------------------------------------------------------
  35. // Maximum pre-allocated events for some plugin types
  36. const ushort kPluginMaxMidiEvents = 512;
  37. // -----------------------------------------------------------------------
  38. // Extra parameter hints, hidden from backend
  39. const uint PARAMETER_MAPPED_RANGES_SET = 0x1000;
  40. const uint PARAMETER_IS_STRICT_BOUNDS = 0x2000;
  41. const uint PARAMETER_IS_TRIGGER = 0x4000;
  42. // -----------------------------------------------------------------------
  43. // Extra plugin hints, hidden from backend
  44. const uint PLUGIN_EXTRA_HINT_HAS_MIDI_IN = 0x01;
  45. const uint PLUGIN_EXTRA_HINT_HAS_MIDI_OUT = 0x02;
  46. // -----------------------------------------------------------------------
  47. // Special parameters
  48. enum SpecialParameterType {
  49. PARAMETER_SPECIAL_NULL = 0,
  50. PARAMETER_SPECIAL_FREEWHEEL = 1,
  51. PARAMETER_SPECIAL_LATENCY = 2,
  52. PARAMETER_SPECIAL_SAMPLE_RATE = 3,
  53. PARAMETER_SPECIAL_TIME = 4
  54. };
  55. // -----------------------------------------------------------------------
  56. /*!
  57. * Post-RT event type.
  58. * These are events postponned from within the process function,
  59. *
  60. * During process, we cannot lock, allocate memory or do UI stuff.
  61. * Events have to be postponned to be executed later, on a separate thread.
  62. * @see PluginPostRtEvent
  63. */
  64. enum PluginPostRtEventType {
  65. kPluginPostRtEventNull = 0,
  66. kPluginPostRtEventDebug,
  67. kPluginPostRtEventParameterChange, // param, (unused), (unused), value
  68. kPluginPostRtEventProgramChange, // index
  69. kPluginPostRtEventMidiProgramChange, // index
  70. kPluginPostRtEventNoteOn, // channel, note, velo
  71. kPluginPostRtEventNoteOff // channel, note
  72. };
  73. /*!
  74. * A Post-RT event.
  75. * @see PluginPostRtEventType
  76. */
  77. struct PluginPostRtEvent {
  78. PluginPostRtEventType type;
  79. bool sendCallback;
  80. int32_t value1;
  81. int32_t value2;
  82. int32_t value3;
  83. float valuef;
  84. };
  85. // -----------------------------------------------------------------------
  86. struct ExternalMidiNote {
  87. int8_t channel; // invalid if -1
  88. uint8_t note; // 0 to 127
  89. uint8_t velo; // 1 to 127, 0 for note-off
  90. };
  91. // -----------------------------------------------------------------------
  92. struct PluginAudioPort {
  93. uint32_t rindex;
  94. CarlaEngineAudioPort* port;
  95. };
  96. struct PluginAudioData {
  97. uint32_t count;
  98. PluginAudioPort* ports;
  99. PluginAudioData() noexcept;
  100. ~PluginAudioData() noexcept;
  101. void createNew(uint32_t newCount);
  102. void clear() noexcept;
  103. void initBuffers() const noexcept;
  104. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioData)
  105. };
  106. // -----------------------------------------------------------------------
  107. struct PluginCVPort {
  108. uint32_t rindex;
  109. //uint32_t param; // FIXME is this needed?
  110. CarlaEngineCVPort* port;
  111. };
  112. struct PluginCVData {
  113. uint32_t count;
  114. PluginCVPort* ports;
  115. PluginCVData() noexcept;
  116. ~PluginCVData() noexcept;
  117. void createNew(uint32_t newCount);
  118. void clear() noexcept;
  119. void initBuffers() const noexcept;
  120. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVData)
  121. };
  122. // -----------------------------------------------------------------------
  123. struct PluginEventData {
  124. CarlaEngineEventPort* portIn;
  125. CarlaEngineEventPort* portOut;
  126. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  127. CarlaEngineCVSourcePorts* cvSourcePorts;
  128. #endif
  129. PluginEventData() noexcept;
  130. ~PluginEventData() noexcept;
  131. void clear() noexcept;
  132. void initBuffers() const noexcept;
  133. CARLA_DECLARE_NON_COPY_STRUCT(PluginEventData)
  134. };
  135. // -----------------------------------------------------------------------
  136. struct PluginParameterData {
  137. uint32_t count;
  138. ParameterData* data;
  139. ParameterRanges* ranges;
  140. SpecialParameterType* special;
  141. PluginParameterData() noexcept;
  142. ~PluginParameterData() noexcept;
  143. void createNew(uint32_t newCount, bool withSpecial);
  144. void clear() noexcept;
  145. float getFixedValue(uint32_t parameterId, float value) const noexcept;
  146. float getFinalUnnormalizedValue(uint32_t parameterId, float value) const noexcept;
  147. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  148. };
  149. // -----------------------------------------------------------------------
  150. typedef const char* ProgramName;
  151. struct PluginProgramData {
  152. uint32_t count;
  153. int32_t current;
  154. ProgramName* names;
  155. PluginProgramData() noexcept;
  156. ~PluginProgramData() noexcept;
  157. void createNew(uint32_t newCount);
  158. void clear() noexcept;
  159. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  160. };
  161. // -----------------------------------------------------------------------
  162. struct PluginMidiProgramData {
  163. uint32_t count;
  164. int32_t current;
  165. MidiProgramData* data;
  166. PluginMidiProgramData() noexcept;
  167. ~PluginMidiProgramData() noexcept;
  168. void createNew(uint32_t newCount);
  169. void clear() noexcept;
  170. const MidiProgramData& getCurrent() const noexcept;
  171. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  172. };
  173. // -----------------------------------------------------------------------
  174. struct CarlaPlugin::ProtectedData {
  175. CarlaEngine* const engine;
  176. CarlaEngineClient* client;
  177. uint id;
  178. uint hints;
  179. uint options;
  180. uint32_t nodeId;
  181. bool active;
  182. bool enabled;
  183. bool needsReset;
  184. bool engineBridged;
  185. bool enginePlugin;
  186. lib_t lib;
  187. lib_t uiLib;
  188. // misc
  189. int8_t ctrlChannel;
  190. uint extraHints;
  191. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  192. uint transientTryCounter;
  193. bool transientFirstTry;
  194. #endif
  195. // data 1
  196. const char* name;
  197. const char* filename;
  198. const char* iconName;
  199. // data 2
  200. PluginAudioData audioIn;
  201. PluginAudioData audioOut;
  202. PluginCVData cvIn;
  203. PluginCVData cvOut;
  204. PluginEventData event;
  205. PluginParameterData param;
  206. PluginProgramData prog;
  207. PluginMidiProgramData midiprog;
  208. LinkedList<CustomData> custom;
  209. CarlaMutex masterMutex; // global master lock
  210. CarlaMutex singleMutex; // small lock used only in processSingle()
  211. CarlaStateSave stateSave;
  212. CarlaString uiTitle;
  213. struct ExternalNotes {
  214. CarlaMutex mutex;
  215. RtLinkedList<ExternalMidiNote>::Pool dataPool;
  216. RtLinkedList<ExternalMidiNote> data;
  217. ExternalNotes() noexcept;
  218. ~ExternalNotes() noexcept;
  219. void appendNonRT(const ExternalMidiNote& note) noexcept;
  220. void clear() noexcept;
  221. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  222. } extNotes;
  223. struct Latency {
  224. uint32_t frames;
  225. #ifndef BUILD_BRIDGE
  226. uint32_t channels;
  227. float** buffers;
  228. #endif
  229. Latency() noexcept;
  230. #ifndef BUILD_BRIDGE
  231. ~Latency() noexcept;
  232. void clearBuffers() noexcept;
  233. void recreateBuffers(uint32_t newChannels, uint32_t newFrames);
  234. #endif
  235. CARLA_DECLARE_NON_COPY_STRUCT(Latency)
  236. } latency;
  237. class PostRtEvents {
  238. public:
  239. PostRtEvents() noexcept;
  240. ~PostRtEvents() noexcept;
  241. void appendRT(const PluginPostRtEvent& event) noexcept;
  242. void trySplice() noexcept;
  243. struct Access {
  244. Access(PostRtEvents& e)
  245. : data2(e.dataPool)
  246. {
  247. const CarlaMutexLocker cml(e.dataMutex);
  248. if (e.data.isNotEmpty())
  249. e.data.moveTo(data2, true);
  250. }
  251. ~Access()
  252. {
  253. data2.clear();
  254. }
  255. inline RtLinkedList<PluginPostRtEvent>::Itenerator getDataIterator() const noexcept
  256. {
  257. return data2.begin2();
  258. }
  259. inline std::size_t isEmpty() const noexcept
  260. {
  261. return data2.isEmpty();
  262. }
  263. private:
  264. RtLinkedList<PluginPostRtEvent> data2;
  265. };
  266. private:
  267. RtLinkedList<PluginPostRtEvent>::Pool dataPool;
  268. RtLinkedList<PluginPostRtEvent> data, dataPendingRT;
  269. CarlaMutex dataMutex;
  270. CarlaMutex dataPendingMutex;
  271. CARLA_DECLARE_NON_COPY_CLASS(PostRtEvents)
  272. } postRtEvents;
  273. struct PostUiEvents {
  274. CarlaMutex mutex;
  275. LinkedList<PluginPostRtEvent> data;
  276. PostUiEvents() noexcept;
  277. ~PostUiEvents() noexcept;
  278. void append(const PluginPostRtEvent& event) noexcept;
  279. void clear() noexcept;
  280. CARLA_DECLARE_NON_COPY_STRUCT(PostUiEvents)
  281. } postUiEvents;
  282. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  283. struct PostProc {
  284. float dryWet;
  285. float volume;
  286. float balanceLeft;
  287. float balanceRight;
  288. float panning;
  289. PostProc() noexcept;
  290. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  291. } postProc;
  292. #endif
  293. ProtectedData(CarlaEngine* engine, uint idx) noexcept;
  294. ~ProtectedData() noexcept;
  295. // -------------------------------------------------------------------
  296. // Buffer functions
  297. void clearBuffers() noexcept;
  298. // -------------------------------------------------------------------
  299. // Post-poned events
  300. void postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept;
  301. void postponeRtEvent(PluginPostRtEventType type, bool sendCallbackLater,
  302. int32_t value1, int32_t value2, int32_t value3, float valuef) noexcept;
  303. // -------------------------------------------------------------------
  304. // Library functions
  305. static const char* libError(const char* filename) noexcept;
  306. bool libOpen(const char* filename) noexcept;
  307. bool libClose() noexcept;
  308. void setCanDeleteLib(bool canDelete) noexcept;
  309. bool uiLibOpen(const char* filename, bool canDelete) noexcept;
  310. bool uiLibClose() noexcept;
  311. template<typename Func>
  312. Func libSymbol(const char* symbol) const noexcept
  313. {
  314. return lib_symbol<Func>(lib, symbol);
  315. }
  316. template<typename Func>
  317. Func uiLibSymbol(const char* symbol) const noexcept
  318. {
  319. return lib_symbol<Func>(uiLib, symbol);
  320. }
  321. // -------------------------------------------------------------------
  322. // Misc
  323. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  324. void tryTransient() noexcept;
  325. #endif
  326. void updateParameterValues(CarlaPlugin* plugin,
  327. bool sendCallback, bool sendOsc, bool useDefault) noexcept;
  328. void updateDefaultParameterValues(CarlaPlugin* plugin) noexcept;
  329. // -------------------------------------------------------------------
  330. #ifdef CARLA_PROPER_CPP11_SUPPORT
  331. ProtectedData() = delete;
  332. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData);
  333. #endif
  334. CARLA_LEAK_DETECTOR(ProtectedData);
  335. };
  336. CARLA_BACKEND_END_NAMESPACE
  337. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED