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.

5259 lines
198KB

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