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.

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