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.

4705 lines
166KB

  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_OPTIONS = 0x100; //!< LV2 Plugin has Options extension
  38. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x200; //!< LV2 Plugin has Programs extension
  39. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x400; //!< LV2 Plugin has State extension
  40. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x800; //!< LV2 Plugin has Worker extension
  41. /**@}*/
  42. /*!
  43. * @defgroup ParameterHints Parameter Hints
  44. * @{
  45. */
  46. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000; //!< LV2 Parameter needs strict bounds
  47. const unsigned int PARAMETER_IS_TRIGGER = 0x2000; //!< LV2 Parameter is trigger (current value should be changed to its default after run())
  48. /**@}*/
  49. /*!
  50. * @defgroup Lv2EventTypes LV2 Event Data/Types
  51. *
  52. * Data and buffer types for LV2 EventData ports.
  53. * @{
  54. */
  55. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  56. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  57. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  58. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10;
  59. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  60. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  61. /**@}*/
  62. /*!
  63. * @defgroup Lv2UriMapIds LV2 URI Map Ids
  64. *
  65. * Static index list of the internal LV2 URI Map Ids.
  66. * @{
  67. */
  68. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 1;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 2;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 3;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 4;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 5;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 6;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 7;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 8;
  77. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 9;
  78. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 10;
  79. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 11;
  80. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 12;
  81. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 13;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 14;
  83. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 15;
  84. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 16;
  85. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 17;
  86. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 18;
  87. const uint32_t CARLA_URI_MAP_ID_COUNT = 19;
  88. /**@}*/
  89. /*!
  90. * @defgroup Lv2FeatureIds LV2 Feature Ids
  91. *
  92. * Static index list of the internal LV2 Feature Ids.
  93. * @{
  94. */
  95. const uint32_t kFeatureIdBufSizeBounded = 0;
  96. const uint32_t kFeatureIdBufSizeFixed = 1;
  97. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  98. const uint32_t kFeatureIdEvent = 3;
  99. const uint32_t kFeatureIdLogs = 4;
  100. const uint32_t kFeatureIdOptions = 5;
  101. const uint32_t kFeatureIdPrograms = 6;
  102. const uint32_t kFeatureIdRtMemPool = 7;
  103. const uint32_t kFeatureIdStateMakePath = 8;
  104. const uint32_t kFeatureIdStateMapPath = 9;
  105. const uint32_t kFeatureIdStrictBounds = 10;
  106. const uint32_t kFeatureIdUriMap = 11;
  107. const uint32_t kFeatureIdUridMap = 12;
  108. const uint32_t kFeatureIdUridUnmap = 13;
  109. const uint32_t kFeatureIdWorker = 14;
  110. const uint32_t kFeatureIdUiDataAccess = 15;
  111. const uint32_t kFeatureIdUiInstanceAccess = 16;
  112. const uint32_t kFeatureIdUiParent = 17;
  113. const uint32_t kFeatureIdUiPortMap = 18;
  114. const uint32_t kFeatureIdUiResize = 19;
  115. const uint32_t kFeatureIdExternalUi = 20;
  116. const uint32_t kFeatureIdExternalUiOld = 21;
  117. const uint32_t kFeatureCount = 22;
  118. /**@}*/
  119. const unsigned int MAX_EVENT_BUFFER = 8192; // 0x2000
  120. enum Lv2PluginGuiType {
  121. PLUGIN_UI_NULL,
  122. PLUGIN_UI_OSC,
  123. PLUGIN_UI_QT,
  124. PLUGIN_UI_PARENT,
  125. PLUGIN_UI_EXTERNAL
  126. };
  127. struct Lv2EventData {
  128. uint32_t type;
  129. uint32_t rindex;
  130. CarlaEngineEventPort* port;
  131. union {
  132. LV2_Atom_Sequence* atom;
  133. LV2_Event_Buffer* event;
  134. LV2_MIDI* midi;
  135. };
  136. Lv2EventData()
  137. : type(0x0),
  138. rindex(0),
  139. port(nullptr) {}
  140. ~Lv2EventData()
  141. {
  142. if (port != nullptr)
  143. {
  144. delete port;
  145. port = nullptr;
  146. }
  147. if (type & CARLA_EVENT_DATA_ATOM)
  148. {
  149. CARLA_ASSERT(atom != nullptr);
  150. if (atom != nullptr)
  151. {
  152. std::free(atom);
  153. atom = nullptr;
  154. }
  155. }
  156. else if (type & CARLA_EVENT_DATA_EVENT)
  157. {
  158. CARLA_ASSERT(event != nullptr);
  159. if (event != nullptr)
  160. {
  161. std::free(event);
  162. event = nullptr;
  163. }
  164. }
  165. else if (type & CARLA_EVENT_DATA_MIDI_LL)
  166. {
  167. CARLA_ASSERT(midi != nullptr && midi->data != nullptr);
  168. if (midi != nullptr)
  169. {
  170. if (midi->data != nullptr)
  171. delete[] midi->data;
  172. delete midi;
  173. midi->data = nullptr;
  174. midi = nullptr;
  175. }
  176. }
  177. }
  178. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2EventData)
  179. };
  180. struct Lv2PluginEventData {
  181. uint32_t count;
  182. Lv2EventData* data;
  183. Lv2EventData* ctrl; // default port
  184. uint32_t ctrlIndex;
  185. Lv2PluginEventData()
  186. : count(0),
  187. data(nullptr),
  188. ctrl(nullptr),
  189. ctrlIndex(0) {}
  190. ~Lv2PluginEventData()
  191. {
  192. CARLA_ASSERT_INT(count == 0, count);
  193. CARLA_ASSERT(data == nullptr);
  194. CARLA_ASSERT(ctrl == nullptr);
  195. CARLA_ASSERT(ctrlIndex == 0);
  196. }
  197. void createNew(const uint32_t newCount)
  198. {
  199. CARLA_ASSERT_INT(count == 0, count);
  200. CARLA_ASSERT(data == nullptr);
  201. CARLA_ASSERT(ctrl == nullptr);
  202. CARLA_ASSERT(ctrlIndex == 0);
  203. CARLA_ASSERT_INT(newCount > 0, newCount);
  204. if (data != nullptr || newCount == 0)
  205. return;
  206. data = new Lv2EventData[newCount];
  207. count = newCount;
  208. ctrl = nullptr;
  209. ctrlIndex = 0;
  210. }
  211. void clear()
  212. {
  213. if (data != nullptr)
  214. {
  215. delete[] data;
  216. data = nullptr;
  217. }
  218. count = 0;
  219. ctrl = nullptr;
  220. ctrlIndex = 0;
  221. }
  222. void initBuffers(CarlaEngine* const engine)
  223. {
  224. for (uint32_t i=0; i < count; ++i)
  225. {
  226. if (data[i].port != nullptr)
  227. data[i].port->initBuffer(engine);
  228. }
  229. }
  230. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2PluginEventData)
  231. };
  232. struct Lv2PluginOptions {
  233. int minBufferSize;
  234. int maxBufferSize;
  235. int sequenceSize;
  236. double sampleRate;
  237. LV2_Options_Option optMinBlockLenth;
  238. LV2_Options_Option optMaxBlockLenth;
  239. LV2_Options_Option optSequenceSize;
  240. LV2_Options_Option optSampleRate;
  241. LV2_Options_Option optNull;
  242. LV2_Options_Option* opts[5];
  243. Lv2PluginOptions()
  244. : minBufferSize(0),
  245. maxBufferSize(0),
  246. sequenceSize(MAX_EVENT_BUFFER),
  247. sampleRate(0.0)
  248. {
  249. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  250. optMinBlockLenth.subject = 0;
  251. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  252. optMinBlockLenth.size = sizeof(int);
  253. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  254. optMinBlockLenth.value = &minBufferSize;
  255. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  256. optMaxBlockLenth.subject = 0;
  257. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  258. optMaxBlockLenth.size = sizeof(int);
  259. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  260. optMaxBlockLenth.value = &maxBufferSize;
  261. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  262. optSequenceSize.subject = 0;
  263. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  264. optSequenceSize.size = sizeof(int);
  265. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  266. optSequenceSize.value = &sequenceSize;
  267. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  268. optSampleRate.subject = 0;
  269. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  270. optSampleRate.size = sizeof(double);
  271. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  272. optSampleRate.value = &sampleRate;
  273. optNull.context = LV2_OPTIONS_INSTANCE;
  274. optNull.subject = 0;
  275. optNull.key = CARLA_URI_MAP_ID_NULL;
  276. optNull.size = 0;
  277. optNull.type = CARLA_URI_MAP_ID_NULL;
  278. optNull.value = nullptr;
  279. opts[0] = &optMinBlockLenth;
  280. opts[1] = &optMaxBlockLenth;
  281. opts[2] = &optSequenceSize;
  282. opts[3] = &optSampleRate;
  283. opts[4] = &optNull;
  284. }
  285. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2PluginOptions)
  286. };
  287. LV2_Atom_Event* getLv2AtomEvent(LV2_Atom_Sequence* const atom, const uint32_t offset)
  288. {
  289. return (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, atom) + offset);
  290. }
  291. // -----------------------------------------------------
  292. class Lv2Plugin : public CarlaPlugin,
  293. public CarlaPluginGui::Callback
  294. {
  295. public:
  296. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  297. : CarlaPlugin(engine, id),
  298. fHandle(nullptr),
  299. fHandle2(nullptr),
  300. fFeatures{nullptr},
  301. fDescriptor(nullptr),
  302. fRdfDescriptor(nullptr),
  303. fAudioInBuffers(nullptr),
  304. fAudioOutBuffers(nullptr),
  305. fParamBuffers(nullptr)
  306. {
  307. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  308. kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  309. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  310. fCustomURIDs.append(nullptr);
  311. }
  312. ~Lv2Plugin() override
  313. {
  314. carla_debug("Lv2Plugin::~Lv2Plugin()");
  315. // close UI
  316. if (fUi.type != PLUGIN_UI_NULL)
  317. {
  318. showGui(false);
  319. if (fUi.type == PLUGIN_UI_OSC)
  320. {
  321. // Wait a bit first, then force kill
  322. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  323. {
  324. carla_stderr("LV2 OSC-GUI thread still running, forcing termination now");
  325. kData->osc.thread.terminate();
  326. }
  327. }
  328. else
  329. {
  330. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  331. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  332. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  333. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  334. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  335. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  336. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  337. {
  338. const LV2_External_UI_Host* const uiHost((const LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data);
  339. if (uiHost->plugin_human_id != nullptr)
  340. delete[] uiHost->plugin_human_id;
  341. delete uiHost;
  342. }
  343. fUi.descriptor = nullptr;
  344. kData->uiLibClose();
  345. }
  346. fUi.rdfDescriptor = nullptr;
  347. }
  348. kData->singleMutex.lock();
  349. kData->masterMutex.lock();
  350. if (kData->active)
  351. {
  352. deactivate();
  353. kData->active = false;
  354. }
  355. if (fDescriptor != nullptr)
  356. {
  357. if (fDescriptor->cleanup != nullptr)
  358. {
  359. if (fHandle != nullptr)
  360. fDescriptor->cleanup(fHandle);
  361. if (fHandle2 != nullptr)
  362. fDescriptor->cleanup(fHandle2);
  363. }
  364. fHandle = nullptr;
  365. fHandle2 = nullptr;
  366. fDescriptor = nullptr;
  367. }
  368. if (fRdfDescriptor != nullptr)
  369. {
  370. delete fRdfDescriptor;
  371. fRdfDescriptor = nullptr;
  372. }
  373. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  374. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  375. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  376. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  377. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  378. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  379. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  380. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  381. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  382. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  383. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  384. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  385. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  386. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  387. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  388. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  389. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  390. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  391. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  392. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  393. for (uint32_t i=0; i < kFeatureCount; ++i)
  394. {
  395. if (fFeatures[i] != nullptr)
  396. delete fFeatures[i];
  397. }
  398. for (auto it = fCustomURIDs.begin(); it.valid(); it.next())
  399. {
  400. const char*& uri(*it);
  401. if (uri != nullptr)
  402. {
  403. delete[] uri;
  404. uri = nullptr;
  405. }
  406. }
  407. fCustomURIDs.clear();
  408. clearBuffers();
  409. }
  410. // -------------------------------------------------------------------
  411. // Information (base)
  412. PluginType type() const override
  413. {
  414. return PLUGIN_LV2;
  415. }
  416. PluginCategory category() override
  417. {
  418. CARLA_ASSERT(fRdfDescriptor != nullptr);
  419. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  420. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  421. if (LV2_IS_DELAY(cat1, cat2))
  422. return PLUGIN_CATEGORY_DELAY;
  423. if (LV2_IS_DISTORTION(cat1, cat2))
  424. return PLUGIN_CATEGORY_OTHER;
  425. if (LV2_IS_DYNAMICS(cat1, cat2))
  426. return PLUGIN_CATEGORY_DYNAMICS;
  427. if (LV2_IS_EQ(cat1, cat2))
  428. return PLUGIN_CATEGORY_EQ;
  429. if (LV2_IS_FILTER(cat1, cat2))
  430. return PLUGIN_CATEGORY_FILTER;
  431. if (LV2_IS_GENERATOR(cat1, cat2))
  432. return PLUGIN_CATEGORY_SYNTH;
  433. if (LV2_IS_MODULATOR(cat1, cat2))
  434. return PLUGIN_CATEGORY_MODULATOR;
  435. if (LV2_IS_REVERB(cat1, cat2))
  436. return PLUGIN_CATEGORY_DELAY;
  437. if (LV2_IS_SIMULATOR(cat1, cat2))
  438. return PLUGIN_CATEGORY_OTHER;
  439. if (LV2_IS_SPATIAL(cat1, cat2))
  440. return PLUGIN_CATEGORY_OTHER;
  441. if (LV2_IS_SPECTRAL(cat1, cat2))
  442. return PLUGIN_CATEGORY_UTILITY;
  443. if (LV2_IS_UTILITY(cat1, cat2))
  444. return PLUGIN_CATEGORY_UTILITY;
  445. return getPluginCategoryFromName(fName);
  446. }
  447. long uniqueId() const override
  448. {
  449. CARLA_ASSERT(fRdfDescriptor != nullptr);
  450. return fRdfDescriptor->UniqueID;
  451. }
  452. // -------------------------------------------------------------------
  453. // Information (count)
  454. uint32_t midiInCount() const override
  455. {
  456. CARLA_ASSERT(fRdfDescriptor != nullptr);
  457. uint32_t i, count = 0;
  458. for (i=0; i < fRdfDescriptor->PortCount; ++i)
  459. {
  460. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  461. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  462. count += 1;
  463. }
  464. return count;
  465. }
  466. uint32_t midiOutCount() const override
  467. {
  468. CARLA_ASSERT(fRdfDescriptor != nullptr);
  469. uint32_t i, count = 0;
  470. for (i=0; i < fRdfDescriptor->PortCount; ++i)
  471. {
  472. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  473. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  474. count += 1;
  475. }
  476. return count;
  477. }
  478. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  479. {
  480. CARLA_ASSERT(fRdfDescriptor != nullptr);
  481. CARLA_ASSERT(parameterId < kData->param.count);
  482. const int32_t rindex(kData->param.data[parameterId].rindex);
  483. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  484. {
  485. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  486. return port.ScalePointCount;
  487. }
  488. return 0;
  489. }
  490. // -------------------------------------------------------------------
  491. // Information (current data)
  492. // nothing
  493. // -------------------------------------------------------------------
  494. // Information (per-plugin data)
  495. unsigned int availableOptions() override
  496. {
  497. unsigned int options = 0x0;
  498. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  499. if (! needsFixedBuffer())
  500. options |= PLUGIN_OPTION_FIXED_BUFFER;
  501. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  502. {
  503. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  504. options |= PLUGIN_OPTION_FORCE_STEREO;
  505. else if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  506. options |= PLUGIN_OPTION_FORCE_STEREO;
  507. }
  508. if (midiInCount() > 0)
  509. {
  510. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  511. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  512. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  513. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  514. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  515. }
  516. return options;
  517. }
  518. float getParameterValue(const uint32_t parameterId) override
  519. {
  520. CARLA_ASSERT(fParamBuffers != nullptr);
  521. CARLA_ASSERT(parameterId < kData->param.count);
  522. return fParamBuffers[parameterId];
  523. }
  524. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  525. {
  526. CARLA_ASSERT(fRdfDescriptor != nullptr);
  527. CARLA_ASSERT(parameterId < kData->param.count);
  528. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  529. const int32_t rindex(kData->param.data[parameterId].rindex);
  530. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  531. {
  532. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  533. if (scalePointId < port.ScalePointCount)
  534. {
  535. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  536. return portScalePoint.Value;
  537. }
  538. }
  539. return 0.0f;
  540. }
  541. void getLabel(char* const strBuf) override
  542. {
  543. CARLA_ASSERT(fRdfDescriptor != nullptr);
  544. CARLA_ASSERT(fRdfDescriptor->URI != nullptr);
  545. if (fRdfDescriptor->URI != nullptr)
  546. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  547. else
  548. CarlaPlugin::getLabel(strBuf);
  549. }
  550. void getMaker(char* const strBuf) override
  551. {
  552. CARLA_ASSERT(fRdfDescriptor != nullptr);
  553. if (fRdfDescriptor->Author != nullptr)
  554. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  555. else
  556. CarlaPlugin::getMaker(strBuf);
  557. }
  558. void getCopyright(char* const strBuf) override
  559. {
  560. CARLA_ASSERT(fRdfDescriptor != nullptr);
  561. if (fRdfDescriptor->License != nullptr)
  562. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  563. else
  564. CarlaPlugin::getCopyright(strBuf);
  565. }
  566. void getRealName(char* const strBuf) override
  567. {
  568. CARLA_ASSERT(fRdfDescriptor != nullptr);
  569. if (fRdfDescriptor->Name != nullptr)
  570. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  571. else
  572. CarlaPlugin::getRealName(strBuf);
  573. }
  574. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  575. {
  576. CARLA_ASSERT(fRdfDescriptor != nullptr);
  577. CARLA_ASSERT(parameterId < kData->param.count);
  578. const int32_t rindex(kData->param.data[parameterId].rindex);
  579. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  580. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  581. else
  582. CarlaPlugin::getParameterName(parameterId, strBuf);
  583. }
  584. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) override
  585. {
  586. CARLA_ASSERT(fRdfDescriptor != nullptr);
  587. CARLA_ASSERT(parameterId < kData->param.count);
  588. const int32_t rindex(kData->param.data[parameterId].rindex);
  589. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  590. strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  591. else
  592. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  593. }
  594. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  595. {
  596. CARLA_ASSERT(fRdfDescriptor != nullptr);
  597. CARLA_ASSERT(parameterId < kData->param.count);
  598. const int32_t rindex(kData->param.data[parameterId].rindex);
  599. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  600. {
  601. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  602. if (LV2_HAVE_PORT_UNIT_SYMBOL(port.Unit.Hints) && port.Unit.Symbol)
  603. std::strncpy(strBuf, port.Unit.Symbol, STR_MAX);
  604. else if (LV2_HAVE_PORT_UNIT_UNIT(port.Unit.Hints))
  605. {
  606. switch (port.Unit.Unit)
  607. {
  608. case LV2_PORT_UNIT_BAR:
  609. std::strncpy(strBuf, "bars", STR_MAX);
  610. return;
  611. case LV2_PORT_UNIT_BEAT:
  612. std::strncpy(strBuf, "beats", STR_MAX);
  613. return;
  614. case LV2_PORT_UNIT_BPM:
  615. std::strncpy(strBuf, "BPM", STR_MAX);
  616. return;
  617. case LV2_PORT_UNIT_CENT:
  618. std::strncpy(strBuf, "ct", STR_MAX);
  619. return;
  620. case LV2_PORT_UNIT_CM:
  621. std::strncpy(strBuf, "cm", STR_MAX);
  622. return;
  623. case LV2_PORT_UNIT_COEF:
  624. std::strncpy(strBuf, "(coef)", STR_MAX);
  625. return;
  626. case LV2_PORT_UNIT_DB:
  627. std::strncpy(strBuf, "dB", STR_MAX);
  628. return;
  629. case LV2_PORT_UNIT_DEGREE:
  630. std::strncpy(strBuf, "deg", STR_MAX);
  631. return;
  632. case LV2_PORT_UNIT_FRAME:
  633. std::strncpy(strBuf, "frames", STR_MAX);
  634. return;
  635. case LV2_PORT_UNIT_HZ:
  636. std::strncpy(strBuf, "Hz", STR_MAX);
  637. return;
  638. case LV2_PORT_UNIT_INCH:
  639. std::strncpy(strBuf, "in", STR_MAX);
  640. return;
  641. case LV2_PORT_UNIT_KHZ:
  642. std::strncpy(strBuf, "kHz", STR_MAX);
  643. return;
  644. case LV2_PORT_UNIT_KM:
  645. std::strncpy(strBuf, "km", STR_MAX);
  646. return;
  647. case LV2_PORT_UNIT_M:
  648. std::strncpy(strBuf, "m", STR_MAX);
  649. return;
  650. case LV2_PORT_UNIT_MHZ:
  651. std::strncpy(strBuf, "MHz", STR_MAX);
  652. return;
  653. case LV2_PORT_UNIT_MIDINOTE:
  654. std::strncpy(strBuf, "note", STR_MAX);
  655. return;
  656. case LV2_PORT_UNIT_MILE:
  657. std::strncpy(strBuf, "mi", STR_MAX);
  658. return;
  659. case LV2_PORT_UNIT_MIN:
  660. std::strncpy(strBuf, "min", STR_MAX);
  661. return;
  662. case LV2_PORT_UNIT_MM:
  663. std::strncpy(strBuf, "mm", STR_MAX);
  664. return;
  665. case LV2_PORT_UNIT_MS:
  666. std::strncpy(strBuf, "ms", STR_MAX);
  667. return;
  668. case LV2_PORT_UNIT_OCT:
  669. std::strncpy(strBuf, "oct", STR_MAX);
  670. return;
  671. case LV2_PORT_UNIT_PC:
  672. std::strncpy(strBuf, "%", STR_MAX);
  673. return;
  674. case LV2_PORT_UNIT_S:
  675. std::strncpy(strBuf, "s", STR_MAX);
  676. return;
  677. case LV2_PORT_UNIT_SEMITONE:
  678. std::strncpy(strBuf, "semi", STR_MAX);
  679. return;
  680. }
  681. }
  682. }
  683. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  684. }
  685. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  686. {
  687. CARLA_ASSERT(fRdfDescriptor != nullptr);
  688. CARLA_ASSERT(parameterId < kData->param.count);
  689. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  690. const int32_t rindex(kData->param.data[parameterId].rindex);
  691. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  692. {
  693. const LV2_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  694. if (scalePointId < port.ScalePointCount)
  695. {
  696. const LV2_RDF_PortScalePoint& portScalePoint(port.ScalePoints[scalePointId]);
  697. if (portScalePoint.Label != nullptr)
  698. {
  699. std::strncpy(strBuf, portScalePoint.Label, STR_MAX);
  700. return;
  701. }
  702. }
  703. }
  704. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  705. }
  706. // -------------------------------------------------------------------
  707. // Set data (state)
  708. void prepareForSave() override
  709. {
  710. CARLA_ASSERT(fHandle != nullptr);
  711. if (fExt.state != nullptr && fExt.state->save != nullptr)
  712. {
  713. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  714. if (fHandle2 != nullptr)
  715. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  716. }
  717. }
  718. // -------------------------------------------------------------------
  719. // Set data (internal stuff)
  720. // nothing
  721. // -------------------------------------------------------------------
  722. // Set data (plugin-specific stuff)
  723. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  724. {
  725. CARLA_ASSERT(parameterId < kData->param.count);
  726. const float fixedValue(kData->param.fixValue(parameterId, value));
  727. fParamBuffers[parameterId] = fixedValue;
  728. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  729. }
  730. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  731. {
  732. CARLA_ASSERT(fDescriptor != nullptr);
  733. CARLA_ASSERT(fHandle != nullptr);
  734. CARLA_ASSERT(type != nullptr);
  735. CARLA_ASSERT(key != nullptr);
  736. CARLA_ASSERT(value != nullptr);
  737. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  738. if (type == nullptr)
  739. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  740. if (key == nullptr)
  741. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  742. if (value == nullptr)
  743. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  744. CarlaPlugin::setCustomData(type, key, value, sendGui);
  745. if (fExt.state != nullptr)
  746. {
  747. LV2_State_Status status;
  748. {
  749. const ScopedSingleProcessLocker spl(this, true);
  750. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  751. if (fHandle2 != nullptr)
  752. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  753. }
  754. switch (status)
  755. {
  756. case LV2_STATE_SUCCESS:
  757. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  758. break;
  759. case LV2_STATE_ERR_UNKNOWN:
  760. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  761. break;
  762. case LV2_STATE_ERR_BAD_TYPE:
  763. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  764. break;
  765. case LV2_STATE_ERR_BAD_FLAGS:
  766. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  767. break;
  768. case LV2_STATE_ERR_NO_FEATURE:
  769. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  770. break;
  771. case LV2_STATE_ERR_NO_PROPERTY:
  772. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  773. break;
  774. }
  775. }
  776. if (sendGui)
  777. {
  778. //CustomData cdata;
  779. //cdata.type = type;
  780. //cdata.key = key;
  781. //cdata.value = value;
  782. //uiTransferCustomData(&cdata);
  783. }
  784. }
  785. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  786. {
  787. CARLA_ASSERT(fDescriptor != nullptr);
  788. CARLA_ASSERT(fHandle != nullptr);
  789. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  790. if (index < -1)
  791. index = -1;
  792. else if (index > static_cast<int32_t>(kData->midiprog.count))
  793. return;
  794. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  795. {
  796. const uint32_t bank = kData->midiprog.data[index].bank;
  797. const uint32_t program = kData->midiprog.data[index].program;
  798. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  799. fExt.programs->select_program(fHandle, bank, program);
  800. if (fHandle2 != nullptr)
  801. fExt.programs->select_program(fHandle2, bank, program);
  802. }
  803. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  804. }
  805. // -------------------------------------------------------------------
  806. // Set gui stuff
  807. void showGui(const bool yesNo) override
  808. {
  809. if (fUi.type == PLUGIN_UI_NULL)
  810. return;
  811. if (fUi.type == PLUGIN_UI_OSC)
  812. {
  813. if (yesNo)
  814. {
  815. kData->osc.thread.start();
  816. }
  817. else
  818. {
  819. if (kData->osc.data.target != nullptr)
  820. {
  821. osc_send_hide(&kData->osc.data);
  822. osc_send_quit(&kData->osc.data);
  823. kData->osc.data.free();
  824. }
  825. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  826. kData->osc.thread.terminate();
  827. }
  828. return;
  829. }
  830. // take some precautions
  831. CARLA_ASSERT(fUi.descriptor != nullptr);
  832. CARLA_ASSERT(fUi.rdfDescriptor != nullptr);
  833. if (fUi.descriptor == nullptr)
  834. return;
  835. if (fUi.rdfDescriptor == nullptr)
  836. return;
  837. if (yesNo)
  838. {
  839. CARLA_ASSERT(fUi.descriptor->instantiate != nullptr);
  840. if (fUi.descriptor->instantiate == nullptr)
  841. return;
  842. }
  843. else
  844. {
  845. CARLA_ASSERT(fUi.descriptor->cleanup != nullptr);
  846. if (fUi.handle == nullptr)
  847. return;
  848. if (fUi.descriptor->cleanup == nullptr)
  849. return;
  850. }
  851. if (fUi.type == PLUGIN_UI_EXTERNAL)
  852. {
  853. if (yesNo)
  854. {
  855. fUi.widget = nullptr;
  856. if (fUi.handle == nullptr)
  857. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  858. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  859. CARLA_ASSERT(fUi.handle != nullptr);
  860. CARLA_ASSERT(fUi.widget != nullptr);
  861. if (fUi.handle == nullptr || fUi.widget == nullptr)
  862. {
  863. fUi.handle = nullptr;
  864. fUi.widget = nullptr;
  865. kData->engine->callback(CALLBACK_ERROR, fId, 0, 0, 0.0f, "Plugin refused to open its own UI");
  866. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  867. return;
  868. }
  869. updateUi();
  870. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  871. }
  872. else
  873. {
  874. CARLA_ASSERT(fUi.widget != nullptr);
  875. if (fUi.widget != nullptr)
  876. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  877. fUi.descriptor->cleanup(fUi.handle);
  878. fUi.handle = nullptr;
  879. fUi.widget = nullptr;
  880. }
  881. }
  882. else // means PLUGIN_UI_PARENT || PLUGIN_UI_QT
  883. {
  884. if (yesNo)
  885. {
  886. if (kData->gui == nullptr)
  887. {
  888. // TODO
  889. CarlaPluginGui::Options guiOptions;
  890. guiOptions.parented = (fUi.type == PLUGIN_UI_PARENT);
  891. guiOptions.resizable = isUiResizable();
  892. kData->gui = new CarlaPluginGui(kData->engine, this, guiOptions);
  893. }
  894. if (fUi.type == PLUGIN_UI_PARENT)
  895. {
  896. fFeatures[kFeatureIdUiParent]->data = kData->gui->getContainerWinId();
  897. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  898. }
  899. fUi.widget = nullptr;
  900. if (fUi.handle == nullptr)
  901. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  902. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  903. CARLA_ASSERT(fUi.handle != nullptr);
  904. CARLA_ASSERT(fUi.widget != nullptr);
  905. if (fUi.handle == nullptr || fUi.widget == nullptr)
  906. {
  907. fUi.handle = nullptr;
  908. fUi.widget = nullptr;
  909. kData->gui->close();
  910. delete kData->gui;
  911. kData->gui = nullptr;
  912. kData->engine->callback(CALLBACK_ERROR, fId, 0, 0, 0.0f, "Plugin refused to open its own UI");
  913. kData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  914. return;
  915. }
  916. if (fUi.type == PLUGIN_UI_QT)
  917. kData->gui->setWidget((QWidget*)fUi.widget);
  918. updateUi();
  919. kData->gui->setWindowTitle(QString("%1 (GUI)").arg((const char*)fName));
  920. kData->gui->show();
  921. }
  922. else
  923. {
  924. fUi.descriptor->cleanup(fUi.handle);
  925. fUi.handle = nullptr;
  926. fUi.widget = nullptr;
  927. if (kData->gui != nullptr)
  928. {
  929. kData->gui->close();
  930. delete kData->gui;
  931. kData->gui = nullptr;
  932. }
  933. }
  934. }
  935. }
  936. void idleGui() override
  937. {
  938. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  939. {
  940. if (fUi.type == PLUGIN_UI_EXTERNAL && fUi.widget != nullptr)
  941. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  942. if (fExt.uiidle != nullptr)
  943. {
  944. if (fExt.uiidle->idle(fUi.handle) != 0)
  945. {
  946. showGui(false);
  947. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  948. }
  949. }
  950. }
  951. CarlaPlugin::idleGui();
  952. }
  953. // -------------------------------------------------------------------
  954. // Plugin state
  955. void reload() override
  956. {
  957. carla_debug("Lv2Plugin::reload() - start");
  958. CARLA_ASSERT(kData->engine != nullptr);
  959. CARLA_ASSERT(fHandle != nullptr);
  960. CARLA_ASSERT(fDescriptor != nullptr);
  961. CARLA_ASSERT(fRdfDescriptor != nullptr);
  962. if (kData->engine == nullptr)
  963. return;
  964. if (fHandle == nullptr)
  965. return;
  966. if (fDescriptor == nullptr)
  967. return;
  968. if (fRdfDescriptor == nullptr)
  969. return;
  970. const ProcessMode processMode(kData->engine->getProccessMode());
  971. // Safely disable plugin for reload
  972. const ScopedDisabler sd(this);
  973. if (kData->active)
  974. deactivate();
  975. clearBuffers();
  976. const float sampleRate = (float)kData->engine->getSampleRate();
  977. const uint32_t portCount = fRdfDescriptor->PortCount;
  978. uint32_t aIns, aOuts, cvIns, cvOuts, params, j;
  979. aIns = aOuts = cvIns = cvOuts = params = 0;
  980. NonRtList<uint32_t> evIns, evOuts;
  981. bool forcedStereoIn, forcedStereoOut;
  982. forcedStereoIn = forcedStereoOut = false;
  983. bool needsCtrlIn, needsCtrlOut;
  984. needsCtrlIn = needsCtrlOut = false;
  985. for (uint32_t i=0; i < portCount; ++i)
  986. {
  987. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  988. if (LV2_IS_PORT_AUDIO(portTypes))
  989. {
  990. if (LV2_IS_PORT_INPUT(portTypes))
  991. aIns += 1;
  992. else if (LV2_IS_PORT_OUTPUT(portTypes))
  993. aOuts += 1;
  994. }
  995. else if (LV2_IS_PORT_CV(portTypes))
  996. {
  997. if (LV2_IS_PORT_INPUT(portTypes))
  998. cvIns += 1;
  999. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1000. cvOuts += 1;
  1001. }
  1002. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1003. {
  1004. if (LV2_IS_PORT_INPUT(portTypes))
  1005. evIns.append(CARLA_EVENT_DATA_ATOM);
  1006. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1007. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1008. }
  1009. else if (LV2_IS_PORT_EVENT(portTypes))
  1010. {
  1011. if (LV2_IS_PORT_INPUT(portTypes))
  1012. evIns.append(CARLA_EVENT_DATA_EVENT);
  1013. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1014. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1015. }
  1016. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1017. {
  1018. if (LV2_IS_PORT_INPUT(portTypes))
  1019. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1020. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1021. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1022. }
  1023. else if (LV2_IS_PORT_CONTROL(portTypes))
  1024. params += 1;
  1025. }
  1026. // check extensions
  1027. fExt.options = nullptr;
  1028. fExt.programs = nullptr;
  1029. fExt.state = nullptr;
  1030. fExt.worker = nullptr;
  1031. if (fDescriptor->extension_data != nullptr)
  1032. {
  1033. if (kData->extraHints & PLUGIN_HAS_EXTENSION_OPTIONS)
  1034. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  1035. if (kData->extraHints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1036. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  1037. if (kData->extraHints & PLUGIN_HAS_EXTENSION_STATE)
  1038. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  1039. if (kData->extraHints & PLUGIN_HAS_EXTENSION_WORKER)
  1040. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  1041. }
  1042. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1043. {
  1044. if (fHandle2 == nullptr)
  1045. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1046. if (aIns == 1)
  1047. {
  1048. aIns = 2;
  1049. forcedStereoIn = true;
  1050. }
  1051. if (aOuts == 1)
  1052. {
  1053. aOuts = 2;
  1054. forcedStereoOut = true;
  1055. }
  1056. }
  1057. if (aIns > 0)
  1058. {
  1059. kData->audioIn.createNew(aIns);
  1060. fAudioInBuffers = new float*[aIns];
  1061. for (uint32_t i=0; i < aIns; ++i)
  1062. fAudioInBuffers[i] = nullptr;
  1063. }
  1064. if (aOuts > 0)
  1065. {
  1066. kData->audioOut.createNew(aOuts);
  1067. fAudioOutBuffers = new float*[aOuts];
  1068. needsCtrlIn = true;
  1069. for (uint32_t i=0; i < aOuts; ++i)
  1070. fAudioOutBuffers[i] = nullptr;
  1071. }
  1072. if (params > 0)
  1073. {
  1074. kData->param.createNew(params);
  1075. fParamBuffers = new float[params];
  1076. for (uint32_t i=0; i < params; ++i)
  1077. fParamBuffers[i] = 0.0f;
  1078. }
  1079. if (evIns.count() > 0)
  1080. {
  1081. const size_t count(evIns.count());
  1082. fEventsIn.createNew(count);
  1083. for (j=0; j < count; ++j)
  1084. {
  1085. const uint32_t& type(evIns.getAt(j));
  1086. if (type == CARLA_EVENT_DATA_ATOM)
  1087. {
  1088. fEventsIn.data[j].type = CARLA_EVENT_DATA_ATOM;
  1089. fEventsIn.data[j].atom = (LV2_Atom_Sequence*)std::malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  1090. fEventsIn.data[j].atom->atom.size = 0;
  1091. fEventsIn.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1092. fEventsIn.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1093. fEventsIn.data[j].atom->body.pad = 0;
  1094. }
  1095. else if (type == CARLA_EVENT_DATA_EVENT)
  1096. {
  1097. fEventsIn.data[j].type = CARLA_EVENT_DATA_EVENT;
  1098. fEventsIn.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1099. }
  1100. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1101. {
  1102. fEventsIn.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1103. fEventsIn.data[j].midi = new LV2_MIDI;
  1104. fEventsIn.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1105. fEventsIn.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1106. }
  1107. }
  1108. }
  1109. if (evOuts.count() > 0)
  1110. {
  1111. const size_t count(evOuts.count());
  1112. fEventsOut.createNew(count);
  1113. for (j=0; j < count; ++j)
  1114. {
  1115. const uint32_t& type(evOuts.getAt(j));
  1116. if (type == CARLA_EVENT_DATA_ATOM)
  1117. {
  1118. fEventsOut.data[j].type = CARLA_EVENT_DATA_ATOM;
  1119. fEventsOut.data[j].atom = (LV2_Atom_Sequence*)std::malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  1120. fEventsOut.data[j].atom->atom.size = 0;
  1121. fEventsOut.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1122. fEventsOut.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1123. fEventsOut.data[j].atom->body.pad = 0;
  1124. }
  1125. else if (type == CARLA_EVENT_DATA_EVENT)
  1126. {
  1127. fEventsOut.data[j].type = CARLA_EVENT_DATA_EVENT;
  1128. fEventsOut.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1129. }
  1130. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1131. {
  1132. fEventsOut.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1133. fEventsOut.data[j].midi = new LV2_MIDI;
  1134. fEventsOut.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1135. fEventsOut.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1136. }
  1137. }
  1138. }
  1139. const uint portNameSize(kData->engine->maxPortNameSize());
  1140. CarlaString portName;
  1141. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1142. {
  1143. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1144. portName.clear();
  1145. 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))
  1146. {
  1147. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1148. {
  1149. portName = fName;
  1150. portName += ":";
  1151. }
  1152. portName += fRdfDescriptor->Ports[i].Name;
  1153. portName.truncate(portNameSize);
  1154. }
  1155. if (LV2_IS_PORT_AUDIO(portTypes))
  1156. {
  1157. if (LV2_IS_PORT_INPUT(portTypes))
  1158. {
  1159. j = iAudioIn++;
  1160. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1161. kData->audioIn.ports[j].rindex = i;
  1162. if (forcedStereoIn)
  1163. {
  1164. portName += "_2";
  1165. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1166. kData->audioIn.ports[1].rindex = i;
  1167. }
  1168. }
  1169. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1170. {
  1171. j = iAudioOut++;
  1172. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1173. kData->audioOut.ports[j].rindex = i;
  1174. if (forcedStereoOut)
  1175. {
  1176. portName += "_2";
  1177. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1178. kData->audioOut.ports[1].rindex = i;
  1179. }
  1180. }
  1181. else
  1182. carla_stderr("WARNING - Got a broken Port (Audio, but not input or output)");
  1183. }
  1184. else if (LV2_IS_PORT_CV(portTypes))
  1185. {
  1186. if (LV2_IS_PORT_INPUT(portTypes))
  1187. {
  1188. carla_stderr("WARNING - CV Ports are not supported yet");
  1189. }
  1190. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1191. {
  1192. carla_stderr("WARNING - CV Ports are not supported yet");
  1193. }
  1194. else
  1195. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1196. fDescriptor->connect_port(fHandle, i, nullptr);
  1197. if (fHandle2 != nullptr)
  1198. fDescriptor->connect_port(fHandle2, i, nullptr);
  1199. }
  1200. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1201. {
  1202. if (LV2_IS_PORT_INPUT(portTypes))
  1203. {
  1204. j = iEvIn++;
  1205. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].atom);
  1206. if (fHandle2 != nullptr)
  1207. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].atom);
  1208. fEventsIn.data[j].rindex = i;
  1209. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1210. {
  1211. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1212. if (evIns.count() == 1)
  1213. {
  1214. needsCtrlIn = true;
  1215. fEventsIn.ctrl = &fEventsIn.data[j];
  1216. fEventsIn.ctrlIndex = j;
  1217. }
  1218. else
  1219. {
  1220. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1221. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1222. {
  1223. fEventsIn.ctrl = &fEventsIn.data[j];
  1224. fEventsIn.ctrlIndex = j;
  1225. }
  1226. }
  1227. }
  1228. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1229. {
  1230. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1231. }
  1232. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1233. {
  1234. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1235. }
  1236. }
  1237. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1238. {
  1239. j = iEvOut++;
  1240. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].atom);
  1241. if (fHandle2 != nullptr)
  1242. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].atom);
  1243. fEventsOut.data[j].rindex = i;
  1244. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1245. {
  1246. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1247. if (evOuts.count() == 1)
  1248. {
  1249. needsCtrlOut = true;
  1250. fEventsOut.ctrl = &fEventsOut.data[j];
  1251. fEventsOut.ctrlIndex = j;
  1252. }
  1253. else
  1254. {
  1255. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1256. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1257. {
  1258. fEventsOut.ctrl = &fEventsOut.data[j];
  1259. fEventsOut.ctrlIndex = j;
  1260. }
  1261. }
  1262. }
  1263. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1264. {
  1265. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1266. }
  1267. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1268. {
  1269. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1270. }
  1271. }
  1272. else
  1273. carla_stderr("WARNING - Got a broken Port (Atom Sequence, but not input or output)");
  1274. }
  1275. else if (LV2_IS_PORT_EVENT(portTypes))
  1276. {
  1277. if (LV2_IS_PORT_INPUT(portTypes))
  1278. {
  1279. j = iEvIn++;
  1280. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1281. if (fHandle2 != nullptr)
  1282. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1283. fEventsIn.data[j].rindex = i;
  1284. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1285. {
  1286. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1287. if (evIns.count() == 1)
  1288. {
  1289. needsCtrlIn = true;
  1290. fEventsIn.ctrl = &fEventsIn.data[j];
  1291. fEventsIn.ctrlIndex = j;
  1292. }
  1293. else
  1294. {
  1295. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1296. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1297. {
  1298. fEventsIn.ctrl = &fEventsIn.data[j];
  1299. fEventsIn.ctrlIndex = j;
  1300. }
  1301. }
  1302. }
  1303. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1304. {
  1305. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1306. }
  1307. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1308. {
  1309. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1310. }
  1311. }
  1312. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1313. {
  1314. j = iEvOut++;
  1315. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1316. if (fHandle2 != nullptr)
  1317. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1318. fEventsOut.data[j].rindex = i;
  1319. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1320. {
  1321. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1322. if (evOuts.count() == 1)
  1323. {
  1324. needsCtrlOut = true;
  1325. fEventsOut.ctrl = &fEventsOut.data[j];
  1326. fEventsOut.ctrlIndex = j;
  1327. }
  1328. else
  1329. {
  1330. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1331. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1332. {
  1333. fEventsOut.ctrl = &fEventsOut.data[j];
  1334. fEventsOut.ctrlIndex = j;
  1335. }
  1336. }
  1337. }
  1338. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1339. {
  1340. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1341. }
  1342. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1343. {
  1344. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1345. }
  1346. }
  1347. else
  1348. carla_stderr("WARNING - Got a broken Port (Event, but not input or output)");
  1349. }
  1350. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1351. {
  1352. if (LV2_IS_PORT_INPUT(portTypes))
  1353. {
  1354. j = iEvIn++;
  1355. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].midi);
  1356. if (fHandle2 != nullptr)
  1357. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].midi);
  1358. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1359. fEventsIn.data[j].rindex = i;
  1360. if (evIns.count() == 1)
  1361. {
  1362. needsCtrlIn = true;
  1363. fEventsIn.ctrl = &fEventsIn.data[j];
  1364. fEventsIn.ctrlIndex = j;
  1365. }
  1366. else
  1367. {
  1368. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1369. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1370. {
  1371. fEventsIn.ctrl = &fEventsIn.data[j];
  1372. fEventsIn.ctrlIndex = j;
  1373. }
  1374. }
  1375. }
  1376. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1377. {
  1378. j = iEvOut++;
  1379. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].midi);
  1380. if (fHandle2 != nullptr)
  1381. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].midi);
  1382. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1383. fEventsOut.data[j].rindex = i;
  1384. if (evOuts.count() == 1)
  1385. {
  1386. needsCtrlOut = true;
  1387. fEventsOut.ctrl = &fEventsOut.data[j];
  1388. fEventsOut.ctrlIndex = j;
  1389. }
  1390. else
  1391. {
  1392. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1393. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1394. {
  1395. fEventsOut.ctrl = &fEventsOut.data[j];
  1396. fEventsOut.ctrlIndex = j;
  1397. }
  1398. }
  1399. }
  1400. else
  1401. carla_stderr("WARNING - Got a broken Port (Midi, but not input or output)");
  1402. }
  1403. else if (LV2_IS_PORT_CONTROL(portTypes))
  1404. {
  1405. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1406. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1407. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1408. j = iCtrl++;
  1409. kData->param.data[j].index = j;
  1410. kData->param.data[j].rindex = i;
  1411. kData->param.data[j].hints = 0x0;
  1412. kData->param.data[j].midiChannel = 0;
  1413. kData->param.data[j].midiCC = -1;
  1414. float min, max, def, step, stepSmall, stepLarge;
  1415. // min value
  1416. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1417. min = portPoints.Minimum;
  1418. else
  1419. min = 0.0f;
  1420. // max value
  1421. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1422. max = portPoints.Maximum;
  1423. else
  1424. max = 1.0f;
  1425. if (min > max)
  1426. max = min;
  1427. else if (max < min)
  1428. min = max;
  1429. // stupid hack for ir.lv2 (broken plugin)
  1430. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1431. {
  1432. min = 0.0f;
  1433. max = (float)0xffffff;
  1434. }
  1435. if (max - min == 0.0f)
  1436. {
  1437. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1438. max = min + 0.1f;
  1439. }
  1440. // default value
  1441. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1442. {
  1443. def = portPoints.Default;
  1444. }
  1445. else
  1446. {
  1447. // no default value
  1448. if (min < 0.0f && max > 0.0f)
  1449. def = 0.0f;
  1450. else
  1451. def = min;
  1452. }
  1453. if (def < min)
  1454. def = min;
  1455. else if (def > max)
  1456. def = max;
  1457. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1458. {
  1459. min *= sampleRate;
  1460. max *= sampleRate;
  1461. def *= sampleRate;
  1462. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1463. }
  1464. if (LV2_IS_PORT_TOGGLED(portProps))
  1465. {
  1466. step = max - min;
  1467. stepSmall = step;
  1468. stepLarge = step;
  1469. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1470. }
  1471. else if (LV2_IS_PORT_INTEGER(portProps))
  1472. {
  1473. step = 1.0f;
  1474. stepSmall = 1.0f;
  1475. stepLarge = 10.0f;
  1476. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1477. }
  1478. else
  1479. {
  1480. float range = max - min;
  1481. step = range/100.0f;
  1482. stepSmall = range/1000.0f;
  1483. stepLarge = range/10.0f;
  1484. }
  1485. if (LV2_IS_PORT_INPUT(portTypes))
  1486. {
  1487. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1488. {
  1489. carla_stderr("Plugin has latency input port, this should not happen!");
  1490. }
  1491. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1492. {
  1493. def = sampleRate;
  1494. step = 1.0f;
  1495. stepSmall = 1.0f;
  1496. stepLarge = 1.0f;
  1497. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1498. kData->param.data[j].hints = 0x0;
  1499. }
  1500. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1501. {
  1502. kData->param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  1503. }
  1504. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1505. {
  1506. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1507. }
  1508. else
  1509. {
  1510. kData->param.data[j].type = PARAMETER_INPUT;
  1511. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1512. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1513. needsCtrlIn = true;
  1514. }
  1515. // MIDI CC value
  1516. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1517. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1518. {
  1519. if (! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1520. kData->param.data[j].midiCC = portMidiMap.Number;
  1521. }
  1522. }
  1523. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1524. {
  1525. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1526. {
  1527. min = 0.0f;
  1528. max = sampleRate;
  1529. def = 0.0f;
  1530. step = 1.0f;
  1531. stepSmall = 1.0f;
  1532. stepLarge = 1.0f;
  1533. kData->param.data[j].type = PARAMETER_LATENCY;
  1534. kData->param.data[j].hints = 0;
  1535. }
  1536. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1537. {
  1538. def = sampleRate;
  1539. step = 1.0f;
  1540. stepSmall = 1.0f;
  1541. stepLarge = 1.0f;
  1542. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1543. kData->param.data[j].hints = 0;
  1544. }
  1545. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1546. {
  1547. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1548. }
  1549. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1550. {
  1551. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1552. }
  1553. else
  1554. {
  1555. kData->param.data[j].type = PARAMETER_OUTPUT;
  1556. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1557. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1558. needsCtrlOut = true;
  1559. }
  1560. }
  1561. else
  1562. {
  1563. kData->param.data[j].type = PARAMETER_UNKNOWN;
  1564. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1565. }
  1566. // extra parameter hints
  1567. if (LV2_IS_PORT_ENUMERATION(portProps))
  1568. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1569. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1570. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1571. if (LV2_IS_PORT_TRIGGER(portProps))
  1572. kData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1573. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1574. kData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1575. // check if parameter is not enabled or automable
  1576. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1577. kData->param.data[j].hints &= ~PARAMETER_IS_ENABLED;
  1578. if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1579. kData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1580. kData->param.ranges[j].min = min;
  1581. kData->param.ranges[j].max = max;
  1582. kData->param.ranges[j].def = def;
  1583. kData->param.ranges[j].step = step;
  1584. kData->param.ranges[j].stepSmall = stepSmall;
  1585. kData->param.ranges[j].stepLarge = stepLarge;
  1586. // Start parameters in their default values
  1587. fParamBuffers[j] = def;
  1588. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1589. if (fHandle2 != nullptr)
  1590. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1591. }
  1592. else
  1593. {
  1594. // Port Type not supported, but it's optional anyway
  1595. fDescriptor->connect_port(fHandle, i, nullptr);
  1596. if (fHandle2 != nullptr)
  1597. fDescriptor->connect_port(fHandle2, i, nullptr);
  1598. }
  1599. }
  1600. if (needsCtrlIn)
  1601. {
  1602. portName.clear();
  1603. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1604. {
  1605. portName = fName;
  1606. portName += ":";
  1607. }
  1608. portName += "events-in";
  1609. portName.truncate(portNameSize);
  1610. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1611. }
  1612. if (needsCtrlOut)
  1613. {
  1614. portName.clear();
  1615. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1616. {
  1617. portName = fName;
  1618. portName += ":";
  1619. }
  1620. portName += "events-out";
  1621. portName.truncate(portNameSize);
  1622. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1623. }
  1624. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1625. fEventsIn.ctrl->port = kData->event.portIn;
  1626. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1627. fEventsOut.ctrl->port = kData->event.portOut;
  1628. if (forcedStereoIn || forcedStereoOut)
  1629. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1630. else
  1631. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  1632. // plugin hints
  1633. fHints = 0x0;
  1634. if (fUi.type != PLUGIN_UI_NULL)
  1635. {
  1636. fHints |= PLUGIN_HAS_GUI;
  1637. if (fUi.type == PLUGIN_UI_QT || fUi.type == PLUGIN_UI_PARENT)
  1638. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  1639. }
  1640. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1641. fHints |= PLUGIN_IS_SYNTH;
  1642. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1643. fHints |= PLUGIN_CAN_DRYWET;
  1644. if (aOuts > 0)
  1645. fHints |= PLUGIN_CAN_VOLUME;
  1646. if (aOuts >= 2 && aOuts % 2 == 0)
  1647. fHints |= PLUGIN_CAN_BALANCE;
  1648. // extra plugin hints
  1649. kData->extraHints &= ~PLUGIN_HINT_CAN_RUN_RACK;
  1650. if (fExt.state != nullptr || fExt.worker != nullptr)
  1651. {
  1652. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1653. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1654. }
  1655. else
  1656. {
  1657. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1658. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1659. }
  1660. bufferSizeChanged(kData->engine->getBufferSize());
  1661. reloadPrograms(true);
  1662. if (kData->active)
  1663. activate();
  1664. evIns.clear();
  1665. evOuts.clear();
  1666. carla_debug("Lv2Plugin::reload() - end");
  1667. }
  1668. void reloadPrograms(const bool init) override
  1669. {
  1670. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  1671. uint32_t i, oldCount = kData->midiprog.count;
  1672. const int32_t current = kData->midiprog.current;
  1673. // Delete old programs
  1674. kData->midiprog.clear();
  1675. // Query new programs
  1676. uint32_t count = 0;
  1677. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1678. {
  1679. while (fExt.programs->get_program(fHandle, count))
  1680. count++;
  1681. }
  1682. if (count > 0)
  1683. {
  1684. kData->midiprog.createNew(count);
  1685. // Update data
  1686. for (i=0; i < count; ++i)
  1687. {
  1688. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1689. CARLA_ASSERT(pdesc != nullptr);
  1690. CARLA_ASSERT(pdesc->name != nullptr);
  1691. kData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->bank);
  1692. kData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->program);
  1693. kData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1694. }
  1695. }
  1696. #ifndef BUILD_BRIDGE
  1697. // Update OSC Names
  1698. if (kData->engine->isOscControlRegistered())
  1699. {
  1700. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  1701. for (i=0; i < count; ++i)
  1702. kData->engine->osc_send_control_set_midi_program_data(fId, i, kData->midiprog.data[i].bank, kData->midiprog.data[i].program, kData->midiprog.data[i].name);
  1703. }
  1704. #endif
  1705. if (init)
  1706. {
  1707. if (count > 0)
  1708. setMidiProgram(0, false, false, false);
  1709. }
  1710. else
  1711. {
  1712. // Check if current program is invalid
  1713. bool programChanged = false;
  1714. if (count == oldCount+1)
  1715. {
  1716. // one midi program added, probably created by user
  1717. kData->midiprog.current = oldCount;
  1718. programChanged = true;
  1719. }
  1720. else if (current < 0 && count > 0)
  1721. {
  1722. // programs exist now, but not before
  1723. kData->midiprog.current = 0;
  1724. programChanged = true;
  1725. }
  1726. else if (current >= 0 && count == 0)
  1727. {
  1728. // programs existed before, but not anymore
  1729. kData->midiprog.current = -1;
  1730. programChanged = true;
  1731. }
  1732. else if (current >= static_cast<int32_t>(count))
  1733. {
  1734. // current midi program > count
  1735. kData->midiprog.current = 0;
  1736. programChanged = true;
  1737. }
  1738. else
  1739. {
  1740. // no change
  1741. kData->midiprog.current = current;
  1742. }
  1743. if (programChanged)
  1744. setMidiProgram(kData->midiprog.current, true, true, true);
  1745. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  1746. }
  1747. }
  1748. // -------------------------------------------------------------------
  1749. // Plugin processing
  1750. void activate() override
  1751. {
  1752. CARLA_ASSERT(fDescriptor != nullptr);
  1753. CARLA_ASSERT(fHandle != nullptr);
  1754. if (fDescriptor->activate != nullptr)
  1755. {
  1756. fDescriptor->activate(fHandle);
  1757. if (fHandle2 != nullptr)
  1758. fDescriptor->activate(fHandle2);
  1759. }
  1760. }
  1761. void deactivate() override
  1762. {
  1763. CARLA_ASSERT(fDescriptor != nullptr);
  1764. CARLA_ASSERT(fHandle != nullptr);
  1765. if (fDescriptor->deactivate != nullptr)
  1766. {
  1767. fDescriptor->deactivate(fHandle);
  1768. if (fHandle2 != nullptr)
  1769. fDescriptor->deactivate(fHandle2);
  1770. }
  1771. }
  1772. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1773. {
  1774. uint32_t i, k;
  1775. // --------------------------------------------------------------------------------------------------------
  1776. // Check if active
  1777. if (! kData->active)
  1778. {
  1779. // disable any output sound
  1780. for (i=0; i < kData->audioOut.count; ++i)
  1781. carla_zeroFloat(outBuffer[i], frames);
  1782. return;
  1783. }
  1784. // handle events from different APIs
  1785. uint32_t evInAtomOffsets[fEventsIn.count];
  1786. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1787. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1788. for (i=0; i < fEventsIn.count; ++i)
  1789. {
  1790. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1791. {
  1792. evInAtomOffsets[i] = 0;
  1793. fEventsIn.data[i].atom->atom.size = 0;
  1794. fEventsIn.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1795. fEventsIn.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1796. fEventsIn.data[i].atom->body.pad = 0;
  1797. }
  1798. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1799. {
  1800. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(fEventsIn.data[i].event + 1));
  1801. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1802. }
  1803. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1804. {
  1805. evInMidiStates[i].midi = fEventsIn.data[i].midi;
  1806. evInMidiStates[i].frame_count = frames;
  1807. evInMidiStates[i].position = 0;
  1808. evInMidiStates[i].midi->event_count = 0;
  1809. evInMidiStates[i].midi->size = 0;
  1810. }
  1811. }
  1812. for (i=0; i < fEventsOut.count; ++i)
  1813. {
  1814. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1815. {
  1816. fEventsOut.data[i].atom->atom.size = 0;
  1817. fEventsOut.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1818. fEventsOut.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1819. fEventsOut.data[i].atom->body.pad = 0;
  1820. }
  1821. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1822. {
  1823. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(fEventsOut.data[i].event + 1));
  1824. }
  1825. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1826. {
  1827. // not needed
  1828. }
  1829. }
  1830. CARLA_PROCESS_CONTINUE_CHECK;
  1831. // --------------------------------------------------------------------------------------------------------
  1832. // Check if needs reset
  1833. if (kData->needsReset)
  1834. {
  1835. // TODO!
  1836. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1837. {
  1838. //for (unsigned char j=0, l=MAX_MIDI_CHANNELS; j < MAX_MIDI_CHANNELS; ++j)
  1839. {
  1840. }
  1841. }
  1842. else
  1843. {
  1844. }
  1845. if (kData->latency > 0)
  1846. {
  1847. //for (i=0; i < kData->audioIn.count; ++i)
  1848. // carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  1849. }
  1850. kData->needsReset = false;
  1851. }
  1852. CARLA_PROCESS_CONTINUE_CHECK;
  1853. // --------------------------------------------------------------------------------------------------------
  1854. // Special Parameters
  1855. {
  1856. int32_t rindex;
  1857. const EngineTimeInfo& timeInfo(kData->engine->getTimeInfo());
  1858. for (k=0; k < kData->param.count; ++k)
  1859. {
  1860. if (kData->param.data[k].type == PARAMETER_LATENCY)
  1861. {
  1862. // nothing
  1863. }
  1864. else if (kData->param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  1865. {
  1866. setParameterValue(k, kData->engine->isOffline() ? 1.0f : 0.0f, false, false, false);
  1867. }
  1868. else if (kData->param.data[k].type == PARAMETER_LV2_TIME)
  1869. {
  1870. rindex = kData->param.data[k].rindex;
  1871. CARLA_ASSERT(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  1872. switch (fRdfDescriptor->Ports[rindex].Designation)
  1873. {
  1874. // Non-BBT
  1875. case LV2_PORT_DESIGNATION_TIME_FRAME:
  1876. setParameterValue(k, timeInfo.frame, false, false, false);
  1877. break;
  1878. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  1879. break;
  1880. //case LV2_PORT_DESIGNATION_TIME_POSITION:
  1881. // setParameterValue(k, timeInfo.usecs, false, false, false);
  1882. // break;
  1883. case LV2_PORT_DESIGNATION_TIME_SPEED:
  1884. setParameterValue(k, timeInfo.playing ? 1.0f : 0.0f, false, false, false);
  1885. break;
  1886. // BBT
  1887. case LV2_PORT_DESIGNATION_TIME_BAR:
  1888. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1889. setParameterValue(k, timeInfo.bbt.bar - 1, false, false, false);
  1890. break;
  1891. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  1892. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1893. setParameterValue(k, float(timeInfo.bbt.beat - 1) + (float(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat), false, false, false);
  1894. break;
  1895. case LV2_PORT_DESIGNATION_TIME_BEAT:
  1896. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1897. setParameterValue(k, timeInfo.bbt.beat - 1, false, false, false);
  1898. break;
  1899. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  1900. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1901. setParameterValue(k, timeInfo.bbt.beatType, false, false, false);
  1902. break;
  1903. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  1904. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1905. setParameterValue(k, timeInfo.bbt.beatsPerBar, false, false, false);
  1906. break;
  1907. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  1908. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1909. setParameterValue(k, timeInfo.bbt.beatsPerMinute, false, false, false);
  1910. break;
  1911. }
  1912. }
  1913. }
  1914. }
  1915. // --------------------------------------------------------------------------------------------------------
  1916. // Event Input and Processing
  1917. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port != nullptr)
  1918. {
  1919. // ----------------------------------------------------------------------------------------------------
  1920. // MIDI Input (External)
  1921. if (kData->extNotes.mutex.tryLock())
  1922. {
  1923. k = fEventsIn.ctrlIndex;
  1924. while (! kData->extNotes.data.isEmpty())
  1925. {
  1926. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  1927. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1928. uint8_t midiEvent[3] = { 0 };
  1929. midiEvent[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1930. midiEvent[0] += note.channel;
  1931. midiEvent[1] = note.note;
  1932. midiEvent[2] = note.velo;
  1933. if (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI)
  1934. {
  1935. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1936. {
  1937. const uint32_t evInPadSize(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 3));
  1938. if (evInAtomOffsets[k] + evInPadSize >= MAX_EVENT_BUFFER)
  1939. continue;
  1940. LV2_Atom_Event* const aev = getLv2AtomEvent(fEventsIn.ctrl->atom, evInAtomOffsets[k]);
  1941. aev->time.frames = 0;
  1942. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1943. aev->body.size = 3;
  1944. std::memcpy(LV2_ATOM_BODY(&aev->body), midiEvent, 3);
  1945. evInAtomOffsets[k] += evInPadSize;
  1946. fEventsIn.ctrl->atom->atom.size = evInAtomOffsets[k];
  1947. }
  1948. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1949. {
  1950. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  1951. }
  1952. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1953. {
  1954. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  1955. }
  1956. }
  1957. }
  1958. kData->extNotes.mutex.unlock();
  1959. } // End of MIDI Input (External)
  1960. // ----------------------------------------------------------------------------------------------------
  1961. // Event Input (System)
  1962. bool allNotesOffSent = false;
  1963. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  1964. uint32_t time, nEvents = fEventsIn.ctrl->port->getEventCount();
  1965. uint32_t startTime = 0;
  1966. uint32_t timeOffset = 0;
  1967. uint32_t nextBankId = 0;
  1968. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1969. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1970. for (i=0; i < nEvents; ++i)
  1971. {
  1972. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  1973. time = event.time;
  1974. if (time >= frames)
  1975. continue;
  1976. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1977. if (time > timeOffset && sampleAccurate)
  1978. {
  1979. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1980. {
  1981. startTime = 0;
  1982. timeOffset = time;
  1983. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1984. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1985. else
  1986. nextBankId = 0;
  1987. // reset iters
  1988. k = fEventsIn.ctrlIndex;
  1989. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1990. {
  1991. evInAtomOffsets[k] = 0;
  1992. fEventsIn.ctrl->atom->atom.size = 0;
  1993. }
  1994. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1995. {
  1996. lv2_event_buffer_reset(fEventsIn.ctrl->event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(fEventsIn.ctrl->event + 1));
  1997. lv2_event_begin(&evInEventIters[k], fEventsIn.ctrl->event);
  1998. }
  1999. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2000. {
  2001. evInMidiStates[k].position = 0;
  2002. evInMidiStates[k].midi->event_count = 0;
  2003. evInMidiStates[k].midi->size = 0;
  2004. }
  2005. }
  2006. else
  2007. startTime += timeOffset;
  2008. }
  2009. // Control change
  2010. switch (event.type)
  2011. {
  2012. case kEngineEventTypeNull:
  2013. break;
  2014. case kEngineEventTypeControl:
  2015. {
  2016. const EngineControlEvent& ctrlEvent = event.ctrl;
  2017. switch (ctrlEvent.type)
  2018. {
  2019. case kEngineControlEventTypeNull:
  2020. break;
  2021. case kEngineControlEventTypeParameter:
  2022. {
  2023. // Control backend stuff
  2024. if (event.channel == kData->ctrlChannel)
  2025. {
  2026. float value;
  2027. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  2028. {
  2029. value = ctrlEvent.value;
  2030. setDryWet(value, false, false);
  2031. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2032. continue;
  2033. }
  2034. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  2035. {
  2036. value = ctrlEvent.value*127.0f/100.0f;
  2037. setVolume(value, false, false);
  2038. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2039. continue;
  2040. }
  2041. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  2042. {
  2043. float left, right;
  2044. value = ctrlEvent.value/0.5f - 1.0f;
  2045. if (value < 0.0f)
  2046. {
  2047. left = -1.0f;
  2048. right = (value*2.0f)+1.0f;
  2049. }
  2050. else if (value > 0.0f)
  2051. {
  2052. left = (value*2.0f)-1.0f;
  2053. right = 1.0f;
  2054. }
  2055. else
  2056. {
  2057. left = -1.0f;
  2058. right = 1.0f;
  2059. }
  2060. setBalanceLeft(left, false, false);
  2061. setBalanceRight(right, false, false);
  2062. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2063. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2064. continue;
  2065. }
  2066. }
  2067. // Control plugin parameters
  2068. for (k=0; k < kData->param.count; ++k)
  2069. {
  2070. if (kData->param.data[k].midiChannel != event.channel)
  2071. continue;
  2072. if (kData->param.data[k].midiCC != ctrlEvent.param)
  2073. continue;
  2074. if (kData->param.data[k].type != PARAMETER_INPUT)
  2075. continue;
  2076. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2077. continue;
  2078. float value;
  2079. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2080. {
  2081. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  2082. }
  2083. else
  2084. {
  2085. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  2086. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2087. value = std::rint(value);
  2088. }
  2089. setParameterValue(k, value, false, false, false);
  2090. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2091. }
  2092. break;
  2093. }
  2094. case kEngineControlEventTypeMidiBank:
  2095. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2096. nextBankId = ctrlEvent.param;
  2097. break;
  2098. case kEngineControlEventTypeMidiProgram:
  2099. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2100. {
  2101. const uint32_t nextProgramId = ctrlEvent.param;
  2102. for (k=0; k < kData->midiprog.count; ++k)
  2103. {
  2104. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  2105. {
  2106. setMidiProgram(k, false, false, false);
  2107. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  2108. break;
  2109. }
  2110. }
  2111. }
  2112. break;
  2113. case kEngineControlEventTypeAllSoundOff:
  2114. if (event.channel == kData->ctrlChannel)
  2115. {
  2116. if (! allNotesOffSent)
  2117. {
  2118. sendMidiAllNotesOff();
  2119. allNotesOffSent = true;
  2120. }
  2121. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  2122. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  2123. }
  2124. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2125. {
  2126. // TODO
  2127. }
  2128. break;
  2129. case kEngineControlEventTypeAllNotesOff:
  2130. if (event.channel == kData->ctrlChannel)
  2131. {
  2132. if (! allNotesOffSent)
  2133. {
  2134. allNotesOffSent = true;
  2135. sendMidiAllNotesOff();
  2136. }
  2137. }
  2138. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2139. {
  2140. // TODO
  2141. }
  2142. break;
  2143. }
  2144. break;
  2145. }
  2146. case kEngineEventTypeMidi:
  2147. {
  2148. const EngineMidiEvent& midiEvent = event.midi;
  2149. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  2150. uint8_t channel = event.channel;
  2151. uint32_t mtime = sampleAccurate ? startTime : time;
  2152. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2153. continue;
  2154. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2155. continue;
  2156. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2157. continue;
  2158. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2159. continue;
  2160. // Fix bad note-off
  2161. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  2162. status -= 0x10;
  2163. k = fEventsIn.ctrlIndex;
  2164. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2165. {
  2166. const uint32_t evInPadSize(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + midiEvent.size));
  2167. if (evInAtomOffsets[k] + evInPadSize >= MAX_EVENT_BUFFER)
  2168. continue;
  2169. LV2_Atom_Event* const aev(getLv2AtomEvent(fEventsIn.ctrl->atom, evInAtomOffsets[k]));
  2170. aev->time.frames = mtime;
  2171. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2172. aev->body.size = midiEvent.size;
  2173. std::memcpy(LV2_ATOM_BODY(&aev->body), midiEvent.data, midiEvent.size);
  2174. evInAtomOffsets[k] += evInPadSize;
  2175. fEventsIn.ctrl->atom->atom.size = evInAtomOffsets[k];
  2176. }
  2177. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2178. {
  2179. lv2_event_write(&evInEventIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2180. }
  2181. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2182. {
  2183. lv2midi_put_event(&evInMidiStates[k], mtime, midiEvent.size, midiEvent.data);
  2184. }
  2185. if (status == MIDI_STATUS_NOTE_ON)
  2186. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2187. else if (status == MIDI_STATUS_NOTE_OFF)
  2188. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2189. break;
  2190. }
  2191. }
  2192. }
  2193. kData->postRtEvents.trySplice();
  2194. if (frames > timeOffset)
  2195. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2196. } // End of Event Input and Processing
  2197. // --------------------------------------------------------------------------------------------------------
  2198. // Plugin processing (no events)
  2199. else
  2200. {
  2201. processSingle(inBuffer, outBuffer, frames, 0);
  2202. } // End of Plugin processing (no events)
  2203. CARLA_PROCESS_CONTINUE_CHECK;
  2204. // --------------------------------------------------------------------------------------------------------
  2205. // Control Output
  2206. if (kData->event.portOut != nullptr)
  2207. {
  2208. uint8_t channel;
  2209. uint16_t param;
  2210. float value;
  2211. for (k=0; k < kData->param.count; ++k)
  2212. {
  2213. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  2214. continue;
  2215. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  2216. if (kData->param.data[k].midiCC > 0)
  2217. {
  2218. channel = kData->param.data[k].midiChannel;
  2219. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  2220. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  2221. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2222. }
  2223. }
  2224. } // End of Control Output
  2225. CARLA_PROCESS_CONTINUE_CHECK;
  2226. // --------------------------------------------------------------------------------------------------------
  2227. // Final work
  2228. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2229. {
  2230. fExt.worker->end_run(fHandle);
  2231. if (fHandle2 != nullptr)
  2232. fExt.worker->end_run(fHandle2);
  2233. }
  2234. // --------------------------------------------------------------------------------------------------------
  2235. }
  2236. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2237. {
  2238. CARLA_ASSERT(frames > 0);
  2239. if (frames == 0)
  2240. return false;
  2241. if (kData->audioIn.count > 0)
  2242. {
  2243. CARLA_ASSERT(inBuffer != nullptr);
  2244. if (inBuffer == nullptr)
  2245. return false;
  2246. }
  2247. if (kData->audioOut.count > 0)
  2248. {
  2249. CARLA_ASSERT(outBuffer != nullptr);
  2250. if (outBuffer == nullptr)
  2251. return false;
  2252. }
  2253. uint32_t i, k;
  2254. // --------------------------------------------------------------------------------------------------------
  2255. // Try lock, silence otherwise
  2256. if (kData->engine->isOffline())
  2257. {
  2258. kData->singleMutex.lock();
  2259. }
  2260. else if (! kData->singleMutex.tryLock())
  2261. {
  2262. for (i=0; i < kData->audioOut.count; ++i)
  2263. {
  2264. for (k=0; k < frames; ++k)
  2265. outBuffer[i][k+timeOffset] = 0.0f;
  2266. }
  2267. return false;
  2268. }
  2269. // --------------------------------------------------------------------------------------------------------
  2270. // Reset audio buffers
  2271. for (i=0; i < kData->audioIn.count; ++i)
  2272. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2273. for (i=0; i < kData->audioOut.count; ++i)
  2274. carla_zeroFloat(fAudioOutBuffers[i], frames);
  2275. // --------------------------------------------------------------------------------------------------------
  2276. // Run plugin
  2277. fDescriptor->run(fHandle, frames);
  2278. if (fHandle2 != nullptr)
  2279. fDescriptor->run(fHandle2, frames);
  2280. // --------------------------------------------------------------------------------------------------------
  2281. // Post-processing (dry/wet, volume and balance)
  2282. {
  2283. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  2284. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  2285. bool isPair;
  2286. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2287. for (i=0; i < kData->audioOut.count; ++i)
  2288. {
  2289. // Dry/Wet
  2290. if (doDryWet)
  2291. {
  2292. for (k=0; k < frames; ++k)
  2293. {
  2294. // TODO
  2295. //if (k < kData->latency && kData->latency < frames)
  2296. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  2297. //else
  2298. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2299. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  2300. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  2301. }
  2302. }
  2303. // Balance
  2304. if (doBalance)
  2305. {
  2306. isPair = (i % 2 == 0);
  2307. if (isPair)
  2308. {
  2309. CARLA_ASSERT(i+1 < kData->audioOut.count);
  2310. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  2311. }
  2312. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  2313. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  2314. for (k=0; k < frames; ++k)
  2315. {
  2316. if (isPair)
  2317. {
  2318. // left
  2319. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2320. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2321. }
  2322. else
  2323. {
  2324. // right
  2325. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2326. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2327. }
  2328. }
  2329. }
  2330. // Volume (and buffer copy)
  2331. {
  2332. for (k=0; k < frames; ++k)
  2333. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  2334. }
  2335. }
  2336. #if 0
  2337. // Latency, save values for next callback, TODO
  2338. if (kData->latency > 0 && kData->latency < frames)
  2339. {
  2340. for (i=0; i < kData->audioIn.count; ++i)
  2341. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  2342. }
  2343. #endif
  2344. } // End of Post-processing
  2345. // --------------------------------------------------------------------------------------------------------
  2346. kData->singleMutex.unlock();
  2347. return true;
  2348. }
  2349. void bufferSizeChanged(const uint32_t newBufferSize) override
  2350. {
  2351. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2352. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2353. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2354. {
  2355. if (fAudioInBuffers[i] != nullptr)
  2356. delete[] fAudioInBuffers[i];
  2357. fAudioInBuffers[i] = new float[newBufferSize];
  2358. }
  2359. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2360. {
  2361. if (fAudioOutBuffers[i] != nullptr)
  2362. delete[] fAudioOutBuffers[i];
  2363. fAudioOutBuffers[i] = new float[newBufferSize];
  2364. }
  2365. if (fHandle2 == nullptr)
  2366. {
  2367. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2368. {
  2369. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2370. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2371. }
  2372. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2373. {
  2374. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2375. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2376. }
  2377. }
  2378. else
  2379. {
  2380. if (kData->audioIn.count > 0)
  2381. {
  2382. CARLA_ASSERT(kData->audioIn.count == 2);
  2383. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2384. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2385. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2386. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2387. }
  2388. if (kData->audioOut.count > 0)
  2389. {
  2390. CARLA_ASSERT(kData->audioOut.count == 2);
  2391. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2392. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2393. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2394. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2395. }
  2396. }
  2397. if (fLv2Options.maxBufferSize != static_cast<int>(newBufferSize) || (fLv2Options.minBufferSize > 1 && fLv2Options.minBufferSize != static_cast<int>(newBufferSize)))
  2398. {
  2399. fLv2Options.maxBufferSize = newBufferSize;
  2400. if (fLv2Options.minBufferSize > 1)
  2401. fLv2Options.minBufferSize = newBufferSize;
  2402. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2403. {
  2404. fExt.options->set(fHandle, &fLv2Options.optMinBlockLenth);
  2405. fExt.options->set(fHandle, &fLv2Options.optMaxBlockLenth);
  2406. }
  2407. }
  2408. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2409. }
  2410. void sampleRateChanged(const double newSampleRate) override
  2411. {
  2412. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2413. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2414. if (fLv2Options.sampleRate != newSampleRate)
  2415. {
  2416. fLv2Options.sampleRate = newSampleRate;
  2417. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2418. fExt.options->set(fHandle, &fLv2Options.optSampleRate);
  2419. }
  2420. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2421. }
  2422. // -------------------------------------------------------------------
  2423. // Plugin buffers
  2424. void initBuffers() override
  2425. {
  2426. fEventsIn.initBuffers(kData->engine);
  2427. fEventsOut.initBuffers(kData->engine);
  2428. CarlaPlugin::initBuffers();
  2429. }
  2430. void clearBuffers() override
  2431. {
  2432. carla_debug("Lv2Plugin::clearBuffers() - start");
  2433. if (fAudioInBuffers != nullptr)
  2434. {
  2435. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2436. {
  2437. if (fAudioInBuffers[i] != nullptr)
  2438. {
  2439. delete[] fAudioInBuffers[i];
  2440. fAudioInBuffers[i] = nullptr;
  2441. }
  2442. }
  2443. delete[] fAudioInBuffers;
  2444. fAudioInBuffers = nullptr;
  2445. }
  2446. if (fAudioOutBuffers != nullptr)
  2447. {
  2448. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2449. {
  2450. if (fAudioOutBuffers[i] != nullptr)
  2451. {
  2452. delete[] fAudioOutBuffers[i];
  2453. fAudioOutBuffers[i] = nullptr;
  2454. }
  2455. }
  2456. delete[] fAudioOutBuffers;
  2457. fAudioOutBuffers = nullptr;
  2458. }
  2459. if (fParamBuffers != nullptr)
  2460. {
  2461. delete[] fParamBuffers;
  2462. fParamBuffers = nullptr;
  2463. }
  2464. fEventsIn.clear();
  2465. fEventsOut.clear();
  2466. CarlaPlugin::clearBuffers();
  2467. carla_debug("Lv2Plugin::clearBuffers() - end");
  2468. }
  2469. // -------------------------------------------------------------------
  2470. // Post-poned UI Stuff
  2471. void uiParameterChange(const uint32_t index, const float value) override
  2472. {
  2473. CARLA_ASSERT(fDescriptor != nullptr);
  2474. CARLA_ASSERT(fHandle != nullptr);
  2475. CARLA_ASSERT(index < kData->param.count);
  2476. if (fDescriptor == nullptr || fHandle == nullptr)
  2477. return;
  2478. if (index >= kData->param.count)
  2479. return;
  2480. if (fUi.type == PLUGIN_UI_OSC)
  2481. {
  2482. if (kData->osc.data.target != nullptr)
  2483. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  2484. }
  2485. else
  2486. {
  2487. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2488. fUi.descriptor->port_event(fUi.handle, kData->param.data[index].rindex, sizeof(float), 0, &value);
  2489. }
  2490. }
  2491. void uiMidiProgramChange(const uint32_t index) override
  2492. {
  2493. CARLA_ASSERT(index < kData->midiprog.count);
  2494. if (index >= kData->midiprog.count)
  2495. return;
  2496. if (fUi.type == PLUGIN_UI_OSC)
  2497. {
  2498. if (kData->osc.data.target != nullptr)
  2499. osc_send_midi_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2500. }
  2501. else
  2502. {
  2503. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2504. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2505. }
  2506. }
  2507. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  2508. {
  2509. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2510. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2511. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  2512. if (channel >= MAX_MIDI_CHANNELS)
  2513. return;
  2514. if (note >= MAX_MIDI_NOTE)
  2515. return;
  2516. if (velo >= MAX_MIDI_VALUE)
  2517. return;
  2518. if (fUi.type == PLUGIN_UI_OSC)
  2519. {
  2520. if (kData->osc.data.target != nullptr)
  2521. {
  2522. uint8_t midiData[4] = { 0 };
  2523. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2524. midiData[2] = note;
  2525. midiData[3] = velo;
  2526. osc_send_midi(&kData->osc.data, midiData);
  2527. }
  2528. }
  2529. else
  2530. {
  2531. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2532. {
  2533. LV2_Atom_MidiEvent midiEv;
  2534. midiEv.event.time.frames = 0;
  2535. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2536. midiEv.event.body.size = 3;
  2537. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2538. midiEv.data[1] = note;
  2539. midiEv.data[2] = velo;
  2540. fUi.descriptor->port_event(fUi.handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2541. }
  2542. }
  2543. }
  2544. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  2545. {
  2546. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2547. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2548. if (channel >= MAX_MIDI_CHANNELS)
  2549. return;
  2550. if (note >= MAX_MIDI_NOTE)
  2551. return;
  2552. if (fUi.type == PLUGIN_UI_OSC)
  2553. {
  2554. if (kData->osc.data.target != nullptr)
  2555. {
  2556. uint8_t midiData[4] = { 0 };
  2557. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2558. midiData[2] = note;
  2559. osc_send_midi(&kData->osc.data, midiData);
  2560. }
  2561. }
  2562. else
  2563. {
  2564. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2565. {
  2566. LV2_Atom_MidiEvent midiEv;
  2567. midiEv.event.time.frames = 0;
  2568. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2569. midiEv.event.body.size = 3;
  2570. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2571. midiEv.data[1] = note;
  2572. midiEv.data[2] = 0;
  2573. fUi.descriptor->port_event(fUi.handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2574. }
  2575. }
  2576. }
  2577. // -------------------------------------------------------------------
  2578. protected:
  2579. void guiClosedCallback() override
  2580. {
  2581. showGui(false);
  2582. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  2583. }
  2584. // -------------------------------------------------------------------
  2585. uint32_t getCustomURID(const char* const uri)
  2586. {
  2587. CARLA_ASSERT(uri != nullptr);
  2588. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  2589. if (uri == nullptr)
  2590. return CARLA_URI_MAP_ID_NULL;
  2591. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  2592. {
  2593. const char*& thisUri(fCustomURIDs.getAt(i));
  2594. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  2595. return i;
  2596. }
  2597. fCustomURIDs.append(carla_strdup(uri));
  2598. return fCustomURIDs.count()-1;
  2599. }
  2600. const char* getCustomURIString(const LV2_URID urid)
  2601. {
  2602. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  2603. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  2604. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  2605. if (urid == CARLA_URI_MAP_ID_NULL)
  2606. return nullptr;
  2607. if (urid < fCustomURIDs.count())
  2608. return fCustomURIDs.getAt(urid);
  2609. return nullptr;
  2610. }
  2611. // -------------------------------------------------------------------
  2612. void handleProgramChanged(const int32_t index)
  2613. {
  2614. if (index == -1)
  2615. {
  2616. const CarlaPlugin::ScopedDisabler m(this);
  2617. return reloadPrograms(false);
  2618. }
  2619. if (index >= 0 && index < static_cast<int32_t>(kData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  2620. {
  2621. if (const LV2_Program_Descriptor* progDesc = fExt.programs->get_program(fHandle, index))
  2622. {
  2623. CARLA_ASSERT(progDesc->name != nullptr);
  2624. if (kData->midiprog.data[index].name != nullptr)
  2625. delete[] kData->midiprog.data[index].name;
  2626. kData->midiprog.data[index].name = carla_strdup(progDesc->name ? progDesc->name : "");
  2627. if (index == kData->midiprog.current)
  2628. kData->engine->callback(CALLBACK_UPDATE, fId, 0, 0, 0.0, nullptr);
  2629. else
  2630. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0, nullptr);
  2631. }
  2632. }
  2633. }
  2634. // -------------------------------------------------------------------
  2635. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2636. {
  2637. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2638. CARLA_ASSERT(value != nullptr);
  2639. CARLA_ASSERT(size > 0);
  2640. // basic checks
  2641. if (key == CARLA_URI_MAP_ID_NULL)
  2642. {
  2643. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2644. return LV2_STATE_ERR_NO_PROPERTY;
  2645. }
  2646. if (value == nullptr || size == 0)
  2647. {
  2648. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid value", key, value, size, type, flags);
  2649. return LV2_STATE_ERR_NO_PROPERTY;
  2650. }
  2651. if ((flags & LV2_STATE_IS_POD) == 0)
  2652. {
  2653. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2654. return LV2_STATE_ERR_BAD_FLAGS;
  2655. }
  2656. const char* const stype(carla_lv2_urid_unmap(this, type));
  2657. if (stype == nullptr)
  2658. {
  2659. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type", key, value, size, type, flags);
  2660. return LV2_STATE_ERR_BAD_TYPE;
  2661. }
  2662. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2663. if (uriKey == nullptr)
  2664. {
  2665. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key URI", key, value, size, type, flags);
  2666. return LV2_STATE_ERR_NO_PROPERTY;
  2667. }
  2668. // Check if we already have this key
  2669. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2670. {
  2671. CustomData& data(*it);
  2672. if (std::strcmp(data.key, uriKey) == 0)
  2673. {
  2674. if (data.value != nullptr)
  2675. delete[] data.value;
  2676. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2677. data.value = carla_strdup((const char*)value);
  2678. else
  2679. data.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2680. return LV2_STATE_SUCCESS;
  2681. }
  2682. }
  2683. // Otherwise store it
  2684. CustomData newData;
  2685. newData.type = carla_strdup(stype);
  2686. newData.key = carla_strdup(uriKey);
  2687. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2688. newData.value = carla_strdup((const char*)value);
  2689. else
  2690. newData.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2691. kData->custom.append(newData);
  2692. return LV2_STATE_SUCCESS;
  2693. }
  2694. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2695. {
  2696. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2697. // basic checks
  2698. if (key == CARLA_URI_MAP_ID_NULL)
  2699. {
  2700. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key", key, size, type, flags);
  2701. return nullptr;
  2702. }
  2703. if (size == nullptr || type == nullptr || flags == nullptr)
  2704. {
  2705. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid data", key, size, type, flags);
  2706. return nullptr;
  2707. }
  2708. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2709. if (uriKey == nullptr)
  2710. {
  2711. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  2712. return nullptr;
  2713. }
  2714. const char* stype = nullptr;
  2715. const char* stringData = nullptr;
  2716. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2717. {
  2718. CustomData& data(*it);
  2719. if (std::strcmp(data.key, uriKey) == 0)
  2720. {
  2721. stype = data.type;
  2722. stringData = data.value;
  2723. break;
  2724. }
  2725. }
  2726. if (stringData == nullptr)
  2727. {
  2728. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  2729. return nullptr;
  2730. }
  2731. *type = key;
  2732. *flags = LV2_STATE_IS_POD;
  2733. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2734. {
  2735. *size = std::strlen(stringData);
  2736. return stringData;
  2737. }
  2738. else
  2739. {
  2740. static QByteArray chunk;
  2741. chunk = QByteArray::fromBase64(stringData);
  2742. *size = chunk.size();
  2743. return chunk.constData();
  2744. }
  2745. }
  2746. // -------------------------------------------------------------------
  2747. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  2748. {
  2749. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  2750. #if 0
  2751. if (! ext.worker)
  2752. {
  2753. carla_stderr("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  2754. return LV2_WORKER_ERR_UNKNOWN;
  2755. }
  2756. if (x_engine->isOffline())
  2757. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2758. else
  2759. postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  2760. #endif
  2761. return LV2_WORKER_SUCCESS;
  2762. }
  2763. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  2764. {
  2765. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  2766. #if 0
  2767. LV2_Atom_Worker workerAtom;
  2768. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  2769. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  2770. workerAtom.body.size = size;
  2771. workerAtom.body.data = data;
  2772. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  2773. #endif
  2774. return LV2_WORKER_SUCCESS;
  2775. }
  2776. // -------------------------------------------------------------------
  2777. void handleExternalUiClosed()
  2778. {
  2779. CARLA_ASSERT(fUi.type == PLUGIN_UI_EXTERNAL);
  2780. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  2781. fUi.descriptor->cleanup(fUi.handle);
  2782. fUi.handle = nullptr;
  2783. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0, nullptr);
  2784. }
  2785. uint32_t handleUiPortMap(const char* const symbol)
  2786. {
  2787. CARLA_ASSERT(symbol != nullptr);
  2788. if (symbol == nullptr)
  2789. return LV2UI_INVALID_PORT_INDEX;
  2790. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  2791. {
  2792. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  2793. return i;
  2794. }
  2795. return LV2UI_INVALID_PORT_INDEX;
  2796. }
  2797. int handleUiResize(const int width, const int height)
  2798. {
  2799. CARLA_ASSERT(kData->gui != nullptr);
  2800. CARLA_ASSERT(width > 0);
  2801. CARLA_ASSERT(height > 0);
  2802. if (width <= 0 || height <= 0)
  2803. return 1;
  2804. if (kData->gui != nullptr)
  2805. kData->gui->setSize(width, height);
  2806. return 0;
  2807. }
  2808. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  2809. {
  2810. if (format == 0)
  2811. {
  2812. CARLA_ASSERT(buffer != nullptr);
  2813. CARLA_ASSERT(bufferSize == sizeof(float));
  2814. if (buffer == nullptr || bufferSize != sizeof(float))
  2815. return;
  2816. float value = *(float*)buffer;
  2817. for (uint32_t i=0; i < kData->param.count; ++i)
  2818. {
  2819. if (kData->param.data[i].rindex == static_cast<int32_t>(rindex))
  2820. return setParameterValue(i, value, false, true, true);
  2821. }
  2822. }
  2823. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2824. {
  2825. CARLA_ASSERT(buffer != nullptr);
  2826. if (buffer == nullptr)
  2827. return;
  2828. //const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2829. //handleTransferAtom(rindex, atom);
  2830. }
  2831. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2832. {
  2833. CARLA_ASSERT(buffer != nullptr);
  2834. if (buffer == nullptr)
  2835. return;
  2836. //const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2837. //handleTransferEvent(rindex, atom);
  2838. }
  2839. }
  2840. // -------------------------------------------------------------------
  2841. bool needsFixedBuffer()
  2842. {
  2843. CARLA_ASSERT(fRdfDescriptor != nullptr);
  2844. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2845. {
  2846. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  2847. return true;
  2848. }
  2849. return false;
  2850. }
  2851. // -------------------------------------------------------------------
  2852. const char* getUiBridgePath(const LV2_Property type)
  2853. {
  2854. const EngineOptions& options(kData->engine->getOptions());
  2855. switch (type)
  2856. {
  2857. case LV2_UI_GTK2:
  2858. return options.bridge_lv2Gtk2;
  2859. case LV2_UI_GTK3:
  2860. return options.bridge_lv2Gtk3;
  2861. case LV2_UI_QT4:
  2862. return options.bridge_lv2Qt4;
  2863. case LV2_UI_QT5:
  2864. return options.bridge_lv2Qt5;
  2865. case LV2_UI_COCOA:
  2866. return options.bridge_lv2Cocoa;
  2867. case LV2_UI_WINDOWS:
  2868. return options.bridge_lv2Win;
  2869. case LV2_UI_X11:
  2870. return options.bridge_lv2X11;
  2871. default:
  2872. return nullptr;
  2873. }
  2874. }
  2875. bool isUiBridgeable(const uint32_t uiId)
  2876. {
  2877. const LV2_RDF_UI& rdfUi(fRdfDescriptor->UIs[uiId]);
  2878. for (uint32_t i=0; i < rdfUi.FeatureCount; ++i)
  2879. {
  2880. if (std::strcmp(rdfUi.Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  2881. return false;
  2882. if (std::strcmp(rdfUi.Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2883. return false;
  2884. }
  2885. return true;
  2886. }
  2887. bool isUiResizable()
  2888. {
  2889. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  2890. {
  2891. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  2892. return false;
  2893. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2894. return false;
  2895. }
  2896. return true;
  2897. }
  2898. void updateUi()
  2899. {
  2900. CARLA_ASSERT(fUi.handle != nullptr);
  2901. CARLA_ASSERT(fUi.descriptor != nullptr);
  2902. fExt.uiidle = nullptr;
  2903. fExt.uiprograms = nullptr;
  2904. if (fUi.descriptor->extension_data != nullptr)
  2905. {
  2906. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  2907. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2908. // check if invalid
  2909. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  2910. fExt.uiidle = nullptr;
  2911. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  2912. fExt.uiprograms = nullptr;
  2913. // update midi program
  2914. if (fExt.uiprograms && kData->midiprog.count > 0 && kData->midiprog.current >= 0)
  2915. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[kData->midiprog.current].bank,
  2916. kData->midiprog.data[kData->midiprog.current].program);
  2917. }
  2918. if (fUi.descriptor->port_event != nullptr)
  2919. {
  2920. // update control ports
  2921. float value;
  2922. for (uint32_t i=0; i < kData->param.count; ++i)
  2923. {
  2924. value = getParameterValue(i);
  2925. fUi.descriptor->port_event(fUi.handle, kData->param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  2926. }
  2927. }
  2928. }
  2929. // -------------------------------------------------------------------
  2930. public:
  2931. bool init(const char* const name, const char* const uri)
  2932. {
  2933. CARLA_ASSERT(kData->engine != nullptr);
  2934. CARLA_ASSERT(kData->client == nullptr);
  2935. CARLA_ASSERT(uri != nullptr);
  2936. // ---------------------------------------------------------------
  2937. // first checks
  2938. if (kData->engine == nullptr)
  2939. {
  2940. return false;
  2941. }
  2942. if (kData->client != nullptr)
  2943. {
  2944. kData->engine->setLastError("Plugin client is already registered");
  2945. return false;
  2946. }
  2947. if (uri == nullptr)
  2948. {
  2949. kData->engine->setLastError("null uri");
  2950. return false;
  2951. }
  2952. // ---------------------------------------------------------------
  2953. // get plugin from lv2_rdf (lilv)
  2954. gLv2World.init();
  2955. fRdfDescriptor = lv2_rdf_new(uri);
  2956. if (fRdfDescriptor == nullptr)
  2957. {
  2958. kData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  2959. return false;
  2960. }
  2961. // ---------------------------------------------------------------
  2962. // open DLL
  2963. if (! kData->libOpen(fRdfDescriptor->Binary))
  2964. {
  2965. kData->engine->setLastError(kData->libError(fRdfDescriptor->Binary));
  2966. return false;
  2967. }
  2968. // ---------------------------------------------------------------
  2969. // initialize options
  2970. fLv2Options.minBufferSize = 1;
  2971. fLv2Options.maxBufferSize = kData->engine->getBufferSize();
  2972. fLv2Options.sampleRate = kData->engine->getSampleRate();
  2973. // ---------------------------------------------------------------
  2974. // initialize features (part 1)
  2975. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  2976. eventFt->callback_data = this;
  2977. eventFt->lv2_event_ref = carla_lv2_event_ref;
  2978. eventFt->lv2_event_unref = carla_lv2_event_unref;
  2979. LV2_Log_Log* const logFt = new LV2_Log_Log;
  2980. logFt->handle = this;
  2981. logFt->printf = carla_lv2_log_printf;
  2982. logFt->vprintf = carla_lv2_log_vprintf;
  2983. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  2984. stateMakePathFt->handle = this;
  2985. stateMakePathFt->path = carla_lv2_state_make_path;
  2986. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  2987. stateMapPathFt->handle = this;
  2988. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  2989. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  2990. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  2991. programsFt->handle = this;
  2992. programsFt->program_changed = carla_lv2_program_changed;
  2993. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  2994. lv2_rtmempool_init(rtMemPoolFt);
  2995. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  2996. uriMapFt->callback_data = this;
  2997. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  2998. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  2999. uridMapFt->handle = this;
  3000. uridMapFt->map = carla_lv2_urid_map;
  3001. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3002. uridUnmapFt->handle = this;
  3003. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3004. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3005. workerFt->handle = this;
  3006. workerFt->schedule_work = carla_lv2_worker_schedule;
  3007. // ---------------------------------------------------------------
  3008. // initialize features (part 2)
  3009. fFeatures[kFeatureIdBufSizeBounded] = new LV2_Feature;
  3010. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3011. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3012. fFeatures[kFeatureIdBufSizeFixed] = new LV2_Feature;
  3013. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3014. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3015. fFeatures[kFeatureIdBufSizePowerOf2] = new LV2_Feature;
  3016. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3017. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3018. fFeatures[kFeatureIdEvent] = new LV2_Feature;
  3019. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3020. fFeatures[kFeatureIdEvent]->data = eventFt;
  3021. fFeatures[kFeatureIdLogs] = new LV2_Feature;
  3022. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3023. fFeatures[kFeatureIdLogs]->data = logFt;
  3024. fFeatures[kFeatureIdOptions] = new LV2_Feature;
  3025. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3026. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3027. fFeatures[kFeatureIdPrograms] = new LV2_Feature;
  3028. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3029. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3030. fFeatures[kFeatureIdRtMemPool] = new LV2_Feature;
  3031. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3032. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3033. fFeatures[kFeatureIdStateMakePath] = new LV2_Feature;
  3034. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3035. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3036. fFeatures[kFeatureIdStateMapPath] = new LV2_Feature;
  3037. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3038. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3039. fFeatures[kFeatureIdStrictBounds] = new LV2_Feature;
  3040. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3041. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3042. fFeatures[kFeatureIdUriMap] = new LV2_Feature;
  3043. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3044. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3045. fFeatures[kFeatureIdUridMap] = new LV2_Feature;
  3046. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3047. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3048. fFeatures[kFeatureIdUridUnmap] = new LV2_Feature;
  3049. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3050. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3051. fFeatures[kFeatureIdWorker] = new LV2_Feature;
  3052. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3053. fFeatures[kFeatureIdWorker]->data = workerFt;
  3054. if (! needsFixedBuffer())
  3055. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3056. // ---------------------------------------------------------------
  3057. // get DLL main entry
  3058. #if 0
  3059. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)kData->libSymbol("lv2_lib_descriptor");
  3060. if (libDescFn != nullptr)
  3061. {
  3062. // -----------------------------------------------------------
  3063. // get lib descriptor
  3064. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  3065. if (libFn == nullptr || libFn->get_plugin == nullptr)
  3066. {
  3067. kData->engine->setLastError("Plugin failed to return library descriptor");
  3068. return false;
  3069. }
  3070. // -----------------------------------------------------------
  3071. // get descriptor that matches URI
  3072. uint32_t i = 0;
  3073. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  3074. {
  3075. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3076. break;
  3077. }
  3078. if (fDescriptor == nullptr)
  3079. libFn->cleanup(libFn->handle);
  3080. else
  3081. #endif
  3082. {
  3083. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)kData->libSymbol("lv2_descriptor");
  3084. if (descFn == nullptr)
  3085. {
  3086. kData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3087. return false;
  3088. }
  3089. // -----------------------------------------------------------
  3090. // get descriptor that matches URI
  3091. uint32_t i = 0;
  3092. while ((fDescriptor = descFn(i++)))
  3093. {
  3094. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3095. break;
  3096. }
  3097. }
  3098. if (fDescriptor == nullptr)
  3099. {
  3100. kData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3101. return false;
  3102. }
  3103. // ---------------------------------------------------------------
  3104. // check supported port-types and features
  3105. bool canContinue = true;
  3106. // Check supported ports
  3107. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3108. {
  3109. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3110. 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)))
  3111. {
  3112. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  3113. {
  3114. kData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3115. canContinue = false;
  3116. break;
  3117. }
  3118. }
  3119. }
  3120. // Check supported features
  3121. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  3122. {
  3123. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  3124. {
  3125. QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  3126. kData->engine->setLastError(msg.toUtf8().constData());
  3127. canContinue = false;
  3128. break;
  3129. }
  3130. }
  3131. // Check extensions
  3132. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3133. {
  3134. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3135. kData->extraHints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3136. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3137. kData->extraHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3138. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3139. kData->extraHints |= PLUGIN_HAS_EXTENSION_STATE;
  3140. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3141. kData->extraHints |= PLUGIN_HAS_EXTENSION_WORKER;
  3142. else
  3143. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3144. }
  3145. if (! canContinue)
  3146. {
  3147. // error already set
  3148. return false;
  3149. }
  3150. // ---------------------------------------------------------------
  3151. // get info
  3152. if (name != nullptr)
  3153. fName = kData->engine->getUniquePluginName(name);
  3154. else
  3155. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3156. // ---------------------------------------------------------------
  3157. // register client
  3158. kData->client = kData->engine->addClient(this);
  3159. if (kData->client == nullptr || ! kData->client->isOk())
  3160. {
  3161. kData->engine->setLastError("Failed to register plugin client");
  3162. return false;
  3163. }
  3164. // ---------------------------------------------------------------
  3165. // initialize plugin
  3166. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3167. if (fHandle == nullptr)
  3168. {
  3169. kData->engine->setLastError("Plugin failed to initialize");
  3170. return false;
  3171. }
  3172. // ---------------------------------------------------------------
  3173. // load plugin settings
  3174. {
  3175. // set default options
  3176. fOptions = 0x0;
  3177. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3178. if (needsFixedBuffer())
  3179. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3180. if (kData->engine->getOptions().forceStereo)
  3181. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  3182. if (midiInCount() > 0)
  3183. {
  3184. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3185. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3186. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  3187. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3188. }
  3189. // load settings
  3190. kData->idStr = "LV2/";
  3191. kData->idStr += uri;
  3192. fOptions = kData->loadSettings(fOptions, availableOptions());
  3193. // ignore settings, we need this anyway
  3194. if (needsFixedBuffer())
  3195. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3196. }
  3197. // ---------------------------------------------------------------
  3198. // gui stuff
  3199. if (fRdfDescriptor->UICount == 0)
  3200. return true;
  3201. // -----------------------------------------------------------
  3202. // find more appropriate ui
  3203. int eQt4, eQt5, eCocoa, eWindows, eX11, eGtk2, eGtk3, iCocoa, iWindows, iX11, iQt4, iQt5, iExt, iFinal;
  3204. eQt4 = eQt5 = eCocoa = eWindows = eX11 = eGtk2 = eGtk3 = iQt4 = iQt5 = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3205. #ifdef BUILD_BRIDGE
  3206. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges);
  3207. #else
  3208. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges && (fHints & PLUGIN_IS_BRIDGE) == 0);
  3209. #endif
  3210. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3211. {
  3212. CARLA_ASSERT(fRdfDescriptor->UIs[i].URI != nullptr);
  3213. if (fRdfDescriptor->UIs[i].URI == nullptr)
  3214. {
  3215. carla_stderr("Plugin has an UI without a valid URI");
  3216. continue;
  3217. }
  3218. switch (fRdfDescriptor->UIs[i].Type)
  3219. {
  3220. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3221. case LV2_UI_QT4:
  3222. if (isUiBridgeable(i))
  3223. eQt4 = i;
  3224. break;
  3225. #else
  3226. case LV2_UI_QT4:
  3227. if (isUiBridgeable(i) && preferUiBridges)
  3228. eQt4 = i;
  3229. iQt4 = i;
  3230. break;
  3231. #endif
  3232. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3233. case LV2_UI_QT5:
  3234. if (isUiBridgeable(i) && preferUiBridges)
  3235. eQt5 = i;
  3236. iQt5 = i;
  3237. break;
  3238. #else
  3239. case LV2_UI_QT5:
  3240. if (isUiBridgeable(i) && preferUiBridges)
  3241. eQt5 = i;
  3242. break;
  3243. #endif
  3244. #ifdef CARLA_OS_MAC
  3245. case LV2_UI_COCOA:
  3246. if (isUiBridgeable(i) && preferUiBridges)
  3247. eCocoa = i;
  3248. iCocoa = i;
  3249. break;
  3250. #endif
  3251. #ifdef CARLA_OS_WIN
  3252. case LV2_UI_WINDOWS:
  3253. if (isUiBridgeable(i) && preferUiBridges)
  3254. eWindows = i;
  3255. iWindows = i;
  3256. break;
  3257. #endif
  3258. case LV2_UI_X11:
  3259. if (isUiBridgeable(i) && preferUiBridges)
  3260. eX11 = i;
  3261. #ifdef Q_WS_X11
  3262. iX11 = i;
  3263. #endif
  3264. break;
  3265. case LV2_UI_GTK2:
  3266. if (isUiBridgeable(i))
  3267. eGtk2 = i;
  3268. break;
  3269. case LV2_UI_GTK3:
  3270. if (isUiBridgeable(i))
  3271. eGtk3 = i;
  3272. break;
  3273. case LV2_UI_EXTERNAL:
  3274. case LV2_UI_OLD_EXTERNAL:
  3275. iExt = i;
  3276. break;
  3277. default:
  3278. break;
  3279. }
  3280. }
  3281. if (eQt4 >= 0)
  3282. iFinal = eQt4;
  3283. else if (eQt5 >= 0)
  3284. iFinal = eQt5;
  3285. else if (eCocoa >= 0)
  3286. iFinal = eCocoa;
  3287. else if (eWindows >= 0)
  3288. iFinal = eWindows;
  3289. else if (eX11 >= 0)
  3290. iFinal = eX11;
  3291. else if (iQt4 >= 0)
  3292. iFinal = iQt4;
  3293. else if (iQt5 >= 0)
  3294. iFinal = iQt5;
  3295. else if (iCocoa >= 0)
  3296. iFinal = iCocoa;
  3297. else if (iWindows >= 0)
  3298. iFinal = iWindows;
  3299. else if (iX11 >= 0)
  3300. iFinal = iX11;
  3301. else if (iExt >= 0)
  3302. iFinal = iExt;
  3303. else if (eGtk2 >= 0)
  3304. iFinal = eGtk2;
  3305. else if (eGtk3 >= 0)
  3306. iFinal = eGtk3;
  3307. if (iFinal < 0)
  3308. {
  3309. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3310. return true;
  3311. }
  3312. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3313. // -----------------------------------------------------------
  3314. // check supported ui features
  3315. canContinue = true;
  3316. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3317. {
  3318. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type) && ! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3319. {
  3320. carla_stderr2("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3321. canContinue = false;
  3322. break;
  3323. }
  3324. }
  3325. if (! canContinue)
  3326. {
  3327. fUi.rdfDescriptor = nullptr;
  3328. return true;
  3329. }
  3330. // -----------------------------------------------------------
  3331. // initialize ui according to type
  3332. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3333. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3)
  3334. {
  3335. // -------------------------------------------------------
  3336. // initialize ui bridge
  3337. if (const char* const oscBinary = getUiBridgePath(uiType))
  3338. {
  3339. fUi.type = PLUGIN_UI_OSC;
  3340. kData->osc.thread.setOscData(oscBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3341. }
  3342. }
  3343. else
  3344. {
  3345. // -------------------------------------------------------
  3346. // open UI DLL
  3347. if (! kData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3348. {
  3349. carla_stderr2("Could not load UI library, error was:\n%s", kData->libError(fUi.rdfDescriptor->Binary));
  3350. fUi.rdfDescriptor = nullptr;
  3351. return true;
  3352. }
  3353. // -------------------------------------------------------
  3354. // get UI DLL main entry
  3355. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)kData->uiLibSymbol("lv2ui_descriptor");
  3356. if (uiDescFn == nullptr)
  3357. {
  3358. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3359. kData->uiLibClose();
  3360. fUi.rdfDescriptor = nullptr;
  3361. return true;
  3362. }
  3363. // -------------------------------------------------------
  3364. // get UI descriptor that matches UI URI
  3365. uint32_t i = 0;
  3366. while ((fUi.descriptor = uiDescFn(i++)))
  3367. {
  3368. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3369. break;
  3370. }
  3371. if (fUi.descriptor == nullptr)
  3372. {
  3373. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3374. kData->uiLibClose();
  3375. fUi.rdfDescriptor = nullptr;
  3376. return true;
  3377. }
  3378. // -------------------------------------------------------
  3379. // check if ui is usable
  3380. switch (uiType)
  3381. {
  3382. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3383. case LV2_UI_QT5:
  3384. carla_debug("Will use LV2 Qt5 UI");
  3385. fUi.type = PLUGIN_UI_QT;
  3386. break;
  3387. #else
  3388. case LV2_UI_QT4:
  3389. carla_debug("Will use LV2 Qt4 UI");
  3390. fUi.type = PLUGIN_UI_QT;
  3391. break;
  3392. #endif
  3393. #ifdef CARLA_OS_MAC
  3394. case LV2_UI_COCOA:
  3395. carla_debug("Will use LV2 Cocoa UI");
  3396. fUi.type = PLUGIN_UI_PARENT;
  3397. break;
  3398. #endif
  3399. #ifdef CARLA_OS_WIN
  3400. case LV2_UI_WINDOWS:
  3401. carla_debug("Will use LV2 Windows UI");
  3402. fUi.type = PLUGIN_UI_PARENT;
  3403. break;
  3404. #endif
  3405. #ifdef Q_WS_X11
  3406. case LV2_UI_X11:
  3407. carla_debug("Will use LV2 X11 UI");
  3408. fUi.type = PLUGIN_UI_PARENT;
  3409. break;
  3410. #endif
  3411. case LV2_UI_GTK2:
  3412. carla_debug("Will use LV2 Gtk2 UI, NOT!");
  3413. break;
  3414. case LV2_UI_GTK3:
  3415. carla_debug("Will use LV2 Gtk3 UI, NOT!");
  3416. break;
  3417. case LV2_UI_EXTERNAL:
  3418. case LV2_UI_OLD_EXTERNAL:
  3419. carla_debug("Will use LV2 External UI");
  3420. fUi.type = PLUGIN_UI_EXTERNAL;
  3421. break;
  3422. }
  3423. if (fUi.type == PLUGIN_UI_NULL)
  3424. {
  3425. kData->uiLibClose();
  3426. fUi.descriptor = nullptr;
  3427. fUi.rdfDescriptor = nullptr;
  3428. return true;
  3429. }
  3430. // -------------------------------------------------------
  3431. // initialize ui features
  3432. QString guiTitle(QString("%1 (GUI)").arg((const char*)fName));
  3433. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3434. uiDataFt->data_access = fDescriptor->extension_data;
  3435. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3436. uiPortMapFt->handle = this;
  3437. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3438. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3439. uiResizeFt->handle = this;
  3440. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3441. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3442. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3443. uiExternalHostFt->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  3444. fFeatures[kFeatureIdUiDataAccess] = new LV2_Feature;
  3445. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3446. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3447. fFeatures[kFeatureIdUiInstanceAccess] = new LV2_Feature;
  3448. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3449. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3450. fFeatures[kFeatureIdUiParent] = new LV2_Feature;
  3451. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3452. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3453. fFeatures[kFeatureIdUiPortMap] = new LV2_Feature;
  3454. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3455. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3456. fFeatures[kFeatureIdUiResize] = new LV2_Feature;
  3457. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3458. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3459. fFeatures[kFeatureIdExternalUi] = new LV2_Feature;
  3460. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3461. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3462. fFeatures[kFeatureIdExternalUiOld] = new LV2_Feature;
  3463. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3464. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3465. }
  3466. return true;
  3467. }
  3468. private:
  3469. LV2_Handle fHandle;
  3470. LV2_Handle fHandle2;
  3471. LV2_Feature* fFeatures[kFeatureCount+1];
  3472. const LV2_Descriptor* fDescriptor;
  3473. const LV2_RDF_Descriptor* fRdfDescriptor;
  3474. float** fAudioInBuffers;
  3475. float** fAudioOutBuffers;
  3476. float* fParamBuffers;
  3477. Lv2PluginEventData fEventsIn;
  3478. Lv2PluginEventData fEventsOut;
  3479. Lv2PluginOptions fLv2Options;
  3480. NonRtList<const char*> fCustomURIDs;
  3481. struct Extensions {
  3482. const LV2_Options_Interface* options;
  3483. const LV2_State_Interface* state;
  3484. const LV2_Worker_Interface* worker;
  3485. const LV2_Programs_Interface* programs;
  3486. const LV2UI_Idle_Interface* uiidle;
  3487. const LV2_Programs_UI_Interface* uiprograms;
  3488. Extensions()
  3489. : options(nullptr),
  3490. state(nullptr),
  3491. worker(nullptr),
  3492. programs(nullptr),
  3493. uiidle(nullptr),
  3494. uiprograms(nullptr) {}
  3495. } fExt;
  3496. struct UI {
  3497. Lv2PluginGuiType type;
  3498. LV2UI_Handle handle;
  3499. LV2UI_Widget widget;
  3500. const LV2UI_Descriptor* descriptor;
  3501. const LV2_RDF_UI* rdfDescriptor;
  3502. UI()
  3503. : type(PLUGIN_UI_NULL),
  3504. handle(nullptr),
  3505. widget(nullptr),
  3506. descriptor(nullptr),
  3507. rdfDescriptor(nullptr) {}
  3508. ~UI()
  3509. {
  3510. CARLA_ASSERT(handle == nullptr);
  3511. CARLA_ASSERT(widget == nullptr);
  3512. CARLA_ASSERT(descriptor == nullptr);
  3513. CARLA_ASSERT(rdfDescriptor == nullptr);
  3514. }
  3515. } fUi;
  3516. // -------------------------------------------------------------------
  3517. // Event Feature
  3518. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3519. {
  3520. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  3521. CARLA_ASSERT(callback_data != nullptr);
  3522. CARLA_ASSERT(event != nullptr);
  3523. return 0;
  3524. }
  3525. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3526. {
  3527. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  3528. CARLA_ASSERT(callback_data != nullptr);
  3529. CARLA_ASSERT(event != nullptr);
  3530. return 0;
  3531. }
  3532. // -------------------------------------------------------------------
  3533. // Logs Feature
  3534. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  3535. {
  3536. CARLA_ASSERT(handle != nullptr);
  3537. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3538. #ifndef DEBUG
  3539. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3540. return 0;
  3541. #endif
  3542. va_list args;
  3543. va_start(args, fmt);
  3544. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  3545. va_end(args);
  3546. return ret;
  3547. }
  3548. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  3549. {
  3550. CARLA_ASSERT(handle != nullptr);
  3551. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3552. #ifndef DEBUG
  3553. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3554. return 0;
  3555. #endif
  3556. int ret = 0;
  3557. switch (type)
  3558. {
  3559. case CARLA_URI_MAP_ID_LOG_ERROR:
  3560. #ifndef CARLA_OS_WIN
  3561. std::fprintf(stderr, "\x1b[31m");
  3562. #endif
  3563. ret = std::vfprintf(stderr, fmt, ap);
  3564. #ifndef CARLA_OS_WIN
  3565. std::fprintf(stderr, "\x1b[0m");
  3566. #endif
  3567. break;
  3568. case CARLA_URI_MAP_ID_LOG_NOTE:
  3569. ret = std::vfprintf(stdout, fmt, ap);
  3570. break;
  3571. case CARLA_URI_MAP_ID_LOG_TRACE:
  3572. #ifdef DEBUG
  3573. # ifndef CARLA_OS_WIN
  3574. std::fprintf(stdout, "\x1b[30;1m");
  3575. # endif
  3576. ret = std::vfprintf(stdout, fmt, ap);
  3577. # ifndef CARLA_OS_WIN
  3578. std::fprintf(stdout, "\x1b[0m");
  3579. # endif
  3580. #endif
  3581. break;
  3582. case CARLA_URI_MAP_ID_LOG_WARNING:
  3583. ret = std::vfprintf(stderr, fmt, ap);
  3584. break;
  3585. default:
  3586. break;
  3587. }
  3588. return ret;
  3589. }
  3590. // -------------------------------------------------------------------
  3591. // Programs Feature
  3592. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  3593. {
  3594. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  3595. CARLA_ASSERT(handle != nullptr);
  3596. if (handle == nullptr)
  3597. return;
  3598. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  3599. }
  3600. // -------------------------------------------------------------------
  3601. // State Feature
  3602. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  3603. {
  3604. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3605. CARLA_ASSERT(handle != nullptr);
  3606. CARLA_ASSERT(path != nullptr);
  3607. if (path == nullptr)
  3608. return nullptr;
  3609. QDir dir;
  3610. dir.mkpath(path);
  3611. return strdup(path);
  3612. }
  3613. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  3614. {
  3615. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3616. CARLA_ASSERT(handle != nullptr);
  3617. CARLA_ASSERT(absolute_path != nullptr);
  3618. if (absolute_path == nullptr)
  3619. return nullptr;
  3620. QDir dir(absolute_path);
  3621. return strdup(dir.canonicalPath().toUtf8().constData());
  3622. }
  3623. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  3624. {
  3625. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  3626. CARLA_ASSERT(handle != nullptr);
  3627. CARLA_ASSERT(abstract_path != nullptr);
  3628. if (abstract_path == nullptr)
  3629. return nullptr;
  3630. QDir dir(abstract_path);
  3631. return strdup(dir.absolutePath().toUtf8().constData());
  3632. }
  3633. 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)
  3634. {
  3635. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  3636. CARLA_ASSERT(handle != nullptr);
  3637. if (handle == nullptr)
  3638. return LV2_STATE_ERR_UNKNOWN;
  3639. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  3640. }
  3641. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  3642. {
  3643. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  3644. CARLA_ASSERT(handle != nullptr);
  3645. if (handle == nullptr)
  3646. return nullptr;
  3647. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  3648. }
  3649. // -------------------------------------------------------------------
  3650. // URI-Map Feature
  3651. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  3652. {
  3653. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  3654. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  3655. // unused
  3656. (void)map;
  3657. }
  3658. // -------------------------------------------------------------------
  3659. // URID Feature
  3660. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  3661. {
  3662. CARLA_ASSERT(handle != nullptr);
  3663. CARLA_ASSERT(uri != nullptr);
  3664. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  3665. if (uri == nullptr)
  3666. return CARLA_URI_MAP_ID_NULL;
  3667. // Atom types
  3668. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  3669. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  3670. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  3671. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  3672. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  3673. return CARLA_URI_MAP_ID_ATOM_INT;
  3674. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  3675. return CARLA_URI_MAP_ID_ATOM_PATH;
  3676. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  3677. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  3678. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  3679. return CARLA_URI_MAP_ID_ATOM_STRING;
  3680. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  3681. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  3682. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  3683. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  3684. // BufSize types
  3685. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  3686. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  3687. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  3688. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  3689. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  3690. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  3691. // Log types
  3692. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  3693. return CARLA_URI_MAP_ID_LOG_ERROR;
  3694. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  3695. return CARLA_URI_MAP_ID_LOG_NOTE;
  3696. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  3697. return CARLA_URI_MAP_ID_LOG_TRACE;
  3698. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  3699. return CARLA_URI_MAP_ID_LOG_WARNING;
  3700. // Others
  3701. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  3702. return CARLA_URI_MAP_ID_MIDI_EVENT;
  3703. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  3704. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  3705. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  3706. return CARLA_URI_MAP_ID_TIME_POSITION;
  3707. if (handle == nullptr)
  3708. return CARLA_URI_MAP_ID_NULL;
  3709. // Custom types
  3710. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  3711. }
  3712. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  3713. {
  3714. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  3715. CARLA_ASSERT(handle != nullptr);
  3716. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  3717. if (urid == CARLA_URI_MAP_ID_NULL)
  3718. return nullptr;
  3719. // Atom types
  3720. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  3721. return LV2_ATOM__Chunk;
  3722. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  3723. return LV2_ATOM__Double;
  3724. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  3725. return LV2_ATOM__Int;
  3726. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  3727. return LV2_ATOM__Path;
  3728. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  3729. return LV2_ATOM__Sequence;
  3730. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  3731. return LV2_ATOM__String;
  3732. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3733. return LV2_ATOM__atomTransfer;
  3734. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3735. return LV2_ATOM__eventTransfer;
  3736. // BufSize types
  3737. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  3738. return LV2_BUF_SIZE__maxBlockLength;
  3739. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  3740. return LV2_BUF_SIZE__minBlockLength;
  3741. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  3742. return LV2_BUF_SIZE__sequenceSize;
  3743. // Log types
  3744. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  3745. return LV2_LOG__Error;
  3746. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  3747. return LV2_LOG__Note;
  3748. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  3749. return LV2_LOG__Trace;
  3750. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  3751. return LV2_LOG__Warning;
  3752. // Others
  3753. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  3754. return LV2_MIDI__MidiEvent;
  3755. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  3756. return LV2_PARAMETERS__sampleRate;
  3757. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  3758. return LV2_TIME__Position;
  3759. if (handle == nullptr)
  3760. return nullptr;
  3761. // Custom types
  3762. return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  3763. }
  3764. // -------------------------------------------------------------------
  3765. // Worker Feature
  3766. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  3767. {
  3768. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  3769. CARLA_ASSERT(handle != nullptr);
  3770. if (handle == nullptr)
  3771. return LV2_WORKER_ERR_UNKNOWN;
  3772. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  3773. }
  3774. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  3775. {
  3776. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  3777. CARLA_ASSERT(handle != nullptr);
  3778. if (handle == nullptr)
  3779. return LV2_WORKER_ERR_UNKNOWN;
  3780. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  3781. }
  3782. // -------------------------------------------------------------------
  3783. // UI Port-Map Feature
  3784. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  3785. {
  3786. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  3787. CARLA_ASSERT(handle);
  3788. if (handle == nullptr)
  3789. return LV2UI_INVALID_PORT_INDEX;
  3790. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  3791. }
  3792. // -------------------------------------------------------------------
  3793. // UI Resize Feature
  3794. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  3795. {
  3796. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  3797. CARLA_ASSERT(handle != nullptr);
  3798. if (handle == nullptr)
  3799. return 1;
  3800. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  3801. }
  3802. // -------------------------------------------------------------------
  3803. // External UI Feature
  3804. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  3805. {
  3806. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  3807. CARLA_ASSERT(controller != nullptr);
  3808. if (controller == nullptr)
  3809. return;
  3810. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  3811. }
  3812. // -------------------------------------------------------------------
  3813. // UI Extension
  3814. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  3815. {
  3816. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  3817. CARLA_ASSERT(controller != nullptr);
  3818. if (controller == nullptr)
  3819. return;
  3820. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  3821. }
  3822. // -------------------------------------------------------------------
  3823. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  3824. };
  3825. CARLA_BACKEND_END_NAMESPACE
  3826. #else // WANT_VST
  3827. # warning Building without LV2 support
  3828. #endif
  3829. CARLA_BACKEND_START_NAMESPACE
  3830. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  3831. {
  3832. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  3833. #ifdef WANT_LV2
  3834. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  3835. if (! plugin->init(init.name, init.label))
  3836. {
  3837. delete plugin;
  3838. return nullptr;
  3839. }
  3840. plugin->reload();
  3841. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  3842. {
  3843. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  3844. delete plugin;
  3845. return nullptr;
  3846. }
  3847. return plugin;
  3848. #else
  3849. init.engine->setLastError("LV2 support not available");
  3850. return nullptr;
  3851. #endif
  3852. }
  3853. CARLA_BACKEND_END_NAMESPACE