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.

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