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.

1811 lines
59KB

  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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_LV2
  19. #include "CarlaPluginGui.hpp"
  20. #include "CarlaLv2Utils.hpp"
  21. #include <QtCore/QDir>
  22. extern "C" {
  23. #include "rtmempool/rtmempool-lv2.h"
  24. }
  25. // -----------------------------------------------------
  26. // Our LV2 World class object
  27. Lv2WorldClass gLv2World;
  28. // -----------------------------------------------------
  29. CARLA_BACKEND_START_NAMESPACE
  30. #if 0
  31. }
  32. #endif
  33. /*!
  34. * @defgroup PluginHints Plugin Hints
  35. * @{
  36. */
  37. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x1000; //!< LV2 Plugin has Programs extension
  38. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x2000; //!< LV2 Plugin has State extension
  39. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x4000; //!< LV2 Plugin has Worker extension
  40. /**@}*/
  41. /*!
  42. * @defgroup ParameterHints Parameter Hints
  43. * @{
  44. */
  45. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000; //!< LV2 Parameter needs strict bounds
  46. const unsigned int PARAMETER_IS_TRIGGER = 0x2000; //!< LV2 Parameter is trigger (current value should be changed to its default after run())
  47. /**@}*/
  48. /*!
  49. * @defgroup Lv2EventTypes LV2 Event Data/Types
  50. *
  51. * Data and buffer types for LV2 EventData ports.
  52. * @{
  53. */
  54. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  55. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  56. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  57. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10;
  58. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  59. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  60. /**@}*/
  61. /*!
  62. * @defgroup Lv2UriMapIds LV2 URI Map Ids
  63. *
  64. * Static index list of the internal LV2 URI Map Ids.
  65. * @{
  66. */
  67. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 1;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 2;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 3;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 4;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 5;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 6;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 7;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 8;
  76. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 9;
  77. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 10;
  78. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 11;
  79. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 12;
  80. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 13;
  81. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 14;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 15;
  83. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 16;
  84. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 17;
  85. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 18;
  86. const uint32_t CARLA_URI_MAP_ID_COUNT = 19;
  87. /**@}*/
  88. /*!
  89. * @defgroup Lv2FeatureIds LV2 Feature Ids
  90. *
  91. * Static index list of the internal LV2 Feature Ids.
  92. * @{
  93. */
  94. const uint32_t kFeatureIdBufSizeBounded = 0;
  95. const uint32_t kFeatureIdBufSizeFixed = 1;
  96. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  97. const uint32_t kFeatureIdEvent = 3;
  98. const uint32_t kFeatureIdLogs = 4;
  99. const uint32_t kFeatureIdOptions = 5;
  100. const uint32_t kFeatureIdPrograms = 6;
  101. const uint32_t kFeatureIdRtMemPool = 7;
  102. const uint32_t kFeatureIdStateMakePath = 8;
  103. const uint32_t kFeatureIdStateMapPath = 9;
  104. const uint32_t kFeatureIdStrictBounds = 10;
  105. const uint32_t kFeatureIdUriMap = 11;
  106. const uint32_t kFeatureIdUridMap = 12;
  107. const uint32_t kFeatureIdUridUnmap = 13;
  108. const uint32_t kFeatureIdWorker = 14;
  109. const uint32_t kFeatureIdUiDataAccess = 15;
  110. const uint32_t kFeatureIdUiInstanceAccess = 16;
  111. const uint32_t kFeatureIdUiParent = 17;
  112. const uint32_t kFeatureIdUiPortMap = 18;
  113. const uint32_t kFeatureIdUiResize = 19;
  114. const uint32_t kFeatureIdExternalUi = 20;
  115. const uint32_t kFeatureIdExternalUiOld = 21;
  116. const uint32_t kFeatureCount = 22;
  117. /**@}*/
  118. struct Lv2EventData {
  119. uint32_t type;
  120. uint32_t rindex;
  121. CarlaEngineEventPort* port;
  122. union {
  123. LV2_Atom_Sequence* atom;
  124. LV2_Event_Buffer* event;
  125. LV2_MIDI* midi;
  126. };
  127. Lv2EventData()
  128. : type(0x0),
  129. rindex(0),
  130. port(nullptr) {}
  131. ~Lv2EventData()
  132. {
  133. CARLA_ASSERT(port == nullptr);
  134. }
  135. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2EventData)
  136. };
  137. struct Lv2PluginEventData {
  138. uint32_t count;
  139. Lv2EventData* data;
  140. Lv2PluginEventData()
  141. : count(0),
  142. data(nullptr) {}
  143. ~Lv2PluginEventData()
  144. {
  145. CARLA_ASSERT_INT(count == 0, count);
  146. CARLA_ASSERT(data == nullptr);
  147. }
  148. void createNew(const uint32_t newCount)
  149. {
  150. CARLA_ASSERT_INT(count == 0, count);
  151. CARLA_ASSERT(data == nullptr);
  152. CARLA_ASSERT_INT(newCount > 0, newCount);
  153. if (data != nullptr || newCount == 0)
  154. return;
  155. data = new Lv2EventData[newCount];
  156. count = newCount;
  157. }
  158. void clear()
  159. {
  160. if (data != nullptr)
  161. {
  162. for (uint32_t i=0; i < count; ++i)
  163. {
  164. if (data[i].port != nullptr)
  165. {
  166. delete data[i].port;
  167. data[i].port = nullptr;
  168. }
  169. }
  170. delete[] data;
  171. data = nullptr;
  172. }
  173. count = 0;
  174. }
  175. void initBuffers(CarlaEngine* const engine)
  176. {
  177. for (uint32_t i=0; i < count; ++i)
  178. {
  179. if (data[i].port != nullptr)
  180. data[i].port->initBuffer(engine);
  181. }
  182. }
  183. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2PluginEventData)
  184. };
  185. LV2_Atom_Event* getLv2AtomEvent(LV2_Atom_Sequence* const atom, const uint32_t offset)
  186. {
  187. return (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atom) + offset);
  188. }
  189. // -----------------------------------------------------
  190. class Lv2Plugin : public CarlaPlugin,
  191. public CarlaPluginGui::Callback
  192. {
  193. public:
  194. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  195. : CarlaPlugin(engine, id),
  196. fHandle(nullptr),
  197. fHandle2(nullptr),
  198. fFeatures{nullptr},
  199. fDescriptor(nullptr),
  200. fRdfDescriptor(nullptr),
  201. fAudioInBuffers(nullptr),
  202. fAudioOutBuffers(nullptr),
  203. fParamBuffers(nullptr)
  204. {
  205. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  206. kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  207. }
  208. ~Lv2Plugin() override
  209. {
  210. carla_debug("Lv2Plugin::~Lv2Plugin()");
  211. // close UI
  212. if (fHints & PLUGIN_HAS_GUI)
  213. {
  214. showGui(false);
  215. //if (fGui.isOsc)
  216. {
  217. // Wait a bit first, then force kill
  218. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  219. {
  220. carla_stderr("LV2 OSC-GUI thread still running, forcing termination now");
  221. kData->osc.thread.terminate();
  222. }
  223. }
  224. if (fUi.descriptor != nullptr)
  225. {
  226. if (fUi.descriptor->cleanup != nullptr && fUi.handle != nullptr)
  227. fUi.descriptor->cleanup(fUi.handle);
  228. fUi.handle = nullptr;
  229. fUi.descriptor = nullptr;
  230. }
  231. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  232. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  233. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  234. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  235. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  236. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  237. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  238. {
  239. const LV2_External_UI_Host* const uiHost((const LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data);
  240. if (uiHost->plugin_human_id != nullptr)
  241. delete[] uiHost->plugin_human_id;
  242. delete uiHost;
  243. }
  244. kData->uiLibClose();
  245. }
  246. kData->singleMutex.lock();
  247. kData->masterMutex.lock();
  248. if (kData->active)
  249. {
  250. deactivate();
  251. kData->active = false;
  252. }
  253. if (fDescriptor != nullptr)
  254. {
  255. if (fDescriptor->cleanup != nullptr)
  256. {
  257. if (fHandle != nullptr)
  258. fDescriptor->cleanup(fHandle);
  259. if (fHandle2 != nullptr)
  260. fDescriptor->cleanup(fHandle2);
  261. }
  262. fHandle = nullptr;
  263. fHandle2 = nullptr;
  264. fDescriptor = nullptr;
  265. }
  266. if (fRdfDescriptor != nullptr)
  267. {
  268. delete fRdfDescriptor;
  269. fRdfDescriptor = nullptr;
  270. }
  271. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  272. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  273. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  274. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  275. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  276. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  277. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  278. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  279. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  280. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  281. for (uint32_t i=0; i < kFeatureCount; ++i)
  282. {
  283. if (fFeatures[i] != nullptr)
  284. delete fFeatures[i];
  285. }
  286. clearBuffers();
  287. }
  288. // -------------------------------------------------------------------
  289. // Information (base)
  290. PluginType type() const override
  291. {
  292. return PLUGIN_LV2;
  293. }
  294. PluginCategory category() override
  295. {
  296. CARLA_ASSERT(fRdfDescriptor != nullptr);
  297. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  298. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  299. if (LV2_IS_DELAY(cat1, cat2))
  300. return PLUGIN_CATEGORY_DELAY;
  301. if (LV2_IS_DISTORTION(cat1, cat2))
  302. return PLUGIN_CATEGORY_OTHER;
  303. if (LV2_IS_DYNAMICS(cat1, cat2))
  304. return PLUGIN_CATEGORY_DYNAMICS;
  305. if (LV2_IS_EQ(cat1, cat2))
  306. return PLUGIN_CATEGORY_EQ;
  307. if (LV2_IS_FILTER(cat1, cat2))
  308. return PLUGIN_CATEGORY_FILTER;
  309. if (LV2_IS_GENERATOR(cat1, cat2))
  310. return PLUGIN_CATEGORY_SYNTH;
  311. if (LV2_IS_MODULATOR(cat1, cat2))
  312. return PLUGIN_CATEGORY_MODULATOR;
  313. if (LV2_IS_REVERB(cat1, cat2))
  314. return PLUGIN_CATEGORY_DELAY;
  315. if (LV2_IS_SIMULATOR(cat1, cat2))
  316. return PLUGIN_CATEGORY_OTHER;
  317. if (LV2_IS_SPATIAL(cat1, cat2))
  318. return PLUGIN_CATEGORY_OTHER;
  319. if (LV2_IS_SPECTRAL(cat1, cat2))
  320. return PLUGIN_CATEGORY_UTILITY;
  321. if (LV2_IS_UTILITY(cat1, cat2))
  322. return PLUGIN_CATEGORY_UTILITY;
  323. return getPluginCategoryFromName(fName);
  324. }
  325. long uniqueId() const override
  326. {
  327. CARLA_ASSERT(fRdfDescriptor != nullptr);
  328. return fRdfDescriptor->UniqueID;
  329. }
  330. // -------------------------------------------------------------------
  331. // Information (count)
  332. uint32_t midiInCount() const override
  333. {
  334. uint32_t i, count = 0;
  335. for (i=0; i < fEventsIn.count; ++i)
  336. {
  337. if (fEventsIn.data[i].type & CARLA_EVENT_TYPE_MIDI)
  338. count += 1;
  339. }
  340. return count;
  341. }
  342. uint32_t midiOutCount() const override
  343. {
  344. uint32_t i, count = 0;
  345. for (i=0; i < fEventsOut.count; ++i)
  346. {
  347. if (fEventsOut.data[i].type & CARLA_EVENT_TYPE_MIDI)
  348. count += 1;
  349. }
  350. return count;
  351. }
  352. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  353. {
  354. CARLA_ASSERT(fRdfDescriptor != nullptr);
  355. CARLA_ASSERT(parameterId < kData->param.count);
  356. const int32_t rindex(kData->param.data[parameterId].rindex);
  357. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  358. {
  359. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  360. return port.ScalePointCount;
  361. }
  362. return 0;
  363. }
  364. // -------------------------------------------------------------------
  365. // Information (current data)
  366. // nothing
  367. // -------------------------------------------------------------------
  368. // Information (per-plugin data)
  369. unsigned int availableOptions() override
  370. {
  371. unsigned int options = 0x0;
  372. options |= PLUGIN_OPTION_FIXED_BUFFER;
  373. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  374. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  375. {
  376. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  377. options |= PLUGIN_OPTION_FORCE_STEREO;
  378. else if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  379. options |= PLUGIN_OPTION_FORCE_STEREO;
  380. }
  381. //if (fDescriptor->midiIns > 0)
  382. {
  383. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  384. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  385. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  386. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  387. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  388. }
  389. return options;
  390. }
  391. float getParameterValue(const uint32_t parameterId) override
  392. {
  393. CARLA_ASSERT(fParamBuffers != nullptr);
  394. CARLA_ASSERT(parameterId < kData->param.count);
  395. return fParamBuffers[parameterId];
  396. }
  397. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  398. {
  399. CARLA_ASSERT(fRdfDescriptor != nullptr);
  400. CARLA_ASSERT(parameterId < kData->param.count);
  401. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  402. const int32_t rindex(kData->param.data[parameterId].rindex);
  403. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  404. {
  405. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  406. if (scalePointId < port.ScalePointCount)
  407. {
  408. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  409. return portScalePoint.Value;
  410. }
  411. }
  412. return 0.0f;
  413. }
  414. void getLabel(char* const strBuf) override
  415. {
  416. CARLA_ASSERT(fRdfDescriptor != nullptr);
  417. CARLA_ASSERT(fRdfDescriptor->URI != nullptr);
  418. if (fRdfDescriptor->URI != nullptr)
  419. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  420. else
  421. CarlaPlugin::getLabel(strBuf);
  422. }
  423. void getMaker(char* const strBuf) override
  424. {
  425. CARLA_ASSERT(fRdfDescriptor != nullptr);
  426. if (fRdfDescriptor->Author != nullptr)
  427. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  428. else
  429. CarlaPlugin::getMaker(strBuf);
  430. }
  431. void getCopyright(char* const strBuf) override
  432. {
  433. CARLA_ASSERT(fRdfDescriptor != nullptr);
  434. if (fRdfDescriptor->License != nullptr)
  435. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  436. else
  437. CarlaPlugin::getCopyright(strBuf);
  438. }
  439. void getRealName(char* const strBuf) override
  440. {
  441. CARLA_ASSERT(fRdfDescriptor != nullptr);
  442. if (fRdfDescriptor->Name != nullptr)
  443. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  444. else
  445. CarlaPlugin::getRealName(strBuf);
  446. }
  447. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  448. {
  449. CARLA_ASSERT(fRdfDescriptor != nullptr);
  450. CARLA_ASSERT(parameterId < kData->param.count);
  451. const int32_t rindex(kData->param.data[parameterId].rindex);
  452. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  453. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  454. else
  455. CarlaPlugin::getParameterName(parameterId, strBuf);
  456. }
  457. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) override
  458. {
  459. CARLA_ASSERT(fRdfDescriptor != nullptr);
  460. CARLA_ASSERT(parameterId < kData->param.count);
  461. const int32_t rindex(kData->param.data[parameterId].rindex);
  462. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  463. strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  464. else
  465. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  466. }
  467. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  468. {
  469. CARLA_ASSERT(fRdfDescriptor != nullptr);
  470. CARLA_ASSERT(parameterId < kData->param.count);
  471. const int32_t rindex(kData->param.data[parameterId].rindex);
  472. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  473. {
  474. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  475. if (LV2_HAVE_PORT_UNIT_SYMBOL(port.Unit.Hints) && port.Unit.Symbol)
  476. std::strncpy(strBuf, port.Unit.Symbol, STR_MAX);
  477. else if (LV2_HAVE_PORT_UNIT_UNIT(port.Unit.Hints))
  478. {
  479. switch (port.Unit.Unit)
  480. {
  481. case LV2_PORT_UNIT_BAR:
  482. std::strncpy(strBuf, "bars", STR_MAX);
  483. return;
  484. case LV2_PORT_UNIT_BEAT:
  485. std::strncpy(strBuf, "beats", STR_MAX);
  486. return;
  487. case LV2_PORT_UNIT_BPM:
  488. std::strncpy(strBuf, "BPM", STR_MAX);
  489. return;
  490. case LV2_PORT_UNIT_CENT:
  491. std::strncpy(strBuf, "ct", STR_MAX);
  492. return;
  493. case LV2_PORT_UNIT_CM:
  494. std::strncpy(strBuf, "cm", STR_MAX);
  495. return;
  496. case LV2_PORT_UNIT_COEF:
  497. std::strncpy(strBuf, "(coef)", STR_MAX);
  498. return;
  499. case LV2_PORT_UNIT_DB:
  500. std::strncpy(strBuf, "dB", STR_MAX);
  501. return;
  502. case LV2_PORT_UNIT_DEGREE:
  503. std::strncpy(strBuf, "deg", STR_MAX);
  504. return;
  505. case LV2_PORT_UNIT_FRAME:
  506. std::strncpy(strBuf, "frames", STR_MAX);
  507. return;
  508. case LV2_PORT_UNIT_HZ:
  509. std::strncpy(strBuf, "Hz", STR_MAX);
  510. return;
  511. case LV2_PORT_UNIT_INCH:
  512. std::strncpy(strBuf, "in", STR_MAX);
  513. return;
  514. case LV2_PORT_UNIT_KHZ:
  515. std::strncpy(strBuf, "kHz", STR_MAX);
  516. return;
  517. case LV2_PORT_UNIT_KM:
  518. std::strncpy(strBuf, "km", STR_MAX);
  519. return;
  520. case LV2_PORT_UNIT_M:
  521. std::strncpy(strBuf, "m", STR_MAX);
  522. return;
  523. case LV2_PORT_UNIT_MHZ:
  524. std::strncpy(strBuf, "MHz", STR_MAX);
  525. return;
  526. case LV2_PORT_UNIT_MIDINOTE:
  527. std::strncpy(strBuf, "note", STR_MAX);
  528. return;
  529. case LV2_PORT_UNIT_MILE:
  530. std::strncpy(strBuf, "mi", STR_MAX);
  531. return;
  532. case LV2_PORT_UNIT_MIN:
  533. std::strncpy(strBuf, "min", STR_MAX);
  534. return;
  535. case LV2_PORT_UNIT_MM:
  536. std::strncpy(strBuf, "mm", STR_MAX);
  537. return;
  538. case LV2_PORT_UNIT_MS:
  539. std::strncpy(strBuf, "ms", STR_MAX);
  540. return;
  541. case LV2_PORT_UNIT_OCT:
  542. std::strncpy(strBuf, "oct", STR_MAX);
  543. return;
  544. case LV2_PORT_UNIT_PC:
  545. std::strncpy(strBuf, "%", STR_MAX);
  546. return;
  547. case LV2_PORT_UNIT_S:
  548. std::strncpy(strBuf, "s", STR_MAX);
  549. return;
  550. case LV2_PORT_UNIT_SEMITONE:
  551. std::strncpy(strBuf, "semi", STR_MAX);
  552. return;
  553. }
  554. }
  555. }
  556. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  557. }
  558. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  559. {
  560. CARLA_ASSERT(fRdfDescriptor != nullptr);
  561. CARLA_ASSERT(parameterId < kData->param.count);
  562. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  563. const int32_t rindex(kData->param.data[parameterId].rindex);
  564. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  565. {
  566. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  567. if (scalePointId < port.ScalePointCount)
  568. {
  569. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  570. if (portScalePoint.Label != nullptr)
  571. {
  572. std::strncpy(strBuf, portScalePoint.Label, STR_MAX);
  573. return;
  574. }
  575. }
  576. }
  577. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  578. }
  579. // -------------------------------------------------------------------
  580. // Set data (state)
  581. void prepareForSave() override
  582. {
  583. CARLA_ASSERT(fHandle != nullptr);
  584. if (fExt.state != nullptr && fExt.state->save != nullptr)
  585. {
  586. //fExt.state->save(handle, carla_lv2_state_store, this, LV2_STATE_IS_POD, features);
  587. //if (fHandle2)
  588. // fExt.state->save(h2, carla_lv2_state_store, this, LV2_STATE_IS_POD, features);
  589. }
  590. }
  591. // -------------------------------------------------------------------
  592. // Set data (internal stuff)
  593. // nothing
  594. // -------------------------------------------------------------------
  595. // Set data (plugin-specific stuff)
  596. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  597. {
  598. CARLA_ASSERT(parameterId < kData->param.count);
  599. const float fixedValue(kData->param.fixValue(parameterId, value));
  600. fParamBuffers[parameterId] = fixedValue;
  601. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  602. }
  603. // -------------------------------------------------------------------
  604. // Set gui stuff
  605. void showGui(const bool yesNo) override
  606. {
  607. if (true)
  608. {
  609. if (yesNo)
  610. {
  611. kData->osc.thread.start();
  612. }
  613. else
  614. {
  615. if (kData->osc.data.target != nullptr)
  616. {
  617. osc_send_hide(&kData->osc.data);
  618. osc_send_quit(&kData->osc.data);
  619. kData->osc.data.free();
  620. }
  621. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  622. kData->osc.thread.terminate();
  623. }
  624. }
  625. else
  626. {
  627. if (yesNo)
  628. {
  629. if (kData->gui == nullptr)
  630. {
  631. // TODO
  632. CarlaPluginGui::Options guiOptions;
  633. guiOptions.parented = false;
  634. guiOptions.resizable = true;
  635. kData->gui = new CarlaPluginGui(kData->engine, this, guiOptions);
  636. }
  637. //void* const ptr = kData->gui->getContainerWinId();
  638. // open
  639. if (true)
  640. {
  641. kData->gui->setWindowTitle(QString("%1 (GUI)").arg((const char*)fName).toUtf8().constData());
  642. kData->gui->show();
  643. }
  644. else
  645. {
  646. if (kData->gui != nullptr)
  647. {
  648. kData->gui->close();
  649. delete kData->gui;
  650. kData->gui = nullptr;
  651. }
  652. kData->engine->callback(CALLBACK_ERROR, fId, 0, 0, 0.0f, "Plugin refused to open its own UI");
  653. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  654. return;
  655. }
  656. }
  657. else
  658. {
  659. // close here
  660. if (kData->gui != nullptr)
  661. {
  662. kData->gui->close();
  663. delete kData->gui;
  664. kData->gui = nullptr;
  665. }
  666. }
  667. }
  668. //fGui.isVisible = yesNo;
  669. }
  670. void idleGui() override
  671. {
  672. CarlaPlugin::idleGui();
  673. }
  674. // -------------------------------------------------------------------
  675. // Plugin processing
  676. void activate() override
  677. {
  678. CARLA_ASSERT(fDescriptor != nullptr);
  679. if (fDescriptor->activate != nullptr)
  680. {
  681. fDescriptor->activate(fHandle);
  682. if (fHandle2 != nullptr)
  683. fDescriptor->activate(fHandle2);
  684. }
  685. }
  686. void deactivate() override
  687. {
  688. CARLA_ASSERT(fDescriptor != nullptr);
  689. if (fDescriptor->deactivate != nullptr)
  690. {
  691. fDescriptor->deactivate(fHandle);
  692. if (fHandle2 != nullptr)
  693. fDescriptor->deactivate(fHandle2);
  694. }
  695. }
  696. void bufferSizeChanged(const uint32_t newBufferSize) override
  697. {
  698. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  699. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  700. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  701. {
  702. if (fAudioInBuffers[i] != nullptr)
  703. delete[] fAudioInBuffers[i];
  704. fAudioInBuffers[i] = new float[newBufferSize];
  705. }
  706. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  707. {
  708. if (fAudioOutBuffers[i] != nullptr)
  709. delete[] fAudioOutBuffers[i];
  710. fAudioOutBuffers[i] = new float[newBufferSize];
  711. }
  712. if (fHandle2 == nullptr)
  713. {
  714. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  715. {
  716. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  717. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  718. }
  719. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  720. {
  721. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  722. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  723. }
  724. }
  725. else
  726. {
  727. if (kData->audioIn.count > 0)
  728. {
  729. CARLA_ASSERT(kData->audioIn.count == 2);
  730. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  731. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  732. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  733. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  734. }
  735. if (kData->audioOut.count > 0)
  736. {
  737. CARLA_ASSERT(kData->audioOut.count == 2);
  738. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  739. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  740. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  741. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  742. }
  743. }
  744. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  745. }
  746. void sampleRateChanged(const double newSampleRate) override
  747. {
  748. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  749. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  750. // TODO
  751. (void)newSampleRate;
  752. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  753. }
  754. // -------------------------------------------------------------------
  755. // Plugin buffers
  756. void clearBuffers() override
  757. {
  758. carla_debug("Lv2Plugin::clearBuffers() - start");
  759. if (fAudioInBuffers != nullptr)
  760. {
  761. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  762. {
  763. if (fAudioInBuffers[i] != nullptr)
  764. {
  765. delete[] fAudioInBuffers[i];
  766. fAudioInBuffers[i] = nullptr;
  767. }
  768. }
  769. delete[] fAudioInBuffers;
  770. fAudioInBuffers = nullptr;
  771. }
  772. if (fAudioOutBuffers != nullptr)
  773. {
  774. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  775. {
  776. if (fAudioOutBuffers[i] != nullptr)
  777. {
  778. delete[] fAudioOutBuffers[i];
  779. fAudioOutBuffers[i] = nullptr;
  780. }
  781. }
  782. delete[] fAudioOutBuffers;
  783. fAudioOutBuffers = nullptr;
  784. }
  785. if (fParamBuffers != nullptr)
  786. {
  787. delete[] fParamBuffers;
  788. fParamBuffers = nullptr;
  789. }
  790. CarlaPlugin::clearBuffers();
  791. carla_debug("Lv2Plugin::clearBuffers() - end");
  792. }
  793. // -------------------------------------------------------------------
  794. // Post-poned UI Stuff
  795. // -------------------------------------------------------------------
  796. protected:
  797. void guiClosedCallback() override
  798. {
  799. showGui(false);
  800. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  801. }
  802. // -------------------------------------------------------------------
  803. public:
  804. bool init(const char* const bundle, const char* const name, const char* const uri)
  805. {
  806. CARLA_ASSERT(kData->engine != nullptr);
  807. CARLA_ASSERT(kData->client == nullptr);
  808. CARLA_ASSERT(bundle != nullptr);
  809. CARLA_ASSERT(uri != nullptr);
  810. // ---------------------------------------------------------------
  811. // first checks
  812. if (kData->engine == nullptr)
  813. {
  814. return false;
  815. }
  816. if (kData->client != nullptr)
  817. {
  818. kData->engine->setLastError("Plugin client is already registered");
  819. return false;
  820. }
  821. if (bundle == nullptr)
  822. {
  823. kData->engine->setLastError("null bundle");
  824. return false;
  825. }
  826. if (uri == nullptr)
  827. {
  828. kData->engine->setLastError("null uri");
  829. return false;
  830. }
  831. // ---------------------------------------------------------------
  832. // get plugin from lv2_rdf (lilv)
  833. gLv2World.init();
  834. fRdfDescriptor = lv2_rdf_new(uri);
  835. if (fRdfDescriptor == nullptr)
  836. {
  837. kData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  838. return false;
  839. }
  840. // ---------------------------------------------------------------
  841. // open DLL
  842. if (! kData->libOpen(fRdfDescriptor->Binary))
  843. {
  844. kData->engine->setLastError(kData->libError(fRdfDescriptor->Binary));
  845. return false;
  846. }
  847. // ---------------------------------------------------------------
  848. // initialize features (part 1)
  849. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  850. eventFt->callback_data = this;
  851. eventFt->lv2_event_ref = carla_lv2_event_ref;
  852. eventFt->lv2_event_unref = carla_lv2_event_unref;
  853. LV2_Log_Log* const logFt = new LV2_Log_Log;
  854. logFt->handle = this;
  855. logFt->printf = carla_lv2_log_printf;
  856. logFt->vprintf = carla_lv2_log_vprintf;
  857. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  858. stateMakePathFt->handle = this;
  859. stateMakePathFt->path = carla_lv2_state_make_path;
  860. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  861. stateMapPathFt->handle = this;
  862. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  863. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  864. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  865. programsFt->handle = this;
  866. programsFt->program_changed = carla_lv2_program_changed;
  867. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  868. lv2_rtmempool_init(rtMemPoolFt);
  869. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  870. uriMapFt->callback_data = this;
  871. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  872. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  873. uridMapFt->handle = this;
  874. uridMapFt->map = carla_lv2_urid_map;
  875. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  876. uridUnmapFt->handle = this;
  877. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  878. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  879. workerFt->handle = this;
  880. workerFt->schedule_work = carla_lv2_worker_schedule;
  881. // ---------------------------------------------------------------
  882. // initialize features (part 2)
  883. fFeatures[kFeatureIdBufSizeBounded] = new LV2_Feature;
  884. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  885. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  886. fFeatures[kFeatureIdBufSizeFixed] = new LV2_Feature;
  887. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  888. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  889. fFeatures[kFeatureIdBufSizePowerOf2] = new LV2_Feature;
  890. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  891. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  892. fFeatures[kFeatureIdEvent] = new LV2_Feature;
  893. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  894. fFeatures[kFeatureIdEvent]->data = eventFt;
  895. fFeatures[kFeatureIdLogs] = new LV2_Feature;
  896. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  897. fFeatures[kFeatureIdLogs]->data = logFt;
  898. fFeatures[kFeatureIdOptions] = new LV2_Feature;
  899. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  900. //fFeatures[kFeatureIdOptions]->data = ft.options;
  901. fFeatures[kFeatureIdPrograms] = new LV2_Feature;
  902. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  903. fFeatures[kFeatureIdPrograms]->data = programsFt;
  904. fFeatures[kFeatureIdRtMemPool] = new LV2_Feature;
  905. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  906. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  907. fFeatures[kFeatureIdStateMakePath] = new LV2_Feature;
  908. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  909. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  910. fFeatures[kFeatureIdStateMapPath] = new LV2_Feature;
  911. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  912. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  913. fFeatures[kFeatureIdStrictBounds] = new LV2_Feature;
  914. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  915. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  916. fFeatures[kFeatureIdUriMap] = new LV2_Feature;
  917. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  918. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  919. fFeatures[kFeatureIdUridMap] = new LV2_Feature;
  920. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  921. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  922. fFeatures[kFeatureIdUridUnmap] = new LV2_Feature;
  923. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  924. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  925. fFeatures[kFeatureIdWorker] = new LV2_Feature;
  926. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  927. fFeatures[kFeatureIdWorker]->data = workerFt;
  928. // ---------------------------------------------------------------
  929. // get DLL main entry
  930. #if 0
  931. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)kData->libSymbol("lv2_lib_descriptor");
  932. if (libDescFn != nullptr)
  933. {
  934. // -----------------------------------------------------------
  935. // get lib descriptor
  936. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  937. if (libFn == nullptr || libFn->get_plugin == nullptr)
  938. {
  939. kData->engine->setLastError("Plugin failed to return library descriptor");
  940. return false;
  941. }
  942. // -----------------------------------------------------------
  943. // get descriptor that matches URI
  944. uint32_t i = 0;
  945. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  946. {
  947. if (std::strcmp(fDescriptor->URI, uri) == 0)
  948. break;
  949. }
  950. if (fDescriptor == nullptr)
  951. libFn->cleanup(libFn->handle);
  952. else
  953. #endif
  954. {
  955. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)kData->libSymbol("lv2_descriptor");
  956. if (descFn == nullptr)
  957. {
  958. kData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  959. return false;
  960. }
  961. // -----------------------------------------------------------
  962. // get descriptor that matches URI
  963. uint32_t i = 0;
  964. while ((fDescriptor = descFn(i++)))
  965. {
  966. if (std::strcmp(fDescriptor->URI, uri) == 0)
  967. break;
  968. }
  969. }
  970. if (fDescriptor == nullptr)
  971. {
  972. kData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  973. return false;
  974. }
  975. // ---------------------------------------------------------------
  976. // check supported port-types and features
  977. bool canContinue = true;
  978. // Check supported ports
  979. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  980. {
  981. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  982. if (! (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_CONTROL(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes)))
  983. {
  984. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  985. {
  986. kData->engine->setLastError("Plugin requires a port type that is not currently supported");
  987. canContinue = false;
  988. break;
  989. }
  990. }
  991. }
  992. // Check supported features
  993. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  994. {
  995. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  996. {
  997. QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  998. kData->engine->setLastError(msg.toUtf8().constData());
  999. canContinue = false;
  1000. break;
  1001. }
  1002. }
  1003. // Check extensions
  1004. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  1005. {
  1006. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  1007. fHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  1008. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  1009. fHints |= PLUGIN_HAS_EXTENSION_STATE;
  1010. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  1011. fHints |= PLUGIN_HAS_EXTENSION_WORKER;
  1012. else
  1013. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  1014. }
  1015. if (! canContinue)
  1016. {
  1017. // error already set
  1018. return false;
  1019. }
  1020. // ---------------------------------------------------------------
  1021. // get info
  1022. if (name != nullptr)
  1023. fName = kData->engine->getUniquePluginName(name);
  1024. else
  1025. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Name);
  1026. fFilename = bundle;
  1027. // ---------------------------------------------------------------
  1028. // register client
  1029. kData->client = kData->engine->addClient(this);
  1030. if (kData->client == nullptr || ! kData->client->isOk())
  1031. {
  1032. kData->engine->setLastError("Failed to register plugin client");
  1033. return false;
  1034. }
  1035. // ---------------------------------------------------------------
  1036. // initialize plugin
  1037. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  1038. if (fHandle == nullptr)
  1039. {
  1040. kData->engine->setLastError("Plugin failed to initialize");
  1041. return false;
  1042. }
  1043. // ---------------------------------------------------------------
  1044. // load plugin settings
  1045. {
  1046. // set default options
  1047. fOptions = 0x0;
  1048. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1049. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1050. if (kData->engine->getOptions().forceStereo)
  1051. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1052. //if (fDescriptor->midiIns > 0)
  1053. {
  1054. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1055. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1056. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1057. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1058. }
  1059. // load settings
  1060. kData->idStr = "LV2/";
  1061. kData->idStr += uri;
  1062. fOptions = kData->loadSettings(fOptions, availableOptions());
  1063. }
  1064. // ---------------------------------------------------------------
  1065. // gui stuff
  1066. if (fRdfDescriptor->UICount == 0)
  1067. return true;
  1068. return true;
  1069. }
  1070. private:
  1071. LV2_Handle fHandle;
  1072. LV2_Handle fHandle2;
  1073. LV2_Feature* fFeatures[kFeatureCount+1];
  1074. const LV2_Descriptor* fDescriptor;
  1075. const LV2_RDF_Descriptor* fRdfDescriptor;
  1076. float** fAudioInBuffers;
  1077. float** fAudioOutBuffers;
  1078. float* fParamBuffers;
  1079. Lv2PluginEventData fEventsIn;
  1080. Lv2PluginEventData fEventsOut;
  1081. struct Extensions {
  1082. const LV2_State_Interface* state;
  1083. const LV2_Worker_Interface* worker;
  1084. const LV2_Programs_Interface* programs;
  1085. const LV2_Programs_UI_Interface* uiprograms;
  1086. Extensions()
  1087. : state(nullptr),
  1088. worker(nullptr),
  1089. programs(nullptr),
  1090. uiprograms(nullptr) {}
  1091. ~Extensions()
  1092. {
  1093. CARLA_ASSERT(state == nullptr);
  1094. CARLA_ASSERT(worker == nullptr);
  1095. CARLA_ASSERT(programs == nullptr);
  1096. CARLA_ASSERT(uiprograms == nullptr);
  1097. }
  1098. } fExt;
  1099. struct UI {
  1100. LV2UI_Handle handle;
  1101. LV2UI_Widget widget;
  1102. const LV2UI_Descriptor* descriptor;
  1103. const LV2_RDF_UI* rdfDescriptor;
  1104. UI()
  1105. : handle(nullptr),
  1106. widget(nullptr),
  1107. descriptor(nullptr),
  1108. rdfDescriptor(nullptr) {}
  1109. ~UI()
  1110. {
  1111. CARLA_ASSERT(handle == nullptr);
  1112. CARLA_ASSERT(widget == nullptr);
  1113. CARLA_ASSERT(descriptor == nullptr);
  1114. CARLA_ASSERT(rdfDescriptor == nullptr);
  1115. }
  1116. } fUi;
  1117. // -------------------------------------------------------------------
  1118. // Event Feature
  1119. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  1120. {
  1121. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  1122. CARLA_ASSERT(callback_data != nullptr);
  1123. CARLA_ASSERT(event != nullptr);
  1124. return 0;
  1125. }
  1126. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  1127. {
  1128. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  1129. CARLA_ASSERT(callback_data != nullptr);
  1130. CARLA_ASSERT(event != nullptr);
  1131. return 0;
  1132. }
  1133. // -------------------------------------------------------------------
  1134. // Logs Feature
  1135. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  1136. {
  1137. CARLA_ASSERT(handle != nullptr);
  1138. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  1139. #ifndef DEBUG
  1140. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  1141. return 0;
  1142. #endif
  1143. va_list args;
  1144. va_start(args, fmt);
  1145. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  1146. va_end(args);
  1147. return ret;
  1148. }
  1149. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  1150. {
  1151. CARLA_ASSERT(handle != nullptr);
  1152. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  1153. #ifndef DEBUG
  1154. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  1155. return 0;
  1156. #endif
  1157. int ret = 0;
  1158. switch (type)
  1159. {
  1160. case CARLA_URI_MAP_ID_LOG_ERROR:
  1161. #ifndef CARLA_OS_WIN
  1162. std::fprintf(stderr, "\x1b[31m");
  1163. #endif
  1164. ret = std::vfprintf(stderr, fmt, ap);
  1165. #ifndef CARLA_OS_WIN
  1166. std::fprintf(stderr, "\x1b[0m");
  1167. #endif
  1168. break;
  1169. case CARLA_URI_MAP_ID_LOG_NOTE:
  1170. ret = std::vfprintf(stdout, fmt, ap);
  1171. break;
  1172. case CARLA_URI_MAP_ID_LOG_TRACE:
  1173. #ifdef DEBUG
  1174. # ifndef CARLA_OS_WIN
  1175. std::fprintf(stdout, "\x1b[30;1m");
  1176. # endif
  1177. ret = std::vfprintf(stdout, fmt, ap);
  1178. # ifndef CARLA_OS_WIN
  1179. std::fprintf(stdout, "\x1b[0m");
  1180. # endif
  1181. #endif
  1182. break;
  1183. case CARLA_URI_MAP_ID_LOG_WARNING:
  1184. ret = std::vfprintf(stderr, fmt, ap);
  1185. break;
  1186. default:
  1187. break;
  1188. }
  1189. return ret;
  1190. }
  1191. // -------------------------------------------------------------------
  1192. // Programs Feature
  1193. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  1194. {
  1195. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  1196. CARLA_ASSERT(handle != nullptr);
  1197. if (handle == nullptr)
  1198. return;
  1199. //((Lv2Plugin*)handle)->handleProgramChanged(index);
  1200. }
  1201. // -------------------------------------------------------------------
  1202. // State Feature
  1203. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  1204. {
  1205. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  1206. CARLA_ASSERT(handle != nullptr);
  1207. CARLA_ASSERT(path != nullptr);
  1208. if (path == nullptr)
  1209. return nullptr;
  1210. QDir dir;
  1211. dir.mkpath(path);
  1212. return strdup(path);
  1213. }
  1214. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  1215. {
  1216. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  1217. CARLA_ASSERT(handle != nullptr);
  1218. CARLA_ASSERT(absolute_path != nullptr);
  1219. if (absolute_path == nullptr)
  1220. return nullptr;
  1221. QDir dir(absolute_path);
  1222. return strdup(dir.canonicalPath().toUtf8().constData());
  1223. }
  1224. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  1225. {
  1226. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  1227. CARLA_ASSERT(handle != nullptr);
  1228. CARLA_ASSERT(abstract_path != nullptr);
  1229. if (abstract_path == nullptr)
  1230. return nullptr;
  1231. QDir dir(abstract_path);
  1232. return strdup(dir.absolutePath().toUtf8().constData());
  1233. }
  1234. static LV2_State_Status carla_lv2_state_store(LV2_State_Handle handle, uint32_t key, const void* value, size_t size, uint32_t type, uint32_t flags)
  1235. {
  1236. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  1237. CARLA_ASSERT(handle != nullptr);
  1238. if (handle == nullptr)
  1239. return LV2_STATE_ERR_UNKNOWN;
  1240. //return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  1241. return LV2_STATE_ERR_UNKNOWN;
  1242. }
  1243. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  1244. {
  1245. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  1246. CARLA_ASSERT(handle != nullptr);
  1247. if (handle == nullptr)
  1248. return nullptr;
  1249. //return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  1250. return nullptr;
  1251. }
  1252. // -------------------------------------------------------------------
  1253. // URI-Map Feature
  1254. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  1255. {
  1256. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  1257. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  1258. }
  1259. // -------------------------------------------------------------------
  1260. // URID Feature
  1261. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  1262. {
  1263. CARLA_ASSERT(handle != nullptr);
  1264. CARLA_ASSERT(uri != nullptr);
  1265. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  1266. if (uri == nullptr)
  1267. return CARLA_URI_MAP_ID_NULL;
  1268. // Atom types
  1269. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  1270. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  1271. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  1272. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  1273. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  1274. return CARLA_URI_MAP_ID_ATOM_INT;
  1275. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  1276. return CARLA_URI_MAP_ID_ATOM_PATH;
  1277. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  1278. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1279. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  1280. return CARLA_URI_MAP_ID_ATOM_STRING;
  1281. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  1282. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  1283. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  1284. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  1285. // BufSize types
  1286. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  1287. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  1288. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  1289. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  1290. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  1291. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  1292. // Log types
  1293. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  1294. return CARLA_URI_MAP_ID_LOG_ERROR;
  1295. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  1296. return CARLA_URI_MAP_ID_LOG_NOTE;
  1297. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  1298. return CARLA_URI_MAP_ID_LOG_TRACE;
  1299. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  1300. return CARLA_URI_MAP_ID_LOG_WARNING;
  1301. // Others
  1302. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  1303. return CARLA_URI_MAP_ID_MIDI_EVENT;
  1304. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  1305. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  1306. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  1307. return CARLA_URI_MAP_ID_TIME_POSITION;
  1308. if (handle == nullptr)
  1309. return CARLA_URI_MAP_ID_NULL;
  1310. // Custom types
  1311. //return ((Lv2Plugin*)handle)->getCustomURID(uri);
  1312. return 0;
  1313. }
  1314. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  1315. {
  1316. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  1317. CARLA_ASSERT(handle != nullptr);
  1318. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  1319. if (urid == CARLA_URI_MAP_ID_NULL)
  1320. return nullptr;
  1321. // Atom types
  1322. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  1323. return LV2_ATOM__Chunk;
  1324. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  1325. return LV2_ATOM__Double;
  1326. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  1327. return LV2_ATOM__Int;
  1328. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  1329. return LV2_ATOM__Path;
  1330. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  1331. return LV2_ATOM__Sequence;
  1332. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  1333. return LV2_ATOM__String;
  1334. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  1335. return LV2_ATOM__atomTransfer;
  1336. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  1337. return LV2_ATOM__eventTransfer;
  1338. // BufSize types
  1339. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  1340. return LV2_BUF_SIZE__maxBlockLength;
  1341. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  1342. return LV2_BUF_SIZE__minBlockLength;
  1343. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  1344. return LV2_BUF_SIZE__sequenceSize;
  1345. // Log types
  1346. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  1347. return LV2_LOG__Error;
  1348. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  1349. return LV2_LOG__Note;
  1350. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  1351. return LV2_LOG__Trace;
  1352. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  1353. return LV2_LOG__Warning;
  1354. // Others
  1355. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  1356. return LV2_MIDI__MidiEvent;
  1357. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  1358. return LV2_PARAMETERS__sampleRate;
  1359. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  1360. return LV2_TIME__Position;
  1361. if (handle == nullptr)
  1362. return nullptr;
  1363. // Custom types
  1364. //return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  1365. return nullptr;
  1366. }
  1367. // -------------------------------------------------------------------
  1368. // Worker Feature
  1369. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  1370. {
  1371. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  1372. CARLA_ASSERT(handle != nullptr);
  1373. if (handle == nullptr)
  1374. return LV2_WORKER_ERR_UNKNOWN;
  1375. //return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  1376. return LV2_WORKER_ERR_UNKNOWN;
  1377. }
  1378. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  1379. {
  1380. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  1381. CARLA_ASSERT(handle != nullptr);
  1382. if (handle == nullptr)
  1383. return LV2_WORKER_ERR_UNKNOWN;
  1384. //return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  1385. return LV2_WORKER_ERR_UNKNOWN;
  1386. }
  1387. // -------------------------------------------------------------------
  1388. // UI Port-Map Feature
  1389. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  1390. {
  1391. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  1392. CARLA_ASSERT(handle);
  1393. if (handle == nullptr)
  1394. return LV2UI_INVALID_PORT_INDEX;
  1395. //return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  1396. return LV2UI_INVALID_PORT_INDEX;
  1397. }
  1398. // -------------------------------------------------------------------
  1399. // UI Resize Feature
  1400. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  1401. {
  1402. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  1403. CARLA_ASSERT(handle != nullptr);
  1404. if (handle == nullptr)
  1405. return 1;
  1406. //return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  1407. return 1;
  1408. }
  1409. // -------------------------------------------------------------------
  1410. // External UI Feature
  1411. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  1412. {
  1413. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  1414. CARLA_ASSERT(controller != nullptr);
  1415. if (controller == nullptr)
  1416. return;
  1417. //((Lv2Plugin*)handle)->handleExternalUiClosed();
  1418. }
  1419. // -------------------------------------------------------------------
  1420. // UI Extension
  1421. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  1422. {
  1423. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  1424. CARLA_ASSERT(controller != nullptr);
  1425. if (controller == nullptr)
  1426. return;
  1427. //((Lv2Plugin*)handle)->handleUiWrite(port_index, buffer_size, format, buffer);
  1428. }
  1429. // -------------------------------------------------------------------
  1430. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  1431. };
  1432. CARLA_BACKEND_END_NAMESPACE
  1433. #else // WANT_VST
  1434. # warning Building without LV2 support
  1435. #endif
  1436. CARLA_BACKEND_START_NAMESPACE
  1437. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  1438. {
  1439. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1440. #ifdef WANT_LV2
  1441. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  1442. if (! plugin->init(init.filename, init.name, init.label))
  1443. {
  1444. delete plugin;
  1445. return nullptr;
  1446. }
  1447. plugin->reload();
  1448. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1449. {
  1450. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  1451. delete plugin;
  1452. return nullptr;
  1453. }
  1454. return plugin;
  1455. #else
  1456. init.engine->setLastError("LV2 support not available");
  1457. return nullptr;
  1458. #endif
  1459. }
  1460. CARLA_BACKEND_END_NAMESPACE