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.

CarlaPluginInternal.hpp 11KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2014 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. const uint PLUGIN_EXTRA_HINT_CAN_RUN_RACK = 0x04;
  41. // -----------------------------------------------------------------------
  42. // Special parameters
  43. enum SpecialParameterType {
  44. PARAMETER_SPECIAL_NULL = 0,
  45. PARAMETER_SPECIAL_FREEWHEEL = 1,
  46. PARAMETER_SPECIAL_LATENCY = 2,
  47. PARAMETER_SPECIAL_SAMPLE_RATE = 3,
  48. PARAMETER_SPECIAL_TIME = 4
  49. };
  50. // -----------------------------------------------------------------------
  51. /*!
  52. * Post-RT event type.
  53. * These are events postponned from within the process function,
  54. *
  55. * During process, we cannot lock, allocate memory or do UI stuff,
  56. * so events have to be postponned to be executed later, on a separate thread.
  57. * @see PluginPostRtEvent
  58. */
  59. enum PluginPostRtEventType {
  60. kPluginPostRtEventNull = 0,
  61. kPluginPostRtEventDebug,
  62. kPluginPostRtEventParameterChange, // param, SP (*), value (SP: if 1 only report change to UI, don't report to Callback and OSC)
  63. kPluginPostRtEventProgramChange, // index
  64. kPluginPostRtEventMidiProgramChange, // index
  65. kPluginPostRtEventNoteOn, // channel, note, velo
  66. kPluginPostRtEventNoteOff // channel, note
  67. };
  68. /*!
  69. * A Post-RT event.
  70. * @see PluginPostRtEventType
  71. */
  72. struct PluginPostRtEvent {
  73. PluginPostRtEventType type;
  74. int32_t value1;
  75. int32_t value2;
  76. float value3;
  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. lib_t lib;
  174. lib_t uiLib;
  175. // misc
  176. int8_t ctrlChannel;
  177. uint extraHints;
  178. uint transientTryCounter;
  179. // data 1
  180. const char* name;
  181. const char* filename;
  182. const char* iconName;
  183. // data 2
  184. PluginAudioData audioIn;
  185. PluginAudioData audioOut;
  186. PluginCVData cvIn;
  187. PluginCVData cvOut;
  188. PluginEventData event;
  189. PluginParameterData param;
  190. PluginProgramData prog;
  191. PluginMidiProgramData midiprog;
  192. LinkedList<CustomData> custom;
  193. CarlaMutex masterMutex; // global master lock
  194. CarlaMutex singleMutex; // small lock used only in processSingle()
  195. CarlaStateSave stateSave;
  196. struct ExternalNotes {
  197. CarlaMutex mutex;
  198. RtLinkedList<ExternalMidiNote>::Pool dataPool;
  199. RtLinkedList<ExternalMidiNote> data;
  200. ExternalNotes() noexcept;
  201. ~ExternalNotes() noexcept;
  202. void appendNonRT(const ExternalMidiNote& note) noexcept;
  203. void clear() noexcept;
  204. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  205. } extNotes;
  206. struct Latency {
  207. uint32_t frames;
  208. #ifndef BUILD_BRIDGE
  209. uint32_t channels;
  210. float** buffers;
  211. #endif
  212. Latency() noexcept;
  213. #ifndef BUILD_BRIDGE
  214. ~Latency() noexcept;
  215. void clearBuffers() noexcept;
  216. void recreateBuffers(const uint32_t newChannels, const uint32_t newFrames);
  217. #endif
  218. CARLA_DECLARE_NON_COPY_STRUCT(Latency)
  219. } latency;
  220. struct PostRtEvents {
  221. CarlaMutex mutex;
  222. RtLinkedList<PluginPostRtEvent>::Pool dataPool;
  223. RtLinkedList<PluginPostRtEvent> data;
  224. RtLinkedList<PluginPostRtEvent> dataPendingRT;
  225. PostRtEvents() noexcept;
  226. ~PostRtEvents() noexcept;
  227. void appendRT(const PluginPostRtEvent& event) noexcept;
  228. void trySplice() noexcept;
  229. void clear() noexcept;
  230. CARLA_DECLARE_NON_COPY_STRUCT(PostRtEvents)
  231. } postRtEvents;
  232. struct PostUiEvents {
  233. CarlaMutex mutex;
  234. LinkedList<PluginPostRtEvent> data;
  235. PostUiEvents() noexcept;
  236. ~PostUiEvents() noexcept;
  237. void append(const PluginPostRtEvent& event) noexcept;
  238. void clear() noexcept;
  239. CARLA_DECLARE_NON_COPY_STRUCT(PostUiEvents)
  240. } postUiEvents;
  241. #ifndef BUILD_BRIDGE
  242. struct PostProc {
  243. float dryWet;
  244. float volume;
  245. float balanceLeft;
  246. float balanceRight;
  247. float panning;
  248. PostProc() noexcept;
  249. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  250. } postProc;
  251. #endif
  252. ProtectedData(CarlaEngine* const engine, const uint idx) noexcept;
  253. ~ProtectedData() noexcept;
  254. // -------------------------------------------------------------------
  255. // Buffer functions
  256. void clearBuffers() noexcept;
  257. // -------------------------------------------------------------------
  258. // Post-poned events
  259. void postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept;
  260. void postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3) noexcept;
  261. // -------------------------------------------------------------------
  262. // Library functions
  263. static const char* libError(const char* const filename) noexcept;
  264. bool libOpen(const char* const filename) noexcept;
  265. bool libClose() noexcept;
  266. bool uiLibOpen(const char* const filename, const bool canDelete) noexcept;
  267. bool uiLibClose() noexcept;
  268. template<typename Func>
  269. Func libSymbol(const char* const symbol) const noexcept
  270. {
  271. return lib_symbol<Func>(lib, symbol);
  272. }
  273. template<typename Func>
  274. Func uiLibSymbol(const char* const symbol) const noexcept
  275. {
  276. return lib_symbol<Func>(uiLib, symbol);
  277. }
  278. // -------------------------------------------------------------------
  279. // Misc
  280. #ifndef BUILD_BRIDGE
  281. void tryTransient() noexcept;
  282. #endif
  283. void updateParameterValues(CarlaPlugin* const plugin, const bool sendOsc, const bool sendCallback, const bool useDefault) noexcept;
  284. // -------------------------------------------------------------------
  285. #ifdef CARLA_PROPER_CPP11_SUPPORT
  286. ProtectedData() = delete;
  287. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData);
  288. #endif
  289. CARLA_LEAK_DETECTOR(ProtectedData);
  290. };
  291. CARLA_BACKEND_END_NAMESPACE
  292. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED