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.

5476 lines
198KB

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