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.

5241 lines
195KB

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