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.

363 lines
9.8KB

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