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.

5261 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 "../engine/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. }
  2242. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  2243. {
  2244. value = ctrlEvent.value*127.0f/100.0f;
  2245. setVolume(value, false, false);
  2246. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2247. }
  2248. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  2249. {
  2250. float left, right;
  2251. value = ctrlEvent.value/0.5f - 1.0f;
  2252. if (value < 0.0f)
  2253. {
  2254. left = -1.0f;
  2255. right = (value*2.0f)+1.0f;
  2256. }
  2257. else if (value > 0.0f)
  2258. {
  2259. left = (value*2.0f)-1.0f;
  2260. right = 1.0f;
  2261. }
  2262. else
  2263. {
  2264. left = -1.0f;
  2265. right = 1.0f;
  2266. }
  2267. setBalanceLeft(left, false, false);
  2268. setBalanceRight(right, false, false);
  2269. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2270. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2271. }
  2272. }
  2273. #endif
  2274. // Control plugin parameters
  2275. for (k=0; k < kData->param.count; ++k)
  2276. {
  2277. if (kData->param.data[k].midiChannel != event.channel)
  2278. continue;
  2279. if (kData->param.data[k].midiCC != ctrlEvent.param)
  2280. continue;
  2281. if (kData->param.data[k].type != PARAMETER_INPUT)
  2282. continue;
  2283. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2284. continue;
  2285. float value;
  2286. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2287. {
  2288. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  2289. }
  2290. else
  2291. {
  2292. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  2293. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2294. value = std::rint(value);
  2295. }
  2296. setParameterValue(k, value, false, false, false);
  2297. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2298. }
  2299. break;
  2300. }
  2301. case kEngineControlEventTypeMidiBank:
  2302. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2303. nextBankId = ctrlEvent.param;
  2304. break;
  2305. case kEngineControlEventTypeMidiProgram:
  2306. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2307. {
  2308. const uint32_t nextProgramId = ctrlEvent.param;
  2309. for (k=0; k < kData->midiprog.count; ++k)
  2310. {
  2311. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  2312. {
  2313. setMidiProgram(k, false, false, false);
  2314. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  2315. break;
  2316. }
  2317. }
  2318. }
  2319. break;
  2320. case kEngineControlEventTypeAllSoundOff:
  2321. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2322. {
  2323. uint8_t midiData[3];
  2324. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2325. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2326. midiData[2] = 0;
  2327. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2328. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2329. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2330. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2331. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2332. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2333. }
  2334. break;
  2335. case kEngineControlEventTypeAllNotesOff:
  2336. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2337. {
  2338. if (event.channel == kData->ctrlChannel && ! allNotesOffSent)
  2339. {
  2340. allNotesOffSent = true;
  2341. sendMidiAllNotesOffToCallback();
  2342. }
  2343. uint8_t midiData[3];
  2344. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2345. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2346. midiData[2] = 0;
  2347. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2348. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2349. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2350. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2351. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2352. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2353. }
  2354. break;
  2355. }
  2356. break;
  2357. }
  2358. case kEngineEventTypeMidi:
  2359. {
  2360. const EngineMidiEvent& midiEvent(event.midi);
  2361. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  2362. uint8_t channel = event.channel;
  2363. uint32_t mtime = sampleAccurate ? startTime : time;
  2364. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2365. continue;
  2366. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2367. continue;
  2368. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2369. continue;
  2370. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2371. continue;
  2372. // Fix bad note-off
  2373. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  2374. status -= 0x10;
  2375. k = fEventsIn.ctrlIndex;
  2376. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2377. lv2_atom_buffer_write(&evInAtomIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2378. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2379. lv2_event_write(&evInEventIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2380. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2381. lv2midi_put_event(&evInMidiStates[k], mtime, midiEvent.size, midiEvent.data);
  2382. if (status == MIDI_STATUS_NOTE_ON)
  2383. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2384. else if (status == MIDI_STATUS_NOTE_OFF)
  2385. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2386. break;
  2387. }
  2388. }
  2389. }
  2390. kData->postRtEvents.trySplice();
  2391. if (frames > timeOffset)
  2392. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2393. } // End of Event Input and Processing
  2394. // --------------------------------------------------------------------------------------------------------
  2395. // Plugin processing (no events)
  2396. else
  2397. {
  2398. processSingle(inBuffer, outBuffer, frames, 0);
  2399. } // End of Plugin processing (no events)
  2400. CARLA_PROCESS_CONTINUE_CHECK;
  2401. // --------------------------------------------------------------------------------------------------------
  2402. // MIDI Output
  2403. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port != nullptr)
  2404. {
  2405. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2406. {
  2407. const LV2_Atom_Event* ev;
  2408. LV2_Atom_Buffer_Iterator iter;
  2409. uint8_t* data;
  2410. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2411. while (true)
  2412. {
  2413. data = nullptr;
  2414. ev = lv2_atom_buffer_get(&iter, &data);
  2415. if (ev == nullptr || data == nullptr)
  2416. break;
  2417. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2418. fEventsOut.ctrl->port->writeMidiEvent(ev->time.frames, data, ev->body.size);
  2419. lv2_atom_buffer_increment(&iter);
  2420. }
  2421. }
  2422. else if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2423. {
  2424. const LV2_Event* ev;
  2425. LV2_Event_Iterator iter;
  2426. uint8_t* data;
  2427. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2428. while (true)
  2429. {
  2430. data = nullptr;
  2431. ev = lv2_event_get(&iter, &data);
  2432. if (ev == nullptr || data == nullptr)
  2433. break;
  2434. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2435. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, data, ev->size);
  2436. lv2_event_increment(&iter);
  2437. }
  2438. }
  2439. else if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2440. {
  2441. LV2_MIDIState state = { fEventsOut.ctrl->midi, frames, 0 };
  2442. uint32_t eventSize;
  2443. double eventTime;
  2444. unsigned char* eventData;
  2445. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2446. {
  2447. if (eventData == nullptr || eventSize == 0)
  2448. break;
  2449. fEventsOut.ctrl->port->writeMidiEvent(eventTime, eventData, eventSize);
  2450. lv2midi_step(&state);
  2451. }
  2452. }
  2453. }
  2454. // --------------------------------------------------------------------------------------------------------
  2455. // Control Output
  2456. if (kData->event.portOut != nullptr)
  2457. {
  2458. uint8_t channel;
  2459. uint16_t param;
  2460. float value;
  2461. for (k=0; k < kData->param.count; ++k)
  2462. {
  2463. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  2464. continue;
  2465. if (kData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS)
  2466. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  2467. if (kData->param.data[k].midiCC > 0)
  2468. {
  2469. channel = kData->param.data[k].midiChannel;
  2470. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  2471. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  2472. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2473. }
  2474. }
  2475. } // End of Control Output
  2476. CARLA_PROCESS_CONTINUE_CHECK;
  2477. #if 0
  2478. // --------------------------------------------------------------------------------------------------------
  2479. // Final work
  2480. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2481. {
  2482. fExt.worker->end_run(fHandle);
  2483. if (fHandle2 != nullptr)
  2484. fExt.worker->end_run(fHandle2);
  2485. }
  2486. #endif
  2487. fFirstActive = false;
  2488. // --------------------------------------------------------------------------------------------------------
  2489. }
  2490. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2491. {
  2492. CARLA_ASSERT(frames > 0);
  2493. if (frames == 0)
  2494. return false;
  2495. if (kData->audioIn.count > 0)
  2496. {
  2497. CARLA_ASSERT(inBuffer != nullptr);
  2498. if (inBuffer == nullptr)
  2499. return false;
  2500. }
  2501. if (kData->audioOut.count > 0)
  2502. {
  2503. CARLA_ASSERT(outBuffer != nullptr);
  2504. if (outBuffer == nullptr)
  2505. return false;
  2506. }
  2507. uint32_t i, k;
  2508. // --------------------------------------------------------------------------------------------------------
  2509. // Try lock, silence otherwise
  2510. if (kData->engine->isOffline())
  2511. {
  2512. kData->singleMutex.lock();
  2513. }
  2514. else if (! kData->singleMutex.tryLock())
  2515. {
  2516. for (i=0; i < kData->audioOut.count; ++i)
  2517. {
  2518. for (k=0; k < frames; ++k)
  2519. outBuffer[i][k+timeOffset] = 0.0f;
  2520. }
  2521. return false;
  2522. }
  2523. // --------------------------------------------------------------------------------------------------------
  2524. // Reset audio buffers
  2525. for (i=0; i < kData->audioIn.count; ++i)
  2526. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2527. for (i=0; i < kData->audioOut.count; ++i)
  2528. carla_zeroFloat(fAudioOutBuffers[i], frames);
  2529. // --------------------------------------------------------------------------------------------------------
  2530. // Run plugin
  2531. fDescriptor->run(fHandle, frames);
  2532. if (fHandle2 != nullptr)
  2533. fDescriptor->run(fHandle2, frames);
  2534. // --------------------------------------------------------------------------------------------------------
  2535. // Special Parameters
  2536. for (k=0; k < kData->param.count; ++k)
  2537. {
  2538. if (kData->param.data[k].type != PARAMETER_INPUT)
  2539. continue;
  2540. if (kData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2541. {
  2542. if (fParamBuffers[k] != kData->param.ranges[k].def)
  2543. {
  2544. fParamBuffers[k] = kData->param.ranges[k].def;
  2545. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2546. }
  2547. }
  2548. }
  2549. kData->postRtEvents.trySplice();
  2550. #ifndef BUILD_BRIDGE
  2551. // --------------------------------------------------------------------------------------------------------
  2552. // Post-processing (dry/wet, volume and balance)
  2553. {
  2554. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  2555. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  2556. bool isPair;
  2557. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2558. for (i=0; i < kData->audioOut.count; ++i)
  2559. {
  2560. // Dry/Wet
  2561. if (doDryWet)
  2562. {
  2563. for (k=0; k < frames; ++k)
  2564. {
  2565. // TODO
  2566. //if (k < kData->latency && kData->latency < frames)
  2567. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  2568. //else
  2569. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2570. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  2571. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  2572. }
  2573. }
  2574. // Balance
  2575. if (doBalance)
  2576. {
  2577. isPair = (i % 2 == 0);
  2578. if (isPair)
  2579. {
  2580. CARLA_ASSERT(i+1 < kData->audioOut.count);
  2581. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  2582. }
  2583. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  2584. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  2585. for (k=0; k < frames; ++k)
  2586. {
  2587. if (isPair)
  2588. {
  2589. // left
  2590. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2591. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2592. }
  2593. else
  2594. {
  2595. // right
  2596. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2597. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2598. }
  2599. }
  2600. }
  2601. // Volume (and buffer copy)
  2602. {
  2603. for (k=0; k < frames; ++k)
  2604. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  2605. }
  2606. }
  2607. # if 0
  2608. // Latency, save values for next callback, TODO
  2609. if (kData->latency > 0 && kData->latency < frames)
  2610. {
  2611. for (i=0; i < kData->audioIn.count; ++i)
  2612. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  2613. }
  2614. # endif
  2615. } // End of Post-processing
  2616. #else
  2617. for (i=0; i < kData->audioOut.count; ++i)
  2618. {
  2619. for (k=0; k < frames; ++k)
  2620. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2621. }
  2622. #endif
  2623. // --------------------------------------------------------------------------------------------------------
  2624. kData->singleMutex.unlock();
  2625. return true;
  2626. }
  2627. void bufferSizeChanged(const uint32_t newBufferSize) override
  2628. {
  2629. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2630. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2631. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2632. {
  2633. if (fAudioInBuffers[i] != nullptr)
  2634. delete[] fAudioInBuffers[i];
  2635. fAudioInBuffers[i] = new float[newBufferSize];
  2636. }
  2637. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2638. {
  2639. if (fAudioOutBuffers[i] != nullptr)
  2640. delete[] fAudioOutBuffers[i];
  2641. fAudioOutBuffers[i] = new float[newBufferSize];
  2642. }
  2643. if (fHandle2 == nullptr)
  2644. {
  2645. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2646. {
  2647. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2648. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2649. }
  2650. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2651. {
  2652. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2653. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2654. }
  2655. }
  2656. else
  2657. {
  2658. if (kData->audioIn.count > 0)
  2659. {
  2660. CARLA_ASSERT(kData->audioIn.count == 2);
  2661. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2662. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2663. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2664. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2665. }
  2666. if (kData->audioOut.count > 0)
  2667. {
  2668. CARLA_ASSERT(kData->audioOut.count == 2);
  2669. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2670. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2671. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2672. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2673. }
  2674. }
  2675. if (fLv2Options.maxBufferSize != static_cast<int>(newBufferSize) || (fLv2Options.minBufferSize > 1 && fLv2Options.minBufferSize != static_cast<int>(newBufferSize)))
  2676. {
  2677. fLv2Options.maxBufferSize = newBufferSize;
  2678. if (fLv2Options.minBufferSize > 1)
  2679. fLv2Options.minBufferSize = newBufferSize;
  2680. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2681. {
  2682. fExt.options->set(fHandle, &fLv2Options.optMinBlockLenth);
  2683. fExt.options->set(fHandle, &fLv2Options.optMaxBlockLenth);
  2684. }
  2685. }
  2686. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2687. }
  2688. void sampleRateChanged(const double newSampleRate) override
  2689. {
  2690. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2691. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2692. if (fLv2Options.sampleRate != newSampleRate)
  2693. {
  2694. fLv2Options.sampleRate = newSampleRate;
  2695. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2696. fExt.options->set(fHandle, &fLv2Options.optSampleRate);
  2697. }
  2698. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2699. }
  2700. // -------------------------------------------------------------------
  2701. // Plugin buffers
  2702. void initBuffers() override
  2703. {
  2704. fEventsIn.initBuffers(kData->engine);
  2705. fEventsOut.initBuffers(kData->engine);
  2706. CarlaPlugin::initBuffers();
  2707. }
  2708. void clearBuffers() override
  2709. {
  2710. carla_debug("Lv2Plugin::clearBuffers() - start");
  2711. if (fAudioInBuffers != nullptr)
  2712. {
  2713. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2714. {
  2715. if (fAudioInBuffers[i] != nullptr)
  2716. {
  2717. delete[] fAudioInBuffers[i];
  2718. fAudioInBuffers[i] = nullptr;
  2719. }
  2720. }
  2721. delete[] fAudioInBuffers;
  2722. fAudioInBuffers = nullptr;
  2723. }
  2724. if (fAudioOutBuffers != nullptr)
  2725. {
  2726. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2727. {
  2728. if (fAudioOutBuffers[i] != nullptr)
  2729. {
  2730. delete[] fAudioOutBuffers[i];
  2731. fAudioOutBuffers[i] = nullptr;
  2732. }
  2733. }
  2734. delete[] fAudioOutBuffers;
  2735. fAudioOutBuffers = nullptr;
  2736. }
  2737. if (fParamBuffers != nullptr)
  2738. {
  2739. delete[] fParamBuffers;
  2740. fParamBuffers = nullptr;
  2741. }
  2742. fEventsIn.clear();
  2743. fEventsOut.clear();
  2744. CarlaPlugin::clearBuffers();
  2745. carla_debug("Lv2Plugin::clearBuffers() - end");
  2746. }
  2747. // -------------------------------------------------------------------
  2748. // Post-poned UI Stuff
  2749. void uiParameterChange(const uint32_t index, const float value) override
  2750. {
  2751. CARLA_ASSERT(fDescriptor != nullptr);
  2752. CARLA_ASSERT(fHandle != nullptr);
  2753. CARLA_ASSERT(index < kData->param.count);
  2754. if (fDescriptor == nullptr || fHandle == nullptr)
  2755. return;
  2756. if (index >= kData->param.count)
  2757. return;
  2758. if (fUi.type == PLUGIN_UI_OSC)
  2759. {
  2760. if (kData->osc.data.target != nullptr)
  2761. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  2762. }
  2763. else
  2764. {
  2765. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2766. fUi.descriptor->port_event(fUi.handle, kData->param.data[index].rindex, sizeof(float), 0, &value);
  2767. }
  2768. }
  2769. void uiMidiProgramChange(const uint32_t index) override
  2770. {
  2771. CARLA_ASSERT(index < kData->midiprog.count);
  2772. if (index >= kData->midiprog.count)
  2773. return;
  2774. if (fUi.type == PLUGIN_UI_OSC)
  2775. {
  2776. if (kData->osc.data.target != nullptr)
  2777. osc_send_midi_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2778. }
  2779. else
  2780. {
  2781. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2782. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2783. }
  2784. }
  2785. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  2786. {
  2787. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2788. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2789. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  2790. if (channel >= MAX_MIDI_CHANNELS)
  2791. return;
  2792. if (note >= MAX_MIDI_NOTE)
  2793. return;
  2794. if (velo >= MAX_MIDI_VALUE)
  2795. return;
  2796. if (fUi.type == PLUGIN_UI_OSC)
  2797. {
  2798. if (kData->osc.data.target != nullptr)
  2799. {
  2800. uint8_t midiData[4] = { 0 };
  2801. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2802. midiData[2] = note;
  2803. midiData[3] = velo;
  2804. osc_send_midi(&kData->osc.data, midiData);
  2805. }
  2806. }
  2807. else
  2808. {
  2809. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2810. {
  2811. LV2_Atom_MidiEvent midiEv;
  2812. midiEv.event.time.frames = 0;
  2813. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2814. midiEv.event.body.size = 3;
  2815. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2816. midiEv.data[1] = note;
  2817. midiEv.data[2] = velo;
  2818. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2819. }
  2820. }
  2821. }
  2822. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  2823. {
  2824. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2825. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2826. if (channel >= MAX_MIDI_CHANNELS)
  2827. return;
  2828. if (note >= MAX_MIDI_NOTE)
  2829. return;
  2830. if (fUi.type == PLUGIN_UI_OSC)
  2831. {
  2832. if (kData->osc.data.target != nullptr)
  2833. {
  2834. uint8_t midiData[4] = { 0 };
  2835. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2836. midiData[2] = note;
  2837. osc_send_midi(&kData->osc.data, midiData);
  2838. }
  2839. }
  2840. else
  2841. {
  2842. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2843. {
  2844. LV2_Atom_MidiEvent midiEv;
  2845. midiEv.event.time.frames = 0;
  2846. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2847. midiEv.event.body.size = 3;
  2848. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2849. midiEv.data[1] = note;
  2850. midiEv.data[2] = 0;
  2851. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2852. }
  2853. }
  2854. }
  2855. // -------------------------------------------------------------------
  2856. // Helpers
  2857. CarlaEngineEventPort* getDefaultEventInPort() const override
  2858. {
  2859. return (fEventsIn.ctrl != nullptr) ? fEventsIn.ctrl->port : nullptr;
  2860. }
  2861. // -------------------------------------------------------------------
  2862. protected:
  2863. void guiClosedCallback() override
  2864. {
  2865. showGui(false);
  2866. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  2867. }
  2868. // -------------------------------------------------------------------
  2869. LV2_URID getCustomURID(const char* const uri)
  2870. {
  2871. CARLA_ASSERT(uri != nullptr);
  2872. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  2873. if (uri == nullptr)
  2874. return CARLA_URI_MAP_ID_NULL;
  2875. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  2876. {
  2877. const char*& thisUri(fCustomURIDs.getAt(i));
  2878. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  2879. return i;
  2880. }
  2881. fCustomURIDs.append(carla_strdup(uri));
  2882. return fCustomURIDs.count()-1;
  2883. }
  2884. const char* getCustomURIString(const LV2_URID urid)
  2885. {
  2886. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  2887. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  2888. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  2889. if (urid == CARLA_URI_MAP_ID_NULL)
  2890. return nullptr;
  2891. if (urid < fCustomURIDs.count())
  2892. return fCustomURIDs.getAt(urid);
  2893. return nullptr;
  2894. }
  2895. // -------------------------------------------------------------------
  2896. void handleProgramChanged(const int32_t index)
  2897. {
  2898. CARLA_ASSERT_INT(index >= -1, index);
  2899. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  2900. if (index == -1)
  2901. {
  2902. const CarlaPlugin::ScopedDisabler m(this);
  2903. return reloadPrograms(false);
  2904. }
  2905. if (index >= 0 && index < static_cast<int32_t>(kData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  2906. {
  2907. if (const LV2_Program_Descriptor* progDesc = fExt.programs->get_program(fHandle, index))
  2908. {
  2909. CARLA_ASSERT(progDesc->name != nullptr);
  2910. if (kData->midiprog.data[index].name != nullptr)
  2911. delete[] kData->midiprog.data[index].name;
  2912. kData->midiprog.data[index].name = carla_strdup(progDesc->name ? progDesc->name : "");
  2913. if (index == kData->midiprog.current)
  2914. kData->engine->callback(CALLBACK_UPDATE, fId, 0, 0, 0.0, nullptr);
  2915. else
  2916. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0, nullptr);
  2917. }
  2918. }
  2919. }
  2920. // -------------------------------------------------------------------
  2921. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2922. {
  2923. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2924. CARLA_ASSERT(value != nullptr);
  2925. CARLA_ASSERT(size > 0);
  2926. carla_debug("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i)", key, value, size, type, flags);
  2927. // basic checks
  2928. if (key == CARLA_URI_MAP_ID_NULL)
  2929. {
  2930. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2931. return LV2_STATE_ERR_NO_PROPERTY;
  2932. }
  2933. if (value == nullptr || size == 0)
  2934. {
  2935. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid value", key, value, size, type, flags);
  2936. return LV2_STATE_ERR_NO_PROPERTY;
  2937. }
  2938. if ((flags & LV2_STATE_IS_POD) == 0)
  2939. {
  2940. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2941. return LV2_STATE_ERR_BAD_FLAGS;
  2942. }
  2943. const char* const stype(carla_lv2_urid_unmap(this, type));
  2944. if (stype == nullptr)
  2945. {
  2946. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type", key, value, size, type, flags);
  2947. return LV2_STATE_ERR_BAD_TYPE;
  2948. }
  2949. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2950. if (uriKey == nullptr)
  2951. {
  2952. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key URI", key, value, size, type, flags);
  2953. return LV2_STATE_ERR_NO_PROPERTY;
  2954. }
  2955. // Check if we already have this key
  2956. for (auto it = kData->custom.begin(); it.valid(); it.next())
  2957. {
  2958. CustomData& data(*it);
  2959. if (std::strcmp(data.key, uriKey) == 0)
  2960. {
  2961. if (data.value != nullptr)
  2962. delete[] data.value;
  2963. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2964. data.value = carla_strdup((const char*)value);
  2965. else
  2966. data.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2967. return LV2_STATE_SUCCESS;
  2968. }
  2969. }
  2970. // Otherwise store it
  2971. CustomData newData;
  2972. newData.type = carla_strdup(stype);
  2973. newData.key = carla_strdup(uriKey);
  2974. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2975. newData.value = carla_strdup((const char*)value);
  2976. else
  2977. newData.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2978. kData->custom.append(newData);
  2979. return LV2_STATE_SUCCESS;
  2980. }
  2981. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2982. {
  2983. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2984. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  2985. // basic checks
  2986. if (key == CARLA_URI_MAP_ID_NULL)
  2987. {
  2988. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key", key, size, type, flags);
  2989. return nullptr;
  2990. }
  2991. if (size == nullptr || type == nullptr || flags == nullptr)
  2992. {
  2993. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid data", key, size, type, flags);
  2994. return nullptr;
  2995. }
  2996. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2997. if (uriKey == nullptr)
  2998. {
  2999. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  3000. return nullptr;
  3001. }
  3002. const char* stype = nullptr;
  3003. const char* stringData = nullptr;
  3004. for (auto it = kData->custom.begin(); it.valid(); it.next())
  3005. {
  3006. CustomData& data(*it);
  3007. if (std::strcmp(data.key, uriKey) == 0)
  3008. {
  3009. stype = data.type;
  3010. stringData = data.value;
  3011. break;
  3012. }
  3013. }
  3014. if (stringData == nullptr)
  3015. {
  3016. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  3017. return nullptr;
  3018. }
  3019. *type = carla_lv2_urid_map(this, stype);
  3020. *flags = LV2_STATE_IS_POD;
  3021. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3022. {
  3023. *size = std::strlen(stringData);
  3024. return stringData;
  3025. }
  3026. else
  3027. {
  3028. static QByteArray chunk;
  3029. chunk = QByteArray::fromBase64(stringData);
  3030. *size = chunk.size();
  3031. return chunk.constData();
  3032. }
  3033. }
  3034. // -------------------------------------------------------------------
  3035. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3036. {
  3037. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3038. if (fExt.worker == nullptr || fExt.worker->work == nullptr)
  3039. {
  3040. carla_stderr("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  3041. return LV2_WORKER_ERR_UNKNOWN;
  3042. }
  3043. //if (kData->engine->isOffline())
  3044. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3045. //else
  3046. // postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  3047. return LV2_WORKER_SUCCESS;
  3048. }
  3049. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3050. {
  3051. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3052. #if 0
  3053. LV2_Atom_Worker workerAtom;
  3054. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3055. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  3056. workerAtom.body.size = size;
  3057. workerAtom.body.data = data;
  3058. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  3059. #endif
  3060. return LV2_WORKER_SUCCESS;
  3061. }
  3062. // -------------------------------------------------------------------
  3063. void handleExternalUiClosed()
  3064. {
  3065. CARLA_ASSERT(fUi.type == PLUGIN_UI_EXTERNAL);
  3066. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3067. fUi.descriptor->cleanup(fUi.handle);
  3068. fUi.handle = nullptr;
  3069. fUi.widget = nullptr;
  3070. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  3071. }
  3072. uint32_t handleUiPortMap(const char* const symbol)
  3073. {
  3074. CARLA_ASSERT(symbol != nullptr);
  3075. if (symbol == nullptr)
  3076. return LV2UI_INVALID_PORT_INDEX;
  3077. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3078. {
  3079. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3080. return i;
  3081. }
  3082. return LV2UI_INVALID_PORT_INDEX;
  3083. }
  3084. int handleUiResize(const int width, const int height)
  3085. {
  3086. CARLA_ASSERT(kData->gui != nullptr);
  3087. CARLA_ASSERT(width > 0);
  3088. CARLA_ASSERT(height > 0);
  3089. if (width <= 0 || height <= 0)
  3090. return 1;
  3091. if (kData->gui != nullptr)
  3092. kData->gui->setSize(width, height);
  3093. return 0;
  3094. }
  3095. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3096. {
  3097. if (format == 0)
  3098. {
  3099. CARLA_ASSERT(buffer != nullptr);
  3100. CARLA_ASSERT(bufferSize == sizeof(float));
  3101. if (buffer == nullptr || bufferSize != sizeof(float))
  3102. return;
  3103. float value = *(float*)buffer;
  3104. for (uint32_t i=0; i < kData->param.count; ++i)
  3105. {
  3106. if (kData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3107. {
  3108. if (fParamBuffers[i] != value)
  3109. setParameterValue(i, value, false, true, true);
  3110. break;
  3111. }
  3112. }
  3113. }
  3114. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3115. {
  3116. CARLA_ASSERT(buffer != nullptr);
  3117. if (buffer == nullptr)
  3118. return;
  3119. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  3120. fAtomQueueIn.put(rindex, atom);
  3121. }
  3122. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3123. {
  3124. CARLA_ASSERT(buffer != nullptr);
  3125. if (buffer == nullptr)
  3126. return;
  3127. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  3128. fAtomQueueIn.put(rindex, atom);
  3129. }
  3130. }
  3131. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, uint32_t size, uint32_t type)
  3132. {
  3133. CARLA_ASSERT(portSymbol != nullptr);
  3134. CARLA_ASSERT(value != nullptr);
  3135. CARLA_ASSERT(size > 0);
  3136. CARLA_ASSERT(type != CARLA_URI_MAP_ID_NULL);
  3137. if (portSymbol == nullptr)
  3138. return;
  3139. if (value == nullptr)
  3140. return;
  3141. if (size == 0)
  3142. return;
  3143. if (type == CARLA_URI_MAP_ID_NULL)
  3144. return;
  3145. const int32_t rindex(handleUiPortMap(portSymbol));
  3146. if (rindex < 0)
  3147. return;
  3148. if (size == sizeof(float) && type == CARLA_URI_MAP_ID_ATOM_FLOAT)
  3149. {
  3150. const float valuef(*(const float*)value);
  3151. for (uint32_t i=0; i < kData->param.count; ++i)
  3152. {
  3153. if (kData->param.data[i].rindex == rindex)
  3154. {
  3155. setParameterValue(i, valuef, true, true, true);
  3156. break;
  3157. }
  3158. }
  3159. }
  3160. }
  3161. // -------------------------------------------------------------------
  3162. bool isRealtimeSafe() const
  3163. {
  3164. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3165. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3166. {
  3167. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3168. return true;
  3169. }
  3170. return false;
  3171. }
  3172. bool needsFixedBuffer() const
  3173. {
  3174. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3175. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3176. {
  3177. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3178. return true;
  3179. }
  3180. return false;
  3181. }
  3182. // -------------------------------------------------------------------
  3183. const char* getUiBridgePath(const LV2_Property type) const
  3184. {
  3185. const EngineOptions& options(kData->engine->getOptions());
  3186. switch (type)
  3187. {
  3188. case LV2_UI_GTK2:
  3189. return options.bridge_lv2Gtk2;
  3190. case LV2_UI_GTK3:
  3191. return options.bridge_lv2Gtk3;
  3192. case LV2_UI_QT4:
  3193. return options.bridge_lv2Qt4;
  3194. case LV2_UI_QT5:
  3195. return options.bridge_lv2Qt5;
  3196. case LV2_UI_COCOA:
  3197. return options.bridge_lv2Cocoa;
  3198. case LV2_UI_WINDOWS:
  3199. return options.bridge_lv2Win;
  3200. case LV2_UI_X11:
  3201. return options.bridge_lv2X11;
  3202. default:
  3203. return nullptr;
  3204. }
  3205. }
  3206. bool isUiBridgeable(const uint32_t uiId) const
  3207. {
  3208. const LV2_RDF_UI& rdfUi(fRdfDescriptor->UIs[uiId]);
  3209. for (uint32_t i=0; i < rdfUi.FeatureCount; ++i)
  3210. {
  3211. if (std::strcmp(rdfUi.Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3212. return false;
  3213. if (std::strcmp(rdfUi.Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3214. return false;
  3215. }
  3216. return true;
  3217. }
  3218. bool isUiResizable() const
  3219. {
  3220. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3221. {
  3222. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3223. return false;
  3224. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3225. return false;
  3226. }
  3227. return true;
  3228. }
  3229. void updateUi()
  3230. {
  3231. CARLA_ASSERT(fUi.handle != nullptr);
  3232. CARLA_ASSERT(fUi.descriptor != nullptr);
  3233. fExt.uiidle = nullptr;
  3234. fExt.uiprograms = nullptr;
  3235. if (fUi.descriptor->extension_data != nullptr)
  3236. {
  3237. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3238. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3239. // check if invalid
  3240. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3241. fExt.uiidle = nullptr;
  3242. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3243. fExt.uiprograms = nullptr;
  3244. // update midi program
  3245. if (fExt.uiprograms && kData->midiprog.count > 0 && kData->midiprog.current >= 0)
  3246. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[kData->midiprog.current].bank,
  3247. kData->midiprog.data[kData->midiprog.current].program);
  3248. }
  3249. if (fUi.descriptor->port_event != nullptr)
  3250. {
  3251. // update control ports
  3252. float value;
  3253. for (uint32_t i=0; i < kData->param.count; ++i)
  3254. {
  3255. value = getParameterValue(i);
  3256. fUi.descriptor->port_event(fUi.handle, kData->param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3257. }
  3258. }
  3259. }
  3260. // -------------------------------------------------------------------
  3261. public:
  3262. bool init(const char* const name, const char* const uri)
  3263. {
  3264. CARLA_ASSERT(kData->engine != nullptr);
  3265. CARLA_ASSERT(kData->client == nullptr);
  3266. CARLA_ASSERT(uri != nullptr);
  3267. // ---------------------------------------------------------------
  3268. // first checks
  3269. if (kData->engine == nullptr)
  3270. {
  3271. return false;
  3272. }
  3273. if (kData->client != nullptr)
  3274. {
  3275. kData->engine->setLastError("Plugin client is already registered");
  3276. return false;
  3277. }
  3278. if (uri == nullptr)
  3279. {
  3280. kData->engine->setLastError("null uri");
  3281. return false;
  3282. }
  3283. // ---------------------------------------------------------------
  3284. // get plugin from lv2_rdf (lilv)
  3285. gLv2World.init();
  3286. fRdfDescriptor = lv2_rdf_new(uri);
  3287. if (fRdfDescriptor == nullptr)
  3288. {
  3289. kData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3290. return false;
  3291. }
  3292. // ---------------------------------------------------------------
  3293. // open DLL
  3294. if (! kData->libOpen(fRdfDescriptor->Binary))
  3295. {
  3296. kData->engine->setLastError(kData->libError(fRdfDescriptor->Binary));
  3297. return false;
  3298. }
  3299. // ---------------------------------------------------------------
  3300. // initialize options
  3301. fLv2Options.minBufferSize = 1;
  3302. fLv2Options.maxBufferSize = kData->engine->getBufferSize();
  3303. fLv2Options.sampleRate = kData->engine->getSampleRate();
  3304. // ---------------------------------------------------------------
  3305. // initialize features (part 1)
  3306. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3307. eventFt->callback_data = this;
  3308. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3309. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3310. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3311. logFt->handle = this;
  3312. logFt->printf = carla_lv2_log_printf;
  3313. logFt->vprintf = carla_lv2_log_vprintf;
  3314. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3315. stateMakePathFt->handle = this;
  3316. stateMakePathFt->path = carla_lv2_state_make_path;
  3317. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3318. stateMapPathFt->handle = this;
  3319. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3320. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3321. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3322. programsFt->handle = this;
  3323. programsFt->program_changed = carla_lv2_program_changed;
  3324. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3325. lv2_rtmempool_init(rtMemPoolFt);
  3326. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3327. uriMapFt->callback_data = this;
  3328. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3329. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3330. uridMapFt->handle = this;
  3331. uridMapFt->map = carla_lv2_urid_map;
  3332. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3333. uridUnmapFt->handle = this;
  3334. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3335. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3336. workerFt->handle = this;
  3337. workerFt->schedule_work = carla_lv2_worker_schedule;
  3338. // ---------------------------------------------------------------
  3339. // initialize features (part 2)
  3340. fFeatures[kFeatureIdBufSizeBounded] = new LV2_Feature;
  3341. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3342. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3343. fFeatures[kFeatureIdBufSizeFixed] = new LV2_Feature;
  3344. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3345. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3346. fFeatures[kFeatureIdBufSizePowerOf2] = new LV2_Feature;
  3347. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3348. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3349. fFeatures[kFeatureIdEvent] = new LV2_Feature;
  3350. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3351. fFeatures[kFeatureIdEvent]->data = eventFt;
  3352. fFeatures[kFeatureIdLogs] = new LV2_Feature;
  3353. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3354. fFeatures[kFeatureIdLogs]->data = logFt;
  3355. fFeatures[kFeatureIdOptions] = new LV2_Feature;
  3356. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3357. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3358. fFeatures[kFeatureIdPrograms] = new LV2_Feature;
  3359. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3360. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3361. fFeatures[kFeatureIdRtMemPool] = new LV2_Feature;
  3362. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3363. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3364. fFeatures[kFeatureIdStateMakePath] = new LV2_Feature;
  3365. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3366. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3367. fFeatures[kFeatureIdStateMapPath] = new LV2_Feature;
  3368. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3369. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3370. fFeatures[kFeatureIdStrictBounds] = new LV2_Feature;
  3371. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3372. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3373. fFeatures[kFeatureIdUriMap] = new LV2_Feature;
  3374. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3375. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3376. fFeatures[kFeatureIdUridMap] = new LV2_Feature;
  3377. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3378. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3379. fFeatures[kFeatureIdUridUnmap] = new LV2_Feature;
  3380. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3381. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3382. fFeatures[kFeatureIdWorker] = new LV2_Feature;
  3383. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3384. fFeatures[kFeatureIdWorker]->data = workerFt;
  3385. if (! needsFixedBuffer())
  3386. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3387. // ---------------------------------------------------------------
  3388. // get DLL main entry
  3389. #if 0
  3390. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)kData->libSymbol("lv2_lib_descriptor");
  3391. if (libDescFn != nullptr)
  3392. {
  3393. // -----------------------------------------------------------
  3394. // get lib descriptor
  3395. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  3396. if (libFn == nullptr || libFn->get_plugin == nullptr)
  3397. {
  3398. kData->engine->setLastError("Plugin failed to return library descriptor");
  3399. return false;
  3400. }
  3401. // -----------------------------------------------------------
  3402. // get descriptor that matches URI
  3403. uint32_t i = 0;
  3404. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  3405. {
  3406. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3407. break;
  3408. }
  3409. if (fDescriptor == nullptr)
  3410. libFn->cleanup(libFn->handle);
  3411. else
  3412. #endif
  3413. {
  3414. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)kData->libSymbol("lv2_descriptor");
  3415. if (descFn == nullptr)
  3416. {
  3417. kData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3418. return false;
  3419. }
  3420. // -----------------------------------------------------------
  3421. // get descriptor that matches URI
  3422. uint32_t i = 0;
  3423. while ((fDescriptor = descFn(i++)))
  3424. {
  3425. carla_debug("LV2 Init @%i => '%s' vs '%s'", i, fDescriptor->URI, uri);
  3426. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3427. break;
  3428. }
  3429. }
  3430. if (fDescriptor == nullptr)
  3431. {
  3432. kData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3433. return false;
  3434. }
  3435. // ---------------------------------------------------------------
  3436. // check supported port-types and features
  3437. bool canContinue = true;
  3438. // Check supported ports
  3439. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3440. {
  3441. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3442. 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)))
  3443. {
  3444. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  3445. {
  3446. kData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3447. canContinue = false;
  3448. break;
  3449. }
  3450. }
  3451. }
  3452. // Check supported features
  3453. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  3454. {
  3455. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  3456. {
  3457. QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  3458. kData->engine->setLastError(msg.toUtf8().constData());
  3459. canContinue = false;
  3460. break;
  3461. }
  3462. }
  3463. // Check extensions
  3464. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3465. {
  3466. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3467. kData->extraHints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3468. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3469. kData->extraHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3470. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3471. kData->extraHints |= PLUGIN_HAS_EXTENSION_STATE;
  3472. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3473. kData->extraHints |= PLUGIN_HAS_EXTENSION_WORKER;
  3474. else
  3475. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3476. }
  3477. if (! canContinue)
  3478. {
  3479. // error already set
  3480. return false;
  3481. }
  3482. // ---------------------------------------------------------------
  3483. // get info
  3484. if (name != nullptr)
  3485. fName = kData->engine->getUniquePluginName(name);
  3486. else
  3487. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3488. // ---------------------------------------------------------------
  3489. // register client
  3490. kData->client = kData->engine->addClient(this);
  3491. if (kData->client == nullptr || ! kData->client->isOk())
  3492. {
  3493. kData->engine->setLastError("Failed to register plugin client");
  3494. return false;
  3495. }
  3496. // ---------------------------------------------------------------
  3497. // initialize plugin
  3498. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3499. if (fHandle == nullptr)
  3500. {
  3501. kData->engine->setLastError("Plugin failed to initialize");
  3502. return false;
  3503. }
  3504. // ---------------------------------------------------------------
  3505. // load plugin settings
  3506. {
  3507. // set default options
  3508. fOptions = 0x0;
  3509. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3510. if (midiInCount() > 0 || needsFixedBuffer())
  3511. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3512. if (kData->engine->getOptions().forceStereo)
  3513. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  3514. if (midiInCount() > 0)
  3515. {
  3516. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3517. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3518. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  3519. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3520. }
  3521. // load settings
  3522. kData->idStr = "LV2/";
  3523. kData->idStr += uri;
  3524. fOptions = kData->loadSettings(fOptions, availableOptions());
  3525. // ignore settings, we need this anyway
  3526. if (midiInCount() > 0 || needsFixedBuffer())
  3527. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3528. }
  3529. // ---------------------------------------------------------------
  3530. // gui stuff
  3531. if (fRdfDescriptor->UICount == 0)
  3532. return true;
  3533. // -----------------------------------------------------------
  3534. // find more appropriate ui
  3535. int eQt4, eQt5, eCocoa, eWindows, eX11, eGtk2, eGtk3, iCocoa, iWindows, iX11, iQt4, iQt5, iExt, iFinal;
  3536. eQt4 = eQt5 = eCocoa = eWindows = eX11 = eGtk2 = eGtk3 = iQt4 = iQt5 = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3537. #ifdef BUILD_BRIDGE
  3538. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges);
  3539. #else
  3540. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges && (fHints & PLUGIN_IS_BRIDGE) == 0);
  3541. #endif
  3542. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3543. {
  3544. CARLA_ASSERT(fRdfDescriptor->UIs[i].URI != nullptr);
  3545. if (fRdfDescriptor->UIs[i].URI == nullptr)
  3546. {
  3547. carla_stderr("Plugin has an UI without a valid URI");
  3548. continue;
  3549. }
  3550. switch (fRdfDescriptor->UIs[i].Type)
  3551. {
  3552. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3553. case LV2_UI_QT4:
  3554. if (isUiBridgeable(i))
  3555. eQt4 = i;
  3556. break;
  3557. #else
  3558. case LV2_UI_QT4:
  3559. if (isUiBridgeable(i) && preferUiBridges)
  3560. eQt4 = i;
  3561. iQt4 = i;
  3562. break;
  3563. #endif
  3564. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3565. case LV2_UI_QT5:
  3566. if (isUiBridgeable(i) && preferUiBridges)
  3567. eQt5 = i;
  3568. iQt5 = i;
  3569. break;
  3570. #else
  3571. case LV2_UI_QT5:
  3572. if (isUiBridgeable(i) && preferUiBridges)
  3573. eQt5 = i;
  3574. break;
  3575. #endif
  3576. #ifdef CARLA_OS_MAC
  3577. case LV2_UI_COCOA:
  3578. if (isUiBridgeable(i) && preferUiBridges)
  3579. eCocoa = i;
  3580. iCocoa = i;
  3581. break;
  3582. #endif
  3583. #ifdef CARLA_OS_WIN
  3584. case LV2_UI_WINDOWS:
  3585. if (isUiBridgeable(i) && preferUiBridges)
  3586. eWindows = i;
  3587. iWindows = i;
  3588. break;
  3589. #endif
  3590. case LV2_UI_X11:
  3591. if (isUiBridgeable(i) && preferUiBridges)
  3592. eX11 = i;
  3593. #ifdef Q_WS_X11
  3594. iX11 = i;
  3595. #endif
  3596. break;
  3597. case LV2_UI_GTK2:
  3598. if (isUiBridgeable(i))
  3599. eGtk2 = i;
  3600. break;
  3601. case LV2_UI_GTK3:
  3602. if (isUiBridgeable(i))
  3603. eGtk3 = i;
  3604. break;
  3605. case LV2_UI_EXTERNAL:
  3606. case LV2_UI_OLD_EXTERNAL:
  3607. iExt = i;
  3608. break;
  3609. default:
  3610. break;
  3611. }
  3612. }
  3613. if (eQt4 >= 0)
  3614. iFinal = eQt4;
  3615. else if (eQt5 >= 0)
  3616. iFinal = eQt5;
  3617. else if (eCocoa >= 0)
  3618. iFinal = eCocoa;
  3619. else if (eWindows >= 0)
  3620. iFinal = eWindows;
  3621. else if (eX11 >= 0)
  3622. iFinal = eX11;
  3623. else if (iQt4 >= 0)
  3624. iFinal = iQt4;
  3625. else if (iQt5 >= 0)
  3626. iFinal = iQt5;
  3627. else if (iCocoa >= 0)
  3628. iFinal = iCocoa;
  3629. else if (iWindows >= 0)
  3630. iFinal = iWindows;
  3631. else if (iX11 >= 0)
  3632. iFinal = iX11;
  3633. else if (iExt >= 0)
  3634. iFinal = iExt;
  3635. else if (eGtk2 >= 0)
  3636. iFinal = eGtk2;
  3637. else if (eGtk3 >= 0)
  3638. iFinal = eGtk3;
  3639. if (iFinal < 0)
  3640. {
  3641. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3642. return true;
  3643. }
  3644. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3645. // -----------------------------------------------------------
  3646. // check supported ui features
  3647. canContinue = true;
  3648. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3649. {
  3650. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type) && ! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3651. {
  3652. carla_stderr2("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3653. canContinue = false;
  3654. break;
  3655. }
  3656. }
  3657. if (! canContinue)
  3658. {
  3659. fUi.rdfDescriptor = nullptr;
  3660. return true;
  3661. }
  3662. // -----------------------------------------------------------
  3663. // initialize ui according to type
  3664. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3665. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3)
  3666. {
  3667. // -------------------------------------------------------
  3668. // initialize ui bridge
  3669. if (const char* const oscBinary = getUiBridgePath(uiType))
  3670. {
  3671. fUi.type = PLUGIN_UI_OSC;
  3672. kData->osc.thread.setOscData(oscBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3673. }
  3674. }
  3675. else
  3676. {
  3677. // -------------------------------------------------------
  3678. // open UI DLL
  3679. if (! kData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3680. {
  3681. carla_stderr2("Could not load UI library, error was:\n%s", kData->libError(fUi.rdfDescriptor->Binary));
  3682. fUi.rdfDescriptor = nullptr;
  3683. return true;
  3684. }
  3685. // -------------------------------------------------------
  3686. // get UI DLL main entry
  3687. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)kData->uiLibSymbol("lv2ui_descriptor");
  3688. if (uiDescFn == nullptr)
  3689. {
  3690. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3691. kData->uiLibClose();
  3692. fUi.rdfDescriptor = nullptr;
  3693. return true;
  3694. }
  3695. // -------------------------------------------------------
  3696. // get UI descriptor that matches UI URI
  3697. uint32_t i = 0;
  3698. while ((fUi.descriptor = uiDescFn(i++)))
  3699. {
  3700. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3701. break;
  3702. }
  3703. if (fUi.descriptor == nullptr)
  3704. {
  3705. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3706. kData->uiLibClose();
  3707. fUi.rdfDescriptor = nullptr;
  3708. return true;
  3709. }
  3710. // -------------------------------------------------------
  3711. // check if ui is usable
  3712. switch (uiType)
  3713. {
  3714. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3715. case LV2_UI_QT5:
  3716. carla_debug("Will use LV2 Qt5 UI");
  3717. fUi.type = PLUGIN_UI_QT;
  3718. break;
  3719. #else
  3720. case LV2_UI_QT4:
  3721. carla_debug("Will use LV2 Qt4 UI");
  3722. fUi.type = PLUGIN_UI_QT;
  3723. break;
  3724. #endif
  3725. #ifdef CARLA_OS_MAC
  3726. case LV2_UI_COCOA:
  3727. carla_debug("Will use LV2 Cocoa UI");
  3728. fUi.type = PLUGIN_UI_PARENT;
  3729. break;
  3730. #endif
  3731. #ifdef CARLA_OS_WIN
  3732. case LV2_UI_WINDOWS:
  3733. carla_debug("Will use LV2 Windows UI");
  3734. fUi.type = PLUGIN_UI_PARENT;
  3735. break;
  3736. #endif
  3737. #ifdef Q_WS_X11
  3738. case LV2_UI_X11:
  3739. carla_debug("Will use LV2 X11 UI");
  3740. fUi.type = PLUGIN_UI_PARENT;
  3741. break;
  3742. #endif
  3743. case LV2_UI_GTK2:
  3744. carla_debug("Will use LV2 Gtk2 UI, NOT!");
  3745. break;
  3746. case LV2_UI_GTK3:
  3747. carla_debug("Will use LV2 Gtk3 UI, NOT!");
  3748. break;
  3749. case LV2_UI_EXTERNAL:
  3750. case LV2_UI_OLD_EXTERNAL:
  3751. carla_debug("Will use LV2 External UI");
  3752. fUi.type = PLUGIN_UI_EXTERNAL;
  3753. break;
  3754. }
  3755. if (fUi.type == PLUGIN_UI_NULL)
  3756. {
  3757. kData->uiLibClose();
  3758. fUi.descriptor = nullptr;
  3759. fUi.rdfDescriptor = nullptr;
  3760. return true;
  3761. }
  3762. // -------------------------------------------------------
  3763. // initialize ui features
  3764. QString guiTitle(QString("%1 (GUI)").arg((const char*)fName));
  3765. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3766. uiDataFt->data_access = fDescriptor->extension_data;
  3767. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3768. uiPortMapFt->handle = this;
  3769. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3770. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3771. uiResizeFt->handle = this;
  3772. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3773. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3774. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3775. uiExternalHostFt->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  3776. fFeatures[kFeatureIdUiDataAccess] = new LV2_Feature;
  3777. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3778. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3779. fFeatures[kFeatureIdUiInstanceAccess] = new LV2_Feature;
  3780. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3781. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3782. fFeatures[kFeatureIdUiParent] = new LV2_Feature;
  3783. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3784. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3785. fFeatures[kFeatureIdUiPortMap] = new LV2_Feature;
  3786. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3787. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3788. fFeatures[kFeatureIdUiResize] = new LV2_Feature;
  3789. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3790. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3791. fFeatures[kFeatureIdExternalUi] = new LV2_Feature;
  3792. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3793. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3794. fFeatures[kFeatureIdExternalUiOld] = new LV2_Feature;
  3795. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3796. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3797. }
  3798. return true;
  3799. }
  3800. // -------------------------------------------------------------------
  3801. void handleTransferAtom(const int32_t portIndex, const char* const typeStr, LV2_Atom* const atom)
  3802. {
  3803. CARLA_ASSERT(portIndex >= 0);
  3804. CARLA_ASSERT(atom != nullptr);
  3805. CARLA_ASSERT(typeStr != nullptr);
  3806. carla_debug("Lv2Plugin::handleTransferAtom(%i, %s, %p)", portIndex, typeStr, atom);
  3807. atom->type = carla_lv2_urid_map(this, typeStr);
  3808. fAtomQueueIn.put(portIndex, atom);
  3809. }
  3810. void handleTransferEvent(const int32_t portIndex, const char* const typeStr, LV2_Atom* const atom)
  3811. {
  3812. CARLA_ASSERT(portIndex >= 0);
  3813. CARLA_ASSERT(atom != nullptr);
  3814. CARLA_ASSERT(typeStr != nullptr);
  3815. carla_debug("Lv2Plugin::handleTransferEvent(%i, %s, %p)", portIndex, typeStr, atom);
  3816. atom->type = carla_lv2_urid_map(this, typeStr);
  3817. fAtomQueueIn.put(portIndex, atom);
  3818. }
  3819. // -------------------------------------------------------------------
  3820. private:
  3821. LV2_Handle fHandle;
  3822. LV2_Handle fHandle2;
  3823. LV2_Feature* fFeatures[kFeatureCount+1];
  3824. const LV2_Descriptor* fDescriptor;
  3825. const LV2_RDF_Descriptor* fRdfDescriptor;
  3826. float** fAudioInBuffers;
  3827. float** fAudioOutBuffers;
  3828. float* fParamBuffers;
  3829. float fParamFreewheel;
  3830. Lv2AtomQueue fAtomQueueIn;
  3831. Lv2AtomQueue fAtomQueueOut;
  3832. LV2_Atom_Forge fAtomForge;
  3833. Lv2PluginEventData fEventsIn;
  3834. Lv2PluginEventData fEventsOut;
  3835. Lv2PluginOptions fLv2Options;
  3836. NonRtList<const char*> fCustomURIDs;
  3837. bool fFirstActive; // first process() call after activate()
  3838. EngineTimeInfo fLastTimeInfo;
  3839. struct Extensions {
  3840. const LV2_Options_Interface* options;
  3841. const LV2_State_Interface* state;
  3842. const LV2_Worker_Interface* worker;
  3843. const LV2_Programs_Interface* programs;
  3844. const LV2UI_Idle_Interface* uiidle;
  3845. const LV2_Programs_UI_Interface* uiprograms;
  3846. Extensions()
  3847. : options(nullptr),
  3848. state(nullptr),
  3849. worker(nullptr),
  3850. programs(nullptr),
  3851. uiidle(nullptr),
  3852. uiprograms(nullptr) {}
  3853. } fExt;
  3854. struct UI {
  3855. Lv2PluginGuiType type;
  3856. LV2UI_Handle handle;
  3857. LV2UI_Widget widget;
  3858. const LV2UI_Descriptor* descriptor;
  3859. const LV2_RDF_UI* rdfDescriptor;
  3860. UI()
  3861. : type(PLUGIN_UI_NULL),
  3862. handle(nullptr),
  3863. widget(nullptr),
  3864. descriptor(nullptr),
  3865. rdfDescriptor(nullptr) {}
  3866. ~UI()
  3867. {
  3868. CARLA_ASSERT(handle == nullptr);
  3869. CARLA_ASSERT(widget == nullptr);
  3870. CARLA_ASSERT(descriptor == nullptr);
  3871. CARLA_ASSERT(rdfDescriptor == nullptr);
  3872. }
  3873. } fUi;
  3874. // -------------------------------------------------------------------
  3875. // Event Feature
  3876. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3877. {
  3878. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  3879. CARLA_ASSERT(callback_data != nullptr);
  3880. CARLA_ASSERT(event != nullptr);
  3881. return 0;
  3882. }
  3883. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3884. {
  3885. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  3886. CARLA_ASSERT(callback_data != nullptr);
  3887. CARLA_ASSERT(event != nullptr);
  3888. return 0;
  3889. }
  3890. // -------------------------------------------------------------------
  3891. // Logs Feature
  3892. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  3893. {
  3894. CARLA_ASSERT(handle != nullptr);
  3895. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3896. #ifndef DEBUG
  3897. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3898. return 0;
  3899. #endif
  3900. va_list args;
  3901. va_start(args, fmt);
  3902. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  3903. va_end(args);
  3904. return ret;
  3905. }
  3906. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  3907. {
  3908. CARLA_ASSERT(handle != nullptr);
  3909. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3910. #ifndef DEBUG
  3911. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3912. return 0;
  3913. #endif
  3914. int ret = 0;
  3915. switch (type)
  3916. {
  3917. case CARLA_URI_MAP_ID_LOG_ERROR:
  3918. #ifndef CARLA_OS_WIN
  3919. std::fprintf(stderr, "\x1b[31m");
  3920. #endif
  3921. ret = std::vfprintf(stderr, fmt, ap);
  3922. #ifndef CARLA_OS_WIN
  3923. std::fprintf(stderr, "\x1b[0m");
  3924. #endif
  3925. break;
  3926. case CARLA_URI_MAP_ID_LOG_NOTE:
  3927. ret = std::vfprintf(stdout, fmt, ap);
  3928. break;
  3929. case CARLA_URI_MAP_ID_LOG_TRACE:
  3930. #ifdef DEBUG
  3931. # ifndef CARLA_OS_WIN
  3932. std::fprintf(stdout, "\x1b[30;1m");
  3933. # endif
  3934. ret = std::vfprintf(stdout, fmt, ap);
  3935. # ifndef CARLA_OS_WIN
  3936. std::fprintf(stdout, "\x1b[0m");
  3937. # endif
  3938. #endif
  3939. break;
  3940. case CARLA_URI_MAP_ID_LOG_WARNING:
  3941. ret = std::vfprintf(stderr, fmt, ap);
  3942. break;
  3943. default:
  3944. break;
  3945. }
  3946. return ret;
  3947. }
  3948. // -------------------------------------------------------------------
  3949. // Programs Feature
  3950. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  3951. {
  3952. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  3953. CARLA_ASSERT(handle != nullptr);
  3954. if (handle == nullptr)
  3955. return;
  3956. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  3957. }
  3958. // -------------------------------------------------------------------
  3959. // State Feature
  3960. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  3961. {
  3962. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3963. CARLA_ASSERT(handle != nullptr);
  3964. CARLA_ASSERT(path != nullptr);
  3965. if (path == nullptr)
  3966. return nullptr;
  3967. QDir dir;
  3968. dir.mkpath(path);
  3969. return strdup(path);
  3970. }
  3971. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  3972. {
  3973. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3974. CARLA_ASSERT(handle != nullptr);
  3975. CARLA_ASSERT(absolute_path != nullptr);
  3976. if (absolute_path == nullptr)
  3977. return nullptr;
  3978. QDir dir(absolute_path);
  3979. return strdup(dir.canonicalPath().toUtf8().constData());
  3980. }
  3981. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  3982. {
  3983. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  3984. CARLA_ASSERT(handle != nullptr);
  3985. CARLA_ASSERT(abstract_path != nullptr);
  3986. if (abstract_path == nullptr)
  3987. return nullptr;
  3988. QDir dir(abstract_path);
  3989. return strdup(dir.absolutePath().toUtf8().constData());
  3990. }
  3991. 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)
  3992. {
  3993. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  3994. CARLA_ASSERT(handle != nullptr);
  3995. if (handle == nullptr)
  3996. return LV2_STATE_ERR_UNKNOWN;
  3997. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  3998. }
  3999. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4000. {
  4001. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4002. CARLA_ASSERT(handle != nullptr);
  4003. if (handle == nullptr)
  4004. return nullptr;
  4005. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4006. }
  4007. // -------------------------------------------------------------------
  4008. // URI-Map Feature
  4009. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4010. {
  4011. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4012. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4013. // unused
  4014. (void)map;
  4015. }
  4016. // -------------------------------------------------------------------
  4017. // URID Feature
  4018. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4019. {
  4020. CARLA_ASSERT(handle != nullptr);
  4021. CARLA_ASSERT(uri != nullptr);
  4022. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4023. if (uri == nullptr)
  4024. return CARLA_URI_MAP_ID_NULL;
  4025. // Atom types
  4026. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4027. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4028. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4029. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4030. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4031. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4032. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4033. return CARLA_URI_MAP_ID_ATOM_INT;
  4034. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4035. return CARLA_URI_MAP_ID_ATOM_PATH;
  4036. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4037. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4038. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4039. return CARLA_URI_MAP_ID_ATOM_STRING;
  4040. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4041. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4042. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4043. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4044. // BufSize types
  4045. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4046. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4047. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4048. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4049. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4050. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4051. // Log types
  4052. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4053. return CARLA_URI_MAP_ID_LOG_ERROR;
  4054. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4055. return CARLA_URI_MAP_ID_LOG_NOTE;
  4056. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4057. return CARLA_URI_MAP_ID_LOG_TRACE;
  4058. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4059. return CARLA_URI_MAP_ID_LOG_WARNING;
  4060. // Time types
  4061. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4062. return CARLA_URI_MAP_ID_TIME_POSITION;
  4063. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4064. return CARLA_URI_MAP_ID_TIME_BAR;
  4065. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4066. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4067. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4068. return CARLA_URI_MAP_ID_TIME_BEAT;
  4069. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4070. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4071. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4072. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4073. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4074. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4075. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4076. return CARLA_URI_MAP_ID_TIME_FRAME;
  4077. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4078. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4079. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4080. return CARLA_URI_MAP_ID_TIME_SPEED;
  4081. // Others
  4082. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4083. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4084. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4085. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4086. if (handle == nullptr)
  4087. return CARLA_URI_MAP_ID_NULL;
  4088. // Custom types
  4089. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4090. }
  4091. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4092. {
  4093. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4094. CARLA_ASSERT(handle != nullptr);
  4095. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  4096. if (urid == CARLA_URI_MAP_ID_NULL)
  4097. return nullptr;
  4098. // Atom types
  4099. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4100. return LV2_ATOM__Chunk;
  4101. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4102. return LV2_ATOM__Double;
  4103. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4104. return LV2_ATOM__Float;
  4105. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4106. return LV2_ATOM__Int;
  4107. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4108. return LV2_ATOM__Path;
  4109. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4110. return LV2_ATOM__Sequence;
  4111. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4112. return LV2_ATOM__String;
  4113. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4114. return LV2_ATOM__atomTransfer;
  4115. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4116. return LV2_ATOM__eventTransfer;
  4117. // BufSize types
  4118. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4119. return LV2_BUF_SIZE__maxBlockLength;
  4120. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4121. return LV2_BUF_SIZE__minBlockLength;
  4122. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4123. return LV2_BUF_SIZE__sequenceSize;
  4124. // Log types
  4125. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4126. return LV2_LOG__Error;
  4127. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4128. return LV2_LOG__Note;
  4129. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4130. return LV2_LOG__Trace;
  4131. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4132. return LV2_LOG__Warning;
  4133. // Time types
  4134. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4135. return LV2_TIME__Position;
  4136. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4137. return LV2_TIME__bar;
  4138. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4139. return LV2_TIME__barBeat;
  4140. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4141. return LV2_TIME__beat;
  4142. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4143. return LV2_TIME__beatUnit;
  4144. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4145. return LV2_TIME__beatsPerBar;
  4146. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4147. return LV2_TIME__beatsPerMinute;
  4148. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4149. return LV2_TIME__frame;
  4150. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4151. return LV2_TIME__framesPerSecond;
  4152. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4153. return LV2_TIME__speed;
  4154. // Others
  4155. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4156. return LV2_MIDI__MidiEvent;
  4157. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4158. return LV2_PARAMETERS__sampleRate;
  4159. if (handle == nullptr)
  4160. return nullptr;
  4161. // Custom types
  4162. return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  4163. }
  4164. // -------------------------------------------------------------------
  4165. // Worker Feature
  4166. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4167. {
  4168. CARLA_ASSERT(handle != nullptr);
  4169. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4170. if (handle == nullptr)
  4171. return LV2_WORKER_ERR_UNKNOWN;
  4172. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4173. }
  4174. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4175. {
  4176. CARLA_ASSERT(handle != nullptr);
  4177. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4178. if (handle == nullptr)
  4179. return LV2_WORKER_ERR_UNKNOWN;
  4180. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4181. }
  4182. // -------------------------------------------------------------------
  4183. // UI Port-Map Feature
  4184. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4185. {
  4186. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4187. CARLA_ASSERT(handle);
  4188. if (handle == nullptr)
  4189. return LV2UI_INVALID_PORT_INDEX;
  4190. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4191. }
  4192. // -------------------------------------------------------------------
  4193. // UI Resize Feature
  4194. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4195. {
  4196. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4197. CARLA_ASSERT(handle != nullptr);
  4198. if (handle == nullptr)
  4199. return 1;
  4200. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4201. }
  4202. // -------------------------------------------------------------------
  4203. // External UI Feature
  4204. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4205. {
  4206. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4207. CARLA_ASSERT(controller != nullptr);
  4208. if (controller == nullptr)
  4209. return;
  4210. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4211. }
  4212. // -------------------------------------------------------------------
  4213. // UI Extension
  4214. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4215. {
  4216. CARLA_ASSERT(controller != nullptr);
  4217. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4218. if (controller == nullptr)
  4219. return;
  4220. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4221. }
  4222. // -------------------------------------------------------------------
  4223. // Lilv State
  4224. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4225. {
  4226. CARLA_ASSERT(user_data != nullptr);
  4227. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4228. if (user_data == nullptr)
  4229. return;
  4230. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4231. }
  4232. // -------------------------------------------------------------------
  4233. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4234. };
  4235. // -------------------------------------------------------------------------------------------------------------------
  4236. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4237. {
  4238. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4239. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iss");
  4240. const int32_t portIndex = argv[0]->i;
  4241. const char* const typeStr = (const char*)&argv[1]->s;
  4242. const char* const atomBuf = (const char*)&argv[2]->s;
  4243. QByteArray chunk;
  4244. chunk = QByteArray::fromBase64(atomBuf);
  4245. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  4246. ((Lv2Plugin*)plugin)->handleTransferAtom(portIndex, typeStr, atom);
  4247. return 0;
  4248. }
  4249. int CarlaEngineOsc::handleMsgLv2EventTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4250. {
  4251. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  4252. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(3, "iss");
  4253. const int32_t portIndex = argv[0]->i;
  4254. const char* const typeStr = (const char*)&argv[1]->s;
  4255. const char* const atomBuf = (const char*)&argv[2]->s;
  4256. QByteArray chunk;
  4257. chunk = QByteArray::fromBase64(atomBuf);
  4258. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  4259. ((Lv2Plugin*)plugin)->handleTransferEvent(portIndex, typeStr, atom);
  4260. return 0;
  4261. }
  4262. CARLA_BACKEND_END_NAMESPACE
  4263. #else // WANT_VST
  4264. # warning Building without LV2 support
  4265. #endif
  4266. CARLA_BACKEND_START_NAMESPACE
  4267. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4268. {
  4269. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4270. #ifdef WANT_LV2
  4271. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4272. if (! plugin->init(init.name, init.label))
  4273. {
  4274. delete plugin;
  4275. return nullptr;
  4276. }
  4277. plugin->reload();
  4278. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  4279. {
  4280. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4281. delete plugin;
  4282. return nullptr;
  4283. }
  4284. return plugin;
  4285. #else
  4286. init.engine->setLastError("LV2 support not available");
  4287. return nullptr;
  4288. #endif
  4289. }
  4290. CARLA_BACKEND_END_NAMESPACE