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.

787 lines
19KB

  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. #include <QtCore/QSettings>
  22. // -----------------------------------------------------------------------
  23. CARLA_BACKEND_START_NAMESPACE
  24. #if 0
  25. } // Fix editor indentation
  26. #endif
  27. // -------------------------------------------------------------------
  28. // Fallback data
  29. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  30. // -----------------------------------------------------------------------
  31. // PluginAudioPort
  32. PluginAudioPort::PluginAudioPort() noexcept
  33. : rindex(0),
  34. port(nullptr) {}
  35. PluginAudioPort::~PluginAudioPort()
  36. {
  37. CARLA_ASSERT(port == nullptr);
  38. }
  39. // -----------------------------------------------------------------------
  40. // PluginAudioData
  41. PluginAudioData::PluginAudioData() noexcept
  42. : count(0),
  43. ports(nullptr) {}
  44. PluginAudioData::~PluginAudioData()
  45. {
  46. CARLA_ASSERT_INT(count == 0, count);
  47. CARLA_ASSERT(ports == nullptr);
  48. }
  49. void PluginAudioData::createNew(const uint32_t newCount)
  50. {
  51. CARLA_ASSERT_INT(count == 0, count);
  52. CARLA_ASSERT(ports == nullptr);
  53. CARLA_ASSERT_INT(newCount > 0, newCount);
  54. if (ports != nullptr || newCount == 0)
  55. return;
  56. ports = new PluginAudioPort[newCount];
  57. count = newCount;
  58. }
  59. void PluginAudioData::clear()
  60. {
  61. if (ports != nullptr)
  62. {
  63. for (uint32_t i=0; i < count; ++i)
  64. {
  65. if (ports[i].port != nullptr)
  66. {
  67. delete ports[i].port;
  68. ports[i].port = nullptr;
  69. }
  70. }
  71. delete[] ports;
  72. ports = nullptr;
  73. }
  74. count = 0;
  75. }
  76. void PluginAudioData::initBuffers()
  77. {
  78. for (uint32_t i=0; i < count; ++i)
  79. {
  80. if (ports[i].port != nullptr)
  81. ports[i].port->initBuffer();
  82. }
  83. }
  84. // -----------------------------------------------------------------------
  85. // PluginCVPort
  86. PluginCVPort::PluginCVPort() noexcept
  87. : rindex(0),
  88. param(0),
  89. port(nullptr) {}
  90. PluginCVPort::~PluginCVPort()
  91. {
  92. CARLA_ASSERT(port == nullptr);
  93. }
  94. // -----------------------------------------------------------------------
  95. // PluginCVData
  96. PluginCVData::PluginCVData() noexcept
  97. : count(0),
  98. ports(nullptr) {}
  99. PluginCVData::~PluginCVData()
  100. {
  101. CARLA_ASSERT_INT(count == 0, count);
  102. CARLA_ASSERT(ports == nullptr);
  103. }
  104. void PluginCVData::createNew(const uint32_t newCount)
  105. {
  106. CARLA_ASSERT_INT(count == 0, count);
  107. CARLA_ASSERT(ports == nullptr);
  108. CARLA_ASSERT_INT(newCount > 0, newCount);
  109. if (ports != nullptr || newCount == 0)
  110. return;
  111. ports = new PluginCVPort[newCount];
  112. count = newCount;
  113. }
  114. void PluginCVData::clear()
  115. {
  116. if (ports != nullptr)
  117. {
  118. for (uint32_t i=0; i < count; ++i)
  119. {
  120. if (ports[i].port != nullptr)
  121. {
  122. delete ports[i].port;
  123. ports[i].port = nullptr;
  124. }
  125. }
  126. delete[] ports;
  127. ports = nullptr;
  128. }
  129. count = 0;
  130. }
  131. void PluginCVData::initBuffers()
  132. {
  133. for (uint32_t i=0; i < count; ++i)
  134. {
  135. if (ports[i].port != nullptr)
  136. ports[i].port->initBuffer();
  137. }
  138. }
  139. // -----------------------------------------------------------------------
  140. // PluginEventData
  141. PluginEventData::PluginEventData() noexcept
  142. : portIn(nullptr),
  143. portOut(nullptr) {}
  144. PluginEventData::~PluginEventData()
  145. {
  146. CARLA_ASSERT(portIn == nullptr);
  147. CARLA_ASSERT(portOut == nullptr);
  148. }
  149. void PluginEventData::clear()
  150. {
  151. if (portIn != nullptr)
  152. {
  153. delete portIn;
  154. portIn = nullptr;
  155. }
  156. if (portOut != nullptr)
  157. {
  158. delete portOut;
  159. portOut = nullptr;
  160. }
  161. }
  162. void PluginEventData::initBuffers()
  163. {
  164. if (portIn != nullptr)
  165. portIn->initBuffer();
  166. if (portOut != nullptr)
  167. portOut->initBuffer();
  168. }
  169. // -----------------------------------------------------------------------
  170. // PluginParameterData
  171. PluginParameterData::PluginParameterData() noexcept
  172. : count(0),
  173. data(nullptr),
  174. ranges(nullptr),
  175. special(nullptr) {}
  176. PluginParameterData::~PluginParameterData()
  177. {
  178. CARLA_ASSERT_INT(count == 0, count);
  179. CARLA_ASSERT(data == nullptr);
  180. CARLA_ASSERT(ranges == nullptr);
  181. CARLA_ASSERT(special == nullptr);
  182. }
  183. void PluginParameterData::createNew(const uint32_t newCount, const bool withSpecial)
  184. {
  185. CARLA_ASSERT_INT(count == 0, count);
  186. CARLA_ASSERT(data == nullptr);
  187. CARLA_ASSERT(ranges == nullptr);
  188. CARLA_ASSERT(special == nullptr);
  189. CARLA_ASSERT_INT(newCount > 0, newCount);
  190. if (data != nullptr || ranges != nullptr || newCount == 0)
  191. return;
  192. data = new ParameterData[newCount];
  193. ranges = new ParameterRanges[newCount];
  194. count = newCount;
  195. if (withSpecial)
  196. special = new SpecialParameterType[newCount];
  197. }
  198. void PluginParameterData::clear()
  199. {
  200. if (data != nullptr)
  201. {
  202. delete[] data;
  203. data = nullptr;
  204. }
  205. if (ranges != nullptr)
  206. {
  207. delete[] ranges;
  208. ranges = nullptr;
  209. }
  210. if (special != nullptr)
  211. {
  212. delete[] special;
  213. special = nullptr;
  214. }
  215. count = 0;
  216. }
  217. float PluginParameterData::getFixedValue(const uint32_t parameterId, const float& value) const
  218. {
  219. CARLA_SAFE_ASSERT_RETURN(parameterId < count, 0.0f);
  220. return ranges[parameterId].getFixedValue(value);
  221. }
  222. // -----------------------------------------------------------------------
  223. // PluginProgramData
  224. PluginProgramData::PluginProgramData() noexcept
  225. : count(0),
  226. current(-1),
  227. names(nullptr) {}
  228. PluginProgramData::~PluginProgramData()
  229. {
  230. CARLA_ASSERT_INT(count == 0, count);
  231. CARLA_ASSERT_INT(current == -1, current);
  232. CARLA_ASSERT(names == nullptr);
  233. }
  234. void PluginProgramData::createNew(const uint32_t newCount)
  235. {
  236. CARLA_ASSERT_INT(count == 0, count);
  237. CARLA_ASSERT_INT(current == -1, current);
  238. CARLA_ASSERT(names == nullptr);
  239. CARLA_ASSERT_INT(newCount > 0, newCount);
  240. if (names != nullptr || newCount == 0)
  241. return;
  242. names = new ProgramName[newCount];
  243. count = newCount;
  244. for (uint32_t i=0; i < newCount; ++i)
  245. names[i] = nullptr;
  246. }
  247. void PluginProgramData::clear()
  248. {
  249. if (names != nullptr)
  250. {
  251. for (uint32_t i=0; i < count; ++i)
  252. {
  253. if (names[i] != nullptr)
  254. {
  255. delete[] names[i];
  256. names[i] = nullptr;
  257. }
  258. }
  259. delete[] names;
  260. names = nullptr;
  261. }
  262. count = 0;
  263. current = -1;
  264. }
  265. // -----------------------------------------------------------------------
  266. // PluginMidiProgramData
  267. PluginMidiProgramData::PluginMidiProgramData() noexcept
  268. : count(0),
  269. current(-1),
  270. data(nullptr) {}
  271. PluginMidiProgramData::~PluginMidiProgramData()
  272. {
  273. CARLA_ASSERT_INT(count == 0, count);
  274. CARLA_ASSERT_INT(current == -1, current);
  275. CARLA_ASSERT(data == nullptr);
  276. }
  277. void PluginMidiProgramData::createNew(const uint32_t newCount)
  278. {
  279. CARLA_ASSERT_INT(count == 0, count);
  280. CARLA_ASSERT_INT(current == -1, current);
  281. CARLA_ASSERT(data == nullptr);
  282. CARLA_ASSERT_INT(newCount > 0, newCount);
  283. if (data != nullptr || newCount == 0)
  284. return;
  285. data = new MidiProgramData[newCount];
  286. count = newCount;
  287. for (uint32_t i=0; i < count; ++i)
  288. {
  289. data[i].bank = 0;
  290. data[i].program = 0;
  291. data[i].name = nullptr;
  292. }
  293. }
  294. void PluginMidiProgramData::clear()
  295. {
  296. if (data != nullptr)
  297. {
  298. for (uint32_t i=0; i < count; ++i)
  299. {
  300. if (data[i].name != nullptr)
  301. {
  302. delete[] data[i].name;
  303. data[i].name = nullptr;
  304. }
  305. }
  306. delete[] data;
  307. data = nullptr;
  308. }
  309. count = 0;
  310. current = -1;
  311. }
  312. const MidiProgramData& PluginMidiProgramData::getCurrent() const noexcept
  313. {
  314. CARLA_SAFE_ASSERT_RETURN(current >= 0 && current < static_cast<int32_t>(count), kMidiProgramDataNull);
  315. return data[current];
  316. }
  317. // -----------------------------------------------------------------------
  318. CarlaPluginProtectedData::ExternalNotes::ExternalNotes()
  319. : dataPool(32, 152),
  320. data(dataPool) {}
  321. CarlaPluginProtectedData::ExternalNotes::~ExternalNotes()
  322. {
  323. mutex.lock();
  324. data.clear();
  325. mutex.unlock();
  326. }
  327. void CarlaPluginProtectedData::ExternalNotes::append(const ExternalMidiNote& note)
  328. {
  329. mutex.lock();
  330. data.append_sleepy(note);
  331. mutex.unlock();
  332. }
  333. // -----------------------------------------------------------------------
  334. CarlaPluginProtectedData::PostRtEvents::PostRtEvents()
  335. : dataPool(128, 128),
  336. data(dataPool),
  337. dataPendingRT(dataPool) {}
  338. CarlaPluginProtectedData::PostRtEvents::~PostRtEvents()
  339. {
  340. clear();
  341. }
  342. void CarlaPluginProtectedData::PostRtEvents::appendRT(const PluginPostRtEvent& event)
  343. {
  344. dataPendingRT.append(event);
  345. }
  346. void CarlaPluginProtectedData::PostRtEvents::trySplice()
  347. {
  348. if (mutex.tryLock())
  349. {
  350. dataPendingRT.spliceAppend(data);
  351. mutex.unlock();
  352. }
  353. }
  354. void CarlaPluginProtectedData::PostRtEvents::clear()
  355. {
  356. mutex.lock();
  357. data.clear();
  358. dataPendingRT.clear();
  359. mutex.unlock();
  360. }
  361. // -----------------------------------------------------------------------
  362. #ifndef BUILD_BRIDGE
  363. CarlaPluginProtectedData::PostProc::PostProc() noexcept
  364. : dryWet(1.0f),
  365. volume(1.0f),
  366. balanceLeft(-1.0f),
  367. balanceRight(1.0f),
  368. panning(0.0f) {}
  369. #endif
  370. // -----------------------------------------------------------------------
  371. CarlaPluginProtectedData::OSC::OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  372. : thread(engine, plugin) {}
  373. // -----------------------------------------------------------------------
  374. CarlaPluginProtectedData::CarlaPluginProtectedData(CarlaEngine* const eng, const unsigned int idx, CarlaPlugin* const self)
  375. : engine(eng),
  376. client(nullptr),
  377. id(idx),
  378. hints(0x0),
  379. options(0x0),
  380. active(false),
  381. enabled(false),
  382. needsReset(false),
  383. lib(nullptr),
  384. uiLib(nullptr),
  385. ctrlChannel(0),
  386. extraHints(0x0),
  387. patchbayClientId(0),
  388. latency(0),
  389. latencyBuffers(nullptr),
  390. name(nullptr),
  391. filename(nullptr),
  392. iconName(nullptr),
  393. identifier(nullptr),
  394. osc(eng, self) {}
  395. CarlaPluginProtectedData::~CarlaPluginProtectedData()
  396. {
  397. CARLA_SAFE_ASSERT(! needsReset);
  398. if (name != nullptr)
  399. {
  400. delete[] name;
  401. name = nullptr;
  402. }
  403. if (filename != nullptr)
  404. {
  405. delete[] filename;
  406. filename = nullptr;
  407. }
  408. if (iconName != nullptr)
  409. {
  410. delete[] iconName;
  411. iconName = nullptr;
  412. }
  413. if (identifier != nullptr)
  414. {
  415. delete[] identifier;
  416. identifier = nullptr;
  417. }
  418. {
  419. // mutex MUST have been locked before
  420. const bool lockMaster(masterMutex.tryLock());
  421. const bool lockSingle(singleMutex.tryLock());
  422. CARLA_SAFE_ASSERT(! lockMaster);
  423. CARLA_SAFE_ASSERT(! lockSingle);
  424. }
  425. if (client != nullptr)
  426. {
  427. if (client->isActive())
  428. {
  429. // must not happen
  430. carla_safe_assert("client->isActive()", __FILE__, __LINE__);
  431. client->deactivate();
  432. }
  433. clearBuffers();
  434. delete client;
  435. client = nullptr;
  436. }
  437. for (LinkedList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  438. {
  439. CustomData& cData(it.getValue());
  440. if (cData.type != nullptr)
  441. {
  442. delete[] cData.type;
  443. cData.type = nullptr;
  444. }
  445. else
  446. carla_safe_assert("cData.type != nullptr", __FILE__, __LINE__);
  447. if (cData.key != nullptr)
  448. {
  449. delete[] cData.key;
  450. cData.key = nullptr;
  451. }
  452. else
  453. carla_safe_assert("cData.key != nullptr", __FILE__, __LINE__);
  454. if (cData.value != nullptr)
  455. {
  456. delete[] cData.value;
  457. cData.value = nullptr;
  458. }
  459. else
  460. carla_safe_assert("cData.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_ASSERT(uiLib == nullptr);
  471. }
  472. // -----------------------------------------------------------------------
  473. // Buffer functions
  474. void CarlaPluginProtectedData::clearBuffers()
  475. {
  476. if (latencyBuffers != nullptr)
  477. {
  478. CARLA_ASSERT(audioIn.count > 0);
  479. for (uint32_t i=0; i < audioIn.count; ++i)
  480. {
  481. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  482. delete[] latencyBuffers[i];
  483. latencyBuffers[i] = nullptr;
  484. }
  485. delete[] latencyBuffers;
  486. latencyBuffers = nullptr;
  487. latency = 0;
  488. }
  489. else
  490. {
  491. CARLA_ASSERT(latency == 0);
  492. }
  493. audioIn.clear();
  494. audioOut.clear();
  495. param.clear();
  496. event.clear();
  497. }
  498. void CarlaPluginProtectedData::recreateLatencyBuffers()
  499. {
  500. if (latencyBuffers != nullptr)
  501. {
  502. CARLA_ASSERT(audioIn.count > 0);
  503. for (uint32_t i=0; i < audioIn.count; ++i)
  504. {
  505. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  506. delete[] latencyBuffers[i];
  507. latencyBuffers[i] = nullptr;
  508. }
  509. delete[] latencyBuffers;
  510. latencyBuffers = nullptr;
  511. }
  512. if (audioIn.count > 0 && latency > 0)
  513. {
  514. latencyBuffers = new float*[audioIn.count];
  515. for (uint32_t i=0; i < audioIn.count; ++i)
  516. {
  517. latencyBuffers[i] = new float[latency];
  518. FLOAT_CLEAR(latencyBuffers[i], latency);
  519. }
  520. }
  521. }
  522. // -----------------------------------------------------------------------
  523. // Post-poned events
  524. void CarlaPluginProtectedData::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  525. {
  526. CARLA_SAFE_ASSERT_RETURN(type != kPluginPostRtEventNull,);
  527. PluginPostRtEvent rtEvent = { type, value1, value2, value3 };
  528. postRtEvents.appendRT(rtEvent);
  529. }
  530. // -----------------------------------------------------------------------
  531. // Library functions
  532. static LibCounter sLibCounter;
  533. const char* CarlaPluginProtectedData::libError(const char* const fname)
  534. {
  535. return lib_error(fname);
  536. }
  537. bool CarlaPluginProtectedData::libOpen(const char* const fname)
  538. {
  539. lib = sLibCounter.open(fname);
  540. return (lib != nullptr);
  541. }
  542. bool CarlaPluginProtectedData::libClose()
  543. {
  544. const bool ret = sLibCounter.close(lib);
  545. lib = nullptr;
  546. return ret;
  547. }
  548. void* CarlaPluginProtectedData::libSymbol(const char* const symbol)
  549. {
  550. return lib_symbol(lib, symbol);
  551. }
  552. bool CarlaPluginProtectedData::uiLibOpen(const char* const fname)
  553. {
  554. uiLib = sLibCounter.open(fname);
  555. return (uiLib != nullptr);
  556. }
  557. bool CarlaPluginProtectedData::uiLibClose()
  558. {
  559. const bool ret = sLibCounter.close(uiLib);
  560. uiLib = nullptr;
  561. return ret;
  562. }
  563. void* CarlaPluginProtectedData::uiLibSymbol(const char* const symbol)
  564. {
  565. return lib_symbol(uiLib, symbol);
  566. }
  567. // -----------------------------------------------------------------------
  568. // Settings functions
  569. void CarlaPluginProtectedData::saveSetting(const uint option, const bool yesNo)
  570. {
  571. CARLA_SAFE_ASSERT_RETURN(identifier != nullptr && identifier[0] != '\0',);
  572. QSettings settings("falkTX", "CarlaPluginSettings");
  573. settings.beginGroup(identifier);
  574. switch (option)
  575. {
  576. case PLUGIN_OPTION_FIXED_BUFFERS:
  577. settings.setValue("FixedBuffers", yesNo);
  578. break;
  579. case PLUGIN_OPTION_FORCE_STEREO:
  580. settings.setValue("ForceStereo", yesNo);
  581. break;
  582. case PLUGIN_OPTION_MAP_PROGRAM_CHANGES:
  583. settings.setValue("MapProgramChanges", yesNo);
  584. break;
  585. case PLUGIN_OPTION_USE_CHUNKS:
  586. settings.setValue("UseChunks", yesNo);
  587. break;
  588. case PLUGIN_OPTION_SEND_CONTROL_CHANGES:
  589. settings.setValue("SendControlChanges", yesNo);
  590. break;
  591. case PLUGIN_OPTION_SEND_CHANNEL_PRESSURE:
  592. settings.setValue("SendChannelPressure", yesNo);
  593. break;
  594. case PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH:
  595. settings.setValue("SendNoteAftertouch", yesNo);
  596. break;
  597. case PLUGIN_OPTION_SEND_PITCHBEND:
  598. settings.setValue("SendPitchbend", yesNo);
  599. break;
  600. case PLUGIN_OPTION_SEND_ALL_SOUND_OFF:
  601. settings.setValue("SendAllSoundOff", yesNo);
  602. break;
  603. default:
  604. break;
  605. }
  606. settings.endGroup();
  607. }
  608. uint CarlaPluginProtectedData::loadSettings(const uint curOptions, const uint availOptions)
  609. {
  610. CARLA_SAFE_ASSERT_RETURN(identifier != nullptr && identifier[0] != '\0', 0x0);
  611. QSettings settings("falkTX", "CarlaPluginSettings");
  612. settings.beginGroup(identifier);
  613. unsigned int newOptions = 0x0;
  614. #define CHECK_AND_SET_OPTION(STR, BIT) \
  615. if ((availOptions & BIT) != 0 || BIT == PLUGIN_OPTION_FORCE_STEREO) \
  616. { \
  617. if (settings.contains(STR)) \
  618. { \
  619. if (settings.value(STR, (curOptions & BIT) != 0).toBool()) \
  620. newOptions |= BIT; \
  621. } \
  622. else if (curOptions & BIT) \
  623. newOptions |= BIT; \
  624. }
  625. CHECK_AND_SET_OPTION("FixedBuffers", PLUGIN_OPTION_FIXED_BUFFERS);
  626. CHECK_AND_SET_OPTION("ForceStereo", PLUGIN_OPTION_FORCE_STEREO);
  627. CHECK_AND_SET_OPTION("MapProgramChanges", PLUGIN_OPTION_MAP_PROGRAM_CHANGES);
  628. CHECK_AND_SET_OPTION("UseChunks", PLUGIN_OPTION_USE_CHUNKS);
  629. CHECK_AND_SET_OPTION("SendControlChanges", PLUGIN_OPTION_SEND_CONTROL_CHANGES);
  630. CHECK_AND_SET_OPTION("SendChannelPressure", PLUGIN_OPTION_SEND_CHANNEL_PRESSURE);
  631. CHECK_AND_SET_OPTION("SendNoteAftertouch", PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH);
  632. CHECK_AND_SET_OPTION("SendPitchbend", PLUGIN_OPTION_SEND_PITCHBEND);
  633. CHECK_AND_SET_OPTION("SendAllSoundOff", PLUGIN_OPTION_SEND_ALL_SOUND_OFF);
  634. #undef CHECK_AND_SET_OPTION
  635. settings.endGroup();
  636. return newOptions;
  637. }
  638. // -----------------------------------------------------------------------
  639. CARLA_BACKEND_END_NAMESPACE