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.

359 lines
9.6KB

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