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.

444 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. kPluginPostRtEventMidiLearn // param, cc, channel
  73. };
  74. /*!
  75. * A Post-RT event.
  76. * @see PluginPostRtEventType
  77. */
  78. struct PluginPostRtEvent {
  79. PluginPostRtEventType type;
  80. bool sendCallback;
  81. int32_t value1;
  82. int32_t value2;
  83. int32_t value3;
  84. float valuef;
  85. };
  86. // -----------------------------------------------------------------------
  87. struct ExternalMidiNote {
  88. int8_t channel; // invalid if -1
  89. uint8_t note; // 0 to 127
  90. uint8_t velo; // 1 to 127, 0 for note-off
  91. };
  92. // -----------------------------------------------------------------------
  93. struct PluginAudioPort {
  94. uint32_t rindex;
  95. CarlaEngineAudioPort* port;
  96. };
  97. struct PluginAudioData {
  98. uint32_t count;
  99. PluginAudioPort* ports;
  100. PluginAudioData() noexcept;
  101. ~PluginAudioData() noexcept;
  102. void createNew(uint32_t newCount);
  103. void clear() noexcept;
  104. void initBuffers() const noexcept;
  105. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioData)
  106. };
  107. // -----------------------------------------------------------------------
  108. struct PluginCVPort {
  109. uint32_t rindex;
  110. //uint32_t param; // FIXME is this needed?
  111. CarlaEngineCVPort* port;
  112. };
  113. struct PluginCVData {
  114. uint32_t count;
  115. PluginCVPort* ports;
  116. PluginCVData() noexcept;
  117. ~PluginCVData() noexcept;
  118. void createNew(uint32_t newCount);
  119. void clear() noexcept;
  120. void initBuffers() const noexcept;
  121. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVData)
  122. };
  123. // -----------------------------------------------------------------------
  124. struct PluginEventData {
  125. CarlaEngineEventPort* portIn;
  126. CarlaEngineEventPort* portOut;
  127. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  128. CarlaEngineCVSourcePorts* cvSourcePorts;
  129. #endif
  130. PluginEventData() noexcept;
  131. ~PluginEventData() noexcept;
  132. void clear() noexcept;
  133. void initBuffers() const noexcept;
  134. CARLA_DECLARE_NON_COPY_STRUCT(PluginEventData)
  135. };
  136. // -----------------------------------------------------------------------
  137. struct PluginParameterData {
  138. uint32_t count;
  139. ParameterData* data;
  140. ParameterRanges* ranges;
  141. SpecialParameterType* special;
  142. PluginParameterData() noexcept;
  143. ~PluginParameterData() noexcept;
  144. void createNew(uint32_t newCount, bool withSpecial);
  145. void clear() noexcept;
  146. float getFixedValue(uint32_t parameterId, float value) const noexcept;
  147. float getFinalUnnormalizedValue(uint32_t parameterId, float normalizedValue) const noexcept;
  148. float getFinalValueWithMidiDelta(uint32_t parameterId, float value, int8_t delta) const noexcept;
  149. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  150. };
  151. // -----------------------------------------------------------------------
  152. typedef const char* ProgramName;
  153. struct PluginProgramData {
  154. uint32_t count;
  155. int32_t current;
  156. ProgramName* names;
  157. PluginProgramData() noexcept;
  158. ~PluginProgramData() noexcept;
  159. void createNew(uint32_t newCount);
  160. void clear() noexcept;
  161. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  162. };
  163. // -----------------------------------------------------------------------
  164. struct PluginMidiProgramData {
  165. uint32_t count;
  166. int32_t current;
  167. MidiProgramData* data;
  168. PluginMidiProgramData() noexcept;
  169. ~PluginMidiProgramData() noexcept;
  170. void createNew(uint32_t newCount);
  171. void clear() noexcept;
  172. const MidiProgramData& getCurrent() const noexcept;
  173. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  174. };
  175. // -----------------------------------------------------------------------
  176. struct CarlaPlugin::ProtectedData {
  177. CarlaEngine* const engine;
  178. CarlaEngineClient* client;
  179. uint id;
  180. uint hints;
  181. uint options;
  182. uint32_t nodeId;
  183. bool active;
  184. bool enabled;
  185. bool needsReset;
  186. bool engineBridged;
  187. bool enginePlugin;
  188. lib_t lib;
  189. lib_t uiLib;
  190. // misc
  191. int8_t ctrlChannel;
  192. uint extraHints;
  193. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  194. int32_t midiLearnParameterIndex;
  195. uint transientTryCounter;
  196. bool transientFirstTry;
  197. #endif
  198. // data 1
  199. const char* name;
  200. const char* filename;
  201. const char* iconName;
  202. // data 2
  203. PluginAudioData audioIn;
  204. PluginAudioData audioOut;
  205. PluginCVData cvIn;
  206. PluginCVData cvOut;
  207. PluginEventData event;
  208. PluginParameterData param;
  209. PluginProgramData prog;
  210. PluginMidiProgramData midiprog;
  211. LinkedList<CustomData> custom;
  212. CarlaMutex masterMutex; // global master lock
  213. CarlaMutex singleMutex; // small lock used only in processSingle()
  214. CarlaStateSave stateSave;
  215. CarlaString uiTitle;
  216. struct ExternalNotes {
  217. CarlaMutex mutex;
  218. RtLinkedList<ExternalMidiNote>::Pool dataPool;
  219. RtLinkedList<ExternalMidiNote> data;
  220. ExternalNotes() noexcept;
  221. ~ExternalNotes() noexcept;
  222. void appendNonRT(const ExternalMidiNote& note) noexcept;
  223. void clear() noexcept;
  224. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  225. } extNotes;
  226. struct Latency {
  227. uint32_t frames;
  228. #ifndef BUILD_BRIDGE
  229. uint32_t channels;
  230. float** buffers;
  231. #endif
  232. Latency() noexcept;
  233. #ifndef BUILD_BRIDGE
  234. ~Latency() noexcept;
  235. void clearBuffers() noexcept;
  236. void recreateBuffers(uint32_t newChannels, uint32_t newFrames);
  237. #endif
  238. CARLA_DECLARE_NON_COPY_STRUCT(Latency)
  239. } latency;
  240. class PostRtEvents {
  241. public:
  242. PostRtEvents() noexcept;
  243. ~PostRtEvents() noexcept;
  244. void appendRT(const PluginPostRtEvent& event) noexcept;
  245. void trySplice() noexcept;
  246. struct Access {
  247. Access(PostRtEvents& e)
  248. : data2(e.dataPool)
  249. {
  250. const CarlaMutexLocker cml(e.dataMutex);
  251. if (e.data.isNotEmpty())
  252. e.data.moveTo(data2, true);
  253. }
  254. ~Access()
  255. {
  256. data2.clear();
  257. }
  258. inline RtLinkedList<PluginPostRtEvent>::Itenerator getDataIterator() const noexcept
  259. {
  260. return data2.begin2();
  261. }
  262. inline std::size_t isEmpty() const noexcept
  263. {
  264. return data2.isEmpty();
  265. }
  266. private:
  267. RtLinkedList<PluginPostRtEvent> data2;
  268. };
  269. private:
  270. RtLinkedList<PluginPostRtEvent>::Pool dataPool;
  271. RtLinkedList<PluginPostRtEvent> data, dataPendingRT;
  272. CarlaMutex dataMutex;
  273. CarlaMutex dataPendingMutex;
  274. CARLA_DECLARE_NON_COPY_CLASS(PostRtEvents)
  275. } postRtEvents;
  276. struct PostUiEvents {
  277. CarlaMutex mutex;
  278. LinkedList<PluginPostRtEvent> data;
  279. PostUiEvents() noexcept;
  280. ~PostUiEvents() noexcept;
  281. void append(const PluginPostRtEvent& event) noexcept;
  282. void clear() noexcept;
  283. CARLA_DECLARE_NON_COPY_STRUCT(PostUiEvents)
  284. } postUiEvents;
  285. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  286. struct PostProc {
  287. float dryWet;
  288. float volume;
  289. float balanceLeft;
  290. float balanceRight;
  291. float panning;
  292. PostProc() noexcept;
  293. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  294. } postProc;
  295. #endif
  296. ProtectedData(CarlaEngine* engine, uint idx) noexcept;
  297. ~ProtectedData() noexcept;
  298. // -------------------------------------------------------------------
  299. // Buffer functions
  300. void clearBuffers() noexcept;
  301. // -------------------------------------------------------------------
  302. // Post-poned events
  303. void postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept;
  304. void postponeRtEvent(PluginPostRtEventType type, bool sendCallbackLater,
  305. int32_t value1, int32_t value2, int32_t value3, float valuef) noexcept;
  306. // -------------------------------------------------------------------
  307. // Library functions
  308. static const char* libError(const char* filename) noexcept;
  309. bool libOpen(const char* filename) noexcept;
  310. bool libClose() noexcept;
  311. void setCanDeleteLib(bool canDelete) noexcept;
  312. bool uiLibOpen(const char* filename, bool canDelete) noexcept;
  313. bool uiLibClose() noexcept;
  314. template<typename Func>
  315. Func libSymbol(const char* symbol) const noexcept
  316. {
  317. return lib_symbol<Func>(lib, symbol);
  318. }
  319. template<typename Func>
  320. Func uiLibSymbol(const char* symbol) const noexcept
  321. {
  322. return lib_symbol<Func>(uiLib, symbol);
  323. }
  324. // -------------------------------------------------------------------
  325. // Misc
  326. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  327. void tryTransient() noexcept;
  328. #endif
  329. void updateParameterValues(CarlaPlugin* plugin,
  330. bool sendCallback, bool sendOsc, bool useDefault) noexcept;
  331. void updateDefaultParameterValues(CarlaPlugin* plugin) noexcept;
  332. // -------------------------------------------------------------------
  333. #ifdef CARLA_PROPER_CPP11_SUPPORT
  334. ProtectedData() = delete;
  335. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData);
  336. #endif
  337. CARLA_LEAK_DETECTOR(ProtectedData);
  338. };
  339. CARLA_BACKEND_END_NAMESPACE
  340. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED