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.

373 lines
9.9KB

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