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.

610 lines
14KB

  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 GPL.txt file
  16. */
  17. #ifndef __CARLA_PLUGIN_INTERNAL_HPP__
  18. #define __CARLA_PLUGIN_INTERNAL_HPP__
  19. #include "CarlaPluginThread.hpp"
  20. #include "CarlaPlugin.hpp"
  21. #include "CarlaEngine.hpp"
  22. #include "CarlaOscUtils.hpp"
  23. #include "CarlaStateUtils.hpp"
  24. #include "CarlaMutex.hpp"
  25. #include "CarlaMIDI.h"
  26. #include "RtList.hpp"
  27. #define CARLA_PROCESS_CONTINUE_CHECK if (! fEnabled) { kData->engine->callback(CALLBACK_DEBUG, fId, 0, 0, 0.0, nullptr); return; }
  28. CARLA_BACKEND_START_NAMESPACE
  29. // -----------------------------------------------------------------------
  30. const unsigned short MAX_RT_EVENTS = 128;
  31. const unsigned short MAX_MIDI_EVENTS = 512;
  32. const unsigned int PLUGIN_HINT_HAS_MIDI_IN = 0x1;
  33. const unsigned int PLUGIN_HINT_HAS_MIDI_OUT = 0x2;
  34. const unsigned int PLUGIN_HINT_CAN_RUN_RACK = 0x4;
  35. // -----------------------------------------------------------------------
  36. struct PluginAudioPort {
  37. uint32_t rindex;
  38. CarlaEngineAudioPort* port;
  39. PluginAudioPort()
  40. : rindex(0),
  41. port(nullptr) {}
  42. ~PluginAudioPort()
  43. {
  44. CARLA_ASSERT(port == nullptr);
  45. }
  46. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginAudioPort)
  47. };
  48. struct PluginAudioData {
  49. uint32_t count;
  50. PluginAudioPort* ports;
  51. PluginAudioData()
  52. : count(0),
  53. ports(nullptr) {}
  54. ~PluginAudioData()
  55. {
  56. CARLA_ASSERT_INT(count == 0, count);
  57. CARLA_ASSERT(ports == nullptr);
  58. }
  59. void createNew(const uint32_t newCount)
  60. {
  61. CARLA_ASSERT_INT(count == 0, count);
  62. CARLA_ASSERT(ports == nullptr);
  63. CARLA_ASSERT_INT(newCount > 0, newCount);
  64. if (ports != nullptr || newCount == 0)
  65. return;
  66. ports = new PluginAudioPort[newCount];
  67. count = newCount;
  68. }
  69. void clear()
  70. {
  71. if (ports != nullptr)
  72. {
  73. for (uint32_t i=0; i < count; i++)
  74. {
  75. if (ports[i].port != nullptr)
  76. {
  77. delete ports[i].port;
  78. ports[i].port = nullptr;
  79. }
  80. }
  81. delete[] ports;
  82. ports = nullptr;
  83. }
  84. count = 0;
  85. }
  86. void initBuffers(CarlaEngine* const engine)
  87. {
  88. for (uint32_t i=0; i < count; i++)
  89. {
  90. if (ports[i].port != nullptr)
  91. ports[i].port->initBuffer(engine);
  92. }
  93. }
  94. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginAudioData)
  95. };
  96. // -----------------------------------------------------------------------
  97. struct PluginEventData {
  98. CarlaEngineEventPort* portIn;
  99. CarlaEngineEventPort* portOut;
  100. PluginEventData()
  101. : portIn(nullptr),
  102. portOut(nullptr) {}
  103. ~PluginEventData()
  104. {
  105. CARLA_ASSERT(portIn == nullptr);
  106. CARLA_ASSERT(portOut == nullptr);
  107. }
  108. void clear()
  109. {
  110. if (portIn != nullptr)
  111. {
  112. delete portIn;
  113. portIn = nullptr;
  114. }
  115. if (portOut != nullptr)
  116. {
  117. delete portOut;
  118. portOut = nullptr;
  119. }
  120. }
  121. void initBuffers(CarlaEngine* const engine)
  122. {
  123. if (portIn != nullptr)
  124. portIn->initBuffer(engine);
  125. if (portOut != nullptr)
  126. portOut->initBuffer(engine);
  127. }
  128. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginEventData)
  129. };
  130. // -----------------------------------------------------------------------
  131. struct PluginParameterData {
  132. uint32_t count;
  133. ParameterData* data;
  134. ParameterRanges* ranges;
  135. PluginParameterData()
  136. : count(0),
  137. data(nullptr),
  138. ranges(nullptr) {}
  139. ~PluginParameterData()
  140. {
  141. CARLA_ASSERT_INT(count == 0, count);
  142. CARLA_ASSERT(data == nullptr);
  143. CARLA_ASSERT(ranges == nullptr);
  144. }
  145. void createNew(const uint32_t newCount)
  146. {
  147. CARLA_ASSERT_INT(count == 0, count);
  148. CARLA_ASSERT(data == nullptr);
  149. CARLA_ASSERT(ranges == nullptr);
  150. CARLA_ASSERT_INT(newCount > 0, newCount);
  151. if (data != nullptr || ranges != nullptr || newCount == 0)
  152. return;
  153. data = new ParameterData[newCount];
  154. ranges = new ParameterRanges[newCount];
  155. count = newCount;
  156. }
  157. void clear()
  158. {
  159. if (data != nullptr)
  160. {
  161. delete[] data;
  162. data = nullptr;
  163. }
  164. if (ranges != nullptr)
  165. {
  166. delete[] ranges;
  167. ranges = nullptr;
  168. }
  169. count = 0;
  170. }
  171. float fixValue(const uint32_t parameterId, const float& value)
  172. {
  173. CARLA_ASSERT(parameterId < count);
  174. return ranges[parameterId].fixValue(value);
  175. }
  176. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginParameterData)
  177. };
  178. // -----------------------------------------------------------------------
  179. typedef const char* ProgramName;
  180. struct PluginProgramData {
  181. uint32_t count;
  182. int32_t current;
  183. ProgramName* names;
  184. PluginProgramData()
  185. : count(0),
  186. current(-1),
  187. names(nullptr) {}
  188. ~PluginProgramData()
  189. {
  190. CARLA_ASSERT_INT(count == 0, count);
  191. CARLA_ASSERT_INT(current == -1, current);
  192. CARLA_ASSERT(names == nullptr);
  193. }
  194. void createNew(const uint32_t newCount)
  195. {
  196. CARLA_ASSERT_INT(count == 0, count);
  197. CARLA_ASSERT_INT(current == -1, current);
  198. CARLA_ASSERT(names == nullptr);
  199. CARLA_ASSERT_INT(newCount > 0, newCount);
  200. if (names != nullptr || newCount == 0)
  201. return;
  202. names = new ProgramName[newCount];
  203. count = newCount;
  204. for (uint32_t i=0; i < newCount; i++)
  205. names[i] = nullptr;
  206. }
  207. void clear()
  208. {
  209. if (names != nullptr)
  210. {
  211. for (uint32_t i=0; i < count; i++)
  212. {
  213. if (names[i] != nullptr)
  214. delete[] names[i];
  215. }
  216. delete[] names;
  217. names = nullptr;
  218. }
  219. count = 0;
  220. current = -1;
  221. }
  222. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginProgramData)
  223. };
  224. // -----------------------------------------------------------------------
  225. struct PluginMidiProgramData {
  226. uint32_t count;
  227. int32_t current;
  228. MidiProgramData* data;
  229. PluginMidiProgramData()
  230. : count(0),
  231. current(-1),
  232. data(nullptr) {}
  233. ~PluginMidiProgramData()
  234. {
  235. CARLA_ASSERT_INT(count == 0, count);
  236. CARLA_ASSERT_INT(current == -1, current);
  237. CARLA_ASSERT(data == nullptr);
  238. }
  239. void createNew(const uint32_t newCount)
  240. {
  241. CARLA_ASSERT_INT(count == 0, count);
  242. CARLA_ASSERT_INT(current == -1, current);
  243. CARLA_ASSERT(data == nullptr);
  244. CARLA_ASSERT_INT(newCount > 0, newCount);
  245. if (data != nullptr || newCount == 0)
  246. return;
  247. data = new MidiProgramData[newCount];
  248. count = newCount;
  249. }
  250. void clear()
  251. {
  252. if (data != nullptr)
  253. {
  254. delete[] data;
  255. data = nullptr;
  256. }
  257. count = 0;
  258. current = -1;
  259. }
  260. const MidiProgramData& getCurrent() const
  261. {
  262. CARLA_ASSERT(current >= 0 && current < static_cast<int32_t>(count));
  263. return data[current];
  264. }
  265. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginMidiProgramData)
  266. };
  267. // -----------------------------------------------------------------------
  268. struct PluginPostRtEvent {
  269. PluginPostRtEventType type;
  270. int32_t value1;
  271. int32_t value2;
  272. float value3;
  273. PluginPostRtEvent()
  274. : type(kPluginPostRtEventNull),
  275. value1(-1),
  276. value2(-1),
  277. value3(0.0f) {}
  278. #if 1//def DEBUG
  279. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginPostRtEvent)
  280. #else
  281. CARLA_DECLARE_NON_COPY_STRUCT(PluginPostRtEvent)
  282. #endif
  283. };
  284. // -----------------------------------------------------------------------
  285. struct ExternalMidiNote {
  286. int8_t channel; // invalid == -1
  287. uint8_t note;
  288. uint8_t velo; // note-off if 0
  289. ExternalMidiNote()
  290. : channel(-1),
  291. note(0),
  292. velo(0) {}
  293. #if 1//def DEBUG
  294. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalMidiNote)
  295. #else
  296. CARLA_DECLARE_NON_COPY_STRUCT(ExternalMidiNote)
  297. #endif
  298. };
  299. // -----------------------------------------------------------------------
  300. class CarlaPluginGui;
  301. class CarlaPluginGuiCallback
  302. {
  303. public:
  304. virtual ~CarlaPluginGuiCallback() {}
  305. protected:
  306. virtual void guiClosedCallback() = 0;
  307. };
  308. // -----------------------------------------------------------------------
  309. struct CarlaPluginProtectedData {
  310. CarlaEngine* const engine;
  311. CarlaEngineClient* client;
  312. CarlaPluginGui* gui;
  313. bool active;
  314. //bool activeBefore;
  315. bool needsReset;
  316. void* lib;
  317. // misc
  318. int8_t ctrlChannel;
  319. unsigned int extraHints;
  320. // latency
  321. uint32_t latency;
  322. float** latencyBuffers;
  323. // data
  324. PluginAudioData audioIn;
  325. PluginAudioData audioOut;
  326. PluginEventData event;
  327. PluginParameterData param;
  328. PluginProgramData prog;
  329. PluginMidiProgramData midiprog;
  330. NonRtList<CustomData> custom;
  331. CarlaMutex masterMutex; // global master lock
  332. CarlaMutex singleMutex; // small lock used only in processSingle()
  333. struct ExternalNotes {
  334. CarlaMutex mutex;
  335. RtList<ExternalMidiNote>::Pool dataPool;
  336. RtList<ExternalMidiNote> data;
  337. ExternalNotes()
  338. : dataPool(32, 152),
  339. data(&dataPool) {}
  340. ~ExternalNotes()
  341. {
  342. mutex.lock();
  343. data.clear();
  344. mutex.unlock();
  345. }
  346. void append(const ExternalMidiNote& note)
  347. {
  348. mutex.lock();
  349. data.append_sleepy(note);
  350. mutex.unlock();
  351. }
  352. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalNotes)
  353. } extNotes;
  354. struct PostRtEvents {
  355. CarlaMutex mutex;
  356. RtList<PluginPostRtEvent>::Pool dataPool;
  357. RtList<PluginPostRtEvent> data;
  358. RtList<PluginPostRtEvent> dataPendingRT;
  359. PostRtEvents()
  360. : dataPool(MAX_RT_EVENTS, MAX_RT_EVENTS),
  361. data(&dataPool),
  362. dataPendingRT(&dataPool) {}
  363. ~PostRtEvents()
  364. {
  365. clear();
  366. }
  367. void appendRT(const PluginPostRtEvent& event)
  368. {
  369. dataPendingRT.append(event);
  370. }
  371. void trySplice()
  372. {
  373. if (mutex.tryLock())
  374. {
  375. dataPendingRT.spliceAppend(data, true);
  376. mutex.unlock();
  377. }
  378. }
  379. void clear()
  380. {
  381. mutex.lock();
  382. data.clear();
  383. dataPendingRT.clear();
  384. mutex.unlock();
  385. }
  386. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostRtEvents)
  387. } postRtEvents;
  388. struct PostProc {
  389. float dryWet;
  390. float volume;
  391. float balanceLeft;
  392. float balanceRight;
  393. float panning;
  394. PostProc()
  395. : dryWet(1.0f),
  396. volume(1.0f),
  397. balanceLeft(-1.0f),
  398. balanceRight(1.0f),
  399. panning(0.0f) {}
  400. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostProc)
  401. } postProc;
  402. struct OSC {
  403. CarlaOscData data;
  404. CarlaPluginThread thread;
  405. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  406. : thread(engine, plugin) {}
  407. OSC() = delete;
  408. OSC(OSC&) = delete;
  409. OSC(const OSC&) = delete;
  410. CARLA_LEAK_DETECTOR(OSC)
  411. } osc;
  412. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  413. : engine(engine_),
  414. client(nullptr),
  415. gui(nullptr),
  416. active(false),
  417. //activeBefore(false),
  418. needsReset(false),
  419. lib(nullptr),
  420. ctrlChannel(0),
  421. extraHints(0x0),
  422. latency(0),
  423. latencyBuffers(nullptr),
  424. osc(engine, plugin) {}
  425. CarlaPluginProtectedData() = delete;
  426. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  427. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  428. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  429. ~CarlaPluginProtectedData()
  430. {
  431. CARLA_ASSERT(gui == nullptr);
  432. CARLA_ASSERT(! active);
  433. CARLA_ASSERT(! needsReset);
  434. CARLA_ASSERT(lib == nullptr);
  435. CARLA_ASSERT(latency == 0);
  436. CARLA_ASSERT(latencyBuffers == nullptr);
  437. }
  438. void recreateLatencyBuffers()
  439. {
  440. if (latencyBuffers != nullptr)
  441. {
  442. for (uint32_t i=0; i < audioIn.count; i++)
  443. {
  444. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  445. if (latencyBuffers[i] != nullptr)
  446. delete[] latencyBuffers[i];
  447. }
  448. delete[] latencyBuffers;
  449. latencyBuffers = nullptr;
  450. }
  451. if (audioIn.count > 0 && latency > 0)
  452. {
  453. latencyBuffers = new float*[audioIn.count];
  454. for (uint32_t i=0; i < audioIn.count; i++)
  455. {
  456. latencyBuffers[i] = new float[latency];
  457. carla_zeroFloat(latencyBuffers[i], latency);
  458. }
  459. }
  460. }
  461. // defined in CarlaPluginGui.cpp
  462. void createUiIfNeeded(CarlaPluginGuiCallback* const callback);
  463. void destroyUiIfNeeded();
  464. void resizeUiLater(int width, int height);
  465. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  466. {
  467. return plugin->kData->engine;
  468. }
  469. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, const uint32_t index)
  470. {
  471. return plugin->kData->audioIn.ports[index].port;
  472. }
  473. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, const uint32_t index)
  474. {
  475. return plugin->kData->audioOut.ports[index].port;
  476. }
  477. static bool canRunInRack(CarlaPlugin* const plugin)
  478. {
  479. return (plugin->kData->extraHints & PLUGIN_HINT_CAN_RUN_RACK);
  480. }
  481. };
  482. // -----------------------------------------------------------------------
  483. CARLA_BACKEND_END_NAMESPACE
  484. #endif // __CARLA_PLUGIN_INTERNAL_HPP__