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.

CarlaPluginInternal.cpp 24KB

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