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.

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