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.

5264 lines
189KB

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