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.

423 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_IS_CV_CONTROLLED = 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. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  147. };
  148. // -----------------------------------------------------------------------
  149. typedef const char* ProgramName;
  150. struct PluginProgramData {
  151. uint32_t count;
  152. int32_t current;
  153. ProgramName* names;
  154. PluginProgramData() noexcept;
  155. ~PluginProgramData() noexcept;
  156. void createNew(uint32_t newCount);
  157. void clear() noexcept;
  158. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  159. };
  160. // -----------------------------------------------------------------------
  161. struct PluginMidiProgramData {
  162. uint32_t count;
  163. int32_t current;
  164. MidiProgramData* data;
  165. PluginMidiProgramData() noexcept;
  166. ~PluginMidiProgramData() noexcept;
  167. void createNew(uint32_t newCount);
  168. void clear() noexcept;
  169. const MidiProgramData& getCurrent() const noexcept;
  170. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  171. };
  172. // -----------------------------------------------------------------------
  173. struct CarlaPlugin::ProtectedData {
  174. CarlaEngine* const engine;
  175. CarlaEngineClient* client;
  176. uint id;
  177. uint hints;
  178. uint options;
  179. uint32_t nodeId;
  180. bool active;
  181. bool enabled;
  182. bool needsReset;
  183. bool engineBridged;
  184. bool enginePlugin;
  185. lib_t lib;
  186. lib_t uiLib;
  187. // misc
  188. int8_t ctrlChannel;
  189. uint extraHints;
  190. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  191. uint transientTryCounter;
  192. bool transientFirstTry;
  193. #endif
  194. // data 1
  195. const char* name;
  196. const char* filename;
  197. const char* iconName;
  198. // data 2
  199. PluginAudioData audioIn;
  200. PluginAudioData audioOut;
  201. PluginCVData cvIn;
  202. PluginCVData cvOut;
  203. PluginEventData event;
  204. PluginParameterData param;
  205. PluginProgramData prog;
  206. PluginMidiProgramData midiprog;
  207. LinkedList<CustomData> custom;
  208. CarlaMutex masterMutex; // global master lock
  209. CarlaMutex singleMutex; // small lock used only in processSingle()
  210. CarlaStateSave stateSave;
  211. struct ExternalNotes {
  212. CarlaMutex mutex;
  213. RtLinkedList<ExternalMidiNote>::Pool dataPool;
  214. RtLinkedList<ExternalMidiNote> data;
  215. ExternalNotes() noexcept;
  216. ~ExternalNotes() noexcept;
  217. void appendNonRT(const ExternalMidiNote& note) noexcept;
  218. void clear() noexcept;
  219. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  220. } extNotes;
  221. struct Latency {
  222. uint32_t frames;
  223. #ifndef BUILD_BRIDGE
  224. uint32_t channels;
  225. float** buffers;
  226. #endif
  227. Latency() noexcept;
  228. #ifndef BUILD_BRIDGE
  229. ~Latency() noexcept;
  230. void clearBuffers() noexcept;
  231. void recreateBuffers(uint32_t newChannels, uint32_t newFrames);
  232. #endif
  233. CARLA_DECLARE_NON_COPY_STRUCT(Latency)
  234. } latency;
  235. class PostRtEvents {
  236. public:
  237. PostRtEvents() noexcept;
  238. ~PostRtEvents() noexcept;
  239. void appendRT(const PluginPostRtEvent& event) noexcept;
  240. void trySplice() noexcept;
  241. void clearData() noexcept;
  242. inline CarlaMutex& getDataMutex() noexcept
  243. {
  244. return dataMutex;
  245. }
  246. inline RtLinkedList<PluginPostRtEvent>::Itenerator getDataIterator() const noexcept
  247. {
  248. return data.begin2();
  249. }
  250. private:
  251. RtLinkedList<PluginPostRtEvent>::Pool dataPool;
  252. RtLinkedList<PluginPostRtEvent> dataPendingRT;
  253. RtLinkedList<PluginPostRtEvent> data;
  254. CarlaMutex dataMutex;
  255. // TESTING for thread-safety, remove later
  256. CarlaMutex dataPendingMutex;
  257. CARLA_DECLARE_NON_COPY_CLASS(PostRtEvents)
  258. } postRtEvents;
  259. struct PostUiEvents {
  260. CarlaMutex mutex;
  261. LinkedList<PluginPostRtEvent> data;
  262. PostUiEvents() noexcept;
  263. ~PostUiEvents() noexcept;
  264. void append(const PluginPostRtEvent& event) noexcept;
  265. void clear() noexcept;
  266. CARLA_DECLARE_NON_COPY_STRUCT(PostUiEvents)
  267. } postUiEvents;
  268. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  269. struct PostProc {
  270. float dryWet;
  271. float volume;
  272. float balanceLeft;
  273. float balanceRight;
  274. float panning;
  275. PostProc() noexcept;
  276. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  277. } postProc;
  278. #endif
  279. ProtectedData(CarlaEngine* engine, uint idx) noexcept;
  280. ~ProtectedData() noexcept;
  281. // -------------------------------------------------------------------
  282. // Buffer functions
  283. void clearBuffers() noexcept;
  284. // -------------------------------------------------------------------
  285. // Post-poned events
  286. void postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept;
  287. void postponeRtEvent(PluginPostRtEventType type, bool sendCallbackLater,
  288. int32_t value1, int32_t value2, int32_t value3, float valuef) noexcept;
  289. // -------------------------------------------------------------------
  290. // Library functions
  291. static const char* libError(const char* filename) noexcept;
  292. bool libOpen(const char* filename) noexcept;
  293. bool libClose() noexcept;
  294. void setCanDeleteLib(bool canDelete) noexcept;
  295. bool uiLibOpen(const char* filename, bool canDelete) noexcept;
  296. bool uiLibClose() noexcept;
  297. template<typename Func>
  298. Func libSymbol(const char* symbol) const noexcept
  299. {
  300. return lib_symbol<Func>(lib, symbol);
  301. }
  302. template<typename Func>
  303. Func uiLibSymbol(const char* symbol) const noexcept
  304. {
  305. return lib_symbol<Func>(uiLib, symbol);
  306. }
  307. // -------------------------------------------------------------------
  308. // Misc
  309. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  310. void tryTransient() noexcept;
  311. #endif
  312. void updateParameterValues(CarlaPlugin* plugin,
  313. bool sendCallback, bool sendOsc, bool useDefault) noexcept;
  314. void updateDefaultParameterValues(CarlaPlugin* plugin) noexcept;
  315. // -------------------------------------------------------------------
  316. #ifdef CARLA_PROPER_CPP11_SUPPORT
  317. ProtectedData() = delete;
  318. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData);
  319. #endif
  320. CARLA_LEAK_DETECTOR(ProtectedData);
  321. };
  322. CARLA_BACKEND_END_NAMESPACE
  323. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED