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.

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