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.

4707 lines
166KB

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