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.

4855 lines
171KB

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