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.

778 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_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()
  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()
  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()
  89. {
  90. CARLA_ASSERT(port == nullptr);
  91. }
  92. // -----------------------------------------------------------------------
  93. // PluginCVData
  94. PluginCVData::PluginCVData() noexcept
  95. : count(0),
  96. ports(nullptr) {}
  97. PluginCVData::~PluginCVData()
  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()
  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()
  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()
  141. {
  142. CARLA_ASSERT(portIn == nullptr);
  143. CARLA_ASSERT(portOut == nullptr);
  144. }
  145. void PluginEventData::clear()
  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()
  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()
  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()
  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
  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()
  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()
  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()
  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()
  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& event)
  337. {
  338. dataPendingRT.append(event);
  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 engine, CarlaPlugin* const plugin)
  366. : thread(engine, plugin) {}
  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. patchbayClientId(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. if (name != nullptr)
  393. {
  394. delete[] name;
  395. name = nullptr;
  396. }
  397. if (filename != nullptr)
  398. {
  399. delete[] filename;
  400. filename = nullptr;
  401. }
  402. if (iconName != nullptr)
  403. {
  404. delete[] iconName;
  405. iconName = nullptr;
  406. }
  407. if (identifier != nullptr)
  408. {
  409. delete[] identifier;
  410. identifier = nullptr;
  411. }
  412. {
  413. // mutex MUST have been locked before
  414. const bool lockMaster(masterMutex.tryLock());
  415. const bool lockSingle(singleMutex.tryLock());
  416. CARLA_SAFE_ASSERT(! lockMaster);
  417. CARLA_SAFE_ASSERT(! lockSingle);
  418. }
  419. if (client != nullptr)
  420. {
  421. if (client->isActive())
  422. {
  423. // must not happen
  424. carla_safe_assert("client->isActive()", __FILE__, __LINE__);
  425. client->deactivate();
  426. }
  427. clearBuffers();
  428. delete client;
  429. client = nullptr;
  430. }
  431. for (LinkedList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  432. {
  433. CustomData& cData(it.getValue());
  434. if (cData.type != nullptr)
  435. {
  436. delete[] cData.type;
  437. cData.type = nullptr;
  438. }
  439. else
  440. carla_safe_assert("cData.type != nullptr", __FILE__, __LINE__);
  441. if (cData.key != nullptr)
  442. {
  443. delete[] cData.key;
  444. cData.key = nullptr;
  445. }
  446. else
  447. carla_safe_assert("cData.key != nullptr", __FILE__, __LINE__);
  448. if (cData.value != nullptr)
  449. {
  450. delete[] cData.value;
  451. cData.value = nullptr;
  452. }
  453. else
  454. carla_safe_assert("cData.value != nullptr", __FILE__, __LINE__);
  455. }
  456. prog.clear();
  457. midiprog.clear();
  458. custom.clear();
  459. // MUST have been locked before
  460. masterMutex.unlock();
  461. singleMutex.unlock();
  462. if (lib != nullptr)
  463. libClose();
  464. CARLA_ASSERT(uiLib == nullptr);
  465. }
  466. // -----------------------------------------------------------------------
  467. // Buffer functions
  468. void CarlaPluginProtectedData::clearBuffers()
  469. {
  470. if (latencyBuffers != nullptr)
  471. {
  472. CARLA_ASSERT(audioIn.count > 0);
  473. for (uint32_t i=0; i < audioIn.count; ++i)
  474. {
  475. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  476. delete[] latencyBuffers[i];
  477. latencyBuffers[i] = nullptr;
  478. }
  479. delete[] latencyBuffers;
  480. latencyBuffers = nullptr;
  481. latency = 0;
  482. }
  483. else
  484. {
  485. CARLA_ASSERT(latency == 0);
  486. }
  487. audioIn.clear();
  488. audioOut.clear();
  489. param.clear();
  490. event.clear();
  491. }
  492. void CarlaPluginProtectedData::recreateLatencyBuffers()
  493. {
  494. if (latencyBuffers != nullptr)
  495. {
  496. CARLA_ASSERT(audioIn.count > 0);
  497. for (uint32_t i=0; i < audioIn.count; ++i)
  498. {
  499. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  500. delete[] latencyBuffers[i];
  501. latencyBuffers[i] = nullptr;
  502. }
  503. delete[] latencyBuffers;
  504. latencyBuffers = nullptr;
  505. }
  506. if (audioIn.count > 0 && latency > 0)
  507. {
  508. latencyBuffers = new float*[audioIn.count];
  509. for (uint32_t i=0; i < audioIn.count; ++i)
  510. {
  511. latencyBuffers[i] = new float[latency];
  512. FLOAT_CLEAR(latencyBuffers[i], latency);
  513. }
  514. }
  515. }
  516. // -----------------------------------------------------------------------
  517. // Post-poned events
  518. void CarlaPluginProtectedData::postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  519. {
  520. CARLA_SAFE_ASSERT_RETURN(type != kPluginPostRtEventNull,);
  521. PluginPostRtEvent rtEvent = { type, value1, value2, value3 };
  522. postRtEvents.appendRT(rtEvent);
  523. }
  524. // -----------------------------------------------------------------------
  525. // Library functions
  526. static LibCounter sLibCounter;
  527. const char* CarlaPluginProtectedData::libError(const char* const fname)
  528. {
  529. return lib_error(fname);
  530. }
  531. bool CarlaPluginProtectedData::libOpen(const char* const fname)
  532. {
  533. lib = sLibCounter.open(fname);
  534. return (lib != nullptr);
  535. }
  536. bool CarlaPluginProtectedData::libClose()
  537. {
  538. const bool ret = sLibCounter.close(lib);
  539. lib = nullptr;
  540. return ret;
  541. }
  542. void* CarlaPluginProtectedData::libSymbol(const char* const symbol)
  543. {
  544. return lib_symbol(lib, symbol);
  545. }
  546. bool CarlaPluginProtectedData::uiLibOpen(const char* const fname)
  547. {
  548. uiLib = sLibCounter.open(fname);
  549. return (uiLib != nullptr);
  550. }
  551. bool CarlaPluginProtectedData::uiLibClose()
  552. {
  553. const bool ret = sLibCounter.close(uiLib);
  554. uiLib = nullptr;
  555. return ret;
  556. }
  557. void* CarlaPluginProtectedData::uiLibSymbol(const char* const symbol)
  558. {
  559. return lib_symbol(uiLib, symbol);
  560. }
  561. // -----------------------------------------------------------------------
  562. // Settings functions
  563. void CarlaPluginProtectedData::saveSetting(const uint option, const bool yesNo)
  564. {
  565. CARLA_SAFE_ASSERT_RETURN(identifier != nullptr && identifier[0] != '\0',);
  566. QSettings settings("falkTX", "CarlaPluginSettings");
  567. settings.beginGroup(identifier);
  568. switch (option)
  569. {
  570. case PLUGIN_OPTION_FIXED_BUFFERS:
  571. settings.setValue("FixedBuffers", yesNo);
  572. break;
  573. case PLUGIN_OPTION_FORCE_STEREO:
  574. settings.setValue("ForceStereo", yesNo);
  575. break;
  576. case PLUGIN_OPTION_MAP_PROGRAM_CHANGES:
  577. settings.setValue("MapProgramChanges", yesNo);
  578. break;
  579. case PLUGIN_OPTION_USE_CHUNKS:
  580. settings.setValue("UseChunks", yesNo);
  581. break;
  582. case PLUGIN_OPTION_SEND_CONTROL_CHANGES:
  583. settings.setValue("SendControlChanges", yesNo);
  584. break;
  585. case PLUGIN_OPTION_SEND_CHANNEL_PRESSURE:
  586. settings.setValue("SendChannelPressure", yesNo);
  587. break;
  588. case PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH:
  589. settings.setValue("SendNoteAftertouch", yesNo);
  590. break;
  591. case PLUGIN_OPTION_SEND_PITCHBEND:
  592. settings.setValue("SendPitchbend", yesNo);
  593. break;
  594. case PLUGIN_OPTION_SEND_ALL_SOUND_OFF:
  595. settings.setValue("SendAllSoundOff", yesNo);
  596. break;
  597. default:
  598. break;
  599. }
  600. settings.endGroup();
  601. }
  602. uint CarlaPluginProtectedData::loadSettings(const uint curOptions, const uint availOptions)
  603. {
  604. CARLA_SAFE_ASSERT_RETURN(identifier != nullptr && identifier[0] != '\0', 0x0);
  605. QSettings settings("falkTX", "CarlaPluginSettings");
  606. settings.beginGroup(identifier);
  607. unsigned int newOptions = 0x0;
  608. #define CHECK_AND_SET_OPTION(STR, BIT) \
  609. if ((availOptions & BIT) != 0 || BIT == PLUGIN_OPTION_FORCE_STEREO) \
  610. { \
  611. if (settings.contains(STR)) \
  612. { \
  613. if (settings.value(STR, (curOptions & BIT) != 0).toBool()) \
  614. newOptions |= BIT; \
  615. } \
  616. else if (curOptions & BIT) \
  617. newOptions |= BIT; \
  618. }
  619. CHECK_AND_SET_OPTION("FixedBuffers", PLUGIN_OPTION_FIXED_BUFFERS);
  620. CHECK_AND_SET_OPTION("ForceStereo", PLUGIN_OPTION_FORCE_STEREO);
  621. CHECK_AND_SET_OPTION("MapProgramChanges", PLUGIN_OPTION_MAP_PROGRAM_CHANGES);
  622. CHECK_AND_SET_OPTION("UseChunks", PLUGIN_OPTION_USE_CHUNKS);
  623. CHECK_AND_SET_OPTION("SendControlChanges", PLUGIN_OPTION_SEND_CONTROL_CHANGES);
  624. CHECK_AND_SET_OPTION("SendChannelPressure", PLUGIN_OPTION_SEND_CHANNEL_PRESSURE);
  625. CHECK_AND_SET_OPTION("SendNoteAftertouch", PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH);
  626. CHECK_AND_SET_OPTION("SendPitchbend", PLUGIN_OPTION_SEND_PITCHBEND);
  627. CHECK_AND_SET_OPTION("SendAllSoundOff", PLUGIN_OPTION_SEND_ALL_SOUND_OFF);
  628. #undef CHECK_AND_SET_OPTION
  629. settings.endGroup();
  630. return newOptions;
  631. }
  632. // -----------------------------------------------------------------------
  633. CARLA_BACKEND_END_NAMESPACE