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.

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