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.

5251 lines
188KB

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