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.

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