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.

410 lines
11KB

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