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.

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