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.

365 lines
9.7KB

  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 "CarlaLibUtils.hpp"
  22. #include "CarlaOscUtils.hpp"
  23. #include "CarlaStateUtils.hpp"
  24. #include "CarlaMIDI.h"
  25. #include "CarlaMutex.hpp"
  26. #include "CarlaString.hpp"
  27. #include "RtLinkedList.hpp"
  28. #include "juce_audio_basics.h"
  29. using juce::ChildProcess;
  30. using juce::FloatVectorOperations;
  31. using juce::ScopedPointer;
  32. CARLA_BACKEND_START_NAMESPACE
  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. lib_t lib;
  175. lib_t 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. PluginCVData cvIn;
  193. PluginCVData cvOut;
  194. PluginEventData event;
  195. PluginParameterData param;
  196. PluginProgramData prog;
  197. PluginMidiProgramData midiprog;
  198. LinkedList<CustomData> custom;
  199. CarlaMutex masterMutex; // global master lock
  200. CarlaMutex singleMutex; // small lock used only in processSingle()
  201. ScopedPointer<ChildProcess> childProcess;
  202. CarlaOscData oscData;
  203. CarlaStateSave stateSave;
  204. struct ExternalNotes {
  205. CarlaMutex mutex;
  206. RtLinkedList<ExternalMidiNote>::Pool dataPool;
  207. RtLinkedList<ExternalMidiNote> data;
  208. ExternalNotes() noexcept;
  209. ~ExternalNotes() noexcept;
  210. void appendNonRT(const ExternalMidiNote& note) noexcept;
  211. void clear() noexcept;
  212. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  213. } extNotes;
  214. struct PostRtEvents {
  215. CarlaMutex mutex;
  216. RtLinkedList<PluginPostRtEvent>::Pool dataPool;
  217. RtLinkedList<PluginPostRtEvent> data;
  218. RtLinkedList<PluginPostRtEvent> dataPendingRT;
  219. PostRtEvents() noexcept;
  220. ~PostRtEvents() noexcept;
  221. void appendRT(const PluginPostRtEvent& event) noexcept;
  222. void trySplice() noexcept;
  223. void clear() noexcept;
  224. CARLA_DECLARE_NON_COPY_STRUCT(PostRtEvents)
  225. } postRtEvents;
  226. #ifndef BUILD_BRIDGE
  227. struct PostProc {
  228. float dryWet;
  229. float volume;
  230. float balanceLeft;
  231. float balanceRight;
  232. float panning;
  233. PostProc() noexcept;
  234. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  235. } postProc;
  236. #endif
  237. ProtectedData(CarlaEngine* const engine, const uint idx) noexcept;
  238. ~ProtectedData() noexcept;
  239. // -------------------------------------------------------------------
  240. // Buffer functions
  241. void clearBuffers() noexcept;
  242. #ifndef BUILD_BRIDGE
  243. void recreateLatencyBuffers();
  244. #endif
  245. // -------------------------------------------------------------------
  246. // Post-poned events
  247. void postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept;
  248. void postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3) noexcept;
  249. // -------------------------------------------------------------------
  250. // Library functions
  251. static const char* libError(const char* const filename) noexcept;
  252. bool libOpen(const char* const filename) noexcept;
  253. bool libClose() noexcept;
  254. bool uiLibOpen(const char* const filename, const bool canDelete) noexcept;
  255. bool uiLibClose() noexcept;
  256. template<typename Func>
  257. Func libSymbol(const char* const symbol) const noexcept
  258. {
  259. return lib_symbol<Func>(lib, symbol);
  260. }
  261. template<typename Func>
  262. Func uiLibSymbol(const char* const symbol) const noexcept
  263. {
  264. return lib_symbol<Func>(uiLib, symbol);
  265. }
  266. // -------------------------------------------------------------------
  267. // Misc
  268. void tryTransient() noexcept;
  269. void updateParameterValues(CarlaPlugin* const plugin, const bool sendOsc, const bool sendCallback, const bool useDefault) noexcept;
  270. // -------------------------------------------------------------------
  271. #ifdef CARLA_PROPER_CPP11_SUPPORT
  272. ProtectedData() = delete;
  273. CARLA_DECLARE_NON_COPY_STRUCT(ProtectedData);
  274. #endif
  275. CARLA_LEAK_DETECTOR(ProtectedData);
  276. };
  277. CARLA_BACKEND_END_NAMESPACE
  278. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED