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.

5224 lines
196KB

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