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.

5465 lines
198KB

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