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.

814 lines
19KB

  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 "CarlaEngine.hpp"
  22. #include "CarlaBackendUtils.hpp"
  23. #include "CarlaOscUtils.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.0f, "Processing while plugin is disabled!!"); return; }
  28. CARLA_BACKEND_START_NAMESPACE
  29. // -----------------------------------------------------------------------
  30. const unsigned short MAX_MIDI_EVENTS = 512;
  31. const unsigned int PLUGIN_HINT_HAS_MIDI_IN = 0x1;
  32. const unsigned int PLUGIN_HINT_HAS_MIDI_OUT = 0x2;
  33. const unsigned int PLUGIN_HINT_CAN_RUN_RACK = 0x4;
  34. // -----------------------------------------------------------------------
  35. /*!
  36. * Post-RT event type.\n
  37. * These are events postponned from within the process function,
  38. *
  39. * During process, we cannot lock, allocate memory or do UI stuff,\n
  40. * so events have to be postponned to be executed later, on a separate thread.
  41. */
  42. enum PluginPostRtEventType {
  43. kPluginPostRtEventNull,
  44. kPluginPostRtEventDebug,
  45. kPluginPostRtEventParameterChange, // param, SP*, value (SP: if 1, don't report change to Callback and OSC)
  46. kPluginPostRtEventProgramChange, // index
  47. kPluginPostRtEventMidiProgramChange, // index
  48. kPluginPostRtEventNoteOn, // channel, note, velo
  49. kPluginPostRtEventNoteOff // channel, note
  50. };
  51. /*!
  52. * A Post-RT event.
  53. * \see PluginPostRtEventType
  54. */
  55. struct PluginPostRtEvent {
  56. PluginPostRtEventType type;
  57. int32_t value1;
  58. int32_t value2;
  59. float value3;
  60. PluginPostRtEvent() noexcept
  61. : type(kPluginPostRtEventNull),
  62. value1(-1),
  63. value2(-1),
  64. value3(0.0f) {}
  65. };
  66. // -----------------------------------------------------------------------
  67. struct PluginAudioPort {
  68. uint32_t rindex;
  69. CarlaEngineAudioPort* port;
  70. PluginAudioPort()
  71. : rindex(0),
  72. port(nullptr) {}
  73. ~PluginAudioPort()
  74. {
  75. CARLA_ASSERT(port == nullptr);
  76. }
  77. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioPort)
  78. };
  79. struct PluginAudioData {
  80. uint32_t count;
  81. PluginAudioPort* ports;
  82. PluginAudioData()
  83. : count(0),
  84. ports(nullptr) {}
  85. ~PluginAudioData()
  86. {
  87. CARLA_ASSERT_INT(count == 0, count);
  88. CARLA_ASSERT(ports == nullptr);
  89. }
  90. void createNew(const uint32_t newCount)
  91. {
  92. CARLA_ASSERT_INT(count == 0, count);
  93. CARLA_ASSERT(ports == nullptr);
  94. CARLA_ASSERT_INT(newCount > 0, newCount);
  95. if (ports != nullptr || newCount == 0)
  96. return;
  97. ports = new PluginAudioPort[newCount];
  98. count = newCount;
  99. }
  100. void clear()
  101. {
  102. if (ports != nullptr)
  103. {
  104. for (uint32_t i=0; i < count; ++i)
  105. {
  106. if (ports[i].port != nullptr)
  107. {
  108. delete ports[i].port;
  109. ports[i].port = nullptr;
  110. }
  111. }
  112. delete[] ports;
  113. ports = nullptr;
  114. }
  115. count = 0;
  116. }
  117. void initBuffers(CarlaEngine* const engine)
  118. {
  119. for (uint32_t i=0; i < count; ++i)
  120. {
  121. if (ports[i].port != nullptr)
  122. ports[i].port->initBuffer(engine);
  123. }
  124. }
  125. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioData)
  126. };
  127. // -----------------------------------------------------------------------
  128. struct PluginCVPort {
  129. uint32_t rindex;
  130. uint32_t param;
  131. CarlaEngineCVPort* port;
  132. PluginCVPort()
  133. : rindex(0),
  134. param(0),
  135. port(nullptr) {}
  136. ~PluginCVPort()
  137. {
  138. CARLA_ASSERT(port == nullptr);
  139. }
  140. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVPort)
  141. };
  142. struct PluginCVData {
  143. uint32_t count;
  144. PluginCVPort* ports;
  145. PluginCVData()
  146. : count(0),
  147. ports(nullptr) {}
  148. ~PluginCVData()
  149. {
  150. CARLA_ASSERT_INT(count == 0, count);
  151. CARLA_ASSERT(ports == nullptr);
  152. }
  153. void createNew(const uint32_t newCount)
  154. {
  155. CARLA_ASSERT_INT(count == 0, count);
  156. CARLA_ASSERT(ports == nullptr);
  157. CARLA_ASSERT_INT(newCount > 0, newCount);
  158. if (ports != nullptr || newCount == 0)
  159. return;
  160. ports = new PluginCVPort[newCount];
  161. count = newCount;
  162. }
  163. void clear()
  164. {
  165. if (ports != nullptr)
  166. {
  167. for (uint32_t i=0; i < count; ++i)
  168. {
  169. if (ports[i].port != nullptr)
  170. {
  171. delete ports[i].port;
  172. ports[i].port = nullptr;
  173. }
  174. }
  175. delete[] ports;
  176. ports = nullptr;
  177. }
  178. count = 0;
  179. }
  180. void initBuffers(CarlaEngine* const engine)
  181. {
  182. for (uint32_t i=0; i < count; ++i)
  183. {
  184. if (ports[i].port != nullptr)
  185. ports[i].port->initBuffer(engine);
  186. }
  187. }
  188. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVData)
  189. };
  190. // -----------------------------------------------------------------------
  191. struct PluginEventData {
  192. CarlaEngineEventPort* portIn;
  193. CarlaEngineEventPort* portOut;
  194. PluginEventData()
  195. : portIn(nullptr),
  196. portOut(nullptr) {}
  197. ~PluginEventData()
  198. {
  199. CARLA_ASSERT(portIn == nullptr);
  200. CARLA_ASSERT(portOut == nullptr);
  201. }
  202. void clear()
  203. {
  204. if (portIn != nullptr)
  205. {
  206. delete portIn;
  207. portIn = nullptr;
  208. }
  209. if (portOut != nullptr)
  210. {
  211. delete portOut;
  212. portOut = nullptr;
  213. }
  214. }
  215. void initBuffers(CarlaEngine* const engine)
  216. {
  217. if (portIn != nullptr)
  218. portIn->initBuffer(engine);
  219. if (portOut != nullptr)
  220. portOut->initBuffer(engine);
  221. }
  222. CARLA_DECLARE_NON_COPY_STRUCT(PluginEventData)
  223. };
  224. // -----------------------------------------------------------------------
  225. struct PluginParameterData {
  226. uint32_t count;
  227. ParameterData* data;
  228. ParameterRanges* ranges;
  229. PluginParameterData()
  230. : count(0),
  231. data(nullptr),
  232. ranges(nullptr) {}
  233. ~PluginParameterData()
  234. {
  235. CARLA_ASSERT_INT(count == 0, count);
  236. CARLA_ASSERT(data == nullptr);
  237. CARLA_ASSERT(ranges == nullptr);
  238. }
  239. void createNew(const uint32_t newCount)
  240. {
  241. CARLA_ASSERT_INT(count == 0, count);
  242. CARLA_ASSERT(data == nullptr);
  243. CARLA_ASSERT(ranges == nullptr);
  244. CARLA_ASSERT_INT(newCount > 0, newCount);
  245. if (data != nullptr || ranges != nullptr || newCount == 0)
  246. return;
  247. data = new ParameterData[newCount];
  248. ranges = new ParameterRanges[newCount];
  249. count = newCount;
  250. }
  251. void clear()
  252. {
  253. if (data != nullptr)
  254. {
  255. delete[] data;
  256. data = nullptr;
  257. }
  258. if (ranges != nullptr)
  259. {
  260. delete[] ranges;
  261. ranges = nullptr;
  262. }
  263. count = 0;
  264. }
  265. float fixValue(const uint32_t parameterId, const float& value)
  266. {
  267. CARLA_ASSERT_INT2(parameterId < count, parameterId, count);
  268. return ranges[parameterId].fixValue(value);
  269. }
  270. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  271. };
  272. // -----------------------------------------------------------------------
  273. typedef const char* ProgramName;
  274. struct PluginProgramData {
  275. uint32_t count;
  276. int32_t current;
  277. ProgramName* names;
  278. PluginProgramData()
  279. : count(0),
  280. current(-1),
  281. names(nullptr) {}
  282. ~PluginProgramData()
  283. {
  284. CARLA_ASSERT_INT(count == 0, count);
  285. CARLA_ASSERT_INT(current == -1, current);
  286. CARLA_ASSERT(names == nullptr);
  287. }
  288. void createNew(const uint32_t newCount)
  289. {
  290. CARLA_ASSERT_INT(count == 0, count);
  291. CARLA_ASSERT_INT(current == -1, current);
  292. CARLA_ASSERT(names == nullptr);
  293. CARLA_ASSERT_INT(newCount > 0, newCount);
  294. if (names != nullptr || newCount == 0)
  295. return;
  296. names = new ProgramName[newCount];
  297. count = newCount;
  298. for (uint32_t i=0; i < newCount; ++i)
  299. names[i] = nullptr;
  300. }
  301. void clear()
  302. {
  303. if (names != nullptr)
  304. {
  305. for (uint32_t i=0; i < count; ++i)
  306. {
  307. if (names[i] != nullptr)
  308. {
  309. delete[] names[i];
  310. names[i] = nullptr;
  311. }
  312. }
  313. delete[] names;
  314. names = nullptr;
  315. }
  316. count = 0;
  317. current = -1;
  318. }
  319. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  320. };
  321. // -----------------------------------------------------------------------
  322. struct PluginMidiProgramData {
  323. uint32_t count;
  324. int32_t current;
  325. MidiProgramData* data;
  326. PluginMidiProgramData()
  327. : count(0),
  328. current(-1),
  329. data(nullptr) {}
  330. ~PluginMidiProgramData()
  331. {
  332. CARLA_ASSERT_INT(count == 0, count);
  333. CARLA_ASSERT_INT(current == -1, current);
  334. CARLA_ASSERT(data == nullptr);
  335. }
  336. void createNew(const uint32_t newCount)
  337. {
  338. CARLA_ASSERT_INT(count == 0, count);
  339. CARLA_ASSERT_INT(current == -1, current);
  340. CARLA_ASSERT(data == nullptr);
  341. CARLA_ASSERT_INT(newCount > 0, newCount);
  342. if (data != nullptr || newCount == 0)
  343. return;
  344. data = new MidiProgramData[newCount];
  345. count = newCount;
  346. }
  347. void clear()
  348. {
  349. if (data != nullptr)
  350. {
  351. for (uint32_t i=0; i < count; ++i)
  352. {
  353. if (data[i].name != nullptr)
  354. {
  355. delete[] data[i].name;
  356. data[i].name = nullptr;
  357. }
  358. }
  359. delete[] data;
  360. data = nullptr;
  361. }
  362. count = 0;
  363. current = -1;
  364. }
  365. const MidiProgramData& getCurrent() const
  366. {
  367. CARLA_ASSERT_INT2(current >= 0 && current < static_cast<int32_t>(count), current, count);
  368. return data[current];
  369. }
  370. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  371. };
  372. // -----------------------------------------------------------------------
  373. struct ExternalMidiNote {
  374. int8_t channel; // invalid == -1
  375. uint8_t note;
  376. uint8_t velo; // note-off if 0
  377. ExternalMidiNote()
  378. : channel(-1),
  379. note(0),
  380. velo(0) {}
  381. CARLA_DECLARE_NON_COPY_STRUCT(ExternalMidiNote)
  382. };
  383. // -----------------------------------------------------------------------
  384. class CarlaPluginGui;
  385. struct CarlaPluginProtectedData {
  386. CarlaEngine* const engine;
  387. CarlaEngineClient* client;
  388. CarlaPluginGui* gui;
  389. QByteArray guiGeometry;
  390. bool active;
  391. bool needsReset;
  392. void* lib;
  393. void* uiLib;
  394. // misc
  395. int8_t ctrlChannel;
  396. unsigned int extraHints;
  397. CarlaString idStr;
  398. // latency
  399. uint32_t latency;
  400. float** latencyBuffers;
  401. // data
  402. PluginAudioData audioIn;
  403. PluginAudioData audioOut;
  404. PluginEventData event;
  405. PluginParameterData param;
  406. PluginProgramData prog;
  407. PluginMidiProgramData midiprog;
  408. NonRtList<CustomData> custom;
  409. CarlaMutex masterMutex; // global master lock
  410. CarlaMutex singleMutex; // small lock used only in processSingle()
  411. struct ExternalNotes {
  412. CarlaMutex mutex;
  413. RtList<ExternalMidiNote>::Pool dataPool;
  414. RtList<ExternalMidiNote> data;
  415. ExternalNotes()
  416. : dataPool(32, 152),
  417. data(dataPool) {}
  418. ~ExternalNotes()
  419. {
  420. mutex.lock();
  421. data.clear();
  422. mutex.unlock();
  423. }
  424. void append(const ExternalMidiNote& note)
  425. {
  426. mutex.lock();
  427. data.append_sleepy(note);
  428. mutex.unlock();
  429. }
  430. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalNotes)
  431. } extNotes;
  432. struct PostRtEvents {
  433. CarlaMutex mutex;
  434. RtList<PluginPostRtEvent>::Pool dataPool;
  435. RtList<PluginPostRtEvent> data;
  436. RtList<PluginPostRtEvent> dataPendingRT;
  437. PostRtEvents()
  438. : dataPool(128, 128),
  439. data(dataPool),
  440. dataPendingRT(dataPool) {}
  441. ~PostRtEvents()
  442. {
  443. clear();
  444. }
  445. void appendRT(const PluginPostRtEvent& event)
  446. {
  447. dataPendingRT.append(event);
  448. }
  449. void trySplice()
  450. {
  451. if (mutex.tryLock())
  452. {
  453. dataPendingRT.spliceAppend(data);
  454. mutex.unlock();
  455. }
  456. }
  457. void clear()
  458. {
  459. mutex.lock();
  460. data.clear();
  461. dataPendingRT.clear();
  462. mutex.unlock();
  463. }
  464. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostRtEvents)
  465. } postRtEvents;
  466. #ifndef BUILD_BRIDGE
  467. struct PostProc {
  468. float dryWet;
  469. float volume;
  470. float balanceLeft;
  471. float balanceRight;
  472. float panning;
  473. PostProc()
  474. : dryWet(1.0f),
  475. volume(1.0f),
  476. balanceLeft(-1.0f),
  477. balanceRight(1.0f),
  478. panning(0.0f) {}
  479. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostProc)
  480. } postProc;
  481. #endif
  482. struct OSC {
  483. CarlaOscData data;
  484. CarlaPluginThread thread;
  485. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  486. : thread(engine, plugin) {}
  487. #ifdef CARLA_PROPER_CPP11_SUPPORT
  488. OSC() = delete;
  489. OSC(OSC&) = delete;
  490. OSC(const OSC&) = delete;
  491. #endif
  492. CARLA_LEAK_DETECTOR(OSC)
  493. } osc;
  494. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  495. : engine(engine_),
  496. client(nullptr),
  497. gui(nullptr),
  498. active(false),
  499. needsReset(false),
  500. lib(nullptr),
  501. uiLib(nullptr),
  502. ctrlChannel(0),
  503. extraHints(0x0),
  504. latency(0),
  505. latencyBuffers(nullptr),
  506. osc(engine, plugin) {}
  507. #ifdef CARLA_PROPER_CPP11_SUPPORT
  508. CarlaPluginProtectedData() = delete;
  509. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  510. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  511. #endif
  512. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  513. ~CarlaPluginProtectedData()
  514. {
  515. CARLA_ASSERT(gui == nullptr);
  516. CARLA_ASSERT(client == nullptr);
  517. CARLA_ASSERT(! active);
  518. CARLA_ASSERT(lib == nullptr);
  519. CARLA_ASSERT(uiLib == nullptr);
  520. CARLA_ASSERT(latency == 0);
  521. CARLA_ASSERT(latencyBuffers == nullptr);
  522. CARLA_SAFE_ASSERT(! needsReset);
  523. }
  524. // -------------------------------------------------------------------
  525. // Cleanup
  526. void cleanup()
  527. {
  528. {
  529. const bool lockMaster(masterMutex.tryLock());
  530. const bool lockSingle(singleMutex.tryLock());
  531. CARLA_ASSERT(! lockMaster);
  532. CARLA_ASSERT(! lockSingle);
  533. }
  534. if (client != nullptr)
  535. {
  536. CARLA_ASSERT(! client->isActive());
  537. if (client->isActive())
  538. client->deactivate();
  539. clearBuffers();
  540. delete client;
  541. client = nullptr;
  542. }
  543. for (NonRtList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  544. {
  545. CustomData& cData(*it);
  546. CARLA_ASSERT(cData.type != nullptr);
  547. CARLA_ASSERT(cData.key != nullptr);
  548. CARLA_ASSERT(cData.value != nullptr);
  549. if (cData.type != nullptr)
  550. {
  551. delete[] cData.type;
  552. cData.type = nullptr;
  553. }
  554. if (cData.key != nullptr)
  555. {
  556. delete[] cData.key;
  557. cData.key = nullptr;
  558. }
  559. if (cData.value != nullptr)
  560. {
  561. delete[] cData.value;
  562. cData.value = nullptr;
  563. }
  564. }
  565. prog.clear();
  566. midiprog.clear();
  567. custom.clear();
  568. // MUST have been locked before
  569. masterMutex.unlock();
  570. singleMutex.unlock();
  571. if (lib != nullptr)
  572. libClose();
  573. }
  574. // -------------------------------------------------------------------
  575. // Buffer functions
  576. void clearBuffers()
  577. {
  578. if (latencyBuffers != nullptr)
  579. {
  580. for (uint32_t i=0; i < audioIn.count; ++i)
  581. {
  582. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  583. if (latencyBuffers[i] != nullptr)
  584. {
  585. delete[] latencyBuffers[i];
  586. latencyBuffers[i] = nullptr;
  587. }
  588. }
  589. delete[] latencyBuffers;
  590. latencyBuffers = nullptr;
  591. latency = 0;
  592. }
  593. audioIn.clear();
  594. audioOut.clear();
  595. param.clear();
  596. event.clear();
  597. }
  598. void recreateLatencyBuffers()
  599. {
  600. if (latencyBuffers != nullptr)
  601. {
  602. for (uint32_t i=0; i < audioIn.count; ++i)
  603. {
  604. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  605. if (latencyBuffers[i] != nullptr)
  606. {
  607. delete[] latencyBuffers[i];
  608. latencyBuffers[i] = nullptr;
  609. }
  610. }
  611. delete[] latencyBuffers;
  612. latencyBuffers = nullptr;
  613. }
  614. if (audioIn.count > 0 && latency > 0)
  615. {
  616. latencyBuffers = new float*[audioIn.count];
  617. for (uint32_t i=0; i < audioIn.count; ++i)
  618. {
  619. latencyBuffers[i] = new float[latency];
  620. carla_zeroFloat(latencyBuffers[i], latency);
  621. }
  622. }
  623. }
  624. // -------------------------------------------------------------------
  625. // Post-poned events
  626. void postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  627. {
  628. PluginPostRtEvent event;
  629. event.type = type;
  630. event.value1 = value1;
  631. event.value2 = value2;
  632. event.value3 = value3;
  633. postRtEvents.appendRT(event);
  634. }
  635. // -------------------------------------------------------------------
  636. // Library functions, see CarlaPlugin.cpp
  637. bool libOpen(const char* const filename);
  638. bool libClose();
  639. void* libSymbol(const char* const symbol);
  640. bool uiLibOpen(const char* const filename);
  641. bool uiLibClose();
  642. void* uiLibSymbol(const char* const symbol);
  643. const char* libError(const char* const filename);
  644. // -------------------------------------------------------------------
  645. // Settings functions, see CarlaPlugin.cpp
  646. void saveSetting(const unsigned int option, const bool yesNo);
  647. unsigned int loadSettings(const unsigned int options, const unsigned int availOptions);
  648. };
  649. CARLA_BACKEND_END_NAMESPACE
  650. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED