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.

5253 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, kData->guiGeometry);
  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->guiGeometry = kData->gui->saveGeometry();
  992. kData->gui->close();
  993. delete kData->gui;
  994. kData->gui = nullptr;
  995. kData->engine->callback(CALLBACK_ERROR, fId, 0, 0, 0.0f, "Plugin refused to open its own UI");
  996. kData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  997. return;
  998. }
  999. if (fUi.type == PLUGIN_UI_QT)
  1000. kData->gui->setWidget((QWidget*)fUi.widget);
  1001. updateUi();
  1002. kData->gui->setWindowTitle(QString("%1 (GUI)").arg((const char*)fName));
  1003. kData->gui->show();
  1004. }
  1005. else
  1006. {
  1007. fUi.descriptor->cleanup(fUi.handle);
  1008. fUi.handle = nullptr;
  1009. fUi.widget = nullptr;
  1010. if (kData->gui != nullptr)
  1011. {
  1012. kData->guiGeometry = kData->gui->saveGeometry();
  1013. kData->gui->close();
  1014. delete kData->gui;
  1015. kData->gui = nullptr;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. void idleGui() override
  1021. {
  1022. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  1023. {
  1024. if (fUi.type == PLUGIN_UI_EXTERNAL && fUi.widget != nullptr)
  1025. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  1026. if (fExt.uiidle != nullptr)
  1027. {
  1028. if (fExt.uiidle->idle(fUi.handle) != 0)
  1029. {
  1030. showGui(false);
  1031. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1032. }
  1033. }
  1034. }
  1035. CarlaPlugin::idleGui();
  1036. }
  1037. // -------------------------------------------------------------------
  1038. // Plugin state
  1039. void reload() override
  1040. {
  1041. carla_debug("Lv2Plugin::reload() - start");
  1042. CARLA_ASSERT(kData->engine != nullptr);
  1043. CARLA_ASSERT(fHandle != nullptr);
  1044. CARLA_ASSERT(fDescriptor != nullptr);
  1045. CARLA_ASSERT(fRdfDescriptor != nullptr);
  1046. if (kData->engine == nullptr)
  1047. return;
  1048. if (fHandle == nullptr)
  1049. return;
  1050. if (fDescriptor == nullptr)
  1051. return;
  1052. if (fRdfDescriptor == nullptr)
  1053. return;
  1054. const ProcessMode processMode(kData->engine->getProccessMode());
  1055. // Safely disable plugin for reload
  1056. const ScopedDisabler sd(this);
  1057. if (kData->active)
  1058. deactivate();
  1059. clearBuffers();
  1060. const float sampleRate(static_cast<float>(kData->engine->getSampleRate()));
  1061. const uint32_t portCount(static_cast<uint32_t>(fRdfDescriptor->PortCount));
  1062. uint32_t aIns, aOuts, cvIns, cvOuts, params, j;
  1063. aIns = aOuts = cvIns = cvOuts = params = 0;
  1064. NonRtList<uint32_t> evIns, evOuts;
  1065. bool forcedStereoIn, forcedStereoOut;
  1066. forcedStereoIn = forcedStereoOut = false;
  1067. bool needsCtrlIn, needsCtrlOut;
  1068. needsCtrlIn = needsCtrlOut = false;
  1069. for (uint32_t i=0; i < portCount; ++i)
  1070. {
  1071. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1072. if (LV2_IS_PORT_AUDIO(portTypes))
  1073. {
  1074. if (LV2_IS_PORT_INPUT(portTypes))
  1075. aIns += 1;
  1076. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1077. aOuts += 1;
  1078. }
  1079. else if (LV2_IS_PORT_CV(portTypes))
  1080. {
  1081. if (LV2_IS_PORT_INPUT(portTypes))
  1082. cvIns += 1;
  1083. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1084. cvOuts += 1;
  1085. }
  1086. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1087. {
  1088. if (LV2_IS_PORT_INPUT(portTypes))
  1089. evIns.append(CARLA_EVENT_DATA_ATOM);
  1090. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1091. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1092. }
  1093. else if (LV2_IS_PORT_EVENT(portTypes))
  1094. {
  1095. if (LV2_IS_PORT_INPUT(portTypes))
  1096. evIns.append(CARLA_EVENT_DATA_EVENT);
  1097. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1098. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1099. }
  1100. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1101. {
  1102. if (LV2_IS_PORT_INPUT(portTypes))
  1103. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1104. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1105. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1106. }
  1107. else if (LV2_IS_PORT_CONTROL(portTypes))
  1108. params += 1;
  1109. }
  1110. // check extensions
  1111. fExt.options = nullptr;
  1112. fExt.programs = nullptr;
  1113. fExt.state = nullptr;
  1114. fExt.worker = nullptr;
  1115. if (fDescriptor->extension_data != nullptr)
  1116. {
  1117. if (kData->extraHints & PLUGIN_HAS_EXTENSION_OPTIONS)
  1118. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  1119. if (kData->extraHints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1120. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  1121. if (kData->extraHints & PLUGIN_HAS_EXTENSION_STATE)
  1122. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  1123. if (kData->extraHints & PLUGIN_HAS_EXTENSION_WORKER)
  1124. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  1125. }
  1126. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1127. {
  1128. if (fHandle2 == nullptr)
  1129. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1130. if (aIns == 1)
  1131. {
  1132. aIns = 2;
  1133. forcedStereoIn = true;
  1134. }
  1135. if (aOuts == 1)
  1136. {
  1137. aOuts = 2;
  1138. forcedStereoOut = true;
  1139. }
  1140. }
  1141. if (aIns > 0)
  1142. {
  1143. kData->audioIn.createNew(aIns);
  1144. fAudioInBuffers = new float*[aIns];
  1145. for (uint32_t i=0; i < aIns; ++i)
  1146. fAudioInBuffers[i] = nullptr;
  1147. }
  1148. if (aOuts > 0)
  1149. {
  1150. kData->audioOut.createNew(aOuts);
  1151. fAudioOutBuffers = new float*[aOuts];
  1152. needsCtrlIn = true;
  1153. for (uint32_t i=0; i < aOuts; ++i)
  1154. fAudioOutBuffers[i] = nullptr;
  1155. }
  1156. if (params > 0)
  1157. {
  1158. kData->param.createNew(params);
  1159. fParamBuffers = new float[params];
  1160. for (uint32_t i=0; i < params; ++i)
  1161. fParamBuffers[i] = 0.0f;
  1162. }
  1163. if (evIns.count() > 0)
  1164. {
  1165. const size_t count(evIns.count());
  1166. fEventsIn.createNew(count);
  1167. for (j=0; j < count; ++j)
  1168. {
  1169. const uint32_t& type(evIns.getAt(j));
  1170. if (type == CARLA_EVENT_DATA_ATOM)
  1171. {
  1172. fEventsIn.data[j].type = CARLA_EVENT_DATA_ATOM;
  1173. fEventsIn.data[j].atom = lv2_atom_buffer_new(MAX_EVENT_BUFFER, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1174. }
  1175. else if (type == CARLA_EVENT_DATA_EVENT)
  1176. {
  1177. fEventsIn.data[j].type = CARLA_EVENT_DATA_EVENT;
  1178. fEventsIn.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1179. }
  1180. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1181. {
  1182. fEventsIn.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1183. fEventsIn.data[j].midi = new LV2_MIDI;
  1184. fEventsIn.data[j].midi->event_count = 0;
  1185. fEventsIn.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1186. fEventsIn.data[j].midi->size = 0;
  1187. fEventsIn.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1188. }
  1189. }
  1190. }
  1191. if (evOuts.count() > 0)
  1192. {
  1193. const size_t count(evOuts.count());
  1194. fEventsOut.createNew(count);
  1195. for (j=0; j < count; ++j)
  1196. {
  1197. const uint32_t& type(evOuts.getAt(j));
  1198. if (type == CARLA_EVENT_DATA_ATOM)
  1199. {
  1200. fEventsOut.data[j].type = CARLA_EVENT_DATA_ATOM;
  1201. fEventsOut.data[j].atom = lv2_atom_buffer_new(MAX_EVENT_BUFFER, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1202. }
  1203. else if (type == CARLA_EVENT_DATA_EVENT)
  1204. {
  1205. fEventsOut.data[j].type = CARLA_EVENT_DATA_EVENT;
  1206. fEventsOut.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1207. }
  1208. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1209. {
  1210. fEventsOut.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1211. fEventsOut.data[j].midi = new LV2_MIDI;
  1212. fEventsOut.data[j].midi->event_count = 0;
  1213. fEventsOut.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1214. fEventsOut.data[j].midi->size = 0;
  1215. fEventsOut.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1216. }
  1217. }
  1218. }
  1219. const uint portNameSize(kData->engine->maxPortNameSize());
  1220. CarlaString portName;
  1221. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1222. {
  1223. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1224. portName.clear();
  1225. 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))
  1226. {
  1227. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1228. {
  1229. portName = fName;
  1230. portName += ":";
  1231. }
  1232. portName += fRdfDescriptor->Ports[i].Name;
  1233. portName.truncate(portNameSize);
  1234. }
  1235. if (LV2_IS_PORT_AUDIO(portTypes))
  1236. {
  1237. if (LV2_IS_PORT_INPUT(portTypes))
  1238. {
  1239. j = iAudioIn++;
  1240. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1241. kData->audioIn.ports[j].rindex = i;
  1242. if (forcedStereoIn)
  1243. {
  1244. portName += "_2";
  1245. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1246. kData->audioIn.ports[1].rindex = i;
  1247. }
  1248. }
  1249. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1250. {
  1251. j = iAudioOut++;
  1252. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1253. kData->audioOut.ports[j].rindex = i;
  1254. if (forcedStereoOut)
  1255. {
  1256. portName += "_2";
  1257. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1258. kData->audioOut.ports[1].rindex = i;
  1259. }
  1260. }
  1261. else
  1262. carla_stderr("WARNING - Got a broken Port (Audio, but not input or output)");
  1263. }
  1264. else if (LV2_IS_PORT_CV(portTypes))
  1265. {
  1266. if (LV2_IS_PORT_INPUT(portTypes))
  1267. {
  1268. carla_stderr("WARNING - CV Ports are not supported yet");
  1269. }
  1270. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1271. {
  1272. carla_stderr("WARNING - CV Ports are not supported yet");
  1273. }
  1274. else
  1275. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1276. fDescriptor->connect_port(fHandle, i, nullptr);
  1277. if (fHandle2 != nullptr)
  1278. fDescriptor->connect_port(fHandle2, i, nullptr);
  1279. }
  1280. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1281. {
  1282. if (LV2_IS_PORT_INPUT(portTypes))
  1283. {
  1284. j = iEvIn++;
  1285. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1286. if (fHandle2 != nullptr)
  1287. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1288. fEventsIn.data[j].rindex = i;
  1289. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1290. {
  1291. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1292. if (evIns.count() == 1)
  1293. {
  1294. needsCtrlIn = true;
  1295. fEventsIn.ctrl = &fEventsIn.data[j];
  1296. fEventsIn.ctrlIndex = j;
  1297. }
  1298. else
  1299. {
  1300. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1301. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1302. {
  1303. fEventsIn.ctrl = &fEventsIn.data[j];
  1304. fEventsIn.ctrlIndex = j;
  1305. }
  1306. }
  1307. }
  1308. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1309. {
  1310. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1311. }
  1312. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1313. {
  1314. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1315. }
  1316. }
  1317. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1318. {
  1319. j = iEvOut++;
  1320. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1321. if (fHandle2 != nullptr)
  1322. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1323. fEventsOut.data[j].rindex = i;
  1324. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1325. {
  1326. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1327. if (evOuts.count() == 1)
  1328. {
  1329. needsCtrlOut = true;
  1330. fEventsOut.ctrl = &fEventsOut.data[j];
  1331. fEventsOut.ctrlIndex = j;
  1332. }
  1333. else
  1334. {
  1335. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1336. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1337. {
  1338. fEventsOut.ctrl = &fEventsOut.data[j];
  1339. fEventsOut.ctrlIndex = j;
  1340. }
  1341. }
  1342. }
  1343. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1344. {
  1345. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1346. }
  1347. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1348. {
  1349. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1350. }
  1351. }
  1352. else
  1353. carla_stderr("WARNING - Got a broken Port (Atom Sequence, but not input or output)");
  1354. }
  1355. else if (LV2_IS_PORT_EVENT(portTypes))
  1356. {
  1357. if (LV2_IS_PORT_INPUT(portTypes))
  1358. {
  1359. j = iEvIn++;
  1360. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1361. if (fHandle2 != nullptr)
  1362. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1363. fEventsIn.data[j].rindex = i;
  1364. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1365. {
  1366. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1367. if (evIns.count() == 1)
  1368. {
  1369. needsCtrlIn = true;
  1370. fEventsIn.ctrl = &fEventsIn.data[j];
  1371. fEventsIn.ctrlIndex = j;
  1372. }
  1373. else
  1374. {
  1375. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1376. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1377. {
  1378. fEventsIn.ctrl = &fEventsIn.data[j];
  1379. fEventsIn.ctrlIndex = j;
  1380. }
  1381. }
  1382. }
  1383. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1384. {
  1385. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1386. }
  1387. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1388. {
  1389. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1390. }
  1391. }
  1392. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1393. {
  1394. j = iEvOut++;
  1395. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1396. if (fHandle2 != nullptr)
  1397. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1398. fEventsOut.data[j].rindex = i;
  1399. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1400. {
  1401. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1402. if (evOuts.count() == 1)
  1403. {
  1404. needsCtrlOut = true;
  1405. fEventsOut.ctrl = &fEventsOut.data[j];
  1406. fEventsOut.ctrlIndex = j;
  1407. }
  1408. else
  1409. {
  1410. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1411. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1412. {
  1413. fEventsOut.ctrl = &fEventsOut.data[j];
  1414. fEventsOut.ctrlIndex = j;
  1415. }
  1416. }
  1417. }
  1418. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1419. {
  1420. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1421. }
  1422. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1423. {
  1424. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1425. }
  1426. }
  1427. else
  1428. carla_stderr("WARNING - Got a broken Port (Event, but not input or output)");
  1429. }
  1430. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1431. {
  1432. if (LV2_IS_PORT_INPUT(portTypes))
  1433. {
  1434. j = iEvIn++;
  1435. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].midi);
  1436. if (fHandle2 != nullptr)
  1437. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].midi);
  1438. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1439. fEventsIn.data[j].rindex = i;
  1440. if (evIns.count() == 1)
  1441. {
  1442. needsCtrlIn = true;
  1443. fEventsIn.ctrl = &fEventsIn.data[j];
  1444. fEventsIn.ctrlIndex = j;
  1445. }
  1446. else
  1447. {
  1448. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1449. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1450. {
  1451. fEventsIn.ctrl = &fEventsIn.data[j];
  1452. fEventsIn.ctrlIndex = j;
  1453. }
  1454. }
  1455. }
  1456. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1457. {
  1458. j = iEvOut++;
  1459. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].midi);
  1460. if (fHandle2 != nullptr)
  1461. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].midi);
  1462. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1463. fEventsOut.data[j].rindex = i;
  1464. if (evOuts.count() == 1)
  1465. {
  1466. needsCtrlOut = true;
  1467. fEventsOut.ctrl = &fEventsOut.data[j];
  1468. fEventsOut.ctrlIndex = j;
  1469. }
  1470. else
  1471. {
  1472. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1473. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1474. {
  1475. fEventsOut.ctrl = &fEventsOut.data[j];
  1476. fEventsOut.ctrlIndex = j;
  1477. }
  1478. }
  1479. }
  1480. else
  1481. carla_stderr("WARNING - Got a broken Port (Midi, but not input or output)");
  1482. }
  1483. else if (LV2_IS_PORT_CONTROL(portTypes))
  1484. {
  1485. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1486. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1487. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1488. j = iCtrl++;
  1489. kData->param.data[j].index = j;
  1490. kData->param.data[j].rindex = i;
  1491. kData->param.data[j].hints = 0x0;
  1492. kData->param.data[j].midiChannel = 0;
  1493. kData->param.data[j].midiCC = -1;
  1494. float min, max, def, step, stepSmall, stepLarge;
  1495. // min value
  1496. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1497. min = portPoints.Minimum;
  1498. else
  1499. min = 0.0f;
  1500. // max value
  1501. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1502. max = portPoints.Maximum;
  1503. else
  1504. max = 1.0f;
  1505. if (min > max)
  1506. max = min;
  1507. else if (max < min)
  1508. min = max;
  1509. // stupid hack for ir.lv2 (broken plugin)
  1510. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1511. {
  1512. min = 0.0f;
  1513. max = (float)0xffffff;
  1514. }
  1515. if (max - min == 0.0f)
  1516. {
  1517. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1518. max = min + 0.1f;
  1519. }
  1520. // default value
  1521. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1522. {
  1523. def = portPoints.Default;
  1524. }
  1525. else
  1526. {
  1527. // no default value
  1528. if (min < 0.0f && max > 0.0f)
  1529. def = 0.0f;
  1530. else
  1531. def = min;
  1532. }
  1533. if (def < min)
  1534. def = min;
  1535. else if (def > max)
  1536. def = max;
  1537. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1538. {
  1539. min *= sampleRate;
  1540. max *= sampleRate;
  1541. def *= sampleRate;
  1542. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1543. }
  1544. if (LV2_IS_PORT_TOGGLED(portProps))
  1545. {
  1546. step = max - min;
  1547. stepSmall = step;
  1548. stepLarge = step;
  1549. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1550. }
  1551. else if (LV2_IS_PORT_INTEGER(portProps))
  1552. {
  1553. step = 1.0f;
  1554. stepSmall = 1.0f;
  1555. stepLarge = 10.0f;
  1556. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1557. }
  1558. else
  1559. {
  1560. float range = max - min;
  1561. step = range/100.0f;
  1562. stepSmall = range/1000.0f;
  1563. stepLarge = range/10.0f;
  1564. }
  1565. if (LV2_IS_PORT_INPUT(portTypes))
  1566. {
  1567. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1568. {
  1569. carla_stderr("Plugin has latency input port, this should not happen!");
  1570. }
  1571. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1572. {
  1573. def = sampleRate;
  1574. step = 1.0f;
  1575. stepSmall = 1.0f;
  1576. stepLarge = 1.0f;
  1577. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1578. kData->param.data[j].hints = 0x0;
  1579. }
  1580. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1581. {
  1582. kData->param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  1583. }
  1584. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1585. {
  1586. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1587. }
  1588. else
  1589. {
  1590. kData->param.data[j].type = PARAMETER_INPUT;
  1591. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1592. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1593. needsCtrlIn = true;
  1594. }
  1595. // MIDI CC value
  1596. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1597. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1598. {
  1599. if (! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1600. kData->param.data[j].midiCC = portMidiMap.Number;
  1601. }
  1602. }
  1603. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1604. {
  1605. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1606. {
  1607. min = 0.0f;
  1608. max = sampleRate;
  1609. def = 0.0f;
  1610. step = 1.0f;
  1611. stepSmall = 1.0f;
  1612. stepLarge = 1.0f;
  1613. kData->param.data[j].type = PARAMETER_LATENCY;
  1614. kData->param.data[j].hints = 0;
  1615. }
  1616. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1617. {
  1618. def = sampleRate;
  1619. step = 1.0f;
  1620. stepSmall = 1.0f;
  1621. stepLarge = 1.0f;
  1622. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1623. kData->param.data[j].hints = 0;
  1624. }
  1625. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1626. {
  1627. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1628. }
  1629. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1630. {
  1631. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1632. }
  1633. else
  1634. {
  1635. kData->param.data[j].type = PARAMETER_OUTPUT;
  1636. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1637. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1638. needsCtrlOut = true;
  1639. }
  1640. }
  1641. else
  1642. {
  1643. kData->param.data[j].type = PARAMETER_UNKNOWN;
  1644. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1645. }
  1646. // extra parameter hints
  1647. if (LV2_IS_PORT_ENUMERATION(portProps))
  1648. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1649. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1650. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1651. if (LV2_IS_PORT_TRIGGER(portProps))
  1652. kData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1653. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1654. kData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1655. // check if parameter is not enabled or automable
  1656. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1657. kData->param.data[j].hints &= ~PARAMETER_IS_ENABLED;
  1658. if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1659. kData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1660. kData->param.ranges[j].min = min;
  1661. kData->param.ranges[j].max = max;
  1662. kData->param.ranges[j].def = def;
  1663. kData->param.ranges[j].step = step;
  1664. kData->param.ranges[j].stepSmall = stepSmall;
  1665. kData->param.ranges[j].stepLarge = stepLarge;
  1666. if (kData->param.data[j].type != PARAMETER_LV2_FREEWHEEL)
  1667. {
  1668. // Start parameters in their default values
  1669. fParamBuffers[j] = def;
  1670. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1671. if (fHandle2 != nullptr)
  1672. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1673. }
  1674. else
  1675. {
  1676. // freewheel param
  1677. fDescriptor->connect_port(fHandle, i, &fParamFreewheel);
  1678. if (fHandle2 != nullptr)
  1679. fDescriptor->connect_port(fHandle2, i, &fParamFreewheel);
  1680. }
  1681. }
  1682. else
  1683. {
  1684. // Port Type not supported, but it's optional anyway
  1685. fDescriptor->connect_port(fHandle, i, nullptr);
  1686. if (fHandle2 != nullptr)
  1687. fDescriptor->connect_port(fHandle2, i, nullptr);
  1688. }
  1689. }
  1690. if (needsCtrlIn)
  1691. {
  1692. portName.clear();
  1693. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1694. {
  1695. portName = fName;
  1696. portName += ":";
  1697. }
  1698. portName += "events-in";
  1699. portName.truncate(portNameSize);
  1700. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1701. }
  1702. if (needsCtrlOut)
  1703. {
  1704. portName.clear();
  1705. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1706. {
  1707. portName = fName;
  1708. portName += ":";
  1709. }
  1710. portName += "events-out";
  1711. portName.truncate(portNameSize);
  1712. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1713. }
  1714. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1715. fEventsIn.ctrl->port = kData->event.portIn;
  1716. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1717. fEventsOut.ctrl->port = kData->event.portOut;
  1718. if (forcedStereoIn || forcedStereoOut)
  1719. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1720. else
  1721. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  1722. // plugin hints
  1723. fHints = 0x0;
  1724. if (isRealtimeSafe())
  1725. fHints |= PLUGIN_IS_RTSAFE;
  1726. if (fUi.type != PLUGIN_UI_NULL)
  1727. {
  1728. fHints |= PLUGIN_HAS_GUI;
  1729. if (fUi.type == PLUGIN_UI_QT || fUi.type == PLUGIN_UI_PARENT)
  1730. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  1731. }
  1732. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1733. fHints |= PLUGIN_IS_SYNTH;
  1734. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1735. fHints |= PLUGIN_CAN_DRYWET;
  1736. if (aOuts > 0)
  1737. fHints |= PLUGIN_CAN_VOLUME;
  1738. if (aOuts >= 2 && aOuts % 2 == 0)
  1739. fHints |= PLUGIN_CAN_BALANCE;
  1740. // extra plugin hints
  1741. kData->extraHints &= ~PLUGIN_HINT_CAN_RUN_RACK;
  1742. if (fExt.state != nullptr || fExt.worker != nullptr)
  1743. {
  1744. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1745. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1746. }
  1747. else
  1748. {
  1749. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1750. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1751. }
  1752. bufferSizeChanged(kData->engine->getBufferSize());
  1753. reloadPrograms(true);
  1754. if (kData->active)
  1755. activate();
  1756. evIns.clear();
  1757. evOuts.clear();
  1758. carla_debug("Lv2Plugin::reload() - end");
  1759. }
  1760. void reloadPrograms(const bool init) override
  1761. {
  1762. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  1763. uint32_t i, oldCount = kData->midiprog.count;
  1764. const int32_t current = kData->midiprog.current;
  1765. // special LV2 programs handling
  1766. if (init)
  1767. {
  1768. kData->prog.clear();
  1769. uint32_t count = fRdfDescriptor->PresetCount;
  1770. if (count > 0)
  1771. {
  1772. kData->prog.createNew(count);
  1773. for (i=0; i < count; ++i)
  1774. kData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1775. }
  1776. }
  1777. // Delete old programs
  1778. kData->midiprog.clear();
  1779. // Query new programs
  1780. uint32_t count = 0;
  1781. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1782. {
  1783. while (fExt.programs->get_program(fHandle, count))
  1784. count++;
  1785. }
  1786. if (count > 0)
  1787. {
  1788. kData->midiprog.createNew(count);
  1789. // Update data
  1790. for (i=0; i < count; ++i)
  1791. {
  1792. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1793. CARLA_ASSERT(pdesc != nullptr);
  1794. CARLA_ASSERT(pdesc->name != nullptr);
  1795. kData->midiprog.data[i].bank = pdesc->bank;
  1796. kData->midiprog.data[i].program = pdesc->program;
  1797. kData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1798. }
  1799. }
  1800. #ifndef BUILD_BRIDGE
  1801. // Update OSC Names
  1802. if (kData->engine->isOscControlRegistered())
  1803. {
  1804. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  1805. for (i=0; i < count; ++i)
  1806. 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);
  1807. }
  1808. #endif
  1809. if (init)
  1810. {
  1811. if (count > 0)
  1812. setMidiProgram(0, false, false, false);
  1813. }
  1814. else
  1815. {
  1816. // Check if current program is invalid
  1817. bool programChanged = false;
  1818. if (count == oldCount+1)
  1819. {
  1820. // one midi program added, probably created by user
  1821. kData->midiprog.current = oldCount;
  1822. programChanged = true;
  1823. }
  1824. else if (current < 0 && count > 0)
  1825. {
  1826. // programs exist now, but not before
  1827. kData->midiprog.current = 0;
  1828. programChanged = true;
  1829. }
  1830. else if (current >= 0 && count == 0)
  1831. {
  1832. // programs existed before, but not anymore
  1833. kData->midiprog.current = -1;
  1834. programChanged = true;
  1835. }
  1836. else if (current >= static_cast<int32_t>(count))
  1837. {
  1838. // current midi program > count
  1839. kData->midiprog.current = 0;
  1840. programChanged = true;
  1841. }
  1842. else
  1843. {
  1844. // no change
  1845. kData->midiprog.current = current;
  1846. }
  1847. if (programChanged)
  1848. setMidiProgram(kData->midiprog.current, true, true, true);
  1849. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  1850. }
  1851. }
  1852. // -------------------------------------------------------------------
  1853. // Plugin processing
  1854. void activate() override
  1855. {
  1856. CARLA_ASSERT(fDescriptor != nullptr);
  1857. CARLA_ASSERT(fHandle != nullptr);
  1858. if (fDescriptor->activate != nullptr)
  1859. {
  1860. fDescriptor->activate(fHandle);
  1861. if (fHandle2 != nullptr)
  1862. fDescriptor->activate(fHandle2);
  1863. }
  1864. fFirstActive = true;
  1865. }
  1866. void deactivate() override
  1867. {
  1868. CARLA_ASSERT(fDescriptor != nullptr);
  1869. CARLA_ASSERT(fHandle != nullptr);
  1870. if (fDescriptor->deactivate != nullptr)
  1871. {
  1872. fDescriptor->deactivate(fHandle);
  1873. if (fHandle2 != nullptr)
  1874. fDescriptor->deactivate(fHandle2);
  1875. }
  1876. }
  1877. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1878. {
  1879. uint32_t i, k;
  1880. // --------------------------------------------------------------------------------------------------------
  1881. // Check if active
  1882. if (! kData->active)
  1883. {
  1884. // disable any output sound
  1885. for (i=0; i < kData->audioOut.count; ++i)
  1886. carla_zeroFloat(outBuffer[i], frames);
  1887. return;
  1888. }
  1889. // handle events from different APIs
  1890. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1891. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1892. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1893. for (i=0; i < fEventsIn.count; ++i)
  1894. {
  1895. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1896. {
  1897. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1898. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1899. }
  1900. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1901. {
  1902. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1903. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1904. }
  1905. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1906. {
  1907. fEventsIn.data[i].midi->event_count = 0;
  1908. fEventsIn.data[i].midi->size = 0;
  1909. evInMidiStates[i].midi = fEventsIn.data[i].midi;
  1910. evInMidiStates[i].frame_count = frames;
  1911. evInMidiStates[i].position = 0;
  1912. }
  1913. }
  1914. for (i=0; i < fEventsOut.count; ++i)
  1915. {
  1916. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1917. {
  1918. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1919. }
  1920. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1921. {
  1922. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1923. }
  1924. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1925. {
  1926. // not needed
  1927. }
  1928. }
  1929. CARLA_PROCESS_CONTINUE_CHECK;
  1930. // --------------------------------------------------------------------------------------------------------
  1931. // Check if needs reset
  1932. if (kData->needsReset)
  1933. {
  1934. uint8_t midiData[3] = { 0 };
  1935. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1936. {
  1937. k = fEventsIn.ctrlIndex;
  1938. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1939. {
  1940. for (i=0; i < MAX_MIDI_CHANNELS; ++i)
  1941. {
  1942. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  1943. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1944. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1945. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1946. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1947. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1948. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1949. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  1950. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  1951. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1952. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1953. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1954. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1955. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1956. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1957. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  1958. }
  1959. }
  1960. else if (kData->ctrlChannel >= 0 && kData->ctrlChannel < MAX_MIDI_CHANNELS)
  1961. {
  1962. for (k=0; k < MAX_MIDI_NOTE; ++k)
  1963. {
  1964. midiData[0] = MIDI_STATUS_NOTE_OFF + kData->ctrlChannel;
  1965. midiData[1] = k;
  1966. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1967. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1968. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1969. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1970. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1971. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  1972. }
  1973. }
  1974. }
  1975. if (kData->latency > 0)
  1976. {
  1977. //for (i=0; i < kData->audioIn.count; ++i)
  1978. // carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  1979. }
  1980. kData->needsReset = false;
  1981. }
  1982. CARLA_PROCESS_CONTINUE_CHECK;
  1983. // --------------------------------------------------------------------------------------------------------
  1984. // Special Parameters
  1985. {
  1986. // TODO - there should be a callback for this
  1987. fParamFreewheel = kData->engine->isOffline() ? 1.0f : 0.0f;
  1988. }
  1989. // --------------------------------------------------------------------------------------------------------
  1990. // TimeInfo
  1991. {
  1992. bool doPostRt;
  1993. int32_t rindex;
  1994. const EngineTimeInfo& timeInfo(kData->engine->getTimeInfo());
  1995. if (fFirstActive || fLastTimeInfo != timeInfo)
  1996. {
  1997. // update input ports
  1998. for (k=0; k < kData->param.count; ++k)
  1999. {
  2000. if (kData->param.data[k].type != PARAMETER_LV2_TIME)
  2001. continue;
  2002. doPostRt = false;
  2003. rindex = kData->param.data[k].rindex;
  2004. CARLA_ASSERT(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2005. switch (fRdfDescriptor->Ports[rindex].Designation)
  2006. {
  2007. // Non-BBT
  2008. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2009. if (fLastTimeInfo.playing != timeInfo.playing)
  2010. {
  2011. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2012. doPostRt = true;
  2013. }
  2014. break;
  2015. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2016. if (fLastTimeInfo.frame != timeInfo.frame)
  2017. {
  2018. fParamBuffers[k] = timeInfo.frame;
  2019. doPostRt = true;
  2020. }
  2021. break;
  2022. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2023. break;
  2024. // BBT
  2025. case LV2_PORT_DESIGNATION_TIME_BAR:
  2026. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2027. {
  2028. fParamBuffers[k] = timeInfo.bbt.bar - 1;
  2029. doPostRt = true;
  2030. }
  2031. break;
  2032. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2033. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2034. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2035. {
  2036. fParamBuffers[k] = timeInfo.bbt.beat - 1 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat);
  2037. doPostRt = true;
  2038. }
  2039. break;
  2040. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2041. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2042. {
  2043. fParamBuffers[k] = timeInfo.bbt.beat - 1;
  2044. doPostRt = true;
  2045. }
  2046. break;
  2047. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2048. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2049. {
  2050. fParamBuffers[k] = timeInfo.bbt.beatType;
  2051. doPostRt = true;
  2052. }
  2053. break;
  2054. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2055. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2056. {
  2057. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2058. doPostRt = true;
  2059. }
  2060. break;
  2061. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2062. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2063. {
  2064. fParamBuffers[k] = timeInfo.bbt.beatsPerMinute;
  2065. doPostRt = true;
  2066. }
  2067. break;
  2068. }
  2069. if (doPostRt)
  2070. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2071. }
  2072. for (i = 0; i < fEventsIn.count; ++i)
  2073. {
  2074. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2075. continue;
  2076. uint8_t timeInfoBuf[256] = { 0 };
  2077. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2078. LV2_Atom_Forge_Frame forgeFrame;
  2079. lv2_atom_forge_blank(&fAtomForge, &forgeFrame, 1, CARLA_URI_MAP_ID_TIME_POSITION);
  2080. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED, 0);
  2081. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2082. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME, 0);
  2083. lv2_atom_forge_long(&fAtomForge, timeInfo.frame);
  2084. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  2085. {
  2086. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR, 0);
  2087. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2088. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT, 0);
  2089. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beat - 1 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2090. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT, 0);
  2091. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.beat -1);
  2092. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT, 0);
  2093. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatType);
  2094. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR, 0);
  2095. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2096. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE, 0);
  2097. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerMinute);
  2098. }
  2099. LV2_Atom* atom = (LV2_Atom*)timeInfoBuf;
  2100. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2NV_ATOM_BODY_CONST(atom));
  2101. CARLA_ASSERT(atom->size < 256);
  2102. }
  2103. kData->postRtEvents.trySplice();
  2104. std::memcpy(&fLastTimeInfo, &timeInfo, sizeof(EngineTimeInfo));
  2105. }
  2106. }
  2107. // --------------------------------------------------------------------------------------------------------
  2108. // Event Input and Processing
  2109. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port != nullptr)
  2110. {
  2111. // ----------------------------------------------------------------------------------------------------
  2112. // Message Input
  2113. if (fAtomQueueIn.tryLock())
  2114. {
  2115. if (! fAtomQueueIn.isEmpty())
  2116. {
  2117. uint32_t portIndex;
  2118. const LV2_Atom* atom;
  2119. k = fEventsIn.ctrlIndex;
  2120. while (lv2_atom_buffer_is_valid(&evInAtomIters[k]) && fAtomQueueIn.get(&portIndex, &atom))
  2121. {
  2122. //if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2123. //{
  2124. // const LV2_Atom_Worker* const atomWorker = (const LV2_Atom_Worker*)atom;
  2125. // fExt.worker->work_response(fHandle, atomWorker->body.size, atomWorker->body.data);
  2126. // continue;
  2127. //}
  2128. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, atom->type, atom->size, LV2NV_ATOM_BODY_CONST(atom));
  2129. }
  2130. }
  2131. fAtomQueueIn.unlock();
  2132. }
  2133. // ----------------------------------------------------------------------------------------------------
  2134. // MIDI Input (External)
  2135. if (kData->extNotes.mutex.tryLock())
  2136. {
  2137. k = fEventsIn.ctrlIndex;
  2138. while (! kData->extNotes.data.isEmpty())
  2139. {
  2140. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  2141. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2142. uint8_t midiEvent[3];
  2143. midiEvent[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  2144. midiEvent[0] += note.channel;
  2145. midiEvent[1] = note.note;
  2146. midiEvent[2] = note.velo;
  2147. if (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI)
  2148. {
  2149. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2150. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2151. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2152. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2153. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2154. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  2155. }
  2156. }
  2157. kData->extNotes.mutex.unlock();
  2158. } // End of MIDI Input (External)
  2159. // ----------------------------------------------------------------------------------------------------
  2160. // Event Input (System)
  2161. bool allNotesOffSent = false;
  2162. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  2163. uint32_t time, nEvents = fEventsIn.ctrl->port->getEventCount();
  2164. uint32_t startTime = 0;
  2165. uint32_t timeOffset = 0;
  2166. uint32_t nextBankId = 0;
  2167. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  2168. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  2169. for (i=0; i < nEvents; ++i)
  2170. {
  2171. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2172. time = event.time;
  2173. if (time >= frames)
  2174. continue;
  2175. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  2176. if (time > timeOffset && sampleAccurate)
  2177. {
  2178. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  2179. {
  2180. startTime = 0;
  2181. timeOffset = time;
  2182. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  2183. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  2184. else
  2185. nextBankId = 0;
  2186. // reset iters
  2187. k = fEventsIn.ctrlIndex;
  2188. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2189. {
  2190. lv2_atom_buffer_reset(fEventsIn.data[k].atom, true);
  2191. lv2_atom_buffer_begin(&evInAtomIters[k], fEventsIn.data[k].atom);
  2192. }
  2193. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2194. {
  2195. lv2_event_buffer_reset(fEventsIn.data[k].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[k].event->data);
  2196. lv2_event_begin(&evInEventIters[k], fEventsIn.data[k].event);
  2197. }
  2198. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2199. {
  2200. fEventsIn.data[k].midi->event_count = 0;
  2201. fEventsIn.data[k].midi->size = 0;
  2202. evInMidiStates[k].position = time;
  2203. }
  2204. }
  2205. else
  2206. startTime += timeOffset;
  2207. }
  2208. // Control change
  2209. switch (event.type)
  2210. {
  2211. case kEngineEventTypeNull:
  2212. break;
  2213. case kEngineEventTypeControl:
  2214. {
  2215. const EngineControlEvent& ctrlEvent = event.ctrl;
  2216. switch (ctrlEvent.type)
  2217. {
  2218. case kEngineControlEventTypeNull:
  2219. break;
  2220. case kEngineControlEventTypeParameter:
  2221. {
  2222. #ifndef BUILD_BRIDGE
  2223. // Control backend stuff
  2224. if (event.channel == kData->ctrlChannel)
  2225. {
  2226. float value;
  2227. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  2228. {
  2229. value = ctrlEvent.value;
  2230. setDryWet(value, false, false);
  2231. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2232. continue;
  2233. }
  2234. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  2235. {
  2236. value = ctrlEvent.value*127.0f/100.0f;
  2237. setVolume(value, false, false);
  2238. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2239. continue;
  2240. }
  2241. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  2242. {
  2243. float left, right;
  2244. value = ctrlEvent.value/0.5f - 1.0f;
  2245. if (value < 0.0f)
  2246. {
  2247. left = -1.0f;
  2248. right = (value*2.0f)+1.0f;
  2249. }
  2250. else if (value > 0.0f)
  2251. {
  2252. left = (value*2.0f)-1.0f;
  2253. right = 1.0f;
  2254. }
  2255. else
  2256. {
  2257. left = -1.0f;
  2258. right = 1.0f;
  2259. }
  2260. setBalanceLeft(left, false, false);
  2261. setBalanceRight(right, false, false);
  2262. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2263. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2264. continue;
  2265. }
  2266. }
  2267. #endif
  2268. // Control plugin parameters
  2269. for (k=0; k < kData->param.count; ++k)
  2270. {
  2271. if (kData->param.data[k].midiChannel != event.channel)
  2272. continue;
  2273. if (kData->param.data[k].midiCC != ctrlEvent.param)
  2274. continue;
  2275. if (kData->param.data[k].type != PARAMETER_INPUT)
  2276. continue;
  2277. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2278. continue;
  2279. float value;
  2280. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2281. {
  2282. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  2283. }
  2284. else
  2285. {
  2286. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  2287. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2288. value = std::rint(value);
  2289. }
  2290. setParameterValue(k, value, false, false, false);
  2291. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2292. }
  2293. break;
  2294. }
  2295. case kEngineControlEventTypeMidiBank:
  2296. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2297. nextBankId = ctrlEvent.param;
  2298. break;
  2299. case kEngineControlEventTypeMidiProgram:
  2300. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2301. {
  2302. const uint32_t nextProgramId = ctrlEvent.param;
  2303. for (k=0; k < kData->midiprog.count; ++k)
  2304. {
  2305. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  2306. {
  2307. setMidiProgram(k, false, false, false);
  2308. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  2309. break;
  2310. }
  2311. }
  2312. }
  2313. break;
  2314. case kEngineControlEventTypeAllSoundOff:
  2315. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2316. {
  2317. uint8_t midiData[3];
  2318. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2319. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2320. midiData[2] = 0;
  2321. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2322. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2323. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2324. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2325. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2326. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2327. }
  2328. break;
  2329. case kEngineControlEventTypeAllNotesOff:
  2330. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2331. {
  2332. if (event.channel == kData->ctrlChannel && ! allNotesOffSent)
  2333. {
  2334. allNotesOffSent = true;
  2335. sendMidiAllNotesOffToCallback();
  2336. }
  2337. uint8_t midiData[3];
  2338. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2339. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2340. midiData[2] = 0;
  2341. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2342. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2343. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2344. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2345. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2346. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2347. }
  2348. break;
  2349. }
  2350. break;
  2351. }
  2352. case kEngineEventTypeMidi:
  2353. {
  2354. const EngineMidiEvent& midiEvent(event.midi);
  2355. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  2356. uint8_t channel = event.channel;
  2357. uint32_t mtime = sampleAccurate ? startTime : time;
  2358. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2359. continue;
  2360. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2361. continue;
  2362. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2363. continue;
  2364. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2365. continue;
  2366. // Fix bad note-off
  2367. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  2368. status -= 0x10;
  2369. k = fEventsIn.ctrlIndex;
  2370. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2371. lv2_atom_buffer_write(&evInAtomIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2372. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2373. lv2_event_write(&evInEventIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2374. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2375. lv2midi_put_event(&evInMidiStates[k], mtime, midiEvent.size, midiEvent.data);
  2376. if (status == MIDI_STATUS_NOTE_ON)
  2377. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2378. else if (status == MIDI_STATUS_NOTE_OFF)
  2379. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2380. break;
  2381. }
  2382. }
  2383. }
  2384. kData->postRtEvents.trySplice();
  2385. if (frames > timeOffset)
  2386. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2387. } // End of Event Input and Processing
  2388. // --------------------------------------------------------------------------------------------------------
  2389. // Plugin processing (no events)
  2390. else
  2391. {
  2392. processSingle(inBuffer, outBuffer, frames, 0);
  2393. } // End of Plugin processing (no events)
  2394. CARLA_PROCESS_CONTINUE_CHECK;
  2395. // --------------------------------------------------------------------------------------------------------
  2396. // MIDI Output
  2397. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port != nullptr)
  2398. {
  2399. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2400. {
  2401. const LV2_Atom_Event* ev;
  2402. LV2_Atom_Buffer_Iterator iter;
  2403. uint8_t* data;
  2404. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2405. while (true)
  2406. {
  2407. data = nullptr;
  2408. ev = lv2_atom_buffer_get(&iter, &data);
  2409. if (ev == nullptr || data == nullptr)
  2410. break;
  2411. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2412. fEventsOut.ctrl->port->writeMidiEvent(ev->time.frames, data, ev->body.size);
  2413. lv2_atom_buffer_increment(&iter);
  2414. }
  2415. }
  2416. else if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2417. {
  2418. const LV2_Event* ev;
  2419. LV2_Event_Iterator iter;
  2420. uint8_t* data;
  2421. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2422. while (true)
  2423. {
  2424. data = nullptr;
  2425. ev = lv2_event_get(&iter, &data);
  2426. if (ev == nullptr || data == nullptr)
  2427. break;
  2428. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2429. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, data, ev->size);
  2430. lv2_event_increment(&iter);
  2431. }
  2432. }
  2433. else if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2434. {
  2435. LV2_MIDIState state = { fEventsOut.ctrl->midi, frames, 0 };
  2436. uint32_t eventSize;
  2437. double eventTime;
  2438. unsigned char* eventData;
  2439. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2440. {
  2441. if (eventData == nullptr || eventSize == 0)
  2442. break;
  2443. fEventsOut.ctrl->port->writeMidiEvent(eventTime, eventData, eventSize);
  2444. lv2midi_step(&state);
  2445. }
  2446. }
  2447. }
  2448. // --------------------------------------------------------------------------------------------------------
  2449. // Control Output
  2450. if (kData->event.portOut != nullptr)
  2451. {
  2452. uint8_t channel;
  2453. uint16_t param;
  2454. float value;
  2455. for (k=0; k < kData->param.count; ++k)
  2456. {
  2457. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  2458. continue;
  2459. if (kData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS)
  2460. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  2461. if (kData->param.data[k].midiCC > 0)
  2462. {
  2463. channel = kData->param.data[k].midiChannel;
  2464. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  2465. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  2466. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2467. }
  2468. }
  2469. } // End of Control Output
  2470. CARLA_PROCESS_CONTINUE_CHECK;
  2471. #if 0
  2472. // --------------------------------------------------------------------------------------------------------
  2473. // Final work
  2474. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2475. {
  2476. fExt.worker->end_run(fHandle);
  2477. if (fHandle2 != nullptr)
  2478. fExt.worker->end_run(fHandle2);
  2479. }
  2480. #endif
  2481. fFirstActive = false;
  2482. // --------------------------------------------------------------------------------------------------------
  2483. }
  2484. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2485. {
  2486. CARLA_ASSERT(frames > 0);
  2487. if (frames == 0)
  2488. return false;
  2489. if (kData->audioIn.count > 0)
  2490. {
  2491. CARLA_ASSERT(inBuffer != nullptr);
  2492. if (inBuffer == nullptr)
  2493. return false;
  2494. }
  2495. if (kData->audioOut.count > 0)
  2496. {
  2497. CARLA_ASSERT(outBuffer != nullptr);
  2498. if (outBuffer == nullptr)
  2499. return false;
  2500. }
  2501. uint32_t i, k;
  2502. // --------------------------------------------------------------------------------------------------------
  2503. // Try lock, silence otherwise
  2504. if (kData->engine->isOffline())
  2505. {
  2506. kData->singleMutex.lock();
  2507. }
  2508. else if (! kData->singleMutex.tryLock())
  2509. {
  2510. for (i=0; i < kData->audioOut.count; ++i)
  2511. {
  2512. for (k=0; k < frames; ++k)
  2513. outBuffer[i][k+timeOffset] = 0.0f;
  2514. }
  2515. return false;
  2516. }
  2517. // --------------------------------------------------------------------------------------------------------
  2518. // Reset audio buffers
  2519. for (i=0; i < kData->audioIn.count; ++i)
  2520. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2521. for (i=0; i < kData->audioOut.count; ++i)
  2522. carla_zeroFloat(fAudioOutBuffers[i], frames);
  2523. // --------------------------------------------------------------------------------------------------------
  2524. // Run plugin
  2525. fDescriptor->run(fHandle, frames);
  2526. if (fHandle2 != nullptr)
  2527. fDescriptor->run(fHandle2, frames);
  2528. // --------------------------------------------------------------------------------------------------------
  2529. // Special Parameters
  2530. for (k=0; k < kData->param.count; ++k)
  2531. {
  2532. if (kData->param.data[k].type != PARAMETER_INPUT)
  2533. continue;
  2534. if (kData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2535. {
  2536. if (fParamBuffers[k] != kData->param.ranges[k].def)
  2537. {
  2538. fParamBuffers[k] = kData->param.ranges[k].def;
  2539. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2540. }
  2541. }
  2542. }
  2543. kData->postRtEvents.trySplice();
  2544. #ifndef BUILD_BRIDGE
  2545. // --------------------------------------------------------------------------------------------------------
  2546. // Post-processing (dry/wet, volume and balance)
  2547. {
  2548. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  2549. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  2550. bool isPair;
  2551. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2552. for (i=0; i < kData->audioOut.count; ++i)
  2553. {
  2554. // Dry/Wet
  2555. if (doDryWet)
  2556. {
  2557. for (k=0; k < frames; ++k)
  2558. {
  2559. // TODO
  2560. //if (k < kData->latency && kData->latency < frames)
  2561. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  2562. //else
  2563. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2564. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  2565. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  2566. }
  2567. }
  2568. // Balance
  2569. if (doBalance)
  2570. {
  2571. isPair = (i % 2 == 0);
  2572. if (isPair)
  2573. {
  2574. CARLA_ASSERT(i+1 < kData->audioOut.count);
  2575. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  2576. }
  2577. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  2578. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  2579. for (k=0; k < frames; ++k)
  2580. {
  2581. if (isPair)
  2582. {
  2583. // left
  2584. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2585. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2586. }
  2587. else
  2588. {
  2589. // right
  2590. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2591. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2592. }
  2593. }
  2594. }
  2595. // Volume (and buffer copy)
  2596. {
  2597. for (k=0; k < frames; ++k)
  2598. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  2599. }
  2600. }
  2601. # if 0
  2602. // Latency, save values for next callback, TODO
  2603. if (kData->latency > 0 && kData->latency < frames)
  2604. {
  2605. for (i=0; i < kData->audioIn.count; ++i)
  2606. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  2607. }
  2608. # endif
  2609. } // End of Post-processing
  2610. #else
  2611. for (i=0; i < kData->audioOut.count; ++i)
  2612. {
  2613. for (k=0; k < frames; ++k)
  2614. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2615. }
  2616. #endif
  2617. // --------------------------------------------------------------------------------------------------------
  2618. kData->singleMutex.unlock();
  2619. return true;
  2620. }
  2621. void bufferSizeChanged(const uint32_t newBufferSize) override
  2622. {
  2623. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2624. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2625. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2626. {
  2627. if (fAudioInBuffers[i] != nullptr)
  2628. delete[] fAudioInBuffers[i];
  2629. fAudioInBuffers[i] = new float[newBufferSize];
  2630. }
  2631. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2632. {
  2633. if (fAudioOutBuffers[i] != nullptr)
  2634. delete[] fAudioOutBuffers[i];
  2635. fAudioOutBuffers[i] = new float[newBufferSize];
  2636. }
  2637. if (fHandle2 == nullptr)
  2638. {
  2639. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2640. {
  2641. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2642. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2643. }
  2644. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2645. {
  2646. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2647. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2648. }
  2649. }
  2650. else
  2651. {
  2652. if (kData->audioIn.count > 0)
  2653. {
  2654. CARLA_ASSERT(kData->audioIn.count == 2);
  2655. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2656. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2657. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2658. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2659. }
  2660. if (kData->audioOut.count > 0)
  2661. {
  2662. CARLA_ASSERT(kData->audioOut.count == 2);
  2663. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2664. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2665. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2666. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2667. }
  2668. }
  2669. if (fLv2Options.maxBufferSize != static_cast<int>(newBufferSize) || (fLv2Options.minBufferSize > 1 && fLv2Options.minBufferSize != static_cast<int>(newBufferSize)))
  2670. {
  2671. fLv2Options.maxBufferSize = newBufferSize;
  2672. if (fLv2Options.minBufferSize > 1)
  2673. fLv2Options.minBufferSize = newBufferSize;
  2674. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2675. {
  2676. fExt.options->set(fHandle, &fLv2Options.optMinBlockLenth);
  2677. fExt.options->set(fHandle, &fLv2Options.optMaxBlockLenth);
  2678. }
  2679. }
  2680. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2681. }
  2682. void sampleRateChanged(const double newSampleRate) override
  2683. {
  2684. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2685. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2686. if (fLv2Options.sampleRate != newSampleRate)
  2687. {
  2688. fLv2Options.sampleRate = newSampleRate;
  2689. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2690. fExt.options->set(fHandle, &fLv2Options.optSampleRate);
  2691. }
  2692. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2693. }
  2694. // -------------------------------------------------------------------
  2695. // Plugin buffers
  2696. void initBuffers() override
  2697. {
  2698. fEventsIn.initBuffers(kData->engine);
  2699. fEventsOut.initBuffers(kData->engine);
  2700. CarlaPlugin::initBuffers();
  2701. }
  2702. void clearBuffers() override
  2703. {
  2704. carla_debug("Lv2Plugin::clearBuffers() - start");
  2705. if (fAudioInBuffers != nullptr)
  2706. {
  2707. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2708. {
  2709. if (fAudioInBuffers[i] != nullptr)
  2710. {
  2711. delete[] fAudioInBuffers[i];
  2712. fAudioInBuffers[i] = nullptr;
  2713. }
  2714. }
  2715. delete[] fAudioInBuffers;
  2716. fAudioInBuffers = nullptr;
  2717. }
  2718. if (fAudioOutBuffers != nullptr)
  2719. {
  2720. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2721. {
  2722. if (fAudioOutBuffers[i] != nullptr)
  2723. {
  2724. delete[] fAudioOutBuffers[i];
  2725. fAudioOutBuffers[i] = nullptr;
  2726. }
  2727. }
  2728. delete[] fAudioOutBuffers;
  2729. fAudioOutBuffers = nullptr;
  2730. }
  2731. if (fParamBuffers != nullptr)
  2732. {
  2733. delete[] fParamBuffers;
  2734. fParamBuffers = nullptr;
  2735. }
  2736. fEventsIn.clear();
  2737. fEventsOut.clear();
  2738. CarlaPlugin::clearBuffers();
  2739. carla_debug("Lv2Plugin::clearBuffers() - end");
  2740. }
  2741. // -------------------------------------------------------------------
  2742. // Post-poned UI Stuff
  2743. void uiParameterChange(const uint32_t index, const float value) override
  2744. {
  2745. CARLA_ASSERT(fDescriptor != nullptr);
  2746. CARLA_ASSERT(fHandle != nullptr);
  2747. CARLA_ASSERT(index < kData->param.count);
  2748. if (fDescriptor == nullptr || fHandle == nullptr)
  2749. return;
  2750. if (index >= kData->param.count)
  2751. return;
  2752. if (fUi.type == PLUGIN_UI_OSC)
  2753. {
  2754. if (kData->osc.data.target != nullptr)
  2755. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  2756. }
  2757. else
  2758. {
  2759. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2760. fUi.descriptor->port_event(fUi.handle, kData->param.data[index].rindex, sizeof(float), 0, &value);
  2761. }
  2762. }
  2763. void uiMidiProgramChange(const uint32_t index) override
  2764. {
  2765. CARLA_ASSERT(index < kData->midiprog.count);
  2766. if (index >= kData->midiprog.count)
  2767. return;
  2768. if (fUi.type == PLUGIN_UI_OSC)
  2769. {
  2770. if (kData->osc.data.target != nullptr)
  2771. osc_send_midi_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2772. }
  2773. else
  2774. {
  2775. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2776. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2777. }
  2778. }
  2779. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  2780. {
  2781. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2782. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2783. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  2784. if (channel >= MAX_MIDI_CHANNELS)
  2785. return;
  2786. if (note >= MAX_MIDI_NOTE)
  2787. return;
  2788. if (velo >= MAX_MIDI_VALUE)
  2789. return;
  2790. if (fUi.type == PLUGIN_UI_OSC)
  2791. {
  2792. if (kData->osc.data.target != nullptr)
  2793. {
  2794. uint8_t midiData[4] = { 0 };
  2795. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2796. midiData[2] = note;
  2797. midiData[3] = velo;
  2798. osc_send_midi(&kData->osc.data, midiData);
  2799. }
  2800. }
  2801. else
  2802. {
  2803. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2804. {
  2805. LV2_Atom_MidiEvent midiEv;
  2806. midiEv.event.time.frames = 0;
  2807. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2808. midiEv.event.body.size = 3;
  2809. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2810. midiEv.data[1] = note;
  2811. midiEv.data[2] = velo;
  2812. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2813. }
  2814. }
  2815. }
  2816. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  2817. {
  2818. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2819. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2820. if (channel >= MAX_MIDI_CHANNELS)
  2821. return;
  2822. if (note >= MAX_MIDI_NOTE)
  2823. return;
  2824. if (fUi.type == PLUGIN_UI_OSC)
  2825. {
  2826. if (kData->osc.data.target != nullptr)
  2827. {
  2828. uint8_t midiData[4] = { 0 };
  2829. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2830. midiData[2] = note;
  2831. osc_send_midi(&kData->osc.data, midiData);
  2832. }
  2833. }
  2834. else
  2835. {
  2836. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2837. {
  2838. LV2_Atom_MidiEvent midiEv;
  2839. midiEv.event.time.frames = 0;
  2840. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2841. midiEv.event.body.size = 3;
  2842. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2843. midiEv.data[1] = note;
  2844. midiEv.data[2] = 0;
  2845. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2846. }
  2847. }
  2848. }
  2849. // -------------------------------------------------------------------
  2850. // Helpers
  2851. CarlaEngineEventPort* getDefaultEventInPort() const override
  2852. {
  2853. return (fEventsIn.ctrl != nullptr) ? fEventsIn.ctrl->port : nullptr;
  2854. }
  2855. // -------------------------------------------------------------------
  2856. protected:
  2857. void guiClosedCallback() override
  2858. {
  2859. showGui(false);
  2860. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  2861. }
  2862. // -------------------------------------------------------------------
  2863. LV2_URID getCustomURID(const char* const uri)
  2864. {
  2865. CARLA_ASSERT(uri != nullptr);
  2866. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  2867. if (uri == nullptr)
  2868. return CARLA_URI_MAP_ID_NULL;
  2869. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  2870. {
  2871. const char*& thisUri(fCustomURIDs.getAt(i));
  2872. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  2873. return i;
  2874. }
  2875. fCustomURIDs.append(carla_strdup(uri));
  2876. return fCustomURIDs.count()-1;
  2877. }
  2878. const char* getCustomURIString(const LV2_URID urid)
  2879. {
  2880. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  2881. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  2882. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  2883. if (urid == CARLA_URI_MAP_ID_NULL)
  2884. return nullptr;
  2885. if (urid < fCustomURIDs.count())
  2886. return fCustomURIDs.getAt(urid);
  2887. return nullptr;
  2888. }
  2889. // -------------------------------------------------------------------
  2890. void handleProgramChanged(const int32_t index)
  2891. {
  2892. CARLA_ASSERT_INT(index >= -1, index);
  2893. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  2894. if (index == -1)
  2895. {
  2896. const CarlaPlugin::ScopedDisabler m(this);
  2897. return reloadPrograms(false);
  2898. }
  2899. if (index >= 0 && index < static_cast<int32_t>(kData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  2900. {
  2901. if (const LV2_Program_Descriptor* progDesc = fExt.programs->get_program(fHandle, index))
  2902. {
  2903. CARLA_ASSERT(progDesc->name != nullptr);
  2904. if (kData->midiprog.data[index].name != nullptr)
  2905. delete[] kData->midiprog.data[index].name;
  2906. kData->midiprog.data[index].name = carla_strdup(progDesc->name ? progDesc->name : "");
  2907. if (index == kData->midiprog.current)
  2908. kData->engine->callback(CALLBACK_UPDATE, fId, 0, 0, 0.0, nullptr);
  2909. else
  2910. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0, nullptr);
  2911. }
  2912. }
  2913. }
  2914. // -------------------------------------------------------------------
  2915. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2916. {
  2917. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2918. CARLA_ASSERT(value != nullptr);
  2919. CARLA_ASSERT(size > 0);
  2920. carla_debug("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i)", key, value, size, type, flags);
  2921. // basic checks
  2922. if (key == CARLA_URI_MAP_ID_NULL)
  2923. {
  2924. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2925. return LV2_STATE_ERR_NO_PROPERTY;
  2926. }
  2927. if (value == nullptr || size == 0)
  2928. {
  2929. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid value", key, value, size, type, flags);
  2930. return LV2_STATE_ERR_NO_PROPERTY;
  2931. }
  2932. if ((flags & LV2_STATE_IS_POD) == 0)
  2933. {
  2934. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2935. return LV2_STATE_ERR_BAD_FLAGS;
  2936. }
  2937. const char* const stype(carla_lv2_urid_unmap(this, type));
  2938. if (stype == nullptr)
  2939. {
  2940. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type", key, value, size, type, flags);
  2941. return LV2_STATE_ERR_BAD_TYPE;
  2942. }
  2943. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2944. if (uriKey == nullptr)
  2945. {
  2946. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key URI", key, value, size, type, flags);
  2947. return LV2_STATE_ERR_NO_PROPERTY;
  2948. }
  2949. // Check if we already have this key
  2950. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2951. {
  2952. CustomData& data(*it);
  2953. if (std::strcmp(data.key, uriKey) == 0)
  2954. {
  2955. if (data.value != nullptr)
  2956. delete[] data.value;
  2957. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2958. data.value = carla_strdup((const char*)value);
  2959. else
  2960. data.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2961. return LV2_STATE_SUCCESS;
  2962. }
  2963. }
  2964. // Otherwise store it
  2965. CustomData newData;
  2966. newData.type = carla_strdup(stype);
  2967. newData.key = carla_strdup(uriKey);
  2968. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2969. newData.value = carla_strdup((const char*)value);
  2970. else
  2971. newData.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2972. kData->custom.append(newData);
  2973. return LV2_STATE_SUCCESS;
  2974. }
  2975. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2976. {
  2977. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2978. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  2979. // basic checks
  2980. if (key == CARLA_URI_MAP_ID_NULL)
  2981. {
  2982. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key", key, size, type, flags);
  2983. return nullptr;
  2984. }
  2985. if (size == nullptr || type == nullptr || flags == nullptr)
  2986. {
  2987. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid data", key, size, type, flags);
  2988. return nullptr;
  2989. }
  2990. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2991. if (uriKey == nullptr)
  2992. {
  2993. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  2994. return nullptr;
  2995. }
  2996. const char* stype = nullptr;
  2997. const char* stringData = nullptr;
  2998. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2999. {
  3000. CustomData& data(*it);
  3001. if (std::strcmp(data.key, uriKey) == 0)
  3002. {
  3003. stype = data.type;
  3004. stringData = data.value;
  3005. break;
  3006. }
  3007. }
  3008. if (stringData == nullptr)
  3009. {
  3010. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  3011. return nullptr;
  3012. }
  3013. *type = carla_lv2_urid_map(this, stype);
  3014. *flags = LV2_STATE_IS_POD;
  3015. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3016. {
  3017. *size = std::strlen(stringData);
  3018. return stringData;
  3019. }
  3020. else
  3021. {
  3022. static QByteArray chunk;
  3023. chunk = QByteArray::fromBase64(stringData);
  3024. *size = chunk.size();
  3025. return chunk.constData();
  3026. }
  3027. }
  3028. // -------------------------------------------------------------------
  3029. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3030. {
  3031. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3032. if (fExt.worker == nullptr || fExt.worker->work == nullptr)
  3033. {
  3034. carla_stderr("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  3035. return LV2_WORKER_ERR_UNKNOWN;
  3036. }
  3037. //if (kData->engine->isOffline())
  3038. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3039. //else
  3040. // postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  3041. return LV2_WORKER_SUCCESS;
  3042. }
  3043. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3044. {
  3045. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3046. #if 0
  3047. LV2_Atom_Worker workerAtom;
  3048. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3049. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  3050. workerAtom.body.size = size;
  3051. workerAtom.body.data = data;
  3052. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  3053. #endif
  3054. return LV2_WORKER_SUCCESS;
  3055. }
  3056. // -------------------------------------------------------------------
  3057. void handleExternalUiClosed()
  3058. {
  3059. CARLA_ASSERT(fUi.type == PLUGIN_UI_EXTERNAL);
  3060. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3061. fUi.descriptor->cleanup(fUi.handle);
  3062. fUi.handle = nullptr;
  3063. fUi.widget = nullptr;
  3064. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  3065. }
  3066. uint32_t handleUiPortMap(const char* const symbol)
  3067. {
  3068. CARLA_ASSERT(symbol != nullptr);
  3069. if (symbol == nullptr)
  3070. return LV2UI_INVALID_PORT_INDEX;
  3071. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3072. {
  3073. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3074. return i;
  3075. }
  3076. return LV2UI_INVALID_PORT_INDEX;
  3077. }
  3078. int handleUiResize(const int width, const int height)
  3079. {
  3080. CARLA_ASSERT(kData->gui != nullptr);
  3081. CARLA_ASSERT(width > 0);
  3082. CARLA_ASSERT(height > 0);
  3083. if (width <= 0 || height <= 0)
  3084. return 1;
  3085. if (kData->gui != nullptr)
  3086. kData->gui->setSize(width, height);
  3087. return 0;
  3088. }
  3089. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3090. {
  3091. if (format == 0)
  3092. {
  3093. CARLA_ASSERT(buffer != nullptr);
  3094. CARLA_ASSERT(bufferSize == sizeof(float));
  3095. if (buffer == nullptr || bufferSize != sizeof(float))
  3096. return;
  3097. float value = *(float*)buffer;
  3098. for (uint32_t i=0; i < kData->param.count; ++i)
  3099. {
  3100. if (kData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3101. {
  3102. if (fParamBuffers[i] != value)
  3103. setParameterValue(i, value, false, true, true);
  3104. break;
  3105. }
  3106. }
  3107. }
  3108. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3109. {
  3110. CARLA_ASSERT(buffer != nullptr);
  3111. if (buffer == nullptr)
  3112. return;
  3113. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  3114. fAtomQueueIn.put(rindex, atom);
  3115. }
  3116. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3117. {
  3118. CARLA_ASSERT(buffer != nullptr);
  3119. if (buffer == nullptr)
  3120. return;
  3121. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  3122. fAtomQueueIn.put(rindex, atom);
  3123. }
  3124. }
  3125. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, uint32_t size, uint32_t type)
  3126. {
  3127. CARLA_ASSERT(portSymbol != nullptr);
  3128. CARLA_ASSERT(value != nullptr);
  3129. CARLA_ASSERT(size > 0);
  3130. CARLA_ASSERT(type != CARLA_URI_MAP_ID_NULL);
  3131. if (portSymbol == nullptr)
  3132. return;
  3133. if (value == nullptr)
  3134. return;
  3135. if (size == 0)
  3136. return;
  3137. if (type == CARLA_URI_MAP_ID_NULL)
  3138. return;
  3139. const int32_t rindex(handleUiPortMap(portSymbol));
  3140. if (rindex < 0)
  3141. return;
  3142. if (size == sizeof(float) && type == CARLA_URI_MAP_ID_ATOM_FLOAT)
  3143. {
  3144. const float valuef(*(const float*)value);
  3145. for (uint32_t i=0; i < kData->param.count; ++i)
  3146. {
  3147. if (kData->param.data[i].rindex == rindex)
  3148. {
  3149. setParameterValue(i, valuef, true, true, true);
  3150. break;
  3151. }
  3152. }
  3153. }
  3154. }
  3155. // -------------------------------------------------------------------
  3156. bool isRealtimeSafe() const
  3157. {
  3158. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3159. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3160. {
  3161. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3162. return true;
  3163. }
  3164. return false;
  3165. }
  3166. bool needsFixedBuffer() const
  3167. {
  3168. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3169. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3170. {
  3171. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3172. return true;
  3173. }
  3174. return false;
  3175. }
  3176. // -------------------------------------------------------------------
  3177. const char* getUiBridgePath(const LV2_Property type) const
  3178. {
  3179. const EngineOptions& options(kData->engine->getOptions());
  3180. switch (type)
  3181. {
  3182. case LV2_UI_GTK2:
  3183. return options.bridge_lv2Gtk2;
  3184. case LV2_UI_GTK3:
  3185. return options.bridge_lv2Gtk3;
  3186. case LV2_UI_QT4:
  3187. return options.bridge_lv2Qt4;
  3188. case LV2_UI_QT5:
  3189. return options.bridge_lv2Qt5;
  3190. case LV2_UI_COCOA:
  3191. return options.bridge_lv2Cocoa;
  3192. case LV2_UI_WINDOWS:
  3193. return options.bridge_lv2Win;
  3194. case LV2_UI_X11:
  3195. return options.bridge_lv2X11;
  3196. default:
  3197. return nullptr;
  3198. }
  3199. }
  3200. bool isUiBridgeable(const uint32_t uiId) const
  3201. {
  3202. const LV2_RDF_UI& rdfUi(fRdfDescriptor->UIs[uiId]);
  3203. for (uint32_t i=0; i < rdfUi.FeatureCount; ++i)
  3204. {
  3205. if (std::strcmp(rdfUi.Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3206. return false;
  3207. if (std::strcmp(rdfUi.Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3208. return false;
  3209. }
  3210. return true;
  3211. }
  3212. bool isUiResizable() const
  3213. {
  3214. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3215. {
  3216. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3217. return false;
  3218. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3219. return false;
  3220. }
  3221. return true;
  3222. }
  3223. void updateUi()
  3224. {
  3225. CARLA_ASSERT(fUi.handle != nullptr);
  3226. CARLA_ASSERT(fUi.descriptor != nullptr);
  3227. fExt.uiidle = nullptr;
  3228. fExt.uiprograms = nullptr;
  3229. if (fUi.descriptor->extension_data != nullptr)
  3230. {
  3231. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3232. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3233. // check if invalid
  3234. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3235. fExt.uiidle = nullptr;
  3236. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3237. fExt.uiprograms = nullptr;
  3238. // update midi program
  3239. if (fExt.uiprograms && kData->midiprog.count > 0 && kData->midiprog.current >= 0)
  3240. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[kData->midiprog.current].bank,
  3241. kData->midiprog.data[kData->midiprog.current].program);
  3242. }
  3243. if (fUi.descriptor->port_event != nullptr)
  3244. {
  3245. // update control ports
  3246. float value;
  3247. for (uint32_t i=0; i < kData->param.count; ++i)
  3248. {
  3249. value = getParameterValue(i);
  3250. fUi.descriptor->port_event(fUi.handle, kData->param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3251. }
  3252. }
  3253. }
  3254. // -------------------------------------------------------------------
  3255. public:
  3256. bool init(const char* const name, const char* const uri)
  3257. {
  3258. CARLA_ASSERT(kData->engine != nullptr);
  3259. CARLA_ASSERT(kData->client == nullptr);
  3260. CARLA_ASSERT(uri != nullptr);
  3261. // ---------------------------------------------------------------
  3262. // first checks
  3263. if (kData->engine == nullptr)
  3264. {
  3265. return false;
  3266. }
  3267. if (kData->client != nullptr)
  3268. {
  3269. kData->engine->setLastError("Plugin client is already registered");
  3270. return false;
  3271. }
  3272. if (uri == nullptr)
  3273. {
  3274. kData->engine->setLastError("null uri");
  3275. return false;
  3276. }
  3277. // ---------------------------------------------------------------
  3278. // get plugin from lv2_rdf (lilv)
  3279. gLv2World.init();
  3280. fRdfDescriptor = lv2_rdf_new(uri);
  3281. if (fRdfDescriptor == nullptr)
  3282. {
  3283. kData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3284. return false;
  3285. }
  3286. // ---------------------------------------------------------------
  3287. // open DLL
  3288. if (! kData->libOpen(fRdfDescriptor->Binary))
  3289. {
  3290. kData->engine->setLastError(kData->libError(fRdfDescriptor->Binary));
  3291. return false;
  3292. }
  3293. // ---------------------------------------------------------------
  3294. // initialize options
  3295. fLv2Options.minBufferSize = 1;
  3296. fLv2Options.maxBufferSize = kData->engine->getBufferSize();
  3297. fLv2Options.sampleRate = kData->engine->getSampleRate();
  3298. // ---------------------------------------------------------------
  3299. // initialize features (part 1)
  3300. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3301. eventFt->callback_data = this;
  3302. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3303. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3304. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3305. logFt->handle = this;
  3306. logFt->printf = carla_lv2_log_printf;
  3307. logFt->vprintf = carla_lv2_log_vprintf;
  3308. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3309. stateMakePathFt->handle = this;
  3310. stateMakePathFt->path = carla_lv2_state_make_path;
  3311. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3312. stateMapPathFt->handle = this;
  3313. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3314. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3315. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3316. programsFt->handle = this;
  3317. programsFt->program_changed = carla_lv2_program_changed;
  3318. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3319. lv2_rtmempool_init(rtMemPoolFt);
  3320. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3321. uriMapFt->callback_data = this;
  3322. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3323. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3324. uridMapFt->handle = this;
  3325. uridMapFt->map = carla_lv2_urid_map;
  3326. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3327. uridUnmapFt->handle = this;
  3328. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3329. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3330. workerFt->handle = this;
  3331. workerFt->schedule_work = carla_lv2_worker_schedule;
  3332. // ---------------------------------------------------------------
  3333. // initialize features (part 2)
  3334. fFeatures[kFeatureIdBufSizeBounded] = new LV2_Feature;
  3335. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3336. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3337. fFeatures[kFeatureIdBufSizeFixed] = new LV2_Feature;
  3338. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3339. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3340. fFeatures[kFeatureIdBufSizePowerOf2] = new LV2_Feature;
  3341. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3342. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3343. fFeatures[kFeatureIdEvent] = new LV2_Feature;
  3344. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3345. fFeatures[kFeatureIdEvent]->data = eventFt;
  3346. fFeatures[kFeatureIdLogs] = new LV2_Feature;
  3347. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3348. fFeatures[kFeatureIdLogs]->data = logFt;
  3349. fFeatures[kFeatureIdOptions] = new LV2_Feature;
  3350. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3351. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3352. fFeatures[kFeatureIdPrograms] = new LV2_Feature;
  3353. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3354. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3355. fFeatures[kFeatureIdRtMemPool] = new LV2_Feature;
  3356. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3357. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3358. fFeatures[kFeatureIdStateMakePath] = new LV2_Feature;
  3359. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3360. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3361. fFeatures[kFeatureIdStateMapPath] = new LV2_Feature;
  3362. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3363. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3364. fFeatures[kFeatureIdStrictBounds] = new LV2_Feature;
  3365. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3366. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3367. fFeatures[kFeatureIdUriMap] = new LV2_Feature;
  3368. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3369. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3370. fFeatures[kFeatureIdUridMap] = new LV2_Feature;
  3371. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3372. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3373. fFeatures[kFeatureIdUridUnmap] = new LV2_Feature;
  3374. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3375. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3376. fFeatures[kFeatureIdWorker] = new LV2_Feature;
  3377. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3378. fFeatures[kFeatureIdWorker]->data = workerFt;
  3379. if (! needsFixedBuffer())
  3380. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3381. // ---------------------------------------------------------------
  3382. // get DLL main entry
  3383. #if 0
  3384. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)kData->libSymbol("lv2_lib_descriptor");
  3385. if (libDescFn != nullptr)
  3386. {
  3387. // -----------------------------------------------------------
  3388. // get lib descriptor
  3389. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  3390. if (libFn == nullptr || libFn->get_plugin == nullptr)
  3391. {
  3392. kData->engine->setLastError("Plugin failed to return library descriptor");
  3393. return false;
  3394. }
  3395. // -----------------------------------------------------------
  3396. // get descriptor that matches URI
  3397. uint32_t i = 0;
  3398. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  3399. {
  3400. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3401. break;
  3402. }
  3403. if (fDescriptor == nullptr)
  3404. libFn->cleanup(libFn->handle);
  3405. else
  3406. #endif
  3407. {
  3408. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)kData->libSymbol("lv2_descriptor");
  3409. if (descFn == nullptr)
  3410. {
  3411. kData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3412. return false;
  3413. }
  3414. // -----------------------------------------------------------
  3415. // get descriptor that matches URI
  3416. uint32_t i = 0;
  3417. while ((fDescriptor = descFn(i++)))
  3418. {
  3419. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3420. break;
  3421. }
  3422. }
  3423. if (fDescriptor == nullptr)
  3424. {
  3425. kData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3426. return false;
  3427. }
  3428. // ---------------------------------------------------------------
  3429. // check supported port-types and features
  3430. bool canContinue = true;
  3431. // Check supported ports
  3432. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3433. {
  3434. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3435. 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)))
  3436. {
  3437. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  3438. {
  3439. kData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3440. canContinue = false;
  3441. break;
  3442. }
  3443. }
  3444. }
  3445. // Check supported features
  3446. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  3447. {
  3448. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  3449. {
  3450. QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  3451. kData->engine->setLastError(msg.toUtf8().constData());
  3452. canContinue = false;
  3453. break;
  3454. }
  3455. }
  3456. // Check extensions
  3457. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3458. {
  3459. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3460. kData->extraHints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3461. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3462. kData->extraHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3463. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3464. kData->extraHints |= PLUGIN_HAS_EXTENSION_STATE;
  3465. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3466. kData->extraHints |= PLUGIN_HAS_EXTENSION_WORKER;
  3467. else
  3468. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3469. }
  3470. if (! canContinue)
  3471. {
  3472. // error already set
  3473. return false;
  3474. }
  3475. // ---------------------------------------------------------------
  3476. // get info
  3477. if (name != nullptr)
  3478. fName = kData->engine->getUniquePluginName(name);
  3479. else
  3480. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3481. // ---------------------------------------------------------------
  3482. // register client
  3483. kData->client = kData->engine->addClient(this);
  3484. if (kData->client == nullptr || ! kData->client->isOk())
  3485. {
  3486. kData->engine->setLastError("Failed to register plugin client");
  3487. return false;
  3488. }
  3489. // ---------------------------------------------------------------
  3490. // initialize plugin
  3491. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3492. if (fHandle == nullptr)
  3493. {
  3494. kData->engine->setLastError("Plugin failed to initialize");
  3495. return false;
  3496. }
  3497. // ---------------------------------------------------------------
  3498. // load plugin settings
  3499. {
  3500. // set default options
  3501. fOptions = 0x0;
  3502. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3503. if (midiInCount() > 0 || needsFixedBuffer())
  3504. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3505. if (kData->engine->getOptions().forceStereo)
  3506. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  3507. if (midiInCount() > 0)
  3508. {
  3509. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3510. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3511. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  3512. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3513. }
  3514. // load settings
  3515. kData->idStr = "LV2/";
  3516. kData->idStr += uri;
  3517. fOptions = kData->loadSettings(fOptions, availableOptions());
  3518. // ignore settings, we need this anyway
  3519. if (midiInCount() > 0 || needsFixedBuffer())
  3520. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3521. }
  3522. // ---------------------------------------------------------------
  3523. // gui stuff
  3524. if (fRdfDescriptor->UICount == 0)
  3525. return true;
  3526. // -----------------------------------------------------------
  3527. // find more appropriate ui
  3528. int eQt4, eQt5, eCocoa, eWindows, eX11, eGtk2, eGtk3, iCocoa, iWindows, iX11, iQt4, iQt5, iExt, iFinal;
  3529. eQt4 = eQt5 = eCocoa = eWindows = eX11 = eGtk2 = eGtk3 = iQt4 = iQt5 = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3530. #ifdef BUILD_BRIDGE
  3531. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges);
  3532. #else
  3533. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges && (fHints & PLUGIN_IS_BRIDGE) == 0);
  3534. #endif
  3535. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3536. {
  3537. CARLA_ASSERT(fRdfDescriptor->UIs[i].URI != nullptr);
  3538. if (fRdfDescriptor->UIs[i].URI == nullptr)
  3539. {
  3540. carla_stderr("Plugin has an UI without a valid URI");
  3541. continue;
  3542. }
  3543. switch (fRdfDescriptor->UIs[i].Type)
  3544. {
  3545. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3546. case LV2_UI_QT4:
  3547. if (isUiBridgeable(i))
  3548. eQt4 = i;
  3549. break;
  3550. #else
  3551. case LV2_UI_QT4:
  3552. if (isUiBridgeable(i) && preferUiBridges)
  3553. eQt4 = i;
  3554. iQt4 = i;
  3555. break;
  3556. #endif
  3557. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3558. case LV2_UI_QT5:
  3559. if (isUiBridgeable(i) && preferUiBridges)
  3560. eQt5 = i;
  3561. iQt5 = i;
  3562. break;
  3563. #else
  3564. case LV2_UI_QT5:
  3565. if (isUiBridgeable(i) && preferUiBridges)
  3566. eQt5 = i;
  3567. break;
  3568. #endif
  3569. #ifdef CARLA_OS_MAC
  3570. case LV2_UI_COCOA:
  3571. if (isUiBridgeable(i) && preferUiBridges)
  3572. eCocoa = i;
  3573. iCocoa = i;
  3574. break;
  3575. #endif
  3576. #ifdef CARLA_OS_WIN
  3577. case LV2_UI_WINDOWS:
  3578. if (isUiBridgeable(i) && preferUiBridges)
  3579. eWindows = i;
  3580. iWindows = i;
  3581. break;
  3582. #endif
  3583. case LV2_UI_X11:
  3584. if (isUiBridgeable(i) && preferUiBridges)
  3585. eX11 = i;
  3586. #ifdef Q_WS_X11
  3587. iX11 = i;
  3588. #endif
  3589. break;
  3590. case LV2_UI_GTK2:
  3591. if (isUiBridgeable(i))
  3592. eGtk2 = i;
  3593. break;
  3594. case LV2_UI_GTK3:
  3595. if (isUiBridgeable(i))
  3596. eGtk3 = i;
  3597. break;
  3598. case LV2_UI_EXTERNAL:
  3599. case LV2_UI_OLD_EXTERNAL:
  3600. iExt = i;
  3601. break;
  3602. default:
  3603. break;
  3604. }
  3605. }
  3606. if (eQt4 >= 0)
  3607. iFinal = eQt4;
  3608. else if (eQt5 >= 0)
  3609. iFinal = eQt5;
  3610. else if (eCocoa >= 0)
  3611. iFinal = eCocoa;
  3612. else if (eWindows >= 0)
  3613. iFinal = eWindows;
  3614. else if (eX11 >= 0)
  3615. iFinal = eX11;
  3616. else if (iQt4 >= 0)
  3617. iFinal = iQt4;
  3618. else if (iQt5 >= 0)
  3619. iFinal = iQt5;
  3620. else if (iCocoa >= 0)
  3621. iFinal = iCocoa;
  3622. else if (iWindows >= 0)
  3623. iFinal = iWindows;
  3624. else if (iX11 >= 0)
  3625. iFinal = iX11;
  3626. else if (iExt >= 0)
  3627. iFinal = iExt;
  3628. else if (eGtk2 >= 0)
  3629. iFinal = eGtk2;
  3630. else if (eGtk3 >= 0)
  3631. iFinal = eGtk3;
  3632. if (iFinal < 0)
  3633. {
  3634. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3635. return true;
  3636. }
  3637. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3638. // -----------------------------------------------------------
  3639. // check supported ui features
  3640. canContinue = true;
  3641. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3642. {
  3643. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type) && ! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3644. {
  3645. carla_stderr2("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3646. canContinue = false;
  3647. break;
  3648. }
  3649. }
  3650. if (! canContinue)
  3651. {
  3652. fUi.rdfDescriptor = nullptr;
  3653. return true;
  3654. }
  3655. // -----------------------------------------------------------
  3656. // initialize ui according to type
  3657. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3658. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3)
  3659. {
  3660. // -------------------------------------------------------
  3661. // initialize ui bridge
  3662. if (const char* const oscBinary = getUiBridgePath(uiType))
  3663. {
  3664. fUi.type = PLUGIN_UI_OSC;
  3665. kData->osc.thread.setOscData(oscBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3666. }
  3667. }
  3668. else
  3669. {
  3670. // -------------------------------------------------------
  3671. // open UI DLL
  3672. if (! kData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3673. {
  3674. carla_stderr2("Could not load UI library, error was:\n%s", kData->libError(fUi.rdfDescriptor->Binary));
  3675. fUi.rdfDescriptor = nullptr;
  3676. return true;
  3677. }
  3678. // -------------------------------------------------------
  3679. // get UI DLL main entry
  3680. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)kData->uiLibSymbol("lv2ui_descriptor");
  3681. if (uiDescFn == nullptr)
  3682. {
  3683. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3684. kData->uiLibClose();
  3685. fUi.rdfDescriptor = nullptr;
  3686. return true;
  3687. }
  3688. // -------------------------------------------------------
  3689. // get UI descriptor that matches UI URI
  3690. uint32_t i = 0;
  3691. while ((fUi.descriptor = uiDescFn(i++)))
  3692. {
  3693. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3694. break;
  3695. }
  3696. if (fUi.descriptor == nullptr)
  3697. {
  3698. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3699. kData->uiLibClose();
  3700. fUi.rdfDescriptor = nullptr;
  3701. return true;
  3702. }
  3703. // -------------------------------------------------------
  3704. // check if ui is usable
  3705. switch (uiType)
  3706. {
  3707. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3708. case LV2_UI_QT5:
  3709. carla_debug("Will use LV2 Qt5 UI");
  3710. fUi.type = PLUGIN_UI_QT;
  3711. break;
  3712. #else
  3713. case LV2_UI_QT4:
  3714. carla_debug("Will use LV2 Qt4 UI");
  3715. fUi.type = PLUGIN_UI_QT;
  3716. break;
  3717. #endif
  3718. #ifdef CARLA_OS_MAC
  3719. case LV2_UI_COCOA:
  3720. carla_debug("Will use LV2 Cocoa UI");
  3721. fUi.type = PLUGIN_UI_PARENT;
  3722. break;
  3723. #endif
  3724. #ifdef CARLA_OS_WIN
  3725. case LV2_UI_WINDOWS:
  3726. carla_debug("Will use LV2 Windows UI");
  3727. fUi.type = PLUGIN_UI_PARENT;
  3728. break;
  3729. #endif
  3730. #ifdef Q_WS_X11
  3731. case LV2_UI_X11:
  3732. carla_debug("Will use LV2 X11 UI");
  3733. fUi.type = PLUGIN_UI_PARENT;
  3734. break;
  3735. #endif
  3736. case LV2_UI_GTK2:
  3737. carla_debug("Will use LV2 Gtk2 UI, NOT!");
  3738. break;
  3739. case LV2_UI_GTK3:
  3740. carla_debug("Will use LV2 Gtk3 UI, NOT!");
  3741. break;
  3742. case LV2_UI_EXTERNAL:
  3743. case LV2_UI_OLD_EXTERNAL:
  3744. carla_debug("Will use LV2 External UI");
  3745. fUi.type = PLUGIN_UI_EXTERNAL;
  3746. break;
  3747. }
  3748. if (fUi.type == PLUGIN_UI_NULL)
  3749. {
  3750. kData->uiLibClose();
  3751. fUi.descriptor = nullptr;
  3752. fUi.rdfDescriptor = nullptr;
  3753. return true;
  3754. }
  3755. // -------------------------------------------------------
  3756. // initialize ui features
  3757. QString guiTitle(QString("%1 (GUI)").arg((const char*)fName));
  3758. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3759. uiDataFt->data_access = fDescriptor->extension_data;
  3760. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3761. uiPortMapFt->handle = this;
  3762. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3763. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3764. uiResizeFt->handle = this;
  3765. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3766. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3767. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3768. uiExternalHostFt->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  3769. fFeatures[kFeatureIdUiDataAccess] = new LV2_Feature;
  3770. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3771. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3772. fFeatures[kFeatureIdUiInstanceAccess] = new LV2_Feature;
  3773. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3774. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3775. fFeatures[kFeatureIdUiParent] = new LV2_Feature;
  3776. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3777. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3778. fFeatures[kFeatureIdUiPortMap] = new LV2_Feature;
  3779. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3780. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3781. fFeatures[kFeatureIdUiResize] = new LV2_Feature;
  3782. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3783. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3784. fFeatures[kFeatureIdExternalUi] = new LV2_Feature;
  3785. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3786. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3787. fFeatures[kFeatureIdExternalUiOld] = new LV2_Feature;
  3788. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3789. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3790. }
  3791. return true;
  3792. }
  3793. // -------------------------------------------------------------------
  3794. void handleTransferAtom(const int32_t portIndex, const char* const typeStr, LV2_Atom* const atom)
  3795. {
  3796. CARLA_ASSERT(portIndex >= 0);
  3797. CARLA_ASSERT(atom != nullptr);
  3798. CARLA_ASSERT(typeStr != nullptr);
  3799. carla_debug("Lv2Plugin::handleTransferAtom(%i, %s, %p)", portIndex, typeStr, atom);
  3800. atom->type = carla_lv2_urid_map(this, typeStr);
  3801. fAtomQueueIn.put(portIndex, atom);
  3802. }
  3803. void handleTransferEvent(const int32_t portIndex, const char* const typeStr, LV2_Atom* const atom)
  3804. {
  3805. CARLA_ASSERT(portIndex >= 0);
  3806. CARLA_ASSERT(atom != nullptr);
  3807. CARLA_ASSERT(typeStr != nullptr);
  3808. carla_debug("Lv2Plugin::handleTransferEvent(%i, %s, %p)", portIndex, typeStr, atom);
  3809. atom->type = carla_lv2_urid_map(this, typeStr);
  3810. fAtomQueueIn.put(portIndex, atom);
  3811. }
  3812. // -------------------------------------------------------------------
  3813. private:
  3814. LV2_Handle fHandle;
  3815. LV2_Handle fHandle2;
  3816. LV2_Feature* fFeatures[kFeatureCount+1];
  3817. const LV2_Descriptor* fDescriptor;
  3818. const LV2_RDF_Descriptor* fRdfDescriptor;
  3819. float** fAudioInBuffers;
  3820. float** fAudioOutBuffers;
  3821. float* fParamBuffers;
  3822. float fParamFreewheel;
  3823. Lv2AtomQueue fAtomQueueIn;
  3824. Lv2AtomQueue fAtomQueueOut;
  3825. LV2_Atom_Forge fAtomForge;
  3826. Lv2PluginEventData fEventsIn;
  3827. Lv2PluginEventData fEventsOut;
  3828. Lv2PluginOptions fLv2Options;
  3829. NonRtList<const char*> fCustomURIDs;
  3830. bool fFirstActive; // first process() call after activate()
  3831. EngineTimeInfo fLastTimeInfo;
  3832. struct Extensions {
  3833. const LV2_Options_Interface* options;
  3834. const LV2_State_Interface* state;
  3835. const LV2_Worker_Interface* worker;
  3836. const LV2_Programs_Interface* programs;
  3837. const LV2UI_Idle_Interface* uiidle;
  3838. const LV2_Programs_UI_Interface* uiprograms;
  3839. Extensions()
  3840. : options(nullptr),
  3841. state(nullptr),
  3842. worker(nullptr),
  3843. programs(nullptr),
  3844. uiidle(nullptr),
  3845. uiprograms(nullptr) {}
  3846. } fExt;
  3847. struct UI {
  3848. Lv2PluginGuiType type;
  3849. LV2UI_Handle handle;
  3850. LV2UI_Widget widget;
  3851. const LV2UI_Descriptor* descriptor;
  3852. const LV2_RDF_UI* rdfDescriptor;
  3853. UI()
  3854. : type(PLUGIN_UI_NULL),
  3855. handle(nullptr),
  3856. widget(nullptr),
  3857. descriptor(nullptr),
  3858. rdfDescriptor(nullptr) {}
  3859. ~UI()
  3860. {
  3861. CARLA_ASSERT(handle == nullptr);
  3862. CARLA_ASSERT(widget == nullptr);
  3863. CARLA_ASSERT(descriptor == nullptr);
  3864. CARLA_ASSERT(rdfDescriptor == nullptr);
  3865. }
  3866. } fUi;
  3867. // -------------------------------------------------------------------
  3868. // Event Feature
  3869. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3870. {
  3871. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  3872. CARLA_ASSERT(callback_data != nullptr);
  3873. CARLA_ASSERT(event != nullptr);
  3874. return 0;
  3875. }
  3876. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3877. {
  3878. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  3879. CARLA_ASSERT(callback_data != nullptr);
  3880. CARLA_ASSERT(event != nullptr);
  3881. return 0;
  3882. }
  3883. // -------------------------------------------------------------------
  3884. // Logs Feature
  3885. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  3886. {
  3887. CARLA_ASSERT(handle != nullptr);
  3888. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3889. #ifndef DEBUG
  3890. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3891. return 0;
  3892. #endif
  3893. va_list args;
  3894. va_start(args, fmt);
  3895. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  3896. va_end(args);
  3897. return ret;
  3898. }
  3899. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  3900. {
  3901. CARLA_ASSERT(handle != nullptr);
  3902. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3903. #ifndef DEBUG
  3904. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3905. return 0;
  3906. #endif
  3907. int ret = 0;
  3908. switch (type)
  3909. {
  3910. case CARLA_URI_MAP_ID_LOG_ERROR:
  3911. #ifndef CARLA_OS_WIN
  3912. std::fprintf(stderr, "\x1b[31m");
  3913. #endif
  3914. ret = std::vfprintf(stderr, fmt, ap);
  3915. #ifndef CARLA_OS_WIN
  3916. std::fprintf(stderr, "\x1b[0m");
  3917. #endif
  3918. break;
  3919. case CARLA_URI_MAP_ID_LOG_NOTE:
  3920. ret = std::vfprintf(stdout, fmt, ap);
  3921. break;
  3922. case CARLA_URI_MAP_ID_LOG_TRACE:
  3923. #ifdef DEBUG
  3924. # ifndef CARLA_OS_WIN
  3925. std::fprintf(stdout, "\x1b[30;1m");
  3926. # endif
  3927. ret = std::vfprintf(stdout, fmt, ap);
  3928. # ifndef CARLA_OS_WIN
  3929. std::fprintf(stdout, "\x1b[0m");
  3930. # endif
  3931. #endif
  3932. break;
  3933. case CARLA_URI_MAP_ID_LOG_WARNING:
  3934. ret = std::vfprintf(stderr, fmt, ap);
  3935. break;
  3936. default:
  3937. break;
  3938. }
  3939. return ret;
  3940. }
  3941. // -------------------------------------------------------------------
  3942. // Programs Feature
  3943. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  3944. {
  3945. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  3946. CARLA_ASSERT(handle != nullptr);
  3947. if (handle == nullptr)
  3948. return;
  3949. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  3950. }
  3951. // -------------------------------------------------------------------
  3952. // State Feature
  3953. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  3954. {
  3955. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3956. CARLA_ASSERT(handle != nullptr);
  3957. CARLA_ASSERT(path != nullptr);
  3958. if (path == nullptr)
  3959. return nullptr;
  3960. QDir dir;
  3961. dir.mkpath(path);
  3962. return strdup(path);
  3963. }
  3964. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  3965. {
  3966. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3967. CARLA_ASSERT(handle != nullptr);
  3968. CARLA_ASSERT(absolute_path != nullptr);
  3969. if (absolute_path == nullptr)
  3970. return nullptr;
  3971. QDir dir(absolute_path);
  3972. return strdup(dir.canonicalPath().toUtf8().constData());
  3973. }
  3974. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  3975. {
  3976. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  3977. CARLA_ASSERT(handle != nullptr);
  3978. CARLA_ASSERT(abstract_path != nullptr);
  3979. if (abstract_path == nullptr)
  3980. return nullptr;
  3981. QDir dir(abstract_path);
  3982. return strdup(dir.absolutePath().toUtf8().constData());
  3983. }
  3984. 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)
  3985. {
  3986. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  3987. CARLA_ASSERT(handle != nullptr);
  3988. if (handle == nullptr)
  3989. return LV2_STATE_ERR_UNKNOWN;
  3990. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  3991. }
  3992. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  3993. {
  3994. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  3995. CARLA_ASSERT(handle != nullptr);
  3996. if (handle == nullptr)
  3997. return nullptr;
  3998. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  3999. }
  4000. // -------------------------------------------------------------------
  4001. // URI-Map Feature
  4002. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4003. {
  4004. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4005. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4006. // unused
  4007. (void)map;
  4008. }
  4009. // -------------------------------------------------------------------
  4010. // URID Feature
  4011. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4012. {
  4013. CARLA_ASSERT(handle != nullptr);
  4014. CARLA_ASSERT(uri != nullptr);
  4015. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4016. if (uri == nullptr)
  4017. return CARLA_URI_MAP_ID_NULL;
  4018. // Atom types
  4019. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4020. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4021. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4022. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4023. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4024. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4025. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4026. return CARLA_URI_MAP_ID_ATOM_INT;
  4027. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4028. return CARLA_URI_MAP_ID_ATOM_PATH;
  4029. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4030. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4031. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4032. return CARLA_URI_MAP_ID_ATOM_STRING;
  4033. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4034. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4035. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4036. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4037. // BufSize types
  4038. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4039. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4040. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4041. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4042. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4043. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4044. // Log types
  4045. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4046. return CARLA_URI_MAP_ID_LOG_ERROR;
  4047. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4048. return CARLA_URI_MAP_ID_LOG_NOTE;
  4049. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4050. return CARLA_URI_MAP_ID_LOG_TRACE;
  4051. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4052. return CARLA_URI_MAP_ID_LOG_WARNING;
  4053. // Time types
  4054. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4055. return CARLA_URI_MAP_ID_TIME_POSITION;
  4056. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4057. return CARLA_URI_MAP_ID_TIME_BAR;
  4058. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4059. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4060. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4061. return CARLA_URI_MAP_ID_TIME_BEAT;
  4062. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4063. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4064. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4065. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4066. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4067. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4068. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4069. return CARLA_URI_MAP_ID_TIME_FRAME;
  4070. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4071. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4072. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4073. return CARLA_URI_MAP_ID_TIME_SPEED;
  4074. // Others
  4075. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4076. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4077. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4078. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4079. if (handle == nullptr)
  4080. return CARLA_URI_MAP_ID_NULL;
  4081. // Custom types
  4082. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4083. }
  4084. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4085. {
  4086. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4087. CARLA_ASSERT(handle != nullptr);
  4088. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  4089. if (urid == CARLA_URI_MAP_ID_NULL)
  4090. return nullptr;
  4091. // Atom types
  4092. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4093. return LV2_ATOM__Chunk;
  4094. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4095. return LV2_ATOM__Double;
  4096. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4097. return LV2_ATOM__Float;
  4098. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4099. return LV2_ATOM__Int;
  4100. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4101. return LV2_ATOM__Path;
  4102. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4103. return LV2_ATOM__Sequence;
  4104. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4105. return LV2_ATOM__String;
  4106. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4107. return LV2_ATOM__atomTransfer;
  4108. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4109. return LV2_ATOM__eventTransfer;
  4110. // BufSize types
  4111. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4112. return LV2_BUF_SIZE__maxBlockLength;
  4113. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4114. return LV2_BUF_SIZE__minBlockLength;
  4115. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4116. return LV2_BUF_SIZE__sequenceSize;
  4117. // Log types
  4118. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4119. return LV2_LOG__Error;
  4120. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4121. return LV2_LOG__Note;
  4122. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4123. return LV2_LOG__Trace;
  4124. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4125. return LV2_LOG__Warning;
  4126. // Time types
  4127. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4128. return LV2_TIME__Position;
  4129. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4130. return LV2_TIME__bar;
  4131. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4132. return LV2_TIME__barBeat;
  4133. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4134. return LV2_TIME__beat;
  4135. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4136. return LV2_TIME__beatUnit;
  4137. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4138. return LV2_TIME__beatsPerBar;
  4139. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4140. return LV2_TIME__beatsPerMinute;
  4141. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4142. return LV2_TIME__frame;
  4143. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4144. return LV2_TIME__framesPerSecond;
  4145. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4146. return LV2_TIME__speed;
  4147. // Others
  4148. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4149. return LV2_MIDI__MidiEvent;
  4150. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4151. return LV2_PARAMETERS__sampleRate;
  4152. if (handle == nullptr)
  4153. return nullptr;
  4154. // Custom types
  4155. return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  4156. }
  4157. // -------------------------------------------------------------------
  4158. // Worker Feature
  4159. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4160. {
  4161. CARLA_ASSERT(handle != nullptr);
  4162. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4163. if (handle == nullptr)
  4164. return LV2_WORKER_ERR_UNKNOWN;
  4165. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4166. }
  4167. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4168. {
  4169. CARLA_ASSERT(handle != nullptr);
  4170. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4171. if (handle == nullptr)
  4172. return LV2_WORKER_ERR_UNKNOWN;
  4173. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4174. }
  4175. // -------------------------------------------------------------------
  4176. // UI Port-Map Feature
  4177. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4178. {
  4179. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4180. CARLA_ASSERT(handle);
  4181. if (handle == nullptr)
  4182. return LV2UI_INVALID_PORT_INDEX;
  4183. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4184. }
  4185. // -------------------------------------------------------------------
  4186. // UI Resize Feature
  4187. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4188. {
  4189. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4190. CARLA_ASSERT(handle != nullptr);
  4191. if (handle == nullptr)
  4192. return 1;
  4193. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4194. }
  4195. // -------------------------------------------------------------------
  4196. // External UI Feature
  4197. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4198. {
  4199. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4200. CARLA_ASSERT(controller != nullptr);
  4201. if (controller == nullptr)
  4202. return;
  4203. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4204. }
  4205. // -------------------------------------------------------------------
  4206. // UI Extension
  4207. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4208. {
  4209. CARLA_ASSERT(controller != nullptr);
  4210. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4211. if (controller == nullptr)
  4212. return;
  4213. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4214. }
  4215. // -------------------------------------------------------------------
  4216. // Lilv State
  4217. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4218. {
  4219. CARLA_ASSERT(user_data != nullptr);
  4220. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4221. if (user_data == nullptr)
  4222. return;
  4223. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4224. }
  4225. // -------------------------------------------------------------------
  4226. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4227. };
  4228. // -------------------------------------------------------------------------------------------------------------------
  4229. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4230. {
  4231. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4232. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iss");
  4233. const int32_t portIndex = argv[0]->i;
  4234. const char* const typeStr = (const char*)&argv[1]->s;
  4235. const char* const atomBuf = (const char*)&argv[2]->s;
  4236. QByteArray chunk;
  4237. chunk = QByteArray::fromBase64(atomBuf);
  4238. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  4239. ((Lv2Plugin*)plugin)->handleTransferAtom(portIndex, typeStr, atom);
  4240. return 0;
  4241. }
  4242. int CarlaEngineOsc::handleMsgLv2EventTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4243. {
  4244. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  4245. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iss");
  4246. const int32_t portIndex = argv[0]->i;
  4247. const char* const typeStr = (const char*)&argv[1]->s;
  4248. const char* const atomBuf = (const char*)&argv[2]->s;
  4249. QByteArray chunk;
  4250. chunk = QByteArray::fromBase64(atomBuf);
  4251. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  4252. ((Lv2Plugin*)plugin)->handleTransferEvent(portIndex, typeStr, atom);
  4253. return 0;
  4254. }
  4255. CARLA_BACKEND_END_NAMESPACE
  4256. #else // WANT_VST
  4257. # warning Building without LV2 support
  4258. #endif
  4259. CARLA_BACKEND_START_NAMESPACE
  4260. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4261. {
  4262. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4263. #ifdef WANT_LV2
  4264. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4265. if (! plugin->init(init.name, init.label))
  4266. {
  4267. delete plugin;
  4268. return nullptr;
  4269. }
  4270. plugin->reload();
  4271. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  4272. {
  4273. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4274. delete plugin;
  4275. return nullptr;
  4276. }
  4277. return plugin;
  4278. #else
  4279. init.engine->setLastError("LV2 support not available");
  4280. return nullptr;
  4281. #endif
  4282. }
  4283. CARLA_BACKEND_END_NAMESPACE