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.

5240 lines
195KB

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