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.

939 lines
23KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2019 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaLibCounter.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -------------------------------------------------------------------
  23. // Fallback data
  24. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  25. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  26. // -----------------------------------------------------------------------
  27. // PluginAudioData
  28. PluginAudioData::PluginAudioData() noexcept
  29. : count(0),
  30. ports(nullptr) {}
  31. PluginAudioData::~PluginAudioData() noexcept
  32. {
  33. CARLA_SAFE_ASSERT_INT(count == 0, count);
  34. CARLA_SAFE_ASSERT(ports == nullptr);
  35. }
  36. void PluginAudioData::createNew(const uint32_t newCount)
  37. {
  38. CARLA_SAFE_ASSERT_INT(count == 0, count);
  39. CARLA_SAFE_ASSERT_RETURN(ports == nullptr,);
  40. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  41. ports = new PluginAudioPort[newCount];
  42. carla_zeroStructs(ports, newCount);
  43. count = newCount;
  44. }
  45. void PluginAudioData::clear() noexcept
  46. {
  47. if (ports != nullptr)
  48. {
  49. for (uint32_t i=0; i < count; ++i)
  50. {
  51. if (ports[i].port != nullptr)
  52. {
  53. delete ports[i].port;
  54. ports[i].port = nullptr;
  55. }
  56. }
  57. delete[] ports;
  58. ports = nullptr;
  59. }
  60. count = 0;
  61. }
  62. void PluginAudioData::initBuffers() const noexcept
  63. {
  64. for (uint32_t i=0; i < count; ++i)
  65. {
  66. if (ports[i].port != nullptr)
  67. ports[i].port->initBuffer();
  68. }
  69. }
  70. // -----------------------------------------------------------------------
  71. // PluginCVData
  72. PluginCVData::PluginCVData() noexcept
  73. : count(0),
  74. ports(nullptr) {}
  75. PluginCVData::~PluginCVData() noexcept
  76. {
  77. CARLA_SAFE_ASSERT_INT(count == 0, count);
  78. CARLA_SAFE_ASSERT(ports == nullptr);
  79. }
  80. void PluginCVData::createNew(const uint32_t newCount)
  81. {
  82. CARLA_SAFE_ASSERT_INT(count == 0, count);
  83. CARLA_SAFE_ASSERT_RETURN(ports == nullptr,);
  84. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  85. ports = new PluginCVPort[newCount];
  86. carla_zeroStructs(ports, newCount);
  87. count = newCount;
  88. }
  89. void PluginCVData::clear() noexcept
  90. {
  91. if (ports != nullptr)
  92. {
  93. for (uint32_t i=0; i < count; ++i)
  94. {
  95. if (ports[i].port != nullptr)
  96. {
  97. delete ports[i].port;
  98. ports[i].port = nullptr;
  99. }
  100. }
  101. delete[] ports;
  102. ports = nullptr;
  103. }
  104. count = 0;
  105. }
  106. void PluginCVData::initBuffers() const noexcept
  107. {
  108. for (uint32_t i=0; i < count; ++i)
  109. {
  110. if (ports[i].port != nullptr)
  111. ports[i].port->initBuffer();
  112. }
  113. }
  114. // -----------------------------------------------------------------------
  115. // PluginEventData
  116. PluginEventData::PluginEventData() noexcept
  117. : portIn(nullptr),
  118. portOut(nullptr)
  119. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  120. , cvSourcePorts(nullptr)
  121. #endif
  122. {
  123. }
  124. PluginEventData::~PluginEventData() noexcept
  125. {
  126. CARLA_SAFE_ASSERT(portIn == nullptr);
  127. CARLA_SAFE_ASSERT(portOut == nullptr);
  128. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  129. CARLA_SAFE_ASSERT(cvSourcePorts == nullptr);
  130. #endif
  131. }
  132. void PluginEventData::clear() noexcept
  133. {
  134. if (portIn != nullptr)
  135. {
  136. delete portIn;
  137. portIn = nullptr;
  138. }
  139. if (portOut != nullptr)
  140. {
  141. delete portOut;
  142. portOut = nullptr;
  143. }
  144. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  145. if (cvSourcePorts != nullptr)
  146. {
  147. cvSourcePorts->cleanup();
  148. cvSourcePorts = nullptr;
  149. }
  150. #endif
  151. }
  152. void PluginEventData::initBuffers() const noexcept
  153. {
  154. if (portIn != nullptr)
  155. portIn->initBuffer();
  156. if (portOut != nullptr)
  157. portOut->initBuffer();
  158. }
  159. // -----------------------------------------------------------------------
  160. // PluginParameterData
  161. PluginParameterData::PluginParameterData() noexcept
  162. : count(0),
  163. data(nullptr),
  164. ranges(nullptr),
  165. special(nullptr) {}
  166. PluginParameterData::~PluginParameterData() noexcept
  167. {
  168. CARLA_SAFE_ASSERT_INT(count == 0, count);
  169. CARLA_SAFE_ASSERT(data == nullptr);
  170. CARLA_SAFE_ASSERT(ranges == nullptr);
  171. CARLA_SAFE_ASSERT(special == nullptr);
  172. }
  173. void PluginParameterData::createNew(const uint32_t newCount, const bool withSpecial)
  174. {
  175. CARLA_SAFE_ASSERT_INT(count == 0, count);
  176. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  177. CARLA_SAFE_ASSERT_RETURN(ranges == nullptr,);
  178. CARLA_SAFE_ASSERT_RETURN(special == nullptr,);
  179. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  180. data = new ParameterData[newCount];
  181. carla_zeroStructs(data, newCount);
  182. for (uint32_t i=0; i < newCount; ++i)
  183. {
  184. data[i].index = PARAMETER_NULL;
  185. data[i].rindex = PARAMETER_NULL;
  186. data[i].mappedControlIndex = CONTROL_INDEX_NONE;
  187. data[i].mappedMinimum = -1.0f;
  188. data[i].mappedMaximum = 1.0f;
  189. }
  190. ranges = new ParameterRanges[newCount];
  191. carla_zeroStructs(ranges, newCount);
  192. if (withSpecial)
  193. {
  194. special = new SpecialParameterType[newCount];
  195. carla_zeroStructs(special, newCount);
  196. }
  197. count = newCount;
  198. }
  199. void PluginParameterData::clear() noexcept
  200. {
  201. if (data != nullptr)
  202. {
  203. delete[] data;
  204. data = nullptr;
  205. }
  206. if (ranges != nullptr)
  207. {
  208. delete[] ranges;
  209. ranges = nullptr;
  210. }
  211. if (special != nullptr)
  212. {
  213. delete[] special;
  214. special = nullptr;
  215. }
  216. count = 0;
  217. }
  218. float PluginParameterData::getFixedValue(const uint32_t parameterId, float value) const noexcept
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(parameterId < count, 0.0f);
  221. const uint paramHints (data[parameterId].hints);
  222. const ParameterRanges& paramRanges(ranges[parameterId]);
  223. // if boolean, return either min or max
  224. if (paramHints & PARAMETER_IS_BOOLEAN)
  225. {
  226. const float middlePoint = paramRanges.min + (paramRanges.max-paramRanges.min)/2.0f;
  227. return value >= middlePoint ? paramRanges.max : paramRanges.min;
  228. }
  229. // if integer, round first
  230. if (paramHints & PARAMETER_IS_INTEGER)
  231. return paramRanges.getFixedValue(std::round(value));
  232. // normal mode
  233. return paramRanges.getFixedValue(value);
  234. }
  235. // copied from ParameterRanges::getUnnormalizedValue
  236. static float _getUnnormalizedValue(const float min, const float max, const float value) noexcept
  237. {
  238. if (value <= 0.0f)
  239. return min;
  240. if (value >= 1.0f)
  241. return max;
  242. return value * (max - min) + min;
  243. }
  244. // copied from ParameterRanges::getUnnormalizedLogValue
  245. static float _getUnnormalizedLogValue(const float min, const float max, const float value) noexcept
  246. {
  247. if (value <= 0.0f)
  248. return min;
  249. if (value >= 1.0f)
  250. return max;
  251. float rmin = min;
  252. if (std::abs(min) < std::numeric_limits<float>::epsilon())
  253. rmin = 0.00001f;
  254. return rmin * std::pow(max/rmin, value);
  255. }
  256. float PluginParameterData::getFinalUnnormalizedValue(const uint32_t parameterId, float value) const noexcept
  257. {
  258. float min, max;
  259. if (data[parameterId].mappedControlIndex != CONTROL_INDEX_CV
  260. && (data[parameterId].hints & PARAMETER_MAPPED_RANGES_SET) != 0x0)
  261. {
  262. min = data[parameterId].mappedMinimum;
  263. max = data[parameterId].mappedMaximum;
  264. }
  265. else
  266. {
  267. min = ranges[parameterId].min;
  268. max = ranges[parameterId].max;
  269. }
  270. if (data[parameterId].hints & PARAMETER_IS_BOOLEAN)
  271. {
  272. value = (value < 0.5f) ? min : max;
  273. }
  274. else
  275. {
  276. if (data[parameterId].hints & PARAMETER_IS_LOGARITHMIC)
  277. value = _getUnnormalizedLogValue(min, max, value);
  278. else
  279. value = _getUnnormalizedValue(min, max, value);
  280. if (data[parameterId].hints & PARAMETER_IS_INTEGER)
  281. value = std::rint(value);
  282. }
  283. return value;
  284. }
  285. // -----------------------------------------------------------------------
  286. // PluginProgramData
  287. PluginProgramData::PluginProgramData() noexcept
  288. : count(0),
  289. current(-1),
  290. names(nullptr) {}
  291. PluginProgramData::~PluginProgramData() noexcept
  292. {
  293. CARLA_SAFE_ASSERT_INT(count == 0, count);
  294. CARLA_SAFE_ASSERT_INT(current == -1, current);
  295. CARLA_SAFE_ASSERT(names == nullptr);
  296. }
  297. void PluginProgramData::createNew(const uint32_t newCount)
  298. {
  299. CARLA_SAFE_ASSERT_INT(count == 0, count);
  300. CARLA_SAFE_ASSERT_INT(current == -1, current);
  301. CARLA_SAFE_ASSERT_RETURN(names == nullptr,);
  302. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  303. names = new ProgramName[newCount];
  304. carla_zeroStructs(names, newCount);
  305. count = newCount;
  306. current = -1;
  307. }
  308. void PluginProgramData::clear() noexcept
  309. {
  310. if (names != nullptr)
  311. {
  312. for (uint32_t i=0; i < count; ++i)
  313. {
  314. if (names[i] != nullptr)
  315. {
  316. delete[] names[i];
  317. names[i] = nullptr;
  318. }
  319. }
  320. delete[] names;
  321. names = nullptr;
  322. }
  323. count = 0;
  324. current = -1;
  325. }
  326. // -----------------------------------------------------------------------
  327. // PluginMidiProgramData
  328. PluginMidiProgramData::PluginMidiProgramData() noexcept
  329. : count(0),
  330. current(-1),
  331. data(nullptr) {}
  332. PluginMidiProgramData::~PluginMidiProgramData() noexcept
  333. {
  334. CARLA_SAFE_ASSERT_INT(count == 0, count);
  335. CARLA_SAFE_ASSERT_INT(current == -1, current);
  336. CARLA_SAFE_ASSERT(data == nullptr);
  337. }
  338. void PluginMidiProgramData::createNew(const uint32_t newCount)
  339. {
  340. CARLA_SAFE_ASSERT_INT(count == 0, count);
  341. CARLA_SAFE_ASSERT_INT(current == -1, current);
  342. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  343. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  344. data = new MidiProgramData[newCount];
  345. carla_zeroStructs(data, newCount);
  346. count = newCount;
  347. current = -1;
  348. }
  349. void PluginMidiProgramData::clear() noexcept
  350. {
  351. if (data != nullptr)
  352. {
  353. for (uint32_t i=0; i < count; ++i)
  354. {
  355. if (data[i].name != nullptr)
  356. {
  357. delete[] data[i].name;
  358. data[i].name = nullptr;
  359. }
  360. }
  361. delete[] data;
  362. data = nullptr;
  363. }
  364. count = 0;
  365. current = -1;
  366. }
  367. const MidiProgramData& PluginMidiProgramData::getCurrent() const noexcept
  368. {
  369. CARLA_SAFE_ASSERT_RETURN(current >= 0 && current < static_cast<int32_t>(count), kMidiProgramDataNull);
  370. return data[current];
  371. }
  372. // -----------------------------------------------------------------------
  373. // ProtectedData::ExternalNotes
  374. CarlaPlugin::ProtectedData::ExternalNotes::ExternalNotes() noexcept
  375. : mutex(),
  376. dataPool(32, 152),
  377. data(dataPool) {}
  378. CarlaPlugin::ProtectedData::ExternalNotes::~ExternalNotes() noexcept
  379. {
  380. clear();
  381. }
  382. void CarlaPlugin::ProtectedData::ExternalNotes::appendNonRT(const ExternalMidiNote& note) noexcept
  383. {
  384. mutex.lock();
  385. data.append_sleepy(note);
  386. mutex.unlock();
  387. }
  388. void CarlaPlugin::ProtectedData::ExternalNotes::clear() noexcept
  389. {
  390. mutex.lock();
  391. data.clear();
  392. mutex.unlock();
  393. }
  394. // -----------------------------------------------------------------------
  395. // ProtectedData::Latency
  396. CarlaPlugin::ProtectedData::Latency::Latency() noexcept
  397. #ifdef BUILD_BRIDGE
  398. : frames(0) {}
  399. #else
  400. : frames(0),
  401. channels(0),
  402. buffers(nullptr) {}
  403. #endif
  404. #ifndef BUILD_BRIDGE
  405. CarlaPlugin::ProtectedData::Latency::~Latency() noexcept
  406. {
  407. clearBuffers();
  408. }
  409. void CarlaPlugin::ProtectedData::Latency::clearBuffers() noexcept
  410. {
  411. if (buffers != nullptr)
  412. {
  413. for (uint32_t i=0; i < channels; ++i)
  414. {
  415. CARLA_SAFE_ASSERT_CONTINUE(buffers[i] != nullptr);
  416. delete[] buffers[i];
  417. buffers[i] = nullptr;
  418. }
  419. delete[] buffers;
  420. buffers = nullptr;
  421. }
  422. channels = 0;
  423. frames = 0;
  424. }
  425. void CarlaPlugin::ProtectedData::Latency::recreateBuffers(const uint32_t newChannels, const uint32_t newFrames)
  426. {
  427. CARLA_SAFE_ASSERT_RETURN(channels != newChannels || frames != newFrames,);
  428. const bool retrieveOldBuffer = (channels == newChannels && channels > 0 && frames > 0 && newFrames > 0);
  429. float** const oldBuffers = buffers;
  430. const uint32_t oldFrames = frames;
  431. channels = newChannels;
  432. frames = newFrames;
  433. if (channels > 0 && frames > 0)
  434. {
  435. buffers = new float*[channels];
  436. for (uint32_t i=0; i < channels; ++i)
  437. {
  438. buffers[i] = new float[frames];
  439. if (retrieveOldBuffer)
  440. {
  441. if (oldFrames > frames)
  442. {
  443. const uint32_t diff = oldFrames - frames;
  444. carla_copyFloats(buffers[i], oldBuffers[i] + diff, frames);
  445. }
  446. else
  447. {
  448. const uint32_t diff = frames - oldFrames;
  449. carla_zeroFloats(buffers[i], diff);
  450. carla_copyFloats(buffers[i] + diff, oldBuffers[i], oldFrames);
  451. }
  452. }
  453. else
  454. {
  455. carla_zeroFloats(buffers[i], frames);
  456. }
  457. }
  458. }
  459. else
  460. {
  461. buffers = nullptr;
  462. }
  463. // delete old buffer
  464. if (oldBuffers != nullptr)
  465. {
  466. for (uint32_t i=0; i < channels; ++i)
  467. {
  468. CARLA_SAFE_ASSERT_CONTINUE(oldBuffers[i] != nullptr);
  469. delete[] oldBuffers[i];
  470. oldBuffers[i] = nullptr;
  471. }
  472. delete[] oldBuffers;
  473. }
  474. }
  475. #endif
  476. // -----------------------------------------------------------------------
  477. // ProtectedData::PostRtEvents
  478. CarlaPlugin::ProtectedData::PostRtEvents::PostRtEvents() noexcept
  479. : dataPool(128, 128),
  480. dataPendingRT(dataPool),
  481. data(dataPool),
  482. dataMutex(),
  483. dataPendingMutex() {}
  484. CarlaPlugin::ProtectedData::PostRtEvents::~PostRtEvents() noexcept
  485. {
  486. dataMutex.lock();
  487. data.clear();
  488. dataMutex.unlock();
  489. dataPendingMutex.lock();
  490. dataPendingRT.clear();
  491. dataPendingMutex.unlock();
  492. }
  493. void CarlaPlugin::ProtectedData::PostRtEvents::appendRT(const PluginPostRtEvent& e) noexcept
  494. {
  495. CARLA_SAFE_ASSERT_INT2_RETURN(dataPendingMutex.tryLock(), e.type, e.value1,);
  496. dataPendingRT.append(e);
  497. dataPendingMutex.unlock();
  498. }
  499. void CarlaPlugin::ProtectedData::PostRtEvents::trySplice() noexcept
  500. {
  501. const CarlaMutexTryLocker cmtl(dataPendingMutex);
  502. if (cmtl.wasLocked() && dataPendingRT.count() > 0 && dataMutex.tryLock())
  503. {
  504. dataPendingRT.moveTo(data, true);
  505. dataMutex.unlock();
  506. }
  507. }
  508. void CarlaPlugin::ProtectedData::PostRtEvents::clearData() noexcept
  509. {
  510. const bool tryLockOk(dataMutex.tryLock());
  511. CARLA_SAFE_ASSERT(! tryLockOk);
  512. data.clear();
  513. if (tryLockOk)
  514. dataMutex.unlock();
  515. }
  516. // -----------------------------------------------------------------------
  517. // ProtectedData::PostUiEvents
  518. CarlaPlugin::ProtectedData::PostUiEvents::PostUiEvents() noexcept
  519. : mutex(),
  520. data() {}
  521. CarlaPlugin::ProtectedData::PostUiEvents::~PostUiEvents() noexcept
  522. {
  523. clear();
  524. }
  525. void CarlaPlugin::ProtectedData::PostUiEvents::append(const PluginPostRtEvent& e) noexcept
  526. {
  527. mutex.lock();
  528. data.append(e);
  529. mutex.unlock();
  530. }
  531. void CarlaPlugin::ProtectedData::PostUiEvents::clear() noexcept
  532. {
  533. mutex.lock();
  534. data.clear();
  535. mutex.unlock();
  536. }
  537. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  538. // -----------------------------------------------------------------------
  539. // ProtectedData::PostProc
  540. CarlaPlugin::ProtectedData::PostProc::PostProc() noexcept
  541. : dryWet(1.0f),
  542. volume(1.0f),
  543. balanceLeft(-1.0f),
  544. balanceRight(1.0f),
  545. panning(0.0f) {}
  546. #endif
  547. // -----------------------------------------------------------------------
  548. CarlaPlugin::ProtectedData::ProtectedData(CarlaEngine* const eng, const uint idx) noexcept
  549. : engine(eng),
  550. client(nullptr),
  551. id(idx),
  552. hints(0x0),
  553. options(0x0),
  554. nodeId(0),
  555. active(false),
  556. enabled(false),
  557. needsReset(false),
  558. engineBridged(eng->getType() == kEngineTypeBridge),
  559. enginePlugin(eng->getType() == kEngineTypePlugin),
  560. lib(nullptr),
  561. uiLib(nullptr),
  562. ctrlChannel(0),
  563. extraHints(0x0),
  564. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  565. transientTryCounter(0),
  566. transientFirstTry(true),
  567. #endif
  568. name(nullptr),
  569. filename(nullptr),
  570. iconName(nullptr),
  571. audioIn(),
  572. audioOut(),
  573. cvIn(),
  574. cvOut(),
  575. event(),
  576. param(),
  577. prog(),
  578. midiprog(),
  579. custom(),
  580. masterMutex(),
  581. singleMutex(),
  582. stateSave(),
  583. extNotes(),
  584. latency(),
  585. postRtEvents(),
  586. postUiEvents()
  587. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  588. , postProc()
  589. #endif
  590. {}
  591. CarlaPlugin::ProtectedData::~ProtectedData() noexcept
  592. {
  593. CARLA_SAFE_ASSERT(! (active && needsReset));
  594. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  595. CARLA_SAFE_ASSERT(transientTryCounter == 0);
  596. #endif
  597. {
  598. // mutex MUST have been locked before
  599. const bool lockMaster(masterMutex.tryLock());
  600. const bool lockSingle(singleMutex.tryLock());
  601. CARLA_SAFE_ASSERT(! lockMaster);
  602. CARLA_SAFE_ASSERT(! lockSingle);
  603. }
  604. if (client != nullptr)
  605. {
  606. if (client->isActive())
  607. {
  608. // must not happen
  609. carla_safe_assert("client->isActive()", __FILE__, __LINE__);
  610. client->deactivate();
  611. }
  612. clearBuffers();
  613. delete client;
  614. client = nullptr;
  615. }
  616. if (name != nullptr)
  617. {
  618. delete[] name;
  619. name = nullptr;
  620. }
  621. if (filename != nullptr)
  622. {
  623. delete[] filename;
  624. filename = nullptr;
  625. }
  626. if (iconName != nullptr)
  627. {
  628. delete[] iconName;
  629. iconName = nullptr;
  630. }
  631. for (LinkedList<CustomData>::Itenerator it = custom.begin2(); it.valid(); it.next())
  632. {
  633. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  634. //CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  635. if (customData.type != nullptr)
  636. {
  637. delete[] customData.type;
  638. customData.type = nullptr;
  639. }
  640. else
  641. carla_safe_assert("customData.type != nullptr", __FILE__, __LINE__);
  642. if (customData.key != nullptr)
  643. {
  644. delete[] customData.key;
  645. customData.key = nullptr;
  646. }
  647. else
  648. carla_safe_assert("customData.key != nullptr", __FILE__, __LINE__);
  649. if (customData.value != nullptr)
  650. {
  651. delete[] customData.value;
  652. customData.value = nullptr;
  653. }
  654. else
  655. carla_safe_assert("customData.value != nullptr", __FILE__, __LINE__);
  656. }
  657. prog.clear();
  658. midiprog.clear();
  659. custom.clear();
  660. // MUST have been locked before
  661. masterMutex.unlock();
  662. singleMutex.unlock();
  663. CARLA_SAFE_ASSERT(uiLib == nullptr);
  664. if (lib != nullptr)
  665. libClose();
  666. }
  667. // -----------------------------------------------------------------------
  668. // Buffer functions
  669. void CarlaPlugin::ProtectedData::clearBuffers() noexcept
  670. {
  671. audioIn.clear();
  672. audioOut.clear();
  673. cvIn.clear();
  674. cvOut.clear();
  675. param.clear();
  676. event.clear();
  677. #ifndef BUILD_BRIDGE
  678. latency.clearBuffers();
  679. #endif
  680. }
  681. // -----------------------------------------------------------------------
  682. // Post-poned events
  683. void CarlaPlugin::ProtectedData::postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept
  684. {
  685. CARLA_SAFE_ASSERT_RETURN(rtEvent.type != kPluginPostRtEventNull,);
  686. postRtEvents.appendRT(rtEvent);
  687. }
  688. void CarlaPlugin::ProtectedData::postponeRtEvent(const PluginPostRtEventType type,
  689. const bool sendCallbackLater,
  690. const int32_t value1,
  691. const int32_t value2,
  692. const int32_t value3,
  693. const float valuef) noexcept
  694. {
  695. CARLA_SAFE_ASSERT_RETURN(type != kPluginPostRtEventNull,);
  696. PluginPostRtEvent rtEvent = { type, sendCallbackLater, value1, value2, value3, valuef };
  697. postRtEvents.appendRT(rtEvent);
  698. }
  699. // -----------------------------------------------------------------------
  700. // Library functions
  701. static LibCounter sLibCounter;
  702. const char* CarlaPlugin::ProtectedData::libError(const char* const fname) noexcept
  703. {
  704. return lib_error(fname);
  705. }
  706. bool CarlaPlugin::ProtectedData::libOpen(const char* const fname) noexcept
  707. {
  708. lib = sLibCounter.open(fname);
  709. return (lib != nullptr);
  710. }
  711. bool CarlaPlugin::ProtectedData::libClose() noexcept
  712. {
  713. const bool ret = sLibCounter.close(lib);
  714. lib = nullptr;
  715. return ret;
  716. }
  717. void CarlaPlugin::ProtectedData::setCanDeleteLib(const bool canDelete) noexcept
  718. {
  719. sLibCounter.setCanDelete(lib, canDelete);
  720. }
  721. bool CarlaPlugin::ProtectedData::uiLibOpen(const char* const fname, const bool canDelete) noexcept
  722. {
  723. uiLib = sLibCounter.open(fname, canDelete);
  724. return (uiLib != nullptr);
  725. }
  726. bool CarlaPlugin::ProtectedData::uiLibClose() noexcept
  727. {
  728. const bool ret = sLibCounter.close(uiLib);
  729. uiLib = nullptr;
  730. return ret;
  731. }
  732. // -----------------------------------------------------------------------
  733. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  734. void CarlaPlugin::ProtectedData::tryTransient() noexcept
  735. {
  736. if (engine->getOptions().frontendWinId != 0)
  737. transientTryCounter = 1;
  738. }
  739. #endif
  740. void CarlaPlugin::ProtectedData::updateParameterValues(CarlaPlugin* const plugin,
  741. const bool sendCallback,
  742. const bool sendOsc,
  743. const bool useDefault) noexcept
  744. {
  745. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback || useDefault,);
  746. for (uint32_t i=0; i < param.count; ++i)
  747. {
  748. const float value(param.ranges[i].getFixedValue(plugin->getParameterValue(i)));
  749. if (useDefault)
  750. param.ranges[i].def = value;
  751. if (useDefault) {
  752. engine->callback(sendCallback, sendOsc,
  753. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  754. id,
  755. static_cast<int>(i),
  756. 0, 0,
  757. value,
  758. nullptr);
  759. }
  760. engine->callback(sendCallback, sendOsc,
  761. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  762. id,
  763. static_cast<int>(i),
  764. 0, 0,
  765. value,
  766. nullptr);
  767. }
  768. }
  769. void CarlaPlugin::ProtectedData::updateDefaultParameterValues(CarlaPlugin* const plugin) noexcept
  770. {
  771. for (uint32_t i=0; i < param.count; ++i)
  772. param.ranges[i].def = param.ranges[i].getFixedValue(plugin->getParameterValue(i));
  773. }
  774. // -----------------------------------------------------------------------
  775. CARLA_BACKEND_END_NAMESPACE