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.

743 lines
18KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2014 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 const CustomData kCustomDataFallback = { nullptr, nullptr, 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_zeroStruct(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_zeroStruct(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. PluginEventData::~PluginEventData() noexcept
  121. {
  122. CARLA_SAFE_ASSERT(portIn == nullptr);
  123. CARLA_SAFE_ASSERT(portOut == nullptr);
  124. }
  125. void PluginEventData::clear() noexcept
  126. {
  127. if (portIn != nullptr)
  128. {
  129. delete portIn;
  130. portIn = nullptr;
  131. }
  132. if (portOut != nullptr)
  133. {
  134. delete portOut;
  135. portOut = nullptr;
  136. }
  137. }
  138. void PluginEventData::initBuffers() const noexcept
  139. {
  140. if (portIn != nullptr)
  141. portIn->initBuffer();
  142. if (portOut != nullptr)
  143. portOut->initBuffer();
  144. }
  145. // -----------------------------------------------------------------------
  146. // PluginParameterData
  147. PluginParameterData::PluginParameterData() noexcept
  148. : count(0),
  149. data(nullptr),
  150. ranges(nullptr),
  151. special(nullptr) {}
  152. PluginParameterData::~PluginParameterData() noexcept
  153. {
  154. CARLA_SAFE_ASSERT_INT(count == 0, count);
  155. CARLA_SAFE_ASSERT(data == nullptr);
  156. CARLA_SAFE_ASSERT(ranges == nullptr);
  157. CARLA_SAFE_ASSERT(special == nullptr);
  158. }
  159. void PluginParameterData::createNew(const uint32_t newCount, const bool withSpecial)
  160. {
  161. CARLA_SAFE_ASSERT_INT(count == 0, count);
  162. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  163. CARLA_SAFE_ASSERT_RETURN(ranges == nullptr,);
  164. CARLA_SAFE_ASSERT_RETURN(special == nullptr,);
  165. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  166. data = new ParameterData[newCount];
  167. carla_zeroStruct(data, newCount);
  168. for (uint32_t i=0; i < newCount; ++i)
  169. {
  170. data[i].index = PARAMETER_NULL;
  171. data[i].rindex = PARAMETER_NULL;
  172. data[i].midiCC = -1;
  173. }
  174. ranges = new ParameterRanges[newCount];
  175. carla_zeroStruct(ranges, newCount);
  176. if (withSpecial)
  177. {
  178. special = new SpecialParameterType[newCount];
  179. carla_zeroStruct(special, newCount);
  180. }
  181. count = newCount;
  182. }
  183. void PluginParameterData::clear() noexcept
  184. {
  185. if (data != nullptr)
  186. {
  187. delete[] data;
  188. data = nullptr;
  189. }
  190. if (ranges != nullptr)
  191. {
  192. delete[] ranges;
  193. ranges = nullptr;
  194. }
  195. if (special != nullptr)
  196. {
  197. delete[] special;
  198. special = nullptr;
  199. }
  200. count = 0;
  201. }
  202. float PluginParameterData::getFixedValue(const uint32_t parameterId, const float& value) const noexcept
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(parameterId < count, 0.0f);
  205. return ranges[parameterId].getFixedValue(value);
  206. }
  207. // -----------------------------------------------------------------------
  208. // PluginProgramData
  209. PluginProgramData::PluginProgramData() noexcept
  210. : count(0),
  211. current(-1),
  212. names(nullptr) {}
  213. PluginProgramData::~PluginProgramData() noexcept
  214. {
  215. CARLA_SAFE_ASSERT_INT(count == 0, count);
  216. CARLA_SAFE_ASSERT_INT(current == -1, current);
  217. CARLA_SAFE_ASSERT(names == nullptr);
  218. }
  219. void PluginProgramData::createNew(const uint32_t newCount)
  220. {
  221. CARLA_SAFE_ASSERT_INT(count == 0, count);
  222. CARLA_SAFE_ASSERT_INT(current == -1, current);
  223. CARLA_SAFE_ASSERT_RETURN(names == nullptr,);
  224. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  225. names = new ProgramName[newCount];
  226. carla_zeroStruct(names, newCount);
  227. count = newCount;
  228. current = -1;
  229. }
  230. void PluginProgramData::clear() noexcept
  231. {
  232. if (names != nullptr)
  233. {
  234. for (uint32_t i=0; i < count; ++i)
  235. {
  236. if (names[i] != nullptr)
  237. {
  238. delete[] names[i];
  239. names[i] = nullptr;
  240. }
  241. }
  242. delete[] names;
  243. names = nullptr;
  244. }
  245. count = 0;
  246. current = -1;
  247. }
  248. // -----------------------------------------------------------------------
  249. // PluginMidiProgramData
  250. PluginMidiProgramData::PluginMidiProgramData() noexcept
  251. : count(0),
  252. current(-1),
  253. data(nullptr) {}
  254. PluginMidiProgramData::~PluginMidiProgramData() noexcept
  255. {
  256. CARLA_SAFE_ASSERT_INT(count == 0, count);
  257. CARLA_SAFE_ASSERT_INT(current == -1, current);
  258. CARLA_SAFE_ASSERT(data == nullptr);
  259. }
  260. void PluginMidiProgramData::createNew(const uint32_t newCount)
  261. {
  262. CARLA_SAFE_ASSERT_INT(count == 0, count);
  263. CARLA_SAFE_ASSERT_INT(current == -1, current);
  264. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  265. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  266. data = new MidiProgramData[newCount];
  267. carla_zeroStruct(data, newCount);
  268. count = newCount;
  269. current = -1;
  270. }
  271. void PluginMidiProgramData::clear() noexcept
  272. {
  273. if (data != nullptr)
  274. {
  275. for (uint32_t i=0; i < count; ++i)
  276. {
  277. if (data[i].name != nullptr)
  278. {
  279. delete[] data[i].name;
  280. data[i].name = nullptr;
  281. }
  282. }
  283. delete[] data;
  284. data = nullptr;
  285. }
  286. count = 0;
  287. current = -1;
  288. }
  289. const MidiProgramData& PluginMidiProgramData::getCurrent() const noexcept
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(current >= 0 && current < static_cast<int32_t>(count), kMidiProgramDataNull);
  292. return data[current];
  293. }
  294. // -----------------------------------------------------------------------
  295. // ProtectedData::ExternalNotes
  296. CarlaPlugin::ProtectedData::ExternalNotes::ExternalNotes() noexcept
  297. : mutex(),
  298. dataPool(32, 152),
  299. data(dataPool) {}
  300. CarlaPlugin::ProtectedData::ExternalNotes::~ExternalNotes() noexcept
  301. {
  302. clear();
  303. }
  304. void CarlaPlugin::ProtectedData::ExternalNotes::appendNonRT(const ExternalMidiNote& note) noexcept
  305. {
  306. mutex.lock();
  307. data.append_sleepy(note);
  308. mutex.unlock();
  309. }
  310. void CarlaPlugin::ProtectedData::ExternalNotes::clear() noexcept
  311. {
  312. mutex.lock();
  313. data.clear();
  314. mutex.unlock();
  315. }
  316. // -----------------------------------------------------------------------
  317. // ProtectedData::PostRtEvents
  318. CarlaPlugin::ProtectedData::PostRtEvents::PostRtEvents() noexcept
  319. : mutex(),
  320. dataPool(128, 128),
  321. data(dataPool),
  322. dataPendingRT(dataPool) {}
  323. CarlaPlugin::ProtectedData::PostRtEvents::~PostRtEvents() noexcept
  324. {
  325. clear();
  326. }
  327. void CarlaPlugin::ProtectedData::PostRtEvents::appendRT(const PluginPostRtEvent& e) noexcept
  328. {
  329. dataPendingRT.append(e);
  330. }
  331. void CarlaPlugin::ProtectedData::PostRtEvents::trySplice() noexcept
  332. {
  333. if (mutex.tryLock())
  334. {
  335. if (dataPendingRT.count() > 0)
  336. dataPendingRT.moveTo(data, true);
  337. mutex.unlock();
  338. }
  339. }
  340. void CarlaPlugin::ProtectedData::PostRtEvents::clear() noexcept
  341. {
  342. mutex.lock();
  343. data.clear();
  344. dataPendingRT.clear();
  345. mutex.unlock();
  346. }
  347. #ifndef BUILD_BRIDGE
  348. // -----------------------------------------------------------------------
  349. // ProtectedData::PostProc
  350. CarlaPlugin::ProtectedData::PostProc::PostProc() noexcept
  351. : dryWet(1.0f),
  352. volume(1.0f),
  353. balanceLeft(-1.0f),
  354. balanceRight(1.0f),
  355. panning(0.0f) {}
  356. #endif
  357. // -----------------------------------------------------------------------
  358. CarlaPlugin::ProtectedData::ProtectedData(CarlaEngine* const eng, const uint idx) noexcept
  359. : engine(eng),
  360. client(nullptr),
  361. id(idx),
  362. hints(0x0),
  363. options(0x0),
  364. nodeId(0),
  365. active(false),
  366. enabled(false),
  367. needsReset(false),
  368. lib(nullptr),
  369. uiLib(nullptr),
  370. ctrlChannel(0),
  371. extraHints(0x0),
  372. transientTryCounter(0),
  373. latency(0),
  374. #ifndef BUILD_BRIDGE
  375. latencyBuffers(nullptr),
  376. #endif
  377. name(nullptr),
  378. filename(nullptr),
  379. iconName(nullptr),
  380. audioIn(),
  381. audioOut(),
  382. cvIn(),
  383. cvOut(),
  384. event(),
  385. param(),
  386. prog(),
  387. midiprog(),
  388. custom(),
  389. masterMutex(),
  390. singleMutex(),
  391. stateSave(),
  392. extNotes(),
  393. postRtEvents(),
  394. #ifndef BUILD_BRIDGE
  395. postProc(),
  396. #endif
  397. leakDetector_ProtectedData() {}
  398. CarlaPlugin::ProtectedData::~ProtectedData() noexcept
  399. {
  400. CARLA_SAFE_ASSERT(! needsReset);
  401. CARLA_SAFE_ASSERT(transientTryCounter == 0);
  402. {
  403. // mutex MUST have been locked before
  404. const bool lockMaster(masterMutex.tryLock());
  405. const bool lockSingle(singleMutex.tryLock());
  406. CARLA_SAFE_ASSERT(! lockMaster);
  407. CARLA_SAFE_ASSERT(! lockSingle);
  408. }
  409. if (client != nullptr)
  410. {
  411. if (client->isActive())
  412. {
  413. // must not happen
  414. carla_safe_assert("client->isActive()", __FILE__, __LINE__);
  415. client->deactivate();
  416. }
  417. clearBuffers();
  418. delete client;
  419. client = nullptr;
  420. }
  421. if (name != nullptr)
  422. {
  423. delete[] name;
  424. name = nullptr;
  425. }
  426. if (filename != nullptr)
  427. {
  428. delete[] filename;
  429. filename = nullptr;
  430. }
  431. if (iconName != nullptr)
  432. {
  433. delete[] iconName;
  434. iconName = nullptr;
  435. }
  436. for (LinkedList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  437. {
  438. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  439. //CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  440. if (customData.type != nullptr)
  441. {
  442. delete[] customData.type;
  443. customData.type = nullptr;
  444. }
  445. else
  446. carla_safe_assert("customData.type != nullptr", __FILE__, __LINE__);
  447. if (customData.key != nullptr)
  448. {
  449. delete[] customData.key;
  450. customData.key = nullptr;
  451. }
  452. else
  453. carla_safe_assert("customData.key != nullptr", __FILE__, __LINE__);
  454. if (customData.value != nullptr)
  455. {
  456. delete[] customData.value;
  457. customData.value = nullptr;
  458. }
  459. else
  460. carla_safe_assert("customData.value != nullptr", __FILE__, __LINE__);
  461. }
  462. prog.clear();
  463. midiprog.clear();
  464. custom.clear();
  465. // MUST have been locked before
  466. masterMutex.unlock();
  467. singleMutex.unlock();
  468. if (lib != nullptr)
  469. libClose();
  470. CARLA_SAFE_ASSERT(uiLib == nullptr);
  471. }
  472. // -----------------------------------------------------------------------
  473. // Buffer functions
  474. void CarlaPlugin::ProtectedData::clearBuffers() noexcept
  475. {
  476. #ifndef BUILD_BRIDGE
  477. if (latencyBuffers != nullptr)
  478. {
  479. CARLA_SAFE_ASSERT(audioIn.count > 0);
  480. for (uint32_t i=0; i < audioIn.count; ++i)
  481. {
  482. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  483. delete[] latencyBuffers[i];
  484. latencyBuffers[i] = nullptr;
  485. }
  486. delete[] latencyBuffers;
  487. latencyBuffers = nullptr;
  488. latency = 0;
  489. }
  490. else
  491. {
  492. if (latency != 0)
  493. {
  494. carla_safe_assert_int("latency != 0", __FILE__, __LINE__, static_cast<int>(latency));
  495. latency = 0;
  496. }
  497. }
  498. #else
  499. latency = 0;
  500. #endif
  501. audioIn.clear();
  502. audioOut.clear();
  503. cvIn.clear();
  504. cvOut.clear();
  505. param.clear();
  506. event.clear();
  507. }
  508. #ifndef BUILD_BRIDGE
  509. void CarlaPlugin::ProtectedData::recreateLatencyBuffers()
  510. {
  511. if (latencyBuffers != nullptr)
  512. {
  513. CARLA_SAFE_ASSERT(audioIn.count > 0);
  514. for (uint32_t i=0; i < audioIn.count; ++i)
  515. {
  516. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  517. delete[] latencyBuffers[i];
  518. latencyBuffers[i] = nullptr;
  519. }
  520. delete[] latencyBuffers;
  521. latencyBuffers = nullptr;
  522. }
  523. if (audioIn.count > 0 && latency > 0)
  524. {
  525. latencyBuffers = new float*[audioIn.count];
  526. for (uint32_t i=0; i < audioIn.count; ++i)
  527. {
  528. latencyBuffers[i] = new float[latency];
  529. FloatVectorOperations::clear(latencyBuffers[i], static_cast<int>(latency));
  530. }
  531. }
  532. }
  533. #endif
  534. // -----------------------------------------------------------------------
  535. // Post-poned events
  536. void CarlaPlugin::ProtectedData::postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept
  537. {
  538. CARLA_SAFE_ASSERT_RETURN(rtEvent.type != kPluginPostRtEventNull,);
  539. postRtEvents.appendRT(rtEvent);
  540. }
  541. void CarlaPlugin::ProtectedData::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3) noexcept
  542. {
  543. CARLA_SAFE_ASSERT_RETURN(type != kPluginPostRtEventNull,);
  544. PluginPostRtEvent rtEvent = { type, value1, value2, value3 };
  545. postRtEvents.appendRT(rtEvent);
  546. }
  547. // -----------------------------------------------------------------------
  548. // Library functions
  549. static LibCounter sLibCounter;
  550. const char* CarlaPlugin::ProtectedData::libError(const char* const fname) noexcept
  551. {
  552. return lib_error(fname);
  553. }
  554. bool CarlaPlugin::ProtectedData::libOpen(const char* const fname) noexcept
  555. {
  556. lib = sLibCounter.open(fname);
  557. return (lib != nullptr);
  558. }
  559. bool CarlaPlugin::ProtectedData::libClose() noexcept
  560. {
  561. const bool ret = sLibCounter.close(lib);
  562. lib = nullptr;
  563. return ret;
  564. }
  565. bool CarlaPlugin::ProtectedData::uiLibOpen(const char* const fname, const bool canDelete) noexcept
  566. {
  567. uiLib = sLibCounter.open(fname, canDelete);
  568. return (uiLib != nullptr);
  569. }
  570. bool CarlaPlugin::ProtectedData::uiLibClose() noexcept
  571. {
  572. const bool ret = sLibCounter.close(uiLib);
  573. uiLib = nullptr;
  574. return ret;
  575. }
  576. // -----------------------------------------------------------------------
  577. void CarlaPlugin::ProtectedData::tryTransient() noexcept
  578. {
  579. if (engine->getOptions().frontendWinId != 0)
  580. transientTryCounter = 1;
  581. }
  582. void CarlaPlugin::ProtectedData::updateParameterValues(CarlaPlugin* const plugin, const bool sendOsc, const bool sendCallback, const bool useDefault) noexcept
  583. {
  584. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback || useDefault,);
  585. for (uint32_t i=0; i < param.count; ++i)
  586. {
  587. const float value(param.ranges[i].getFixedValue(plugin->getParameterValue(i)));
  588. if (useDefault)
  589. param.ranges[i].def = value;
  590. #ifndef BUILD_BRIDGE
  591. if (sendOsc)
  592. {
  593. if (useDefault)
  594. engine->oscSend_control_set_default_value(id, i, value);
  595. engine->oscSend_control_set_parameter_value(id, static_cast<int32_t>(i), value);
  596. }
  597. #endif
  598. if (sendCallback)
  599. {
  600. if (useDefault)
  601. engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, id, static_cast<int>(i), 0, value, nullptr);
  602. engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, id, static_cast<int>(i), 0, value, nullptr);
  603. }
  604. }
  605. #ifdef BUILD_BRIDGE
  606. // unused
  607. return; (void)sendOsc;
  608. #endif
  609. }
  610. // -----------------------------------------------------------------------
  611. CARLA_BACKEND_END_NAMESPACE