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.

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