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.

5482 lines
199KB

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