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.

387 lines
11KB

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