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.

745 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. childProcess(),
  392. oscData(),
  393. stateSave(),
  394. extNotes(),
  395. postRtEvents(),
  396. #ifndef BUILD_BRIDGE
  397. postProc(),
  398. #endif
  399. leakDetector_ProtectedData() {}
  400. CarlaPlugin::ProtectedData::~ProtectedData() noexcept
  401. {
  402. CARLA_SAFE_ASSERT(! needsReset);
  403. CARLA_SAFE_ASSERT(transientTryCounter == 0);
  404. {
  405. // mutex MUST have been locked before
  406. const bool lockMaster(masterMutex.tryLock());
  407. const bool lockSingle(singleMutex.tryLock());
  408. CARLA_SAFE_ASSERT(! lockMaster);
  409. CARLA_SAFE_ASSERT(! lockSingle);
  410. }
  411. if (client != nullptr)
  412. {
  413. if (client->isActive())
  414. {
  415. // must not happen
  416. carla_safe_assert("client->isActive()", __FILE__, __LINE__);
  417. client->deactivate();
  418. }
  419. clearBuffers();
  420. delete client;
  421. client = nullptr;
  422. }
  423. if (name != nullptr)
  424. {
  425. delete[] name;
  426. name = nullptr;
  427. }
  428. if (filename != nullptr)
  429. {
  430. delete[] filename;
  431. filename = nullptr;
  432. }
  433. if (iconName != nullptr)
  434. {
  435. delete[] iconName;
  436. iconName = nullptr;
  437. }
  438. for (LinkedList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  439. {
  440. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  441. //CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  442. if (customData.type != nullptr)
  443. {
  444. delete[] customData.type;
  445. customData.type = nullptr;
  446. }
  447. else
  448. carla_safe_assert("customData.type != nullptr", __FILE__, __LINE__);
  449. if (customData.key != nullptr)
  450. {
  451. delete[] customData.key;
  452. customData.key = nullptr;
  453. }
  454. else
  455. carla_safe_assert("customData.key != nullptr", __FILE__, __LINE__);
  456. if (customData.value != nullptr)
  457. {
  458. delete[] customData.value;
  459. customData.value = nullptr;
  460. }
  461. else
  462. carla_safe_assert("customData.value != nullptr", __FILE__, __LINE__);
  463. }
  464. prog.clear();
  465. midiprog.clear();
  466. custom.clear();
  467. // MUST have been locked before
  468. masterMutex.unlock();
  469. singleMutex.unlock();
  470. if (lib != nullptr)
  471. libClose();
  472. CARLA_SAFE_ASSERT(uiLib == nullptr);
  473. }
  474. // -----------------------------------------------------------------------
  475. // Buffer functions
  476. void CarlaPlugin::ProtectedData::clearBuffers() noexcept
  477. {
  478. #ifndef BUILD_BRIDGE
  479. if (latencyBuffers != nullptr)
  480. {
  481. CARLA_SAFE_ASSERT(audioIn.count > 0);
  482. for (uint32_t i=0; i < audioIn.count; ++i)
  483. {
  484. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  485. delete[] latencyBuffers[i];
  486. latencyBuffers[i] = nullptr;
  487. }
  488. delete[] latencyBuffers;
  489. latencyBuffers = nullptr;
  490. latency = 0;
  491. }
  492. else
  493. {
  494. if (latency != 0)
  495. {
  496. carla_safe_assert_int("latency != 0", __FILE__, __LINE__, static_cast<int>(latency));
  497. latency = 0;
  498. }
  499. }
  500. #else
  501. latency = 0;
  502. #endif
  503. audioIn.clear();
  504. audioOut.clear();
  505. cvIn.clear();
  506. cvOut.clear();
  507. param.clear();
  508. event.clear();
  509. }
  510. #ifndef BUILD_BRIDGE
  511. void CarlaPlugin::ProtectedData::recreateLatencyBuffers()
  512. {
  513. if (latencyBuffers != nullptr)
  514. {
  515. CARLA_SAFE_ASSERT(audioIn.count > 0);
  516. for (uint32_t i=0; i < audioIn.count; ++i)
  517. {
  518. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  519. delete[] latencyBuffers[i];
  520. latencyBuffers[i] = nullptr;
  521. }
  522. delete[] latencyBuffers;
  523. latencyBuffers = nullptr;
  524. }
  525. if (audioIn.count > 0 && latency > 0)
  526. {
  527. latencyBuffers = new float*[audioIn.count];
  528. for (uint32_t i=0; i < audioIn.count; ++i)
  529. {
  530. latencyBuffers[i] = new float[latency];
  531. FloatVectorOperations::clear(latencyBuffers[i], static_cast<int>(latency));
  532. }
  533. }
  534. }
  535. #endif
  536. // -----------------------------------------------------------------------
  537. // Post-poned events
  538. void CarlaPlugin::ProtectedData::postponeRtEvent(const PluginPostRtEvent& rtEvent) noexcept
  539. {
  540. CARLA_SAFE_ASSERT_RETURN(rtEvent.type != kPluginPostRtEventNull,);
  541. postRtEvents.appendRT(rtEvent);
  542. }
  543. void CarlaPlugin::ProtectedData::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3) noexcept
  544. {
  545. CARLA_SAFE_ASSERT_RETURN(type != kPluginPostRtEventNull,);
  546. PluginPostRtEvent rtEvent = { type, value1, value2, value3 };
  547. postRtEvents.appendRT(rtEvent);
  548. }
  549. // -----------------------------------------------------------------------
  550. // Library functions
  551. static LibCounter sLibCounter;
  552. const char* CarlaPlugin::ProtectedData::libError(const char* const fname) noexcept
  553. {
  554. return lib_error(fname);
  555. }
  556. bool CarlaPlugin::ProtectedData::libOpen(const char* const fname) noexcept
  557. {
  558. lib = sLibCounter.open(fname);
  559. return (lib != nullptr);
  560. }
  561. bool CarlaPlugin::ProtectedData::libClose() noexcept
  562. {
  563. const bool ret = sLibCounter.close(lib);
  564. lib = nullptr;
  565. return ret;
  566. }
  567. bool CarlaPlugin::ProtectedData::uiLibOpen(const char* const fname, const bool canDelete) noexcept
  568. {
  569. uiLib = sLibCounter.open(fname, canDelete);
  570. return (uiLib != nullptr);
  571. }
  572. bool CarlaPlugin::ProtectedData::uiLibClose() noexcept
  573. {
  574. const bool ret = sLibCounter.close(uiLib);
  575. uiLib = nullptr;
  576. return ret;
  577. }
  578. // -----------------------------------------------------------------------
  579. void CarlaPlugin::ProtectedData::tryTransient() noexcept
  580. {
  581. if (engine->getOptions().frontendWinId != 0)
  582. transientTryCounter = 1;
  583. }
  584. void CarlaPlugin::ProtectedData::updateParameterValues(CarlaPlugin* const plugin, const bool sendOsc, const bool sendCallback, const bool useDefault) noexcept
  585. {
  586. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback || useDefault,);
  587. for (uint32_t i=0; i < param.count; ++i)
  588. {
  589. const float value(param.ranges[i].getFixedValue(plugin->getParameterValue(i)));
  590. if (useDefault)
  591. param.ranges[i].def = value;
  592. #ifndef BUILD_BRIDGE
  593. if (sendOsc)
  594. {
  595. if (useDefault)
  596. engine->oscSend_control_set_default_value(id, i, value);
  597. engine->oscSend_control_set_parameter_value(id, static_cast<int32_t>(i), value);
  598. }
  599. #endif
  600. if (sendCallback)
  601. {
  602. if (useDefault)
  603. engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, id, static_cast<int>(i), 0, value, nullptr);
  604. engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, id, static_cast<int>(i), 0, value, nullptr);
  605. }
  606. }
  607. #ifdef BUILD_BRIDGE
  608. // unused
  609. return; (void)sendOsc;
  610. #endif
  611. }
  612. // -----------------------------------------------------------------------
  613. CARLA_BACKEND_END_NAMESPACE