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.

813 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. // #include <QtCore/QByteArray>
  28. #define CARLA_PROCESS_CONTINUE_CHECK if (! fEnabled) { pData->engine->callback(CALLBACK_DEBUG, fId, 0, 0, 0.0f, "Processing while plugin is disabled!!"); return; }
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -----------------------------------------------------------------------
  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. /*!
  37. * Post-RT event type.\n
  38. * These are events postponned from within the process function,
  39. *
  40. * During process, we cannot lock, allocate memory or do UI stuff,\n
  41. * so events have to be postponned to be executed later, on a separate thread.
  42. */
  43. enum PluginPostRtEventType {
  44. kPluginPostRtEventNull,
  45. kPluginPostRtEventDebug,
  46. kPluginPostRtEventParameterChange, // param, SP*, value (SP: if 1, don't report change to Callback and OSC)
  47. kPluginPostRtEventProgramChange, // index
  48. kPluginPostRtEventMidiProgramChange, // index
  49. kPluginPostRtEventNoteOn, // channel, note, velo
  50. kPluginPostRtEventNoteOff // channel, note
  51. };
  52. /*!
  53. * A Post-RT event.
  54. * \see PluginPostRtEventType
  55. */
  56. struct PluginPostRtEvent {
  57. PluginPostRtEventType type;
  58. int32_t value1;
  59. int32_t value2;
  60. float value3;
  61. PluginPostRtEvent() noexcept
  62. : type(kPluginPostRtEventNull),
  63. value1(-1),
  64. value2(-1),
  65. value3(0.0f) {}
  66. };
  67. // -----------------------------------------------------------------------
  68. struct PluginAudioPort {
  69. uint32_t rindex;
  70. CarlaEngineAudioPort* port;
  71. PluginAudioPort()
  72. : rindex(0),
  73. port(nullptr) {}
  74. ~PluginAudioPort()
  75. {
  76. CARLA_ASSERT(port == nullptr);
  77. }
  78. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioPort)
  79. };
  80. struct PluginAudioData {
  81. uint32_t count;
  82. PluginAudioPort* ports;
  83. PluginAudioData()
  84. : count(0),
  85. ports(nullptr) {}
  86. ~PluginAudioData()
  87. {
  88. CARLA_ASSERT_INT(count == 0, count);
  89. CARLA_ASSERT(ports == nullptr);
  90. }
  91. void createNew(const uint32_t newCount)
  92. {
  93. CARLA_ASSERT_INT(count == 0, count);
  94. CARLA_ASSERT(ports == nullptr);
  95. CARLA_ASSERT_INT(newCount > 0, newCount);
  96. if (ports != nullptr || newCount == 0)
  97. return;
  98. ports = new PluginAudioPort[newCount];
  99. count = newCount;
  100. }
  101. void clear()
  102. {
  103. if (ports != nullptr)
  104. {
  105. for (uint32_t i=0; i < count; ++i)
  106. {
  107. if (ports[i].port != nullptr)
  108. {
  109. delete ports[i].port;
  110. ports[i].port = nullptr;
  111. }
  112. }
  113. delete[] ports;
  114. ports = nullptr;
  115. }
  116. count = 0;
  117. }
  118. void initBuffers()
  119. {
  120. for (uint32_t i=0; i < count; ++i)
  121. {
  122. if (ports[i].port != nullptr)
  123. ports[i].port->initBuffer();
  124. }
  125. }
  126. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioData)
  127. };
  128. // -----------------------------------------------------------------------
  129. struct PluginCVPort {
  130. uint32_t rindex;
  131. uint32_t param;
  132. CarlaEngineCVPort* port;
  133. PluginCVPort()
  134. : rindex(0),
  135. param(0),
  136. port(nullptr) {}
  137. ~PluginCVPort()
  138. {
  139. CARLA_ASSERT(port == nullptr);
  140. }
  141. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVPort)
  142. };
  143. struct PluginCVData {
  144. uint32_t count;
  145. PluginCVPort* ports;
  146. PluginCVData()
  147. : count(0),
  148. ports(nullptr) {}
  149. ~PluginCVData()
  150. {
  151. CARLA_ASSERT_INT(count == 0, count);
  152. CARLA_ASSERT(ports == nullptr);
  153. }
  154. void createNew(const uint32_t newCount)
  155. {
  156. CARLA_ASSERT_INT(count == 0, count);
  157. CARLA_ASSERT(ports == nullptr);
  158. CARLA_ASSERT_INT(newCount > 0, newCount);
  159. if (ports != nullptr || newCount == 0)
  160. return;
  161. ports = new PluginCVPort[newCount];
  162. count = newCount;
  163. }
  164. void clear()
  165. {
  166. if (ports != nullptr)
  167. {
  168. for (uint32_t i=0; i < count; ++i)
  169. {
  170. if (ports[i].port != nullptr)
  171. {
  172. delete ports[i].port;
  173. ports[i].port = nullptr;
  174. }
  175. }
  176. delete[] ports;
  177. ports = nullptr;
  178. }
  179. count = 0;
  180. }
  181. void initBuffers()
  182. {
  183. for (uint32_t i=0; i < count; ++i)
  184. {
  185. if (ports[i].port != nullptr)
  186. ports[i].port->initBuffer();
  187. }
  188. }
  189. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVData)
  190. };
  191. // -----------------------------------------------------------------------
  192. struct PluginEventData {
  193. CarlaEngineEventPort* portIn;
  194. CarlaEngineEventPort* portOut;
  195. PluginEventData()
  196. : portIn(nullptr),
  197. portOut(nullptr) {}
  198. ~PluginEventData()
  199. {
  200. CARLA_ASSERT(portIn == nullptr);
  201. CARLA_ASSERT(portOut == nullptr);
  202. }
  203. void clear()
  204. {
  205. if (portIn != nullptr)
  206. {
  207. delete portIn;
  208. portIn = nullptr;
  209. }
  210. if (portOut != nullptr)
  211. {
  212. delete portOut;
  213. portOut = nullptr;
  214. }
  215. }
  216. void initBuffers()
  217. {
  218. if (portIn != nullptr)
  219. portIn->initBuffer();
  220. if (portOut != nullptr)
  221. portOut->initBuffer();
  222. }
  223. CARLA_DECLARE_NON_COPY_STRUCT(PluginEventData)
  224. };
  225. // -----------------------------------------------------------------------
  226. struct PluginParameterData {
  227. uint32_t count;
  228. ParameterData* data;
  229. ParameterRanges* ranges;
  230. PluginParameterData()
  231. : count(0),
  232. data(nullptr),
  233. ranges(nullptr) {}
  234. ~PluginParameterData()
  235. {
  236. CARLA_ASSERT_INT(count == 0, count);
  237. CARLA_ASSERT(data == nullptr);
  238. CARLA_ASSERT(ranges == nullptr);
  239. }
  240. void createNew(const uint32_t newCount)
  241. {
  242. CARLA_ASSERT_INT(count == 0, count);
  243. CARLA_ASSERT(data == nullptr);
  244. CARLA_ASSERT(ranges == nullptr);
  245. CARLA_ASSERT_INT(newCount > 0, newCount);
  246. if (data != nullptr || ranges != nullptr || newCount == 0)
  247. return;
  248. data = new ParameterData[newCount];
  249. ranges = new ParameterRanges[newCount];
  250. count = newCount;
  251. }
  252. void clear()
  253. {
  254. if (data != nullptr)
  255. {
  256. delete[] data;
  257. data = nullptr;
  258. }
  259. if (ranges != nullptr)
  260. {
  261. delete[] ranges;
  262. ranges = nullptr;
  263. }
  264. count = 0;
  265. }
  266. float getFixedValue(const uint32_t parameterId, const float& value) const
  267. {
  268. CARLA_SAFE_ASSERT_RETURN(parameterId < count, 0.0f);
  269. return ranges[parameterId].getFixedValue(value);
  270. }
  271. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  272. };
  273. // -----------------------------------------------------------------------
  274. typedef const char* ProgramName;
  275. struct PluginProgramData {
  276. uint32_t count;
  277. int32_t current;
  278. ProgramName* names;
  279. PluginProgramData()
  280. : count(0),
  281. current(-1),
  282. names(nullptr) {}
  283. ~PluginProgramData()
  284. {
  285. CARLA_ASSERT_INT(count == 0, count);
  286. CARLA_ASSERT_INT(current == -1, current);
  287. CARLA_ASSERT(names == nullptr);
  288. }
  289. void createNew(const uint32_t newCount)
  290. {
  291. CARLA_ASSERT_INT(count == 0, count);
  292. CARLA_ASSERT_INT(current == -1, current);
  293. CARLA_ASSERT(names == nullptr);
  294. CARLA_ASSERT_INT(newCount > 0, newCount);
  295. if (names != nullptr || newCount == 0)
  296. return;
  297. names = new ProgramName[newCount];
  298. count = newCount;
  299. for (uint32_t i=0; i < newCount; ++i)
  300. names[i] = nullptr;
  301. }
  302. void clear()
  303. {
  304. if (names != nullptr)
  305. {
  306. for (uint32_t i=0; i < count; ++i)
  307. {
  308. if (names[i] != nullptr)
  309. {
  310. delete[] names[i];
  311. names[i] = nullptr;
  312. }
  313. }
  314. delete[] names;
  315. names = nullptr;
  316. }
  317. count = 0;
  318. current = -1;
  319. }
  320. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  321. };
  322. // -----------------------------------------------------------------------
  323. struct PluginMidiProgramData {
  324. uint32_t count;
  325. int32_t current;
  326. MidiProgramData* data;
  327. PluginMidiProgramData()
  328. : count(0),
  329. current(-1),
  330. data(nullptr) {}
  331. ~PluginMidiProgramData()
  332. {
  333. CARLA_ASSERT_INT(count == 0, count);
  334. CARLA_ASSERT_INT(current == -1, current);
  335. CARLA_ASSERT(data == nullptr);
  336. }
  337. void createNew(const uint32_t newCount)
  338. {
  339. CARLA_ASSERT_INT(count == 0, count);
  340. CARLA_ASSERT_INT(current == -1, current);
  341. CARLA_ASSERT(data == nullptr);
  342. CARLA_ASSERT_INT(newCount > 0, newCount);
  343. if (data != nullptr || newCount == 0)
  344. return;
  345. data = new MidiProgramData[newCount];
  346. count = newCount;
  347. }
  348. void clear()
  349. {
  350. if (data != nullptr)
  351. {
  352. for (uint32_t i=0; i < count; ++i)
  353. {
  354. if (data[i].name != nullptr)
  355. {
  356. delete[] data[i].name;
  357. data[i].name = nullptr;
  358. }
  359. }
  360. delete[] data;
  361. data = nullptr;
  362. }
  363. count = 0;
  364. current = -1;
  365. }
  366. const MidiProgramData& getCurrent() const
  367. {
  368. CARLA_ASSERT_INT2(current >= 0 && current < static_cast<int32_t>(count), current, count);
  369. return data[current];
  370. }
  371. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  372. };
  373. // -----------------------------------------------------------------------
  374. struct ExternalMidiNote {
  375. int8_t channel; // invalid == -1
  376. uint8_t note;
  377. uint8_t velo; // note-off if 0
  378. ExternalMidiNote()
  379. : channel(-1),
  380. note(0),
  381. velo(0) {}
  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(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(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(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