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.

1645 lines
55KB

  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2013 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. #ifdef WANT_LV2
  20. #include "CarlaLv2Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "Lv2AtomQueue.hpp"
  23. #include "../engine/CarlaEngineOsc.hpp"
  24. extern "C" {
  25. #include "rtmempool/rtmempool-lv2.h"
  26. }
  27. #include <QtCore/QDir>
  28. #include <QtCore/QUrl>
  29. // -----------------------------------------------------
  30. CARLA_BACKEND_START_NAMESPACE
  31. #if 0
  32. }
  33. #endif
  34. // Extra Parameter Hints
  35. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  36. const unsigned int PARAMETER_IS_TRIGGER = 0x2000;
  37. // LV2 Feature Ids
  38. const uint32_t kFeatureCount = 0;
  39. // -----------------------------------------------------
  40. class Lv2Plugin : public CarlaPlugin
  41. {
  42. public:
  43. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  44. : CarlaPlugin(engine, id),
  45. fHandle(nullptr),
  46. fHandle2(nullptr),
  47. fDescriptor(nullptr),
  48. fRdfDescriptor(nullptr),
  49. fAudioInBuffers(nullptr),
  50. fAudioOutBuffers(nullptr),
  51. fParamBuffers(nullptr)
  52. {
  53. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  54. carla_fill<LV2_Feature*>(fFeatures, kFeatureCount+1, nullptr);
  55. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  56. }
  57. ~Lv2Plugin() override
  58. {
  59. carla_debug("Lv2Plugin::~Lv2Plugin()");
  60. pData->singleMutex.lock();
  61. pData->masterMutex.lock();
  62. if (pData->client != nullptr && pData->client->isActive())
  63. pData->client->deactivate();
  64. if (pData->active)
  65. {
  66. deactivate();
  67. pData->active = false;
  68. }
  69. if (fDescriptor != nullptr)
  70. {
  71. if (fDescriptor->cleanup != nullptr)
  72. {
  73. if (fHandle != nullptr)
  74. fDescriptor->cleanup(fHandle);
  75. if (fHandle2 != nullptr)
  76. fDescriptor->cleanup(fHandle2);
  77. }
  78. fHandle = nullptr;
  79. fHandle2 = nullptr;
  80. fDescriptor = nullptr;
  81. }
  82. if (fRdfDescriptor != nullptr)
  83. {
  84. delete fRdfDescriptor;
  85. fRdfDescriptor = nullptr;
  86. }
  87. clearBuffers();
  88. }
  89. // -------------------------------------------------------------------
  90. // Information (base)
  91. PluginType getType() const noexcept override
  92. {
  93. return PLUGIN_LV2;
  94. }
  95. PluginCategory getCategory() const noexcept override
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  98. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  99. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  100. if (LV2_IS_DELAY(cat1, cat2))
  101. return PLUGIN_CATEGORY_DELAY;
  102. if (LV2_IS_DISTORTION(cat1, cat2))
  103. return PLUGIN_CATEGORY_OTHER;
  104. if (LV2_IS_DYNAMICS(cat1, cat2))
  105. return PLUGIN_CATEGORY_DYNAMICS;
  106. if (LV2_IS_EQ(cat1, cat2))
  107. return PLUGIN_CATEGORY_EQ;
  108. if (LV2_IS_FILTER(cat1, cat2))
  109. return PLUGIN_CATEGORY_FILTER;
  110. if (LV2_IS_GENERATOR(cat1, cat2))
  111. return PLUGIN_CATEGORY_SYNTH;
  112. if (LV2_IS_MODULATOR(cat1, cat2))
  113. return PLUGIN_CATEGORY_MODULATOR;
  114. if (LV2_IS_REVERB(cat1, cat2))
  115. return PLUGIN_CATEGORY_DELAY;
  116. if (LV2_IS_SIMULATOR(cat1, cat2))
  117. return PLUGIN_CATEGORY_OTHER;
  118. if (LV2_IS_SPATIAL(cat1, cat2))
  119. return PLUGIN_CATEGORY_OTHER;
  120. if (LV2_IS_SPECTRAL(cat1, cat2))
  121. return PLUGIN_CATEGORY_UTILITY;
  122. if (LV2_IS_UTILITY(cat1, cat2))
  123. return PLUGIN_CATEGORY_UTILITY;
  124. return CarlaPlugin::getCategory();
  125. }
  126. long getUniqueId() const noexcept override
  127. {
  128. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  129. return fRdfDescriptor->UniqueID;
  130. }
  131. // -------------------------------------------------------------------
  132. // Information (count)
  133. uint32_t getMidiInCount() const noexcept override
  134. {
  135. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  136. uint32_t count = 0;
  137. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  138. {
  139. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  140. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  141. count += 1;
  142. }
  143. return count;
  144. }
  145. uint32_t getMidiOutCount() const noexcept override
  146. {
  147. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  148. uint32_t count = 0;
  149. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  150. {
  151. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  152. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  153. count += 1;
  154. }
  155. return count;
  156. }
  157. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  160. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  161. const int32_t rindex(pData->param.data[parameterId].rindex);
  162. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  163. {
  164. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  165. return port.ScalePointCount;
  166. }
  167. return 0;
  168. }
  169. // -------------------------------------------------------------------
  170. // Information (current data)
  171. // nothing
  172. // -------------------------------------------------------------------
  173. // Information (per-plugin data)
  174. unsigned int getOptionsAvailable() const noexcept override
  175. {
  176. const uint32_t hasMidiIn(getMidiInCount() > 0);
  177. unsigned int options = 0x0;
  178. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  179. if (! (hasMidiIn || needsFixedBuffer()))
  180. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  181. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  182. {
  183. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  184. options |= PLUGIN_OPTION_FORCE_STEREO;
  185. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  186. options |= PLUGIN_OPTION_FORCE_STEREO;
  187. }
  188. if (hasMidiIn)
  189. {
  190. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  191. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  192. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  193. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  194. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  195. }
  196. return options;
  197. }
  198. float getParameterValue(const uint32_t parameterId) const noexcept override
  199. {
  200. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  201. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  202. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  203. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  204. return fParamBuffers[parameterId];
  205. }
  206. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  207. {
  208. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  209. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  210. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  211. const int32_t rindex(pData->param.data[parameterId].rindex);
  212. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  213. {
  214. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  215. if (scalePointId < port.ScalePointCount)
  216. {
  217. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  218. return portScalePoint.Value;
  219. }
  220. }
  221. return 0.0f;
  222. }
  223. void getLabel(char* const strBuf) const noexcept override
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  226. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  227. if (fRdfDescriptor->URI != nullptr)
  228. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  229. else
  230. CarlaPlugin::getLabel(strBuf);
  231. }
  232. void getMaker(char* const strBuf) const noexcept override
  233. {
  234. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  235. if (fRdfDescriptor->Author != nullptr)
  236. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  237. else
  238. CarlaPlugin::getMaker(strBuf);
  239. }
  240. void getCopyright(char* const strBuf) const noexcept override
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  243. if (fRdfDescriptor->License != nullptr)
  244. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  245. else
  246. CarlaPlugin::getCopyright(strBuf);
  247. }
  248. void getRealName(char* const strBuf) const noexcept override
  249. {
  250. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  251. if (fRdfDescriptor->Name != nullptr)
  252. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  253. else
  254. CarlaPlugin::getRealName(strBuf);
  255. }
  256. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  257. {
  258. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  259. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  260. const int32_t rindex(pData->param.data[parameterId].rindex);
  261. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  262. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  263. else
  264. CarlaPlugin::getParameterName(parameterId, strBuf);
  265. }
  266. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  267. {
  268. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  269. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  270. const int32_t rindex(pData->param.data[parameterId].rindex);
  271. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  272. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  273. else
  274. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  275. }
  276. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  277. {
  278. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  279. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  280. const int32_t rindex(pData->param.data[parameterId].rindex);
  281. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  282. {
  283. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  284. if (LV2_HAVE_PORT_UNIT_SYMBOL(port.Unit.Hints) && port.Unit.Symbol != nullptr)
  285. {
  286. std::strncpy(strBuf, port.Unit.Symbol, STR_MAX);
  287. return;
  288. }
  289. else if (LV2_HAVE_PORT_UNIT_UNIT(port.Unit.Hints))
  290. {
  291. switch (port.Unit.Unit)
  292. {
  293. case LV2_PORT_UNIT_BAR:
  294. std::strncpy(strBuf, "bars", STR_MAX);
  295. return;
  296. case LV2_PORT_UNIT_BEAT:
  297. std::strncpy(strBuf, "beats", STR_MAX);
  298. return;
  299. case LV2_PORT_UNIT_BPM:
  300. std::strncpy(strBuf, "BPM", STR_MAX);
  301. return;
  302. case LV2_PORT_UNIT_CENT:
  303. std::strncpy(strBuf, "ct", STR_MAX);
  304. return;
  305. case LV2_PORT_UNIT_CM:
  306. std::strncpy(strBuf, "cm", STR_MAX);
  307. return;
  308. case LV2_PORT_UNIT_COEF:
  309. std::strncpy(strBuf, "(coef)", STR_MAX);
  310. return;
  311. case LV2_PORT_UNIT_DB:
  312. std::strncpy(strBuf, "dB", STR_MAX);
  313. return;
  314. case LV2_PORT_UNIT_DEGREE:
  315. std::strncpy(strBuf, "deg", STR_MAX);
  316. return;
  317. case LV2_PORT_UNIT_FRAME:
  318. std::strncpy(strBuf, "frames", STR_MAX);
  319. return;
  320. case LV2_PORT_UNIT_HZ:
  321. std::strncpy(strBuf, "Hz", STR_MAX);
  322. return;
  323. case LV2_PORT_UNIT_INCH:
  324. std::strncpy(strBuf, "in", STR_MAX);
  325. return;
  326. case LV2_PORT_UNIT_KHZ:
  327. std::strncpy(strBuf, "kHz", STR_MAX);
  328. return;
  329. case LV2_PORT_UNIT_KM:
  330. std::strncpy(strBuf, "km", STR_MAX);
  331. return;
  332. case LV2_PORT_UNIT_M:
  333. std::strncpy(strBuf, "m", STR_MAX);
  334. return;
  335. case LV2_PORT_UNIT_MHZ:
  336. std::strncpy(strBuf, "MHz", STR_MAX);
  337. return;
  338. case LV2_PORT_UNIT_MIDINOTE:
  339. std::strncpy(strBuf, "note", STR_MAX);
  340. return;
  341. case LV2_PORT_UNIT_MILE:
  342. std::strncpy(strBuf, "mi", STR_MAX);
  343. return;
  344. case LV2_PORT_UNIT_MIN:
  345. std::strncpy(strBuf, "min", STR_MAX);
  346. return;
  347. case LV2_PORT_UNIT_MM:
  348. std::strncpy(strBuf, "mm", STR_MAX);
  349. return;
  350. case LV2_PORT_UNIT_MS:
  351. std::strncpy(strBuf, "ms", STR_MAX);
  352. return;
  353. case LV2_PORT_UNIT_OCT:
  354. std::strncpy(strBuf, "oct", STR_MAX);
  355. return;
  356. case LV2_PORT_UNIT_PC:
  357. std::strncpy(strBuf, "%", STR_MAX);
  358. return;
  359. case LV2_PORT_UNIT_S:
  360. std::strncpy(strBuf, "s", STR_MAX);
  361. return;
  362. case LV2_PORT_UNIT_SEMITONE:
  363. std::strncpy(strBuf, "semi", STR_MAX);
  364. return;
  365. }
  366. }
  367. }
  368. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  369. }
  370. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  371. {
  372. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  373. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  374. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  375. const int32_t rindex(pData->param.data[parameterId].rindex);
  376. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  377. {
  378. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  379. if (scalePointId < port.ScalePointCount)
  380. {
  381. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  382. if (portScalePoint.Label != nullptr)
  383. {
  384. std::strncpy(strBuf, portScalePoint.Label, STR_MAX);
  385. return;
  386. }
  387. }
  388. }
  389. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  390. }
  391. // -------------------------------------------------------------------
  392. // Set data (plugin-specific stuff)
  393. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  394. {
  395. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  396. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  397. fParamBuffers[parameterId] = fixedValue;
  398. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  399. }
  400. // -------------------------------------------------------------------
  401. // Plugin state
  402. void reload() override
  403. {
  404. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  405. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  406. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  407. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  408. carla_debug("Lv2Plugin::reload() - start");
  409. const EngineProcessMode processMode(pData->engine->getProccessMode());
  410. // Safely disable plugin for reload
  411. const ScopedDisabler sd(this);
  412. if (pData->active)
  413. deactivate();
  414. clearBuffers();
  415. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  416. const uint32_t portCount(static_cast<uint32_t>(fRdfDescriptor->PortCount));
  417. uint32_t aIns, aOuts, params, j;
  418. aIns = aOuts = params = 0;
  419. bool forcedStereoIn, forcedStereoOut;
  420. forcedStereoIn = forcedStereoOut = false;
  421. bool needsCtrlIn, needsCtrlOut;
  422. needsCtrlIn = needsCtrlOut = false;
  423. for (uint32_t i=0; i < portCount; ++i)
  424. {
  425. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  426. if (LV2_IS_PORT_AUDIO(portTypes))
  427. {
  428. if (LV2_IS_PORT_INPUT(portTypes))
  429. aIns += 1;
  430. else if (LV2_IS_PORT_OUTPUT(portTypes))
  431. aOuts += 1;
  432. }
  433. else if (LV2_IS_PORT_CONTROL(portTypes))
  434. params += 1;
  435. }
  436. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) /*&& fExt.state == nullptr && fExt.worker == nullptr*/)
  437. {
  438. if (fHandle2 == nullptr)
  439. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  440. if (fHandle2 != nullptr)
  441. {
  442. if (aIns == 1)
  443. {
  444. aIns = 2;
  445. forcedStereoIn = true;
  446. }
  447. if (aOuts == 1)
  448. {
  449. aOuts = 2;
  450. forcedStereoOut = true;
  451. }
  452. }
  453. }
  454. if (aIns > 0)
  455. {
  456. pData->audioIn.createNew(aIns);
  457. fAudioInBuffers = new float*[aIns];
  458. for (uint32_t i=0; i < aIns; ++i)
  459. fAudioInBuffers[i] = nullptr;
  460. }
  461. if (aOuts > 0)
  462. {
  463. pData->audioOut.createNew(aOuts);
  464. fAudioOutBuffers = new float*[aOuts];
  465. needsCtrlIn = true;
  466. for (uint32_t i=0; i < aOuts; ++i)
  467. fAudioOutBuffers[i] = nullptr;
  468. }
  469. if (params > 0)
  470. {
  471. pData->param.createNew(params, true);
  472. fParamBuffers = new float[params];
  473. FLOAT_CLEAR(fParamBuffers, params);
  474. }
  475. const uint portNameSize(pData->engine->getMaxPortNameSize());
  476. CarlaString portName;
  477. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  478. {
  479. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  480. portName.clear();
  481. if (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_CV(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  482. {
  483. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  484. {
  485. portName = pData->name;
  486. portName += ":";
  487. }
  488. portName += fRdfDescriptor->Ports[i].Name;
  489. portName.truncate(portNameSize);
  490. }
  491. if (LV2_IS_PORT_AUDIO(portTypes))
  492. {
  493. if (LV2_IS_PORT_INPUT(portTypes))
  494. {
  495. j = iAudioIn++;
  496. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  497. pData->audioIn.ports[j].rindex = i;
  498. if (forcedStereoIn)
  499. {
  500. portName += "_2";
  501. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  502. pData->audioIn.ports[1].rindex = i;
  503. }
  504. }
  505. else if (LV2_IS_PORT_OUTPUT(portTypes))
  506. {
  507. j = iAudioOut++;
  508. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  509. pData->audioOut.ports[j].rindex = i;
  510. if (forcedStereoOut)
  511. {
  512. portName += "_2";
  513. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  514. pData->audioOut.ports[1].rindex = i;
  515. }
  516. }
  517. else
  518. carla_stderr("WARNING - Got a broken Port (Audio, but not input or output)");
  519. }
  520. else if (LV2_IS_PORT_CONTROL(portTypes))
  521. {
  522. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  523. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  524. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  525. j = iCtrl++;
  526. pData->param.data[j].type = PARAMETER_UNKNOWN;
  527. pData->param.data[j].hints = 0x0;
  528. pData->param.data[j].index = j;
  529. pData->param.data[j].rindex = i;
  530. pData->param.data[j].midiCC = -1;
  531. pData->param.data[j].midiChannel = 0;
  532. float min, max, def, step, stepSmall, stepLarge;
  533. // min value
  534. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  535. min = portPoints.Minimum;
  536. else
  537. min = 0.0f;
  538. // max value
  539. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  540. max = portPoints.Maximum;
  541. else
  542. max = 1.0f;
  543. if (min > max)
  544. max = min;
  545. else if (max < min)
  546. min = max;
  547. // stupid hack for ir.lv2 (broken plugin)
  548. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  549. {
  550. min = 0.0f;
  551. max = (float)0xffffff;
  552. }
  553. if (max - min == 0.0f)
  554. {
  555. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  556. max = min + 0.1f;
  557. }
  558. // default value
  559. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  560. {
  561. def = portPoints.Default;
  562. }
  563. else
  564. {
  565. // no default value
  566. if (min < 0.0f && max > 0.0f)
  567. def = 0.0f;
  568. else
  569. def = min;
  570. }
  571. if (def < min)
  572. def = min;
  573. else if (def > max)
  574. def = max;
  575. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  576. {
  577. min *= sampleRate;
  578. max *= sampleRate;
  579. def *= sampleRate;
  580. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  581. }
  582. if (LV2_IS_PORT_TOGGLED(portProps))
  583. {
  584. step = max - min;
  585. stepSmall = step;
  586. stepLarge = step;
  587. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  588. }
  589. else if (LV2_IS_PORT_INTEGER(portProps))
  590. {
  591. step = 1.0f;
  592. stepSmall = 1.0f;
  593. stepLarge = 10.0f;
  594. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  595. }
  596. else
  597. {
  598. float range = max - min;
  599. step = range/100.0f;
  600. stepSmall = range/1000.0f;
  601. stepLarge = range/10.0f;
  602. }
  603. if (LV2_IS_PORT_INPUT(portTypes))
  604. {
  605. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  606. {
  607. carla_stderr("Plugin has latency input port, this should not happen!");
  608. }
  609. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  610. {
  611. def = sampleRate;
  612. step = 1.0f;
  613. stepSmall = 1.0f;
  614. stepLarge = 1.0f;
  615. //pData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  616. pData->param.data[j].hints = 0x0;
  617. }
  618. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  619. {
  620. //pData->param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  621. }
  622. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  623. {
  624. //pData->param.data[j].type = PARAMETER_LV2_TIME;
  625. }
  626. else
  627. {
  628. pData->param.data[j].type = PARAMETER_INPUT;
  629. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  630. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  631. needsCtrlIn = true;
  632. }
  633. // MIDI CC value
  634. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  635. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  636. {
  637. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  638. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  639. }
  640. }
  641. else if (LV2_IS_PORT_OUTPUT(portTypes))
  642. {
  643. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  644. {
  645. min = 0.0f;
  646. max = sampleRate;
  647. def = 0.0f;
  648. step = 1.0f;
  649. stepSmall = 1.0f;
  650. stepLarge = 1.0f;
  651. //pData->param.data[j].type = PARAMETER_LATENCY;
  652. pData->param.data[j].hints = 0x0;
  653. }
  654. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  655. {
  656. def = sampleRate;
  657. step = 1.0f;
  658. stepSmall = 1.0f;
  659. stepLarge = 1.0f;
  660. //pData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  661. pData->param.data[j].hints = 0x0;
  662. }
  663. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  664. {
  665. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  666. }
  667. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  668. {
  669. //pData->param.data[j].type = PARAMETER_LV2_TIME;
  670. }
  671. else
  672. {
  673. pData->param.data[j].type = PARAMETER_OUTPUT;
  674. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  675. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  676. needsCtrlOut = true;
  677. }
  678. }
  679. else
  680. {
  681. pData->param.data[j].type = PARAMETER_UNKNOWN;
  682. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  683. }
  684. // extra parameter hints
  685. if (LV2_IS_PORT_ENUMERATION(portProps))
  686. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  687. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  688. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  689. if (LV2_IS_PORT_TRIGGER(portProps))
  690. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  691. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  692. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  693. // check if parameter is not enabled or automable
  694. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  695. {
  696. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  697. }
  698. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  699. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  700. pData->param.ranges[j].min = min;
  701. pData->param.ranges[j].max = max;
  702. pData->param.ranges[j].def = def;
  703. pData->param.ranges[j].step = step;
  704. pData->param.ranges[j].stepSmall = stepSmall;
  705. pData->param.ranges[j].stepLarge = stepLarge;
  706. // Start parameters in their default values
  707. //if (pData->param.data[j].type != PARAMETER_LV2_FREEWHEEL)
  708. fParamBuffers[j] = def;
  709. //else
  710. // fParamBuffers[j] = min;
  711. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  712. if (fHandle2 != nullptr)
  713. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  714. }
  715. else
  716. {
  717. // Port Type not supported, but it's optional anyway
  718. fDescriptor->connect_port(fHandle, i, nullptr);
  719. if (fHandle2 != nullptr)
  720. fDescriptor->connect_port(fHandle2, i, nullptr);
  721. }
  722. }
  723. if (needsCtrlIn)
  724. {
  725. portName.clear();
  726. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  727. {
  728. portName = pData->name;
  729. portName += ":";
  730. }
  731. portName += "events-in";
  732. portName.truncate(portNameSize);
  733. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  734. }
  735. if (needsCtrlOut)
  736. {
  737. portName.clear();
  738. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  739. {
  740. portName = pData->name;
  741. portName += ":";
  742. }
  743. portName += "events-out";
  744. portName.truncate(portNameSize);
  745. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  746. }
  747. if (forcedStereoIn || forcedStereoOut)
  748. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  749. else
  750. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  751. // plugin hints
  752. pData->hints = 0x0;
  753. if (isRealtimeSafe())
  754. pData->hints |= PLUGIN_IS_RTSAFE;
  755. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  756. pData->hints |= PLUGIN_IS_SYNTH;
  757. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  758. pData->hints |= PLUGIN_CAN_DRYWET;
  759. if (aOuts > 0)
  760. pData->hints |= PLUGIN_CAN_VOLUME;
  761. if (aOuts >= 2 && aOuts % 2 == 0)
  762. pData->hints |= PLUGIN_CAN_BALANCE;
  763. // extra plugin hints
  764. pData->extraHints &= ~PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  765. bufferSizeChanged(pData->engine->getBufferSize());
  766. reloadPrograms(true);
  767. if (pData->active)
  768. activate();
  769. carla_debug("Lv2Plugin::reload() - end");
  770. }
  771. // -------------------------------------------------------------------
  772. // Plugin processing
  773. void activate() noexcept override
  774. {
  775. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  776. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  777. if (fDescriptor->activate != nullptr)
  778. {
  779. try {
  780. fDescriptor->activate(fHandle);
  781. } catch(...) {}
  782. if (fHandle2 != nullptr)
  783. {
  784. try {
  785. fDescriptor->activate(fHandle2);
  786. } catch(...) {}
  787. }
  788. }
  789. }
  790. void deactivate() noexcept override
  791. {
  792. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  793. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  794. if (fDescriptor->deactivate != nullptr)
  795. {
  796. try {
  797. fDescriptor->deactivate(fHandle);
  798. } catch(...) {}
  799. if (fHandle2 != nullptr)
  800. {
  801. try {
  802. fDescriptor->deactivate(fHandle2);
  803. } catch(...) {}
  804. }
  805. }
  806. }
  807. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  808. {
  809. uint32_t i, k;
  810. // --------------------------------------------------------------------------------------------------------
  811. // Check if active
  812. if (! pData->active)
  813. {
  814. // disable any output sound
  815. for (i=0; i < pData->audioOut.count; ++i)
  816. FLOAT_CLEAR(outBuffer[i], frames);
  817. return;
  818. }
  819. // --------------------------------------------------------------------------------------------------------
  820. // Plugin processing (no events)
  821. {
  822. processSingle(inBuffer, outBuffer, frames, 0);
  823. } // End of Plugin processing (no events)
  824. // --------------------------------------------------------------------------------------------------------
  825. // Control Output
  826. if (pData->event.portOut != nullptr)
  827. {
  828. uint8_t channel;
  829. uint16_t param;
  830. float value;
  831. for (k=0; k < pData->param.count; ++k)
  832. {
  833. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  834. continue;
  835. if (pData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS)
  836. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  837. if (pData->param.data[k].midiCC > 0)
  838. {
  839. channel = pData->param.data[k].midiChannel;
  840. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  841. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  842. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  843. }
  844. }
  845. } // End of Control Output
  846. CARLA_PROCESS_CONTINUE_CHECK;
  847. // --------------------------------------------------------------------------------------------------------
  848. }
  849. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  850. {
  851. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  852. if (pData->audioIn.count > 0)
  853. {
  854. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  855. }
  856. if (pData->audioOut.count > 0)
  857. {
  858. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  859. }
  860. uint32_t i, k;
  861. // --------------------------------------------------------------------------------------------------------
  862. // Try lock, silence otherwise
  863. if (pData->engine->isOffline())
  864. {
  865. pData->singleMutex.lock();
  866. }
  867. else if (! pData->singleMutex.tryLock())
  868. {
  869. for (i=0; i < pData->audioOut.count; ++i)
  870. {
  871. for (k=0; k < frames; ++k)
  872. outBuffer[i][k+timeOffset] = 0.0f;
  873. }
  874. return false;
  875. }
  876. // --------------------------------------------------------------------------------------------------------
  877. // Reset audio buffers
  878. for (i=0; i < pData->audioIn.count; ++i)
  879. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  880. for (i=0; i < pData->audioOut.count; ++i)
  881. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  882. // --------------------------------------------------------------------------------------------------------
  883. // Run plugin
  884. fDescriptor->run(fHandle, frames);
  885. if (fHandle2 != nullptr)
  886. fDescriptor->run(fHandle2, frames);
  887. // --------------------------------------------------------------------------------------------------------
  888. // Special Parameters
  889. for (k=0; k < pData->param.count; ++k)
  890. {
  891. if (pData->param.data[k].type != PARAMETER_INPUT)
  892. continue;
  893. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  894. {
  895. if (fParamBuffers[k] != pData->param.ranges[k].def)
  896. {
  897. fParamBuffers[k] = pData->param.ranges[k].def;
  898. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  899. }
  900. }
  901. }
  902. pData->postRtEvents.trySplice();
  903. #ifndef BUILD_BRIDGE
  904. // --------------------------------------------------------------------------------------------------------
  905. // Post-processing (dry/wet, volume and balance)
  906. {
  907. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  908. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  909. bool isPair;
  910. float bufValue, oldBufLeft[doBalance ? frames : 1];
  911. for (i=0; i < pData->audioOut.count; ++i)
  912. {
  913. // Dry/Wet
  914. if (doDryWet)
  915. {
  916. for (k=0; k < frames; ++k)
  917. {
  918. // TODO
  919. //if (k < pData->latency && pData->latency < frames)
  920. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  921. //else
  922. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  923. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  924. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  925. }
  926. }
  927. // Balance
  928. if (doBalance)
  929. {
  930. isPair = (i % 2 == 0);
  931. if (isPair)
  932. {
  933. CARLA_ASSERT(i+1 < pData->audioOut.count);
  934. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  935. }
  936. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  937. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  938. for (k=0; k < frames; ++k)
  939. {
  940. if (isPair)
  941. {
  942. // left
  943. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  944. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  945. }
  946. else
  947. {
  948. // right
  949. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  950. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  951. }
  952. }
  953. }
  954. // Volume (and buffer copy)
  955. {
  956. for (k=0; k < frames; ++k)
  957. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  958. }
  959. }
  960. } // End of Post-processing
  961. #else
  962. for (i=0; i < pData->audioOut.count; ++i)
  963. {
  964. for (k=0; k < frames; ++k)
  965. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  966. }
  967. #endif
  968. // --------------------------------------------------------------------------------------------------------
  969. pData->singleMutex.unlock();
  970. return true;
  971. }
  972. void bufferSizeChanged(const uint32_t newBufferSize) override
  973. {
  974. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  975. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  976. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  977. {
  978. if (fAudioInBuffers[i] != nullptr)
  979. delete[] fAudioInBuffers[i];
  980. fAudioInBuffers[i] = new float[newBufferSize];
  981. }
  982. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  983. {
  984. if (fAudioOutBuffers[i] != nullptr)
  985. delete[] fAudioOutBuffers[i];
  986. fAudioOutBuffers[i] = new float[newBufferSize];
  987. }
  988. if (fHandle2 == nullptr)
  989. {
  990. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  991. {
  992. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  993. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  994. }
  995. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  996. {
  997. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  998. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  999. }
  1000. }
  1001. else
  1002. {
  1003. if (pData->audioIn.count > 0)
  1004. {
  1005. CARLA_ASSERT(pData->audioIn.count == 2);
  1006. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1007. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1008. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1009. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1010. }
  1011. if (pData->audioOut.count > 0)
  1012. {
  1013. CARLA_ASSERT(pData->audioOut.count == 2);
  1014. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1015. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1016. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1017. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1018. }
  1019. }
  1020. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  1021. }
  1022. void sampleRateChanged(const double newSampleRate) override
  1023. {
  1024. CARLA_ASSERT_INT(newSampleRate > 0.0, int(newSampleRate));
  1025. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  1026. for (uint32_t k=0; k < pData->param.count; ++k)
  1027. {
  1028. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  1029. {
  1030. fParamBuffers[k] = float(newSampleRate);
  1031. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  1032. break;
  1033. }
  1034. }
  1035. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  1036. }
  1037. void offlineModeChanged(const bool isOffline) override
  1038. {
  1039. for (uint32_t k=0; k < pData->param.count; ++k)
  1040. {
  1041. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1042. {
  1043. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  1044. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. // -------------------------------------------------------------------
  1050. // Plugin buffers
  1051. void clearBuffers() override
  1052. {
  1053. carla_debug("Lv2Plugin::clearBuffers() - start");
  1054. if (fAudioInBuffers != nullptr)
  1055. {
  1056. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1057. {
  1058. if (fAudioInBuffers[i] != nullptr)
  1059. {
  1060. delete[] fAudioInBuffers[i];
  1061. fAudioInBuffers[i] = nullptr;
  1062. }
  1063. }
  1064. delete[] fAudioInBuffers;
  1065. fAudioInBuffers = nullptr;
  1066. }
  1067. if (fAudioOutBuffers != nullptr)
  1068. {
  1069. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1070. {
  1071. if (fAudioOutBuffers[i] != nullptr)
  1072. {
  1073. delete[] fAudioOutBuffers[i];
  1074. fAudioOutBuffers[i] = nullptr;
  1075. }
  1076. }
  1077. delete[] fAudioOutBuffers;
  1078. fAudioOutBuffers = nullptr;
  1079. }
  1080. if (fParamBuffers != nullptr)
  1081. {
  1082. delete[] fParamBuffers;
  1083. fParamBuffers = nullptr;
  1084. }
  1085. CarlaPlugin::clearBuffers();
  1086. carla_debug("Lv2Plugin::clearBuffers() - end");
  1087. }
  1088. // -------------------------------------------------------------------
  1089. bool isRealtimeSafe() const
  1090. {
  1091. CARLA_ASSERT(fRdfDescriptor != nullptr);
  1092. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  1093. {
  1094. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  1095. return true;
  1096. }
  1097. return false;
  1098. }
  1099. bool needsFixedBuffer() const
  1100. {
  1101. CARLA_ASSERT(fRdfDescriptor != nullptr);
  1102. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  1103. {
  1104. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  1105. return true;
  1106. }
  1107. return false;
  1108. }
  1109. // -------------------------------------------------------------------
  1110. public:
  1111. bool init(const char* const bundle, const char* const name, const char* const uri)
  1112. {
  1113. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1114. // ---------------------------------------------------------------
  1115. // first checks
  1116. if (pData->client != nullptr)
  1117. {
  1118. pData->engine->setLastError("Plugin client is already registered");
  1119. return false;
  1120. }
  1121. if (bundle == nullptr || bundle[0] == '\0')
  1122. {
  1123. pData->engine->setLastError("null bundle");
  1124. return false;
  1125. }
  1126. if (uri == nullptr || uri[0] == '\0')
  1127. {
  1128. pData->engine->setLastError("null uri");
  1129. return false;
  1130. }
  1131. // ---------------------------------------------------------------
  1132. // get plugin from lv2_rdf (lilv)
  1133. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  1134. // Convert bundle filename to URI
  1135. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  1136. if (! qBundle.endsWith(OS_SEP_STR))
  1137. qBundle += OS_SEP_STR;
  1138. // Load bundle
  1139. Lilv::Node lilvBundle(lv2World.new_uri(qBundle.toUtf8().constData()));
  1140. lv2World.load_bundle(lilvBundle);
  1141. fRdfDescriptor = lv2_rdf_new(uri, true);
  1142. if (fRdfDescriptor == nullptr)
  1143. {
  1144. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  1145. return false;
  1146. }
  1147. // ---------------------------------------------------------------
  1148. // open DLL
  1149. if (! pData->libOpen(fRdfDescriptor->Binary))
  1150. {
  1151. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  1152. return false;
  1153. }
  1154. // ---------------------------------------------------------------
  1155. // get DLL main entry
  1156. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  1157. if (descFn == nullptr)
  1158. {
  1159. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  1160. return false;
  1161. }
  1162. // -----------------------------------------------------------
  1163. // get descriptor that matches URI
  1164. uint32_t i = 0;
  1165. while ((fDescriptor = descFn(i++)))
  1166. {
  1167. carla_debug("LV2 Init @%i => '%s' vs '%s'", i, fDescriptor->URI, uri);
  1168. if (std::strcmp(fDescriptor->URI, uri) == 0)
  1169. break;
  1170. }
  1171. if (fDescriptor == nullptr)
  1172. {
  1173. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  1174. return false;
  1175. }
  1176. // ---------------------------------------------------------------
  1177. // check supported port-types and features
  1178. bool canContinue = true;
  1179. // Check supported ports
  1180. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  1181. {
  1182. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  1183. if (! is_lv2_port_supported(portTypes))
  1184. {
  1185. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  1186. {
  1187. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  1188. canContinue = false;
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. // Check supported features
  1194. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  1195. {
  1196. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  1197. {
  1198. // QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  1199. // pData->engine->setLastError(msg.toUtf8().constData());
  1200. canContinue = false;
  1201. break;
  1202. }
  1203. }
  1204. if (! canContinue)
  1205. {
  1206. // error already set
  1207. return false;
  1208. }
  1209. // ---------------------------------------------------------------
  1210. // get info
  1211. if (name != nullptr && name[0] != '\0')
  1212. pData->name = pData->engine->getUniquePluginName(name);
  1213. else
  1214. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  1215. // ---------------------------------------------------------------
  1216. // register client
  1217. pData->client = pData->engine->addClient(this);
  1218. if (pData->client == nullptr || ! pData->client->isOk())
  1219. {
  1220. pData->engine->setLastError("Failed to register plugin client");
  1221. return false;
  1222. }
  1223. // ---------------------------------------------------------------
  1224. // initialize plugin
  1225. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  1226. if (fHandle == nullptr)
  1227. {
  1228. pData->engine->setLastError("Plugin failed to initialize");
  1229. return false;
  1230. }
  1231. // ---------------------------------------------------------------
  1232. // load plugin settings
  1233. {
  1234. // set default options
  1235. pData->options = 0x0;
  1236. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1237. if (getMidiInCount() > 0 || needsFixedBuffer())
  1238. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1239. if (pData->engine->getOptions().forceStereo)
  1240. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1241. if (getMidiInCount() > 0)
  1242. {
  1243. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1244. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1245. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1246. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1247. }
  1248. // set identifier string
  1249. CarlaString identifier("V2/");
  1250. identifier += uri;
  1251. // load settings
  1252. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1253. // ignore settings, we need this anyway
  1254. if (getMidiInCount() > 0 || needsFixedBuffer())
  1255. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1256. }
  1257. // ---------------------------------------------------------------
  1258. // gui stuff
  1259. if (fRdfDescriptor->UICount == 0)
  1260. return true;
  1261. return true;
  1262. }
  1263. // -------------------------------------------------------------------
  1264. private:
  1265. LV2_Handle fHandle;
  1266. LV2_Handle fHandle2;
  1267. LV2_Feature* fFeatures[kFeatureCount+1];
  1268. const LV2_Descriptor* fDescriptor;
  1269. const LV2_RDF_Descriptor* fRdfDescriptor;
  1270. float** fAudioInBuffers;
  1271. float** fAudioOutBuffers;
  1272. float* fParamBuffers;
  1273. // -------------------------------------------------------------------
  1274. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  1275. };
  1276. // -------------------------------------------------------------------------------------------------------------------
  1277. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  1278. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  1279. {
  1280. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  1281. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  1282. return 0;
  1283. // unused for now
  1284. (void)argv;
  1285. (void)plugin;
  1286. }
  1287. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  1288. {
  1289. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  1290. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  1291. return 0;
  1292. // unused for now
  1293. (void)argv;
  1294. (void)plugin;
  1295. }
  1296. #undef lv2PluginPtr
  1297. CARLA_BACKEND_END_NAMESPACE
  1298. #endif // WANT_LV2
  1299. // -------------------------------------------------------------------------------------------------------------------
  1300. CARLA_BACKEND_START_NAMESPACE
  1301. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  1302. {
  1303. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  1304. #ifdef WANT_LV2
  1305. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  1306. if (! plugin->init(init.filename, init.name, init.label))
  1307. {
  1308. delete plugin;
  1309. return nullptr;
  1310. }
  1311. plugin->reload();
  1312. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1313. {
  1314. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  1315. delete plugin;
  1316. return nullptr;
  1317. }
  1318. return plugin;
  1319. #else
  1320. init.engine->setLastError("LV2 support not available");
  1321. return nullptr;
  1322. #endif
  1323. }
  1324. CARLA_BACKEND_END_NAMESPACE
  1325. // -------------------------------------------------------------------------------------------------------------------