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.

392 lines
10KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2013 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 "CarlaMutex.hpp"
  24. #include "RtLinkedList.hpp"
  25. #include "CarlaMIDI.h"
  26. #include <cmath>
  27. #ifdef HAVE_JUCE
  28. # include "juce_audio_basics.h"
  29. using juce::FloatVectorOperations;
  30. #endif
  31. // -----------------------------------------------------------------------
  32. #define CARLA_PROCESS_CONTINUE_CHECK if (! pData->enabled) { pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, 0, 0, 0.0f, "Processing while plugin is disabled!!"); return; }
  33. // -----------------------------------------------------------------------
  34. // Float operations
  35. #ifdef HAVE_JUCE
  36. # define FLOAT_ADD(bufDst, bufSrc, frames) FloatVectorOperations::add(bufDst, bufSrc, frames)
  37. # define FLOAT_COPY(bufDst, bufSrc, frames) FloatVectorOperations::copy(bufDst, bufSrc, frames)
  38. # define FLOAT_CLEAR(buf, frames) FloatVectorOperations::clear(buf, frames)
  39. #else
  40. # define FLOAT_ADD(bufDst, bufSrc, frames) carla_addFloat(bufDst, bufSrc, frames)
  41. # define FLOAT_COPY(bufDst, bufSrc, frames) carla_copyFloat(bufDst, bufSrc, frames)
  42. # define FLOAT_CLEAR(buf, frames) carla_zeroFloat(buf, frames)
  43. #endif
  44. // -----------------------------------------------------------------------
  45. CARLA_BACKEND_START_NAMESPACE
  46. #if 0
  47. } // Fix editor indentation
  48. #endif
  49. // -----------------------------------------------------------------------
  50. // Forward declarations of CarlaEngine classes
  51. class CarlaEngineAudioPort;
  52. class CarlaEngineCVPort;
  53. class CarlaEngineEventPort;
  54. class CarlaEngineClient;
  55. // -----------------------------------------------------------------------
  56. // Maximum pre-allocated events for some plugin types
  57. const unsigned short kPluginMaxMidiEvents = 512;
  58. // -----------------------------------------------------------------------
  59. // Extra plugin hints, hidden from backend
  60. const unsigned int PLUGIN_EXTRA_HINT_HAS_MIDI_IN = 0x01;
  61. const unsigned int PLUGIN_EXTRA_HINT_HAS_MIDI_OUT = 0x02;
  62. const unsigned int PLUGIN_EXTRA_HINT_CAN_RUN_RACK = 0x04;
  63. // -----------------------------------------------------------------------
  64. /*!
  65. * Post-RT event type.\n
  66. * These are events postponned from within the process function,
  67. *
  68. * During process, we cannot lock, allocate memory or do UI stuff,\n
  69. * so events have to be postponned to be executed later, on a separate thread.
  70. */
  71. enum PluginPostRtEventType {
  72. kPluginPostRtEventNull,
  73. kPluginPostRtEventDebug,
  74. kPluginPostRtEventParameterChange, // param, SP (*), value (SP: if 1, don't report change to Callback and OSC)
  75. kPluginPostRtEventProgramChange, // index
  76. kPluginPostRtEventMidiProgramChange, // index
  77. kPluginPostRtEventNoteOn, // channel, note, velo
  78. kPluginPostRtEventNoteOff // channel, note
  79. };
  80. /*!
  81. * A Post-RT event.
  82. * \see PluginPostRtEventType
  83. */
  84. struct PluginPostRtEvent {
  85. PluginPostRtEventType type;
  86. int32_t value1;
  87. int32_t value2;
  88. float value3;
  89. };
  90. // -----------------------------------------------------------------------
  91. struct ExternalMidiNote {
  92. int8_t channel; // invalid if -1
  93. uint8_t note; // 0 to 127
  94. uint8_t velo; // note-off if 0
  95. };
  96. // -----------------------------------------------------------------------
  97. struct PluginAudioPort {
  98. uint32_t rindex;
  99. CarlaEngineAudioPort* port;
  100. PluginAudioPort() noexcept;
  101. ~PluginAudioPort();
  102. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioPort)
  103. };
  104. struct PluginAudioData {
  105. uint32_t count;
  106. PluginAudioPort* ports;
  107. PluginAudioData() noexcept;
  108. ~PluginAudioData();
  109. void createNew(const uint32_t newCount);
  110. void clear();
  111. void initBuffers();
  112. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioData)
  113. };
  114. // -----------------------------------------------------------------------
  115. struct PluginCVPort {
  116. uint32_t rindex;
  117. uint32_t param;
  118. CarlaEngineCVPort* port;
  119. PluginCVPort() noexcept;
  120. ~PluginCVPort();
  121. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVPort)
  122. };
  123. struct PluginCVData {
  124. uint32_t count;
  125. PluginCVPort* ports;
  126. PluginCVData() noexcept;
  127. ~PluginCVData();
  128. void createNew(const uint32_t newCount);
  129. void clear();
  130. void initBuffers();
  131. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVData)
  132. };
  133. // -----------------------------------------------------------------------
  134. struct PluginEventData {
  135. CarlaEngineEventPort* portIn;
  136. CarlaEngineEventPort* portOut;
  137. PluginEventData() noexcept;
  138. ~PluginEventData();
  139. void clear();
  140. void initBuffers();
  141. CARLA_DECLARE_NON_COPY_STRUCT(PluginEventData)
  142. };
  143. // -----------------------------------------------------------------------
  144. enum SpecialParameterType {
  145. PARAMETER_SPECIAL_NULL = 0,
  146. PARAMETER_SPECIAL_LATENCY = 1,
  147. PARAMETER_SPECIAL_SAMPLE_RATE = 2,
  148. PARAMETER_SPECIAL_LV2_FREEWHEEL = 3,
  149. PARAMETER_SPECIAL_LV2_TIME = 4
  150. };
  151. struct PluginParameterData {
  152. uint32_t count;
  153. ParameterData* data;
  154. ParameterRanges* ranges;
  155. SpecialParameterType* special;
  156. PluginParameterData() noexcept;
  157. ~PluginParameterData();
  158. void createNew(const uint32_t newCount, const bool withSpecial);
  159. void clear();
  160. float getFixedValue(const uint32_t parameterId, const float& value) const;
  161. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  162. };
  163. // -----------------------------------------------------------------------
  164. typedef const char* ProgramName;
  165. struct PluginProgramData {
  166. uint32_t count;
  167. int32_t current;
  168. ProgramName* names;
  169. PluginProgramData() noexcept;
  170. ~PluginProgramData();
  171. void createNew(const uint32_t newCount);
  172. void clear();
  173. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  174. };
  175. // -----------------------------------------------------------------------
  176. struct PluginMidiProgramData {
  177. uint32_t count;
  178. int32_t current;
  179. MidiProgramData* data;
  180. PluginMidiProgramData() noexcept;
  181. ~PluginMidiProgramData();
  182. void createNew(const uint32_t newCount);
  183. void clear();
  184. const MidiProgramData& getCurrent() const noexcept;
  185. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  186. };
  187. // -----------------------------------------------------------------------
  188. struct CarlaPluginProtectedData {
  189. CarlaEngine* const engine;
  190. CarlaEngineClient* client;
  191. unsigned int id;
  192. unsigned int hints;
  193. unsigned int options;
  194. bool active;
  195. bool enabled;
  196. bool needsReset;
  197. void* lib;
  198. void* uiLib;
  199. // misc
  200. int8_t ctrlChannel;
  201. uint extraHints;
  202. int patchbayClientId;
  203. // latency
  204. uint32_t latency;
  205. float** latencyBuffers;
  206. // data 1
  207. const char* name;
  208. const char* filename;
  209. const char* iconName;
  210. const char* identifier; // used for save/restore settings per plugin
  211. // data 2
  212. PluginAudioData audioIn;
  213. PluginAudioData audioOut;
  214. PluginEventData event;
  215. PluginParameterData param;
  216. PluginProgramData prog;
  217. PluginMidiProgramData midiprog;
  218. LinkedList<CustomData> custom;
  219. SaveState saveState;
  220. CarlaMutex masterMutex; // global master lock
  221. CarlaMutex singleMutex; // small lock used only in processSingle()
  222. struct ExternalNotes {
  223. CarlaMutex mutex;
  224. RtLinkedList<ExternalMidiNote>::Pool dataPool;
  225. RtLinkedList<ExternalMidiNote> data;
  226. ExternalNotes();
  227. ~ExternalNotes();
  228. void append(const ExternalMidiNote& note);
  229. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  230. } extNotes;
  231. struct PostRtEvents {
  232. CarlaMutex mutex;
  233. RtLinkedList<PluginPostRtEvent>::Pool dataPool;
  234. RtLinkedList<PluginPostRtEvent> data;
  235. RtLinkedList<PluginPostRtEvent> dataPendingRT;
  236. PostRtEvents();
  237. ~PostRtEvents();
  238. void appendRT(const PluginPostRtEvent& event);
  239. void trySplice();
  240. void clear();
  241. CARLA_DECLARE_NON_COPY_STRUCT(PostRtEvents)
  242. } postRtEvents;
  243. #ifndef BUILD_BRIDGE
  244. struct PostProc {
  245. float dryWet;
  246. float volume;
  247. float balanceLeft;
  248. float balanceRight;
  249. float panning;
  250. PostProc() noexcept;
  251. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  252. } postProc;
  253. #endif
  254. struct OSC {
  255. CarlaOscData data;
  256. CarlaPluginThread thread;
  257. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin);
  258. #ifdef CARLA_PROPER_CPP11_SUPPORT
  259. OSC() = delete;
  260. CARLA_DECLARE_NON_COPY_STRUCT(OSC)
  261. #endif
  262. } osc;
  263. CarlaPluginProtectedData(CarlaEngine* const eng, const unsigned int idx, CarlaPlugin* const self);
  264. ~CarlaPluginProtectedData();
  265. // -------------------------------------------------------------------
  266. // Buffer functions
  267. void clearBuffers();
  268. void recreateLatencyBuffers();
  269. // -------------------------------------------------------------------
  270. // Post-poned events
  271. void postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3);
  272. // -------------------------------------------------------------------
  273. // Library functions, see CarlaPlugin.cpp
  274. const char* libError(const char* const filename);
  275. bool libOpen(const char* const filename);
  276. bool libClose();
  277. void* libSymbol(const char* const symbol);
  278. bool uiLibOpen(const char* const filename);
  279. bool uiLibClose();
  280. void* uiLibSymbol(const char* const symbol);
  281. // -------------------------------------------------------------------
  282. // Settings functions, see CarlaPlugin.cpp
  283. void saveSetting(const uint option, const bool yesNo);
  284. uint loadSettings(const uint options, const uint availOptions);
  285. // -------------------------------------------------------------------
  286. #ifdef CARLA_PROPER_CPP11_SUPPORT
  287. CarlaPluginProtectedData() = delete;
  288. CARLA_DECLARE_NON_COPY_STRUCT(CarlaPluginProtectedData)
  289. #endif
  290. };
  291. CARLA_BACKEND_END_NAMESPACE
  292. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED