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.

7380 lines
271KB

  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2019 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. // testing macros
  18. // #define LV2_UIS_ONLY_BRIDGES
  19. // #define LV2_UIS_ONLY_INPROCESS
  20. #include "CarlaPluginInternal.hpp"
  21. #include "CarlaEngine.hpp"
  22. #include "CarlaLv2Utils.hpp"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaBase64Utils.hpp"
  25. #include "CarlaEngineUtils.hpp"
  26. #include "CarlaPipeUtils.hpp"
  27. #include "CarlaPluginUI.hpp"
  28. #include "CarlaScopeUtils.hpp"
  29. #include "Lv2AtomRingBuffer.hpp"
  30. #include "../modules/lilv/config/lilv_config.h"
  31. extern "C" {
  32. #include "rtmempool/rtmempool-lv2.h"
  33. }
  34. #include "water/files/File.h"
  35. #include "water/misc/Time.h"
  36. #include <string>
  37. #include <vector>
  38. using water::File;
  39. #define URI_CARLA_ATOM_WORKER_IN "http://kxstudio.sf.net/ns/carla/atomWorkerIn"
  40. #define URI_CARLA_ATOM_WORKER_RESP "http://kxstudio.sf.net/ns/carla/atomWorkerResp"
  41. CARLA_BACKEND_START_NAMESPACE
  42. // -------------------------------------------------------------------------------------------------------------------
  43. // Fallback data
  44. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  45. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  46. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  47. static const char* const kUnmapFallback = "urn:null";
  48. // -------------------------------------------------------------------------------------------------------------------
  49. // Maximum default buffer size
  50. const uint MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  51. // Extra Plugin Hints
  52. const uint PLUGIN_HAS_EXTENSION_OPTIONS = 0x01000;
  53. const uint PLUGIN_HAS_EXTENSION_PROGRAMS = 0x02000;
  54. const uint PLUGIN_HAS_EXTENSION_STATE = 0x04000;
  55. const uint PLUGIN_HAS_EXTENSION_WORKER = 0x08000;
  56. const uint PLUGIN_HAS_EXTENSION_INLINE_DISPLAY = 0x10000;
  57. const uint PLUGIN_HAS_EXTENSION_MIDNAM = 0x20000;
  58. // LV2 Event Data/Types
  59. const uint CARLA_EVENT_DATA_ATOM = 0x01;
  60. const uint CARLA_EVENT_DATA_EVENT = 0x02;
  61. const uint CARLA_EVENT_DATA_MIDI_LL = 0x04;
  62. const uint CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  63. const uint CARLA_EVENT_TYPE_MIDI = 0x20;
  64. const uint CARLA_EVENT_TYPE_TIME = 0x40;
  65. // LV2 URI Map Ids
  66. enum CarlaLv2URIDs {
  67. kUridNull = 0,
  68. kUridAtomBlank,
  69. kUridAtomBool,
  70. kUridAtomChunk,
  71. kUridAtomDouble,
  72. kUridAtomEvent,
  73. kUridAtomFloat,
  74. kUridAtomInt,
  75. kUridAtomLiteral,
  76. kUridAtomLong,
  77. kUridAtomNumber,
  78. kUridAtomObject,
  79. kUridAtomPath,
  80. kUridAtomProperty,
  81. kUridAtomResource,
  82. kUridAtomSequence,
  83. kUridAtomSound,
  84. kUridAtomString,
  85. kUridAtomTuple,
  86. kUridAtomURI,
  87. kUridAtomURID,
  88. kUridAtomVector,
  89. kUridAtomTransferAtom,
  90. kUridAtomTransferEvent,
  91. kUridBufMaxLength,
  92. kUridBufMinLength,
  93. kUridBufNominalLength,
  94. kUridBufSequenceSize,
  95. kUridLogError,
  96. kUridLogNote,
  97. kUridLogTrace,
  98. kUridLogWarning,
  99. kUridPatchSet,
  100. kUridPatchPoperty,
  101. kUridPatchValue,
  102. // time base type
  103. kUridTimePosition,
  104. // time values
  105. kUridTimeBar,
  106. kUridTimeBarBeat,
  107. kUridTimeBeat,
  108. kUridTimeBeatUnit,
  109. kUridTimeBeatsPerBar,
  110. kUridTimeBeatsPerMinute,
  111. kUridTimeFrame,
  112. kUridTimeFramesPerSecond,
  113. kUridTimeSpeed,
  114. kUridTimeTicksPerBeat,
  115. kUridMidiEvent,
  116. kUridParamSampleRate,
  117. // ui stuff
  118. kUridBackgroundColor,
  119. kUridForegroundColor,
  120. kUridScaleFactor,
  121. kUridWindowTitle,
  122. // custom carla props
  123. kUridCarlaAtomWorkerIn,
  124. kUridCarlaAtomWorkerResp,
  125. kUridCarlaTransientWindowId,
  126. // count
  127. kUridCount
  128. };
  129. // LV2 Feature Ids
  130. enum CarlaLv2Features {
  131. // DSP features
  132. kFeatureIdBufSizeBounded = 0,
  133. kFeatureIdBufSizeFixed,
  134. kFeatureIdBufSizePowerOf2,
  135. kFeatureIdEvent,
  136. kFeatureIdHardRtCapable,
  137. kFeatureIdInPlaceBroken,
  138. kFeatureIdIsLive,
  139. kFeatureIdLogs,
  140. kFeatureIdOptions,
  141. kFeatureIdPrograms,
  142. kFeatureIdResizePort,
  143. kFeatureIdRtMemPool,
  144. kFeatureIdRtMemPoolOld,
  145. kFeatureIdStateMakePath,
  146. kFeatureIdStateMapPath,
  147. kFeatureIdStrictBounds,
  148. kFeatureIdUriMap,
  149. kFeatureIdUridMap,
  150. kFeatureIdUridUnmap,
  151. kFeatureIdWorker,
  152. kFeatureIdInlineDisplay,
  153. kFeatureIdMidnam,
  154. kFeatureCountPlugin,
  155. // UI features
  156. kFeatureIdUiDataAccess = kFeatureCountPlugin,
  157. kFeatureIdUiInstanceAccess,
  158. kFeatureIdUiIdleInterface,
  159. kFeatureIdUiFixedSize,
  160. kFeatureIdUiMakeResident,
  161. kFeatureIdUiMakeResident2,
  162. kFeatureIdUiNoUserResize,
  163. kFeatureIdUiParent,
  164. kFeatureIdUiPortMap,
  165. kFeatureIdUiPortSubscribe,
  166. kFeatureIdUiResize,
  167. kFeatureIdUiRequestValue,
  168. kFeatureIdUiTouch,
  169. kFeatureIdExternalUi,
  170. kFeatureIdExternalUiOld,
  171. kFeatureCountAll
  172. };
  173. // -------------------------------------------------------------------------------------------------------------------
  174. struct Lv2EventData {
  175. uint32_t type;
  176. uint32_t rindex;
  177. CarlaEngineEventPort* port;
  178. union {
  179. LV2_Atom_Buffer* atom;
  180. LV2_Event_Buffer* event;
  181. LV2_MIDI midi;
  182. };
  183. Lv2EventData() noexcept
  184. : type(0x0),
  185. rindex(0),
  186. port(nullptr) {}
  187. ~Lv2EventData() noexcept
  188. {
  189. if (port != nullptr)
  190. {
  191. delete port;
  192. port = nullptr;
  193. }
  194. const uint32_t rtype(type);
  195. type = 0x0;
  196. if (rtype & CARLA_EVENT_DATA_ATOM)
  197. {
  198. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  199. std::free(atom);
  200. atom = nullptr;
  201. }
  202. else if (rtype & CARLA_EVENT_DATA_EVENT)
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  205. std::free(event);
  206. event = nullptr;
  207. }
  208. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  211. delete[] midi.data;
  212. midi.data = nullptr;
  213. }
  214. }
  215. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  216. };
  217. struct CarlaPluginLV2EventData {
  218. uint32_t count;
  219. Lv2EventData* data;
  220. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  221. uint32_t ctrlIndex;
  222. CarlaPluginLV2EventData() noexcept
  223. : count(0),
  224. data(nullptr),
  225. ctrl(nullptr),
  226. ctrlIndex(0) {}
  227. ~CarlaPluginLV2EventData() noexcept
  228. {
  229. CARLA_SAFE_ASSERT_INT(count == 0, count);
  230. CARLA_SAFE_ASSERT(data == nullptr);
  231. CARLA_SAFE_ASSERT(ctrl == nullptr);
  232. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  233. }
  234. void createNew(const uint32_t newCount)
  235. {
  236. CARLA_SAFE_ASSERT_INT(count == 0, count);
  237. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  238. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  239. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  240. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  241. data = new Lv2EventData[newCount];
  242. count = newCount;
  243. ctrl = nullptr;
  244. ctrlIndex = 0;
  245. }
  246. void clear() noexcept
  247. {
  248. if (data != nullptr)
  249. {
  250. for (uint32_t i=0; i < count; ++i)
  251. {
  252. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  253. data[i].port = nullptr;
  254. }
  255. delete[] data;
  256. data = nullptr;
  257. }
  258. count = 0;
  259. ctrl = nullptr;
  260. ctrlIndex = 0;
  261. }
  262. void initBuffers() const noexcept
  263. {
  264. for (uint32_t i=0; i < count; ++i)
  265. {
  266. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  267. data[i].port->initBuffer();
  268. }
  269. }
  270. CARLA_DECLARE_NON_COPY_STRUCT(CarlaPluginLV2EventData)
  271. };
  272. // -------------------------------------------------------------------------------------------------------------------
  273. struct CarlaPluginLV2Options {
  274. enum OptIndex {
  275. MaxBlockLenth = 0,
  276. MinBlockLenth,
  277. NominalBlockLenth,
  278. SequenceSize,
  279. SampleRate,
  280. TransientWinId,
  281. BackgroundColor,
  282. ForegroundColor,
  283. ScaleFactor,
  284. WindowTitle,
  285. Null,
  286. Count
  287. };
  288. int maxBufferSize;
  289. int minBufferSize;
  290. int nominalBufferSize;
  291. int sequenceSize;
  292. float sampleRate;
  293. int64_t transientWinId;
  294. uint32_t bgColor;
  295. uint32_t fgColor;
  296. float uiScale;
  297. const char* windowTitle;
  298. LV2_Options_Option opts[Count];
  299. CarlaPluginLV2Options() noexcept
  300. : maxBufferSize(0),
  301. minBufferSize(0),
  302. nominalBufferSize(0),
  303. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  304. sampleRate(0.0),
  305. transientWinId(0),
  306. bgColor(0x000000ff),
  307. fgColor(0xffffffff),
  308. uiScale(1.0f),
  309. windowTitle(nullptr)
  310. {
  311. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  312. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  313. optMaxBlockLenth.subject = 0;
  314. optMaxBlockLenth.key = kUridBufMaxLength;
  315. optMaxBlockLenth.size = sizeof(int);
  316. optMaxBlockLenth.type = kUridAtomInt;
  317. optMaxBlockLenth.value = &maxBufferSize;
  318. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  319. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  320. optMinBlockLenth.subject = 0;
  321. optMinBlockLenth.key = kUridBufMinLength;
  322. optMinBlockLenth.size = sizeof(int);
  323. optMinBlockLenth.type = kUridAtomInt;
  324. optMinBlockLenth.value = &minBufferSize;
  325. LV2_Options_Option& optNominalBlockLenth(opts[NominalBlockLenth]);
  326. optNominalBlockLenth.context = LV2_OPTIONS_INSTANCE;
  327. optNominalBlockLenth.subject = 0;
  328. optNominalBlockLenth.key = kUridBufNominalLength;
  329. optNominalBlockLenth.size = sizeof(int);
  330. optNominalBlockLenth.type = kUridAtomInt;
  331. optNominalBlockLenth.value = &nominalBufferSize;
  332. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  333. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  334. optSequenceSize.subject = 0;
  335. optSequenceSize.key = kUridBufSequenceSize;
  336. optSequenceSize.size = sizeof(int);
  337. optSequenceSize.type = kUridAtomInt;
  338. optSequenceSize.value = &sequenceSize;
  339. LV2_Options_Option& optBackgroundColor(opts[BackgroundColor]);
  340. optBackgroundColor.context = LV2_OPTIONS_INSTANCE;
  341. optBackgroundColor.subject = 0;
  342. optBackgroundColor.key = kUridBackgroundColor;
  343. optBackgroundColor.size = sizeof(int32_t);
  344. optBackgroundColor.type = kUridAtomInt;
  345. optBackgroundColor.value = &bgColor;
  346. LV2_Options_Option& optForegroundColor(opts[ForegroundColor]);
  347. optForegroundColor.context = LV2_OPTIONS_INSTANCE;
  348. optForegroundColor.subject = 0;
  349. optForegroundColor.key = kUridForegroundColor;
  350. optForegroundColor.size = sizeof(int32_t);
  351. optForegroundColor.type = kUridAtomInt;
  352. optForegroundColor.value = &fgColor;
  353. LV2_Options_Option& optScaleFactor(opts[ScaleFactor]);
  354. optScaleFactor.context = LV2_OPTIONS_INSTANCE;
  355. optScaleFactor.subject = 0;
  356. optScaleFactor.key = kUridScaleFactor;
  357. optScaleFactor.size = sizeof(float);
  358. optScaleFactor.type = kUridAtomFloat;
  359. optScaleFactor.value = &uiScale;
  360. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  361. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  362. optSampleRate.subject = 0;
  363. optSampleRate.key = kUridParamSampleRate;
  364. optSampleRate.size = sizeof(float);
  365. optSampleRate.type = kUridAtomFloat;
  366. optSampleRate.value = &sampleRate;
  367. LV2_Options_Option& optTransientWinId(opts[TransientWinId]);
  368. optTransientWinId.context = LV2_OPTIONS_INSTANCE;
  369. optTransientWinId.subject = 0;
  370. optTransientWinId.key = kUridCarlaTransientWindowId;
  371. optTransientWinId.size = sizeof(int64_t);
  372. optTransientWinId.type = kUridAtomLong;
  373. optTransientWinId.value = &transientWinId;
  374. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  375. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  376. optWindowTitle.subject = 0;
  377. optWindowTitle.key = kUridWindowTitle;
  378. optWindowTitle.size = 0;
  379. optWindowTitle.type = kUridAtomString;
  380. optWindowTitle.value = nullptr;
  381. LV2_Options_Option& optNull(opts[Null]);
  382. optNull.context = LV2_OPTIONS_INSTANCE;
  383. optNull.subject = 0;
  384. optNull.key = kUridNull;
  385. optNull.size = 0;
  386. optNull.type = kUridNull;
  387. optNull.value = nullptr;
  388. }
  389. ~CarlaPluginLV2Options() noexcept
  390. {
  391. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  392. optWindowTitle.size = 0;
  393. optWindowTitle.value = nullptr;
  394. if (windowTitle != nullptr)
  395. {
  396. delete[] windowTitle;
  397. windowTitle = nullptr;
  398. }
  399. }
  400. CARLA_DECLARE_NON_COPY_STRUCT(CarlaPluginLV2Options);
  401. };
  402. // -------------------------------------------------------------------------------------------------------------------
  403. class CarlaPluginLV2;
  404. class CarlaPipeServerLV2 : public CarlaPipeServer
  405. {
  406. public:
  407. enum UiState {
  408. UiNone = 0,
  409. UiHide,
  410. UiShow,
  411. UiCrashed
  412. };
  413. CarlaPipeServerLV2(CarlaEngine* const engine, CarlaPluginLV2* const plugin)
  414. : kEngine(engine),
  415. kPlugin(plugin),
  416. fFilename(),
  417. fPluginURI(),
  418. fUiURI(),
  419. fUiState(UiNone) {}
  420. ~CarlaPipeServerLV2() noexcept override
  421. {
  422. CARLA_SAFE_ASSERT_INT(fUiState == UiNone, fUiState);
  423. }
  424. UiState getAndResetUiState() noexcept
  425. {
  426. const UiState uiState(fUiState);
  427. fUiState = UiNone;
  428. return uiState;
  429. }
  430. void setData(const char* const filename, const char* const pluginURI, const char* const uiURI) noexcept
  431. {
  432. fFilename = filename;
  433. fPluginURI = pluginURI;
  434. fUiURI = uiURI;
  435. }
  436. bool startPipeServer(const int size) noexcept
  437. {
  438. char sampleRateStr[32];
  439. {
  440. const CarlaScopedLocale csl;
  441. std::snprintf(sampleRateStr, 31, "%.12g", kEngine->getSampleRate());
  442. }
  443. sampleRateStr[31] = '\0';
  444. const ScopedEngineEnvironmentLocker _seel(kEngine);
  445. const CarlaScopedEnvVar _sev1("LV2_PATH", kEngine->getOptions().pathLV2);
  446. #ifdef CARLA_OS_LINUX
  447. const CarlaScopedEnvVar _sev2("LD_PRELOAD", nullptr);
  448. #endif
  449. carla_setenv("CARLA_SAMPLE_RATE", sampleRateStr);
  450. return CarlaPipeServer::startPipeServer(fFilename, fPluginURI, fUiURI, size);
  451. }
  452. void writeUiTitleMessage(const char* const title) const noexcept
  453. {
  454. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0',);
  455. const CarlaMutexLocker cml(getPipeLock());
  456. if (! _writeMsgBuffer("uiTitle\n", 8))
  457. return;
  458. if (! writeAndFixMessage(title))
  459. return;
  460. flushMessages();
  461. }
  462. protected:
  463. // returns true if msg was handled
  464. bool msgReceived(const char* const msg) noexcept override;
  465. private:
  466. CarlaEngine* const kEngine;
  467. CarlaPluginLV2* const kPlugin;
  468. CarlaString fFilename;
  469. CarlaString fPluginURI;
  470. CarlaString fUiURI;
  471. UiState fUiState;
  472. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeServerLV2)
  473. };
  474. // -------------------------------------------------------------------------------------------------------------------
  475. class CarlaPluginLV2 : public CarlaPlugin,
  476. private CarlaPluginUI::Callback
  477. {
  478. public:
  479. CarlaPluginLV2(CarlaEngine* const engine, const uint id)
  480. : CarlaPlugin(engine, id),
  481. fHandle(nullptr),
  482. fHandle2(nullptr),
  483. fDescriptor(nullptr),
  484. fRdfDescriptor(nullptr),
  485. fAudioInBuffers(nullptr),
  486. fAudioOutBuffers(nullptr),
  487. fCvInBuffers(nullptr),
  488. fCvOutBuffers(nullptr),
  489. fParamBuffers(nullptr),
  490. fNeedsFixedBuffers(false),
  491. fNeedsUiClose(false),
  492. fInlineDisplayNeedsRedraw(false),
  493. fInlineDisplayLastRedrawTime(0),
  494. fLatencyIndex(-1),
  495. fStrictBounds(-1),
  496. fAtomBufferEvIn(),
  497. fAtomBufferUiOut(),
  498. fAtomBufferWorkerIn(),
  499. fAtomBufferWorkerResp(),
  500. fAtomForge(),
  501. fAtomBufferUiOutTmpData(nullptr),
  502. fAtomBufferWorkerInTmpData(nullptr),
  503. fEventsIn(),
  504. fEventsOut(),
  505. fLv2Options(),
  506. fPipeServer(engine, this),
  507. fCustomURIDs(kUridCount, std::string("urn:null")),
  508. fFirstActive(true),
  509. fLastStateChunk(nullptr),
  510. fLastTimeInfo(),
  511. fFilePathURI(),
  512. fExt(),
  513. fUI()
  514. {
  515. carla_debug("CarlaPluginLV2::CarlaPluginLV2(%p, %i)", engine, id);
  516. CARLA_SAFE_ASSERT(fCustomURIDs.size() == kUridCount);
  517. carla_zeroPointers(fFeatures, kFeatureCountAll+1);
  518. #if defined(__clang__)
  519. # pragma clang diagnostic push
  520. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  521. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  522. # pragma GCC diagnostic push
  523. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  524. #endif
  525. fAtomForge.Blank = kUridAtomBlank;
  526. fAtomForge.Bool = kUridAtomBool;
  527. fAtomForge.Chunk = kUridAtomChunk;
  528. fAtomForge.Double = kUridAtomDouble;
  529. fAtomForge.Float = kUridAtomFloat;
  530. fAtomForge.Int = kUridAtomInt;
  531. fAtomForge.Literal = kUridAtomLiteral;
  532. fAtomForge.Long = kUridAtomLong;
  533. fAtomForge.Object = kUridAtomObject;
  534. fAtomForge.Path = kUridAtomPath;
  535. fAtomForge.Property = kUridAtomProperty;
  536. fAtomForge.Resource = kUridAtomResource;
  537. fAtomForge.Sequence = kUridAtomSequence;
  538. fAtomForge.String = kUridAtomString;
  539. fAtomForge.Tuple = kUridAtomTuple;
  540. fAtomForge.URI = kUridAtomURI;
  541. fAtomForge.URID = kUridAtomURID;
  542. fAtomForge.Vector = kUridAtomVector;
  543. #if defined(__clang__)
  544. # pragma clang diagnostic pop
  545. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  546. # pragma GCC diagnostic pop
  547. #endif
  548. }
  549. ~CarlaPluginLV2() override
  550. {
  551. carla_debug("CarlaPluginLV2::~CarlaPluginLV2()");
  552. fInlineDisplayNeedsRedraw = false;
  553. // close UI
  554. if (fUI.type != UI::TYPE_NULL)
  555. {
  556. showCustomUI(false);
  557. if (fUI.type == UI::TYPE_BRIDGE)
  558. {
  559. fPipeServer.stopPipeServer(pData->engine->getOptions().uiBridgesTimeout);
  560. }
  561. else
  562. {
  563. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  564. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  565. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  566. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  567. if (fFeatures[kFeatureIdUiRequestValue] != nullptr && fFeatures[kFeatureIdUiRequestValue]->data != nullptr)
  568. delete (LV2UI_Request_Value*)fFeatures[kFeatureIdUiRequestValue]->data;
  569. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  570. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  571. if (fFeatures[kFeatureIdUiTouch] != nullptr && fFeatures[kFeatureIdUiTouch]->data != nullptr)
  572. delete (LV2UI_Touch*)fFeatures[kFeatureIdUiTouch]->data;
  573. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  574. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  575. fUI.descriptor = nullptr;
  576. pData->uiLibClose();
  577. }
  578. #ifndef LV2_UIS_ONLY_BRIDGES
  579. if (fUI.window != nullptr)
  580. {
  581. delete fUI.window;
  582. fUI.window = nullptr;
  583. }
  584. #endif
  585. fUI.rdfDescriptor = nullptr;
  586. }
  587. pData->singleMutex.lock();
  588. pData->masterMutex.lock();
  589. if (pData->client != nullptr && pData->client->isActive())
  590. pData->client->deactivate();
  591. if (pData->active)
  592. {
  593. deactivate();
  594. pData->active = false;
  595. }
  596. if (fDescriptor != nullptr)
  597. {
  598. if (fDescriptor->cleanup != nullptr)
  599. {
  600. if (fHandle != nullptr)
  601. fDescriptor->cleanup(fHandle);
  602. if (fHandle2 != nullptr)
  603. fDescriptor->cleanup(fHandle2);
  604. }
  605. fHandle = nullptr;
  606. fHandle2 = nullptr;
  607. fDescriptor = nullptr;
  608. }
  609. if (fRdfDescriptor != nullptr)
  610. {
  611. delete fRdfDescriptor;
  612. fRdfDescriptor = nullptr;
  613. }
  614. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  615. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  616. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  617. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  618. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  619. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  620. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  621. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  622. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  623. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  624. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  625. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  626. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  627. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  628. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  629. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  630. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  631. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  632. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  633. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  634. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  635. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  636. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  637. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  638. if (fFeatures[kFeatureIdInlineDisplay] != nullptr && fFeatures[kFeatureIdInlineDisplay]->data != nullptr)
  639. delete (LV2_Inline_Display*)fFeatures[kFeatureIdInlineDisplay]->data;
  640. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  641. {
  642. if (fFeatures[i] != nullptr)
  643. {
  644. delete fFeatures[i];
  645. fFeatures[i] = nullptr;
  646. }
  647. }
  648. if (fLastStateChunk != nullptr)
  649. {
  650. std::free(fLastStateChunk);
  651. fLastStateChunk = nullptr;
  652. }
  653. if (fAtomBufferUiOutTmpData != nullptr)
  654. {
  655. delete[] fAtomBufferUiOutTmpData;
  656. fAtomBufferUiOutTmpData = nullptr;
  657. }
  658. if (fAtomBufferWorkerInTmpData != nullptr)
  659. {
  660. delete[] fAtomBufferWorkerInTmpData;
  661. fAtomBufferWorkerInTmpData = nullptr;
  662. }
  663. clearBuffers();
  664. }
  665. // -------------------------------------------------------------------
  666. // Information (base)
  667. PluginType getType() const noexcept override
  668. {
  669. return PLUGIN_LV2;
  670. }
  671. PluginCategory getCategory() const noexcept override
  672. {
  673. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  674. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  675. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  676. if (LV2_IS_DELAY(cat1, cat2))
  677. return PLUGIN_CATEGORY_DELAY;
  678. if (LV2_IS_DISTORTION(cat1, cat2))
  679. return PLUGIN_CATEGORY_OTHER;
  680. if (LV2_IS_DYNAMICS(cat1, cat2))
  681. return PLUGIN_CATEGORY_DYNAMICS;
  682. if (LV2_IS_EQ(cat1, cat2))
  683. return PLUGIN_CATEGORY_EQ;
  684. if (LV2_IS_FILTER(cat1, cat2))
  685. return PLUGIN_CATEGORY_FILTER;
  686. if (LV2_IS_GENERATOR(cat1, cat2))
  687. return PLUGIN_CATEGORY_SYNTH;
  688. if (LV2_IS_MODULATOR(cat1, cat2))
  689. return PLUGIN_CATEGORY_MODULATOR;
  690. if (LV2_IS_REVERB(cat1, cat2))
  691. return PLUGIN_CATEGORY_DELAY;
  692. if (LV2_IS_SIMULATOR(cat1, cat2))
  693. return PLUGIN_CATEGORY_OTHER;
  694. if (LV2_IS_SPATIAL(cat1, cat2))
  695. return PLUGIN_CATEGORY_OTHER;
  696. if (LV2_IS_SPECTRAL(cat1, cat2))
  697. return PLUGIN_CATEGORY_UTILITY;
  698. if (LV2_IS_UTILITY(cat1, cat2))
  699. return PLUGIN_CATEGORY_UTILITY;
  700. return CarlaPlugin::getCategory();
  701. }
  702. uint32_t getLatencyInFrames() const noexcept override
  703. {
  704. if (fLatencyIndex < 0 || fParamBuffers == nullptr)
  705. return 0;
  706. const float latency(fParamBuffers[fLatencyIndex]);
  707. CARLA_SAFE_ASSERT_RETURN(latency >= 0.0f, 0);
  708. return static_cast<uint32_t>(latency);
  709. }
  710. // -------------------------------------------------------------------
  711. // Information (count)
  712. uint32_t getMidiInCount() const noexcept override
  713. {
  714. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  715. uint32_t count = 0;
  716. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  717. {
  718. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  719. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  720. ++count;
  721. }
  722. return count;
  723. }
  724. uint32_t getMidiOutCount() const noexcept override
  725. {
  726. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  727. uint32_t count = 0;
  728. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  729. {
  730. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  731. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  732. ++count;
  733. }
  734. return count;
  735. }
  736. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  737. {
  738. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  739. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  740. const int32_t rindex(pData->param.data[parameterId].rindex);
  741. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  742. {
  743. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  744. return port->ScalePointCount;
  745. }
  746. return 0;
  747. }
  748. // -------------------------------------------------------------------
  749. // Information (current data)
  750. // nothing
  751. // -------------------------------------------------------------------
  752. // Information (per-plugin data)
  753. uint getOptionsAvailable() const noexcept override
  754. {
  755. uint options = 0x0;
  756. // can't disable fixed buffers if using latency or MIDI output
  757. if (fLatencyIndex == -1 && getMidiOutCount() == 0 && ! fNeedsFixedBuffers)
  758. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  759. // can't disable forced stereo if enabled in the engine
  760. if (pData->engine->getOptions().forceStereo)
  761. pass();
  762. // if inputs or outputs are just 1, then yes we can force stereo
  763. else if ((pData->audioIn.count == 1 || pData->audioOut.count == 1) || fHandle2 != nullptr)
  764. options |= PLUGIN_OPTION_FORCE_STEREO;
  765. if (fExt.programs != nullptr)
  766. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  767. if (getMidiInCount() != 0)
  768. {
  769. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  770. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  771. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  772. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  773. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  774. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  775. }
  776. return options;
  777. }
  778. float getParameterValue(const uint32_t parameterId) const noexcept override
  779. {
  780. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  781. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  782. if (pData->param.data[parameterId].type == PARAMETER_INPUT)
  783. {
  784. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  785. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  786. }
  787. else
  788. {
  789. if (fStrictBounds >= 0 && (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS) == 0)
  790. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  791. }
  792. return fParamBuffers[parameterId];
  793. }
  794. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  795. {
  796. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  797. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  798. const int32_t rindex(pData->param.data[parameterId].rindex);
  799. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  800. {
  801. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  802. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, 0.0f);
  803. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  804. return portScalePoint->Value;
  805. }
  806. return 0.0f;
  807. }
  808. bool getLabel(char* const strBuf) const noexcept override
  809. {
  810. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  811. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr, false);
  812. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  813. return true;
  814. }
  815. bool getMaker(char* const strBuf) const noexcept override
  816. {
  817. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  818. if (fRdfDescriptor->Author != nullptr)
  819. {
  820. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  821. return true;
  822. }
  823. return false;
  824. }
  825. bool getCopyright(char* const strBuf) const noexcept override
  826. {
  827. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  828. if (fRdfDescriptor->License != nullptr)
  829. {
  830. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  831. return true;
  832. }
  833. return false;
  834. }
  835. bool getRealName(char* const strBuf) const noexcept override
  836. {
  837. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  838. if (fRdfDescriptor->Name != nullptr)
  839. {
  840. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  841. return true;
  842. }
  843. return false;
  844. }
  845. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  846. {
  847. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  848. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  849. int32_t rindex = pData->param.data[parameterId].rindex;
  850. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  851. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  852. {
  853. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  854. return true;
  855. }
  856. rindex -= static_cast<int32_t>(fRdfDescriptor->PortCount);
  857. if (rindex < static_cast<int32_t>(fRdfDescriptor->ParameterCount))
  858. {
  859. std::strncpy(strBuf, fRdfDescriptor->Parameters[rindex].Label, STR_MAX);
  860. return true;
  861. }
  862. return CarlaPlugin::getParameterName(parameterId, strBuf);
  863. }
  864. bool getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  865. {
  866. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  867. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  868. int32_t rindex = pData->param.data[parameterId].rindex;
  869. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  870. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  871. {
  872. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  873. return true;
  874. }
  875. rindex -= static_cast<int32_t>(fRdfDescriptor->PortCount);
  876. if (rindex < static_cast<int32_t>(fRdfDescriptor->ParameterCount))
  877. {
  878. std::strncpy(strBuf, fRdfDescriptor->Parameters[rindex].URI, STR_MAX);
  879. return true;
  880. }
  881. return CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  882. }
  883. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  884. {
  885. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  886. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  887. LV2_RDF_PortUnit* portUnit = nullptr;
  888. int32_t rindex = pData->param.data[parameterId].rindex;
  889. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  890. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  891. {
  892. portUnit = &fRdfDescriptor->Ports[rindex].Unit;
  893. }
  894. else
  895. {
  896. rindex -= static_cast<int32_t>(fRdfDescriptor->PortCount);
  897. if (rindex < static_cast<int32_t>(fRdfDescriptor->ParameterCount))
  898. {
  899. portUnit = &fRdfDescriptor->Parameters[rindex].Unit;
  900. }
  901. }
  902. if (portUnit != nullptr)
  903. {
  904. if (LV2_HAVE_PORT_UNIT_SYMBOL(portUnit->Hints) && portUnit->Symbol != nullptr)
  905. {
  906. std::strncpy(strBuf, portUnit->Symbol, STR_MAX);
  907. return true;
  908. }
  909. if (LV2_HAVE_PORT_UNIT_UNIT(portUnit->Hints))
  910. {
  911. switch (portUnit->Unit)
  912. {
  913. case LV2_PORT_UNIT_BAR:
  914. std::strncpy(strBuf, "bars", STR_MAX);
  915. return true;
  916. case LV2_PORT_UNIT_BEAT:
  917. std::strncpy(strBuf, "beats", STR_MAX);
  918. return true;
  919. case LV2_PORT_UNIT_BPM:
  920. std::strncpy(strBuf, "BPM", STR_MAX);
  921. return true;
  922. case LV2_PORT_UNIT_CENT:
  923. std::strncpy(strBuf, "ct", STR_MAX);
  924. return true;
  925. case LV2_PORT_UNIT_CM:
  926. std::strncpy(strBuf, "cm", STR_MAX);
  927. return true;
  928. case LV2_PORT_UNIT_COEF:
  929. std::strncpy(strBuf, "(coef)", STR_MAX);
  930. return true;
  931. case LV2_PORT_UNIT_DB:
  932. std::strncpy(strBuf, "dB", STR_MAX);
  933. return true;
  934. case LV2_PORT_UNIT_DEGREE:
  935. std::strncpy(strBuf, "deg", STR_MAX);
  936. return true;
  937. case LV2_PORT_UNIT_FRAME:
  938. std::strncpy(strBuf, "frames", STR_MAX);
  939. return true;
  940. case LV2_PORT_UNIT_HZ:
  941. std::strncpy(strBuf, "Hz", STR_MAX);
  942. return true;
  943. case LV2_PORT_UNIT_INCH:
  944. std::strncpy(strBuf, "in", STR_MAX);
  945. return true;
  946. case LV2_PORT_UNIT_KHZ:
  947. std::strncpy(strBuf, "kHz", STR_MAX);
  948. return true;
  949. case LV2_PORT_UNIT_KM:
  950. std::strncpy(strBuf, "km", STR_MAX);
  951. return true;
  952. case LV2_PORT_UNIT_M:
  953. std::strncpy(strBuf, "m", STR_MAX);
  954. return true;
  955. case LV2_PORT_UNIT_MHZ:
  956. std::strncpy(strBuf, "MHz", STR_MAX);
  957. return true;
  958. case LV2_PORT_UNIT_MIDINOTE:
  959. std::strncpy(strBuf, "note", STR_MAX);
  960. return true;
  961. case LV2_PORT_UNIT_MILE:
  962. std::strncpy(strBuf, "mi", STR_MAX);
  963. return true;
  964. case LV2_PORT_UNIT_MIN:
  965. std::strncpy(strBuf, "min", STR_MAX);
  966. return true;
  967. case LV2_PORT_UNIT_MM:
  968. std::strncpy(strBuf, "mm", STR_MAX);
  969. return true;
  970. case LV2_PORT_UNIT_MS:
  971. std::strncpy(strBuf, "ms", STR_MAX);
  972. return true;
  973. case LV2_PORT_UNIT_OCT:
  974. std::strncpy(strBuf, "oct", STR_MAX);
  975. return true;
  976. case LV2_PORT_UNIT_PC:
  977. std::strncpy(strBuf, "%", STR_MAX);
  978. return true;
  979. case LV2_PORT_UNIT_S:
  980. std::strncpy(strBuf, "s", STR_MAX);
  981. return true;
  982. case LV2_PORT_UNIT_SEMITONE:
  983. std::strncpy(strBuf, "semi", STR_MAX);
  984. return true;
  985. }
  986. }
  987. }
  988. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  989. }
  990. bool getParameterComment(const uint32_t parameterId, char* const strBuf) const noexcept override
  991. {
  992. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  993. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  994. int32_t rindex = pData->param.data[parameterId].rindex;
  995. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  996. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  997. {
  998. if (const char* const comment = fRdfDescriptor->Ports[rindex].Comment)
  999. {
  1000. std::strncpy(strBuf, comment, STR_MAX);
  1001. return true;
  1002. }
  1003. return false;
  1004. }
  1005. rindex -= static_cast<int32_t>(fRdfDescriptor->PortCount);
  1006. if (rindex < static_cast<int32_t>(fRdfDescriptor->ParameterCount))
  1007. {
  1008. if (const char* const comment = fRdfDescriptor->Parameters[rindex].Comment)
  1009. {
  1010. std::strncpy(strBuf, comment, STR_MAX);
  1011. return true;
  1012. }
  1013. return false;
  1014. }
  1015. return CarlaPlugin::getParameterComment(parameterId, strBuf);
  1016. }
  1017. bool getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept override
  1018. {
  1019. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  1020. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  1021. int32_t rindex = pData->param.data[parameterId].rindex;
  1022. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  1023. const char* uri = nullptr;
  1024. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  1025. {
  1026. uri = fRdfDescriptor->Ports[rindex].GroupURI;
  1027. }
  1028. else
  1029. {
  1030. rindex -= static_cast<int32_t>(fRdfDescriptor->PortCount);
  1031. if (rindex < static_cast<int32_t>(fRdfDescriptor->ParameterCount))
  1032. uri = fRdfDescriptor->Parameters[rindex].GroupURI;
  1033. }
  1034. if (uri == nullptr)
  1035. return false;
  1036. for (uint32_t i=0; i<fRdfDescriptor->PortGroupCount; ++i)
  1037. {
  1038. if (std::strcmp(fRdfDescriptor->PortGroups[i].URI, uri) == 0)
  1039. {
  1040. const char* const name = fRdfDescriptor->PortGroups[i].Name;
  1041. const char* const symbol = fRdfDescriptor->PortGroups[i].Symbol;
  1042. if (name != nullptr && symbol != nullptr)
  1043. {
  1044. std::snprintf(strBuf, STR_MAX, "%s:%s", symbol, name);
  1045. return true;
  1046. }
  1047. return false;
  1048. }
  1049. }
  1050. return false;
  1051. }
  1052. bool getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  1053. {
  1054. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  1055. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  1056. const int32_t rindex(pData->param.data[parameterId].rindex);
  1057. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, false);
  1058. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  1059. {
  1060. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  1061. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, false);
  1062. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  1063. if (portScalePoint->Label != nullptr)
  1064. {
  1065. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  1066. return true;
  1067. }
  1068. }
  1069. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  1070. }
  1071. // -------------------------------------------------------------------
  1072. // Set data (state)
  1073. void prepareForSave() override
  1074. {
  1075. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1076. if (fExt.state != nullptr && fExt.state->save != nullptr)
  1077. {
  1078. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  1079. if (fHandle2 != nullptr)
  1080. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  1081. }
  1082. }
  1083. // -------------------------------------------------------------------
  1084. // Set data (internal stuff)
  1085. void setName(const char* const newName) override
  1086. {
  1087. CarlaPlugin::setName(newName);
  1088. if (fLv2Options.windowTitle == nullptr)
  1089. return;
  1090. CarlaString guiTitle(pData->name);
  1091. guiTitle += " (GUI)";
  1092. delete[] fLv2Options.windowTitle;
  1093. fLv2Options.windowTitle = guiTitle.dup();
  1094. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  1095. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].value = fLv2Options.windowTitle;
  1096. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  1097. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fLv2Options.windowTitle;
  1098. if (fPipeServer.isPipeRunning())
  1099. fPipeServer.writeUiTitleMessage(fLv2Options.windowTitle);
  1100. #ifndef LV2_UIS_ONLY_BRIDGES
  1101. if (fUI.window != nullptr)
  1102. fUI.window->setTitle(fLv2Options.windowTitle);
  1103. #endif
  1104. }
  1105. // -------------------------------------------------------------------
  1106. // Set data (plugin-specific stuff)
  1107. float setParamterValueCommon(const uint32_t parameterId, const float value) noexcept
  1108. {
  1109. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  1110. fParamBuffers[parameterId] = fixedValue;
  1111. if (pData->param.data[parameterId].rindex >= static_cast<int32_t>(fRdfDescriptor->PortCount))
  1112. {
  1113. const uint32_t rparamId = static_cast<uint32_t>(pData->param.data[parameterId].rindex) - fRdfDescriptor->PortCount;
  1114. CARLA_SAFE_ASSERT_UINT2_RETURN(rparamId < fRdfDescriptor->ParameterCount,
  1115. rparamId, fRdfDescriptor->PortCount, fixedValue);
  1116. uint8_t atomBuf[256];
  1117. lv2_atom_forge_set_buffer(&fAtomForge, atomBuf, sizeof(atomBuf));
  1118. LV2_Atom_Forge_Frame forgeFrame;
  1119. lv2_atom_forge_object(&fAtomForge, &forgeFrame, kUridNull, kUridPatchSet);
  1120. lv2_atom_forge_key(&fAtomForge, kUridPatchPoperty);
  1121. lv2_atom_forge_urid(&fAtomForge, getCustomURID(fRdfDescriptor->Parameters[rparamId].URI));
  1122. lv2_atom_forge_key(&fAtomForge, kUridPatchValue);
  1123. switch (fRdfDescriptor->Parameters[rparamId].Type)
  1124. {
  1125. case LV2_PARAMETER_BOOL:
  1126. lv2_atom_forge_bool(&fAtomForge, fixedValue > 0.5f);
  1127. break;
  1128. case LV2_PARAMETER_INT:
  1129. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(fixedValue + 0.5f));
  1130. break;
  1131. case LV2_PARAMETER_LONG:
  1132. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(fixedValue + 0.5f));
  1133. break;
  1134. case LV2_PARAMETER_FLOAT:
  1135. lv2_atom_forge_float(&fAtomForge, fixedValue);
  1136. break;
  1137. case LV2_PARAMETER_DOUBLE:
  1138. lv2_atom_forge_double(&fAtomForge, fixedValue);
  1139. break;
  1140. default:
  1141. carla_stderr2("setParameterValue called for invalid parameter, expect issues!");
  1142. break;
  1143. }
  1144. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  1145. LV2_Atom* const atom((LV2_Atom*)atomBuf);
  1146. CARLA_SAFE_ASSERT(atom->size < sizeof(atomBuf));
  1147. fAtomBufferEvIn.put(atom, fEventsIn.ctrlIndex);
  1148. }
  1149. return fixedValue;
  1150. }
  1151. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  1152. {
  1153. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  1154. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1155. const float fixedValue = setParamterValueCommon(parameterId, value);
  1156. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  1157. }
  1158. void setParameterValueRT(const uint32_t parameterId, const float value, const bool sendCallbackLater) noexcept override
  1159. {
  1160. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  1161. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1162. const float fixedValue = setParamterValueCommon(parameterId, value);
  1163. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, sendCallbackLater);
  1164. }
  1165. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  1166. {
  1167. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1168. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1169. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1170. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1171. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1172. carla_debug("CarlaPluginLV2::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  1173. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  1174. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  1175. CarlaPlugin::setCustomData(type, key, value, sendGui);
  1176. }
  1177. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  1178. {
  1179. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1180. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1181. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,);
  1182. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  1183. {
  1184. const LV2_URID_Map* const uridMap = (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  1185. LilvState* const state = Lv2WorldClass::getInstance().getStateFromURI(fRdfDescriptor->Presets[index].URI,
  1186. uridMap);
  1187. CARLA_SAFE_ASSERT_RETURN(state != nullptr,);
  1188. // invalidate midi-program selection
  1189. CarlaPlugin::setMidiProgram(-1, false, false, sendCallback, false);
  1190. if (fExt.state != nullptr)
  1191. {
  1192. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  1193. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1194. if (fHandle2 != nullptr)
  1195. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1196. }
  1197. else
  1198. {
  1199. lilv_state_emit_port_values(state, carla_lilv_set_port_value, this);
  1200. }
  1201. lilv_state_free(state);
  1202. }
  1203. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  1204. }
  1205. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  1206. {
  1207. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1208. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1209. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  1210. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  1211. {
  1212. const uint32_t bank(pData->midiprog.data[index].bank);
  1213. const uint32_t program(pData->midiprog.data[index].program);
  1214. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  1215. try {
  1216. fExt.programs->select_program(fHandle, bank, program);
  1217. } catch(...) {}
  1218. if (fHandle2 != nullptr)
  1219. {
  1220. try {
  1221. fExt.programs->select_program(fHandle2, bank, program);
  1222. } catch(...) {}
  1223. }
  1224. }
  1225. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  1226. }
  1227. void setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept override
  1228. {
  1229. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1230. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1231. if (fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  1232. {
  1233. const uint32_t bank(pData->midiprog.data[uindex].bank);
  1234. const uint32_t program(pData->midiprog.data[uindex].program);
  1235. try {
  1236. fExt.programs->select_program(fHandle, bank, program);
  1237. } catch(...) {}
  1238. if (fHandle2 != nullptr)
  1239. {
  1240. try {
  1241. fExt.programs->select_program(fHandle2, bank, program);
  1242. } catch(...) {}
  1243. }
  1244. }
  1245. CarlaPlugin::setMidiProgramRT(uindex, sendCallbackLater);
  1246. }
  1247. // -------------------------------------------------------------------
  1248. // Set ui stuff
  1249. void showCustomUI(const bool yesNo) override
  1250. {
  1251. if (fUI.type == UI::TYPE_NULL)
  1252. {
  1253. if (fFilePathURI.isNotEmpty())
  1254. {
  1255. const char* const path = pData->engine->runFileCallback(FILE_CALLBACK_OPEN, false, "Open File", "");
  1256. if (path != nullptr && path[0] != '\0')
  1257. {
  1258. carla_stdout("LV2 file path to send: '%s'", path);
  1259. writeAtomPath(path, getCustomURID(fFilePathURI));
  1260. }
  1261. }
  1262. else
  1263. {
  1264. CARLA_SAFE_ASSERT(!yesNo);
  1265. }
  1266. pData->engine->callback(true, true,
  1267. ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  1268. return;
  1269. }
  1270. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  1271. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1272. if (! yesNo)
  1273. pData->transientTryCounter = 0;
  1274. #endif
  1275. if (fUI.type == UI::TYPE_BRIDGE)
  1276. {
  1277. if (yesNo)
  1278. {
  1279. if (fPipeServer.isPipeRunning())
  1280. {
  1281. fPipeServer.writeFocusMessage();
  1282. return;
  1283. }
  1284. if (! fPipeServer.startPipeServer(std::min(fLv2Options.sequenceSize, 819200)))
  1285. {
  1286. pData->engine->callback(true, true,
  1287. ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  1288. return;
  1289. }
  1290. // manually write messages so we can take the lock for ourselves
  1291. {
  1292. char tmpBuf[0xff];
  1293. tmpBuf[0xfe] = '\0';
  1294. const CarlaMutexLocker cml(fPipeServer.getPipeLock());
  1295. const CarlaScopedLocale csl;
  1296. // write URI mappings
  1297. uint32_t u = 0;
  1298. for (std::vector<std::string>::iterator it=fCustomURIDs.begin(), end=fCustomURIDs.end(); it != end; ++it, ++u)
  1299. {
  1300. if (u < kUridCount)
  1301. continue;
  1302. const std::string& uri(*it);
  1303. if (! fPipeServer.writeMessage("urid\n", 5))
  1304. return;
  1305. std::snprintf(tmpBuf, 0xfe, "%u\n", u);
  1306. if (! fPipeServer.writeMessage(tmpBuf))
  1307. return;
  1308. std::snprintf(tmpBuf, 0xfe, "%lu\n", static_cast<long unsigned>(uri.length()));
  1309. if (! fPipeServer.writeMessage(tmpBuf))
  1310. return;
  1311. if (! fPipeServer.writeAndFixMessage(uri.c_str()))
  1312. return;
  1313. }
  1314. // write UI options
  1315. if (! fPipeServer.writeMessage("uiOptions\n", 10))
  1316. return;
  1317. const EngineOptions& opts(pData->engine->getOptions());
  1318. std::snprintf(tmpBuf, 0xff, "%g\n", pData->engine->getSampleRate());
  1319. if (! fPipeServer.writeMessage(tmpBuf))
  1320. return;
  1321. std::snprintf(tmpBuf, 0xff, "%u\n", opts.bgColor);
  1322. if (! fPipeServer.writeMessage(tmpBuf))
  1323. return;
  1324. std::snprintf(tmpBuf, 0xff, "%u\n", opts.fgColor);
  1325. if (! fPipeServer.writeMessage(tmpBuf))
  1326. return;
  1327. std::snprintf(tmpBuf, 0xff, "%.12g\n", static_cast<double>(opts.uiScale));
  1328. if (! fPipeServer.writeMessage(tmpBuf))
  1329. return;
  1330. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(true)); // useTheme
  1331. if (! fPipeServer.writeMessage(tmpBuf))
  1332. return;
  1333. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(true)); // useThemeColors
  1334. if (! fPipeServer.writeMessage(tmpBuf))
  1335. return;
  1336. if (! fPipeServer.writeAndFixMessage(fLv2Options.windowTitle != nullptr
  1337. ? fLv2Options.windowTitle
  1338. : ""))
  1339. return;
  1340. std::snprintf(tmpBuf, 0xff, P_INTPTR "\n", frontendWinId);
  1341. if (! fPipeServer.writeMessage(tmpBuf))
  1342. return;
  1343. // write parameter values
  1344. for (uint32_t i=0; i < pData->param.count; ++i)
  1345. {
  1346. if (! fPipeServer.writeMessage("control\n", 8))
  1347. return;
  1348. std::snprintf(tmpBuf, 0xff, "%i\n", pData->param.data[i].rindex);
  1349. if (! fPipeServer.writeMessage(tmpBuf))
  1350. return;
  1351. std::snprintf(tmpBuf, 0xff, "%.12g\n", static_cast<double>(getParameterValue(i)));
  1352. if (! fPipeServer.writeMessage(tmpBuf))
  1353. return;
  1354. }
  1355. // ready to show
  1356. if (! fPipeServer.writeMessage("show\n", 5))
  1357. return;
  1358. fPipeServer.flushMessages();
  1359. }
  1360. #ifndef BUILD_BRIDGE
  1361. if (fUI.rdfDescriptor->Type == LV2_UI_MOD)
  1362. pData->tryTransient();
  1363. #endif
  1364. }
  1365. else
  1366. {
  1367. fPipeServer.stopPipeServer(pData->engine->getOptions().uiBridgesTimeout);
  1368. }
  1369. return;
  1370. }
  1371. // take some precautions
  1372. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  1373. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr,);
  1374. if (yesNo)
  1375. {
  1376. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->instantiate != nullptr,);
  1377. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->cleanup != nullptr,);
  1378. }
  1379. else
  1380. {
  1381. if (fUI.handle == nullptr)
  1382. return;
  1383. }
  1384. if (yesNo)
  1385. {
  1386. if (fUI.handle == nullptr)
  1387. {
  1388. #ifndef LV2_UIS_ONLY_BRIDGES
  1389. if (fUI.type == UI::TYPE_EMBED && fUI.rdfDescriptor->Type != LV2_UI_NONE && fUI.window == nullptr)
  1390. {
  1391. const char* msg = nullptr;
  1392. switch (fUI.rdfDescriptor->Type)
  1393. {
  1394. case LV2_UI_GTK2:
  1395. case LV2_UI_GTK3:
  1396. case LV2_UI_QT4:
  1397. case LV2_UI_QT5:
  1398. case LV2_UI_EXTERNAL:
  1399. case LV2_UI_OLD_EXTERNAL:
  1400. msg = "Invalid UI type";
  1401. break;
  1402. case LV2_UI_COCOA:
  1403. # ifdef CARLA_OS_MAC
  1404. fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId, isUiResizable());
  1405. # else
  1406. msg = "UI is for MacOS only";
  1407. # endif
  1408. break;
  1409. case LV2_UI_WINDOWS:
  1410. # ifdef CARLA_OS_WIN
  1411. fUI.window = CarlaPluginUI::newWindows(this, frontendWinId, isUiResizable());
  1412. # else
  1413. msg = "UI is for Windows only";
  1414. # endif
  1415. break;
  1416. case LV2_UI_X11:
  1417. # ifdef HAVE_X11
  1418. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, isUiResizable());
  1419. # else
  1420. msg = "UI is only for systems with X11";
  1421. # endif
  1422. break;
  1423. default:
  1424. msg = "Unknown UI type";
  1425. break;
  1426. }
  1427. if (fUI.window == nullptr && fExt.uishow == nullptr)
  1428. return pData->engine->callback(true, true,
  1429. ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, msg);
  1430. if (fUI.window != nullptr)
  1431. fFeatures[kFeatureIdUiParent]->data = fUI.window->getPtr();
  1432. }
  1433. #endif
  1434. fUI.widget = nullptr;
  1435. fUI.handle = fUI.descriptor->instantiate(fUI.descriptor, fRdfDescriptor->URI, fUI.rdfDescriptor->Bundle,
  1436. carla_lv2_ui_write_function, this, &fUI.widget, fFeatures);
  1437. if (fUI.window != nullptr)
  1438. {
  1439. if (fUI.widget != nullptr)
  1440. fUI.window->setChildWindow(fUI.widget);
  1441. fUI.window->setTitle(fLv2Options.windowTitle);
  1442. }
  1443. }
  1444. CARLA_SAFE_ASSERT(fUI.handle != nullptr);
  1445. CARLA_SAFE_ASSERT(fUI.type != UI::TYPE_EXTERNAL || fUI.widget != nullptr);
  1446. if (fUI.handle == nullptr || (fUI.type == UI::TYPE_EXTERNAL && fUI.widget == nullptr))
  1447. {
  1448. fUI.widget = nullptr;
  1449. if (fUI.handle != nullptr)
  1450. {
  1451. fUI.descriptor->cleanup(fUI.handle);
  1452. fUI.handle = nullptr;
  1453. }
  1454. return pData->engine->callback(true, true,
  1455. ENGINE_CALLBACK_UI_STATE_CHANGED,
  1456. pData->id,
  1457. -1,
  1458. 0, 0, 0.0f,
  1459. "Plugin refused to open its own UI");
  1460. }
  1461. updateUi();
  1462. #ifndef LV2_UIS_ONLY_BRIDGES
  1463. if (fUI.type == UI::TYPE_EMBED)
  1464. {
  1465. if (fUI.window != nullptr)
  1466. {
  1467. fUI.window->show();
  1468. }
  1469. else if (fExt.uishow != nullptr)
  1470. {
  1471. fExt.uishow->show(fUI.handle);
  1472. # ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1473. pData->tryTransient();
  1474. # endif
  1475. }
  1476. }
  1477. else
  1478. #endif
  1479. {
  1480. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUI.widget);
  1481. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1482. pData->tryTransient();
  1483. #endif
  1484. }
  1485. }
  1486. else
  1487. {
  1488. #ifndef LV2_UIS_ONLY_BRIDGES
  1489. if (fUI.type == UI::TYPE_EMBED)
  1490. {
  1491. if (fUI.window != nullptr)
  1492. fUI.window->hide();
  1493. else if (fExt.uishow != nullptr)
  1494. fExt.uishow->hide(fUI.handle);
  1495. }
  1496. else
  1497. #endif
  1498. {
  1499. CARLA_SAFE_ASSERT(fUI.widget != nullptr);
  1500. if (fUI.widget != nullptr)
  1501. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUI.widget);
  1502. }
  1503. fUI.descriptor->cleanup(fUI.handle);
  1504. fUI.handle = nullptr;
  1505. fUI.widget = nullptr;
  1506. }
  1507. }
  1508. void idle() override
  1509. {
  1510. if (fAtomBufferWorkerIn.isDataAvailableForReading())
  1511. {
  1512. Lv2AtomRingBuffer tmpRingBuffer(fAtomBufferWorkerIn, fAtomBufferWorkerInTmpData);
  1513. CARLA_SAFE_ASSERT_RETURN(tmpRingBuffer.isDataAvailableForReading(),);
  1514. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr,);
  1515. uint32_t portIndex;
  1516. const LV2_Atom* atom;
  1517. for (; tmpRingBuffer.get(atom, portIndex);)
  1518. {
  1519. CARLA_SAFE_ASSERT_CONTINUE(atom->type == kUridCarlaAtomWorkerIn);
  1520. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1521. }
  1522. }
  1523. if (fInlineDisplayNeedsRedraw)
  1524. {
  1525. // TESTING
  1526. CARLA_SAFE_ASSERT(pData->enabled)
  1527. CARLA_SAFE_ASSERT(!pData->engine->isAboutToClose());
  1528. CARLA_SAFE_ASSERT(pData->client->isActive());
  1529. if (pData->enabled && !pData->engine->isAboutToClose() && pData->client->isActive())
  1530. {
  1531. const int64_t timeNow = water::Time::currentTimeMillis();
  1532. if (timeNow - fInlineDisplayLastRedrawTime > (1000 / 30))
  1533. {
  1534. fInlineDisplayNeedsRedraw = false;
  1535. fInlineDisplayLastRedrawTime = timeNow;
  1536. pData->engine->callback(true, true,
  1537. ENGINE_CALLBACK_INLINE_DISPLAY_REDRAW,
  1538. pData->id,
  1539. 0, 0, 0, 0.0f, nullptr);
  1540. }
  1541. }
  1542. }
  1543. CarlaPlugin::idle();
  1544. }
  1545. void uiIdle() override
  1546. {
  1547. if (const char* const fileNeededForURI = fUI.fileNeededForURI)
  1548. {
  1549. fUI.fileBrowserOpen = true;
  1550. fUI.fileNeededForURI = nullptr;
  1551. const char* const path = pData->engine->runFileCallback(FILE_CALLBACK_OPEN,
  1552. /* isDir */ false,
  1553. /* title */ "File open",
  1554. /* filters */ "");
  1555. fUI.fileBrowserOpen = false;
  1556. if (path != nullptr)
  1557. {
  1558. carla_stdout("LV2 requested path to send: '%s'", path);
  1559. writeAtomPath(path, getCustomURID(fileNeededForURI));
  1560. }
  1561. // this function will be called recursively, stop here
  1562. return;
  1563. }
  1564. if (fAtomBufferUiOut.isDataAvailableForReading())
  1565. {
  1566. Lv2AtomRingBuffer tmpRingBuffer(fAtomBufferUiOut, fAtomBufferUiOutTmpData);
  1567. CARLA_SAFE_ASSERT(tmpRingBuffer.isDataAvailableForReading());
  1568. uint32_t portIndex;
  1569. const LV2_Atom* atom;
  1570. const bool hasPortEvent(fUI.handle != nullptr &&
  1571. fUI.descriptor != nullptr &&
  1572. fUI.descriptor->port_event != nullptr);
  1573. for (; tmpRingBuffer.get(atom, portIndex);)
  1574. {
  1575. if (fUI.type == UI::TYPE_BRIDGE)
  1576. {
  1577. if (fPipeServer.isPipeRunning())
  1578. fPipeServer.writeLv2AtomMessage(portIndex, atom);
  1579. }
  1580. else
  1581. {
  1582. if (hasPortEvent && ! fNeedsUiClose)
  1583. fUI.descriptor->port_event(fUI.handle, portIndex, lv2_atom_total_size(atom), kUridAtomTransferEvent, atom);
  1584. }
  1585. }
  1586. }
  1587. if (fPipeServer.isPipeRunning())
  1588. {
  1589. fPipeServer.idlePipe();
  1590. switch (fPipeServer.getAndResetUiState())
  1591. {
  1592. case CarlaPipeServerLV2::UiNone:
  1593. case CarlaPipeServerLV2::UiShow:
  1594. break;
  1595. case CarlaPipeServerLV2::UiHide:
  1596. fPipeServer.stopPipeServer(2000);
  1597. // fall through
  1598. case CarlaPipeServerLV2::UiCrashed:
  1599. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1600. pData->transientTryCounter = 0;
  1601. #endif
  1602. pData->engine->callback(true, true,
  1603. ENGINE_CALLBACK_UI_STATE_CHANGED,
  1604. pData->id,
  1605. 0,
  1606. 0, 0, 0.0f, nullptr);
  1607. break;
  1608. }
  1609. }
  1610. else
  1611. {
  1612. // TODO - detect if ui-bridge crashed
  1613. }
  1614. if (fNeedsUiClose)
  1615. {
  1616. fNeedsUiClose = false;
  1617. showCustomUI(false);
  1618. pData->engine->callback(true, true,
  1619. ENGINE_CALLBACK_UI_STATE_CHANGED,
  1620. pData->id,
  1621. 0,
  1622. 0, 0, 0.0f, nullptr);
  1623. }
  1624. else if (fUI.handle != nullptr && fUI.descriptor != nullptr)
  1625. {
  1626. if (fUI.type == UI::TYPE_EXTERNAL && fUI.widget != nullptr)
  1627. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUI.widget);
  1628. #ifndef LV2_UIS_ONLY_BRIDGES
  1629. else if (fUI.type == UI::TYPE_EMBED && fUI.window != nullptr)
  1630. fUI.window->idle();
  1631. // note: UI might have been closed by window idle
  1632. if (fNeedsUiClose)
  1633. {
  1634. pass();
  1635. }
  1636. else if (fUI.handle != nullptr && fExt.uiidle != nullptr && fExt.uiidle->idle(fUI.handle) != 0)
  1637. {
  1638. showCustomUI(false);
  1639. pData->engine->callback(true, true,
  1640. ENGINE_CALLBACK_UI_STATE_CHANGED,
  1641. pData->id,
  1642. 0,
  1643. 0, 0, 0.0f, nullptr);
  1644. CARLA_SAFE_ASSERT(fUI.handle == nullptr);
  1645. }
  1646. #endif
  1647. }
  1648. CarlaPlugin::uiIdle();
  1649. }
  1650. // -------------------------------------------------------------------
  1651. // Plugin state
  1652. void reload() override
  1653. {
  1654. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1655. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1656. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1657. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1658. carla_debug("CarlaPluginLV2::reload() - start");
  1659. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1660. // Safely disable plugin for reload
  1661. const ScopedDisabler sd(this);
  1662. if (pData->active)
  1663. deactivate();
  1664. clearBuffers();
  1665. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1666. const uint32_t portCount(fRdfDescriptor->PortCount);
  1667. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1668. aIns = aOuts = cvIns = cvOuts = params = 0;
  1669. LinkedList<uint> evIns, evOuts;
  1670. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize)+0xff);
  1671. bool forcedStereoIn, forcedStereoOut;
  1672. forcedStereoIn = forcedStereoOut = false;
  1673. bool needsCtrlIn, needsCtrlOut;
  1674. needsCtrlIn = needsCtrlOut = false;
  1675. for (uint32_t i=0; i < portCount; ++i)
  1676. {
  1677. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1678. if (LV2_IS_PORT_AUDIO(portTypes))
  1679. {
  1680. if (LV2_IS_PORT_INPUT(portTypes))
  1681. aIns += 1;
  1682. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1683. aOuts += 1;
  1684. }
  1685. else if (LV2_IS_PORT_CV(portTypes))
  1686. {
  1687. if (LV2_IS_PORT_INPUT(portTypes))
  1688. cvIns += 1;
  1689. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1690. cvOuts += 1;
  1691. }
  1692. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1693. {
  1694. if (LV2_IS_PORT_INPUT(portTypes))
  1695. evIns.append(CARLA_EVENT_DATA_ATOM);
  1696. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1697. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1698. }
  1699. else if (LV2_IS_PORT_EVENT(portTypes))
  1700. {
  1701. if (LV2_IS_PORT_INPUT(portTypes))
  1702. evIns.append(CARLA_EVENT_DATA_EVENT);
  1703. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1704. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1705. }
  1706. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1707. {
  1708. if (LV2_IS_PORT_INPUT(portTypes))
  1709. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1710. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1711. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1712. }
  1713. else if (LV2_IS_PORT_CONTROL(portTypes))
  1714. params += 1;
  1715. }
  1716. for (uint32_t i=0; i < fRdfDescriptor->ParameterCount; ++i)
  1717. {
  1718. switch (fRdfDescriptor->Parameters[i].Type)
  1719. {
  1720. case LV2_PARAMETER_BOOL:
  1721. case LV2_PARAMETER_INT:
  1722. // case LV2_PARAMETER_LONG:
  1723. case LV2_PARAMETER_FLOAT:
  1724. case LV2_PARAMETER_DOUBLE:
  1725. params += 1;
  1726. break;
  1727. case LV2_PARAMETER_PATH:
  1728. if (fFilePathURI.isEmpty())
  1729. fFilePathURI = fRdfDescriptor->Parameters[i].URI;
  1730. break;
  1731. }
  1732. }
  1733. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && aIns <= 1 && aOuts <= 1 && fExt.state == nullptr && fExt.worker == nullptr)
  1734. {
  1735. if (fHandle2 == nullptr)
  1736. {
  1737. try {
  1738. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1739. } catch(...) {}
  1740. }
  1741. if (fHandle2 != nullptr)
  1742. {
  1743. if (aIns == 1)
  1744. {
  1745. aIns = 2;
  1746. forcedStereoIn = true;
  1747. }
  1748. if (aOuts == 1)
  1749. {
  1750. aOuts = 2;
  1751. forcedStereoOut = true;
  1752. }
  1753. }
  1754. }
  1755. if (aIns > 0)
  1756. {
  1757. pData->audioIn.createNew(aIns);
  1758. fAudioInBuffers = new float*[aIns];
  1759. for (uint32_t i=0; i < aIns; ++i)
  1760. fAudioInBuffers[i] = nullptr;
  1761. }
  1762. if (aOuts > 0)
  1763. {
  1764. pData->audioOut.createNew(aOuts);
  1765. fAudioOutBuffers = new float*[aOuts];
  1766. needsCtrlIn = true;
  1767. for (uint32_t i=0; i < aOuts; ++i)
  1768. fAudioOutBuffers[i] = nullptr;
  1769. }
  1770. if (cvIns > 0)
  1771. {
  1772. pData->cvIn.createNew(cvIns);
  1773. fCvInBuffers = new float*[cvIns];
  1774. for (uint32_t i=0; i < cvIns; ++i)
  1775. fCvInBuffers[i] = nullptr;
  1776. }
  1777. if (cvOuts > 0)
  1778. {
  1779. pData->cvOut.createNew(cvOuts);
  1780. fCvOutBuffers = new float*[cvOuts];
  1781. for (uint32_t i=0; i < cvOuts; ++i)
  1782. fCvOutBuffers[i] = nullptr;
  1783. }
  1784. if (params > 0)
  1785. {
  1786. pData->param.createNew(params, true);
  1787. fParamBuffers = new float[params];
  1788. carla_zeroFloats(fParamBuffers, params);
  1789. }
  1790. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1791. {
  1792. fEventsIn.createNew(count);
  1793. for (uint32_t i=0; i < count; ++i)
  1794. {
  1795. const uint32_t& type(evIns.getAt(i, 0x0));
  1796. if (type == CARLA_EVENT_DATA_ATOM)
  1797. {
  1798. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1799. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, kUridNull, kUridAtomSequence, true);
  1800. }
  1801. else if (type == CARLA_EVENT_DATA_EVENT)
  1802. {
  1803. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1804. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1805. }
  1806. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1807. {
  1808. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1809. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1810. fEventsIn.data[i].midi.data = new uchar[eventBufferSize];
  1811. }
  1812. }
  1813. }
  1814. else
  1815. {
  1816. fEventsIn.createNew(1);
  1817. fEventsIn.ctrl = &fEventsIn.data[0];
  1818. }
  1819. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1820. {
  1821. fEventsOut.createNew(count);
  1822. for (uint32_t i=0; i < count; ++i)
  1823. {
  1824. const uint32_t& type(evOuts.getAt(i, 0x0));
  1825. if (type == CARLA_EVENT_DATA_ATOM)
  1826. {
  1827. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1828. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, kUridNull, kUridAtomSequence, false);
  1829. }
  1830. else if (type == CARLA_EVENT_DATA_EVENT)
  1831. {
  1832. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1833. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1834. }
  1835. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1836. {
  1837. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1838. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1839. fEventsOut.data[i].midi.data = new uchar[eventBufferSize];
  1840. }
  1841. }
  1842. }
  1843. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1844. CarlaString portName;
  1845. uint32_t iCtrl = 0;
  1846. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0; i < portCount; ++i)
  1847. {
  1848. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1849. portName.clear();
  1850. 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))
  1851. {
  1852. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1853. {
  1854. portName = pData->name;
  1855. portName += ":";
  1856. }
  1857. portName += fRdfDescriptor->Ports[i].Name;
  1858. portName.truncate(portNameSize);
  1859. }
  1860. if (LV2_IS_PORT_AUDIO(portTypes))
  1861. {
  1862. if (LV2_IS_PORT_INPUT(portTypes))
  1863. {
  1864. const uint32_t j = iAudioIn++;
  1865. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  1866. pData->audioIn.ports[j].rindex = i;
  1867. if (forcedStereoIn)
  1868. {
  1869. portName += "_2";
  1870. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  1871. pData->audioIn.ports[1].rindex = i;
  1872. }
  1873. }
  1874. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1875. {
  1876. const uint32_t j = iAudioOut++;
  1877. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  1878. pData->audioOut.ports[j].rindex = i;
  1879. if (forcedStereoOut)
  1880. {
  1881. portName += "_2";
  1882. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  1883. pData->audioOut.ports[1].rindex = i;
  1884. }
  1885. }
  1886. else
  1887. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1888. }
  1889. else if (LV2_IS_PORT_CV(portTypes))
  1890. {
  1891. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1892. float min, max;
  1893. // min value
  1894. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1895. min = portPoints.Minimum;
  1896. else
  1897. min = -1.0f;
  1898. // max value
  1899. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1900. max = portPoints.Maximum;
  1901. else
  1902. max = 1.0f;
  1903. if (LV2_IS_PORT_INPUT(portTypes))
  1904. {
  1905. const uint32_t j = iCvIn++;
  1906. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  1907. pData->cvIn.ports[j].rindex = i;
  1908. pData->cvIn.ports[j].port->setRange(min, max);
  1909. }
  1910. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1911. {
  1912. const uint32_t j = iCvOut++;
  1913. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  1914. pData->cvOut.ports[j].rindex = i;
  1915. pData->cvOut.ports[j].port->setRange(min, max);
  1916. }
  1917. else
  1918. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1919. }
  1920. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1921. {
  1922. if (LV2_IS_PORT_INPUT(portTypes))
  1923. {
  1924. const uint32_t j = iEvIn++;
  1925. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1926. if (fHandle2 != nullptr)
  1927. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1928. fEventsIn.data[j].rindex = i;
  1929. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1930. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1931. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1932. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1933. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1934. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1935. if (evIns.count() == 1)
  1936. {
  1937. fEventsIn.ctrl = &fEventsIn.data[j];
  1938. fEventsIn.ctrlIndex = j;
  1939. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1940. needsCtrlIn = true;
  1941. }
  1942. else
  1943. {
  1944. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1945. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1946. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1947. {
  1948. fEventsIn.ctrl = &fEventsIn.data[j];
  1949. fEventsIn.ctrlIndex = j;
  1950. }
  1951. }
  1952. }
  1953. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1954. {
  1955. const uint32_t j = iEvOut++;
  1956. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1957. if (fHandle2 != nullptr)
  1958. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1959. fEventsOut.data[j].rindex = i;
  1960. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1961. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1962. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1963. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1964. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1965. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1966. if (evOuts.count() == 1)
  1967. {
  1968. fEventsOut.ctrl = &fEventsOut.data[j];
  1969. fEventsOut.ctrlIndex = j;
  1970. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1971. needsCtrlOut = true;
  1972. }
  1973. else
  1974. {
  1975. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1976. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1977. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1978. {
  1979. fEventsOut.ctrl = &fEventsOut.data[j];
  1980. fEventsOut.ctrlIndex = j;
  1981. }
  1982. }
  1983. }
  1984. else
  1985. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1986. }
  1987. else if (LV2_IS_PORT_EVENT(portTypes))
  1988. {
  1989. if (LV2_IS_PORT_INPUT(portTypes))
  1990. {
  1991. const uint32_t j = iEvIn++;
  1992. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1993. if (fHandle2 != nullptr)
  1994. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1995. fEventsIn.data[j].rindex = i;
  1996. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1997. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1998. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1999. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  2000. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  2001. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  2002. if (evIns.count() == 1)
  2003. {
  2004. fEventsIn.ctrl = &fEventsIn.data[j];
  2005. fEventsIn.ctrlIndex = j;
  2006. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  2007. needsCtrlIn = true;
  2008. }
  2009. else
  2010. {
  2011. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  2012. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  2013. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  2014. {
  2015. fEventsIn.ctrl = &fEventsIn.data[j];
  2016. fEventsIn.ctrlIndex = j;
  2017. }
  2018. }
  2019. }
  2020. else if (LV2_IS_PORT_OUTPUT(portTypes))
  2021. {
  2022. const uint32_t j = iEvOut++;
  2023. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  2024. if (fHandle2 != nullptr)
  2025. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  2026. fEventsOut.data[j].rindex = i;
  2027. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  2028. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  2029. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  2030. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  2031. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  2032. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  2033. if (evOuts.count() == 1)
  2034. {
  2035. fEventsOut.ctrl = &fEventsOut.data[j];
  2036. fEventsOut.ctrlIndex = j;
  2037. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  2038. needsCtrlOut = true;
  2039. }
  2040. else
  2041. {
  2042. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  2043. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  2044. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  2045. {
  2046. fEventsOut.ctrl = &fEventsOut.data[j];
  2047. fEventsOut.ctrlIndex = j;
  2048. }
  2049. }
  2050. }
  2051. else
  2052. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  2053. }
  2054. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  2055. {
  2056. if (LV2_IS_PORT_INPUT(portTypes))
  2057. {
  2058. const uint32_t j = iEvIn++;
  2059. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  2060. if (fHandle2 != nullptr)
  2061. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  2062. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  2063. fEventsIn.data[j].rindex = i;
  2064. if (evIns.count() == 1)
  2065. {
  2066. needsCtrlIn = true;
  2067. fEventsIn.ctrl = &fEventsIn.data[j];
  2068. fEventsIn.ctrlIndex = j;
  2069. }
  2070. else
  2071. {
  2072. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  2073. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  2074. {
  2075. fEventsIn.ctrl = &fEventsIn.data[j];
  2076. fEventsIn.ctrlIndex = j;
  2077. }
  2078. }
  2079. }
  2080. else if (LV2_IS_PORT_OUTPUT(portTypes))
  2081. {
  2082. const uint32_t j = iEvOut++;
  2083. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  2084. if (fHandle2 != nullptr)
  2085. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  2086. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  2087. fEventsOut.data[j].rindex = i;
  2088. if (evOuts.count() == 1)
  2089. {
  2090. needsCtrlOut = true;
  2091. fEventsOut.ctrl = &fEventsOut.data[j];
  2092. fEventsOut.ctrlIndex = j;
  2093. }
  2094. else
  2095. {
  2096. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  2097. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  2098. {
  2099. fEventsOut.ctrl = &fEventsOut.data[j];
  2100. fEventsOut.ctrlIndex = j;
  2101. }
  2102. }
  2103. }
  2104. else
  2105. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  2106. }
  2107. else if (LV2_IS_PORT_CONTROL(portTypes))
  2108. {
  2109. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  2110. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  2111. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  2112. const uint32_t j = iCtrl++;
  2113. pData->param.data[j].index = static_cast<int32_t>(j);
  2114. pData->param.data[j].rindex = static_cast<int32_t>(i);
  2115. float min, max, def, step, stepSmall, stepLarge;
  2116. // min value
  2117. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  2118. min = portPoints.Minimum;
  2119. else
  2120. min = 0.0f;
  2121. // max value
  2122. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  2123. max = portPoints.Maximum;
  2124. else
  2125. max = 1.0f;
  2126. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  2127. {
  2128. min *= sampleRate;
  2129. max *= sampleRate;
  2130. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  2131. }
  2132. // stupid hack for ir.lv2 (broken plugin)
  2133. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  2134. {
  2135. min = 0.0f;
  2136. max = (float)0xffffff;
  2137. }
  2138. if (min >= max)
  2139. {
  2140. carla_stderr2("WARNING - Broken plugin parameter '%s': min >= max", fRdfDescriptor->Ports[i].Name);
  2141. max = min + 0.1f;
  2142. }
  2143. // default value
  2144. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  2145. {
  2146. def = portPoints.Default;
  2147. }
  2148. else
  2149. {
  2150. // no default value
  2151. if (min < 0.0f && max > 0.0f)
  2152. def = 0.0f;
  2153. else
  2154. def = min;
  2155. }
  2156. if (def < min)
  2157. def = min;
  2158. else if (def > max)
  2159. def = max;
  2160. if (LV2_IS_PORT_TOGGLED(portProps))
  2161. {
  2162. step = max - min;
  2163. stepSmall = step;
  2164. stepLarge = step;
  2165. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  2166. }
  2167. else if (LV2_IS_PORT_INTEGER(portProps))
  2168. {
  2169. step = 1.0f;
  2170. stepSmall = 1.0f;
  2171. stepLarge = 10.0f;
  2172. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  2173. }
  2174. else
  2175. {
  2176. float range = max - min;
  2177. step = range/100.0f;
  2178. stepSmall = range/1000.0f;
  2179. stepLarge = range/10.0f;
  2180. }
  2181. if (LV2_IS_PORT_INPUT(portTypes))
  2182. {
  2183. pData->param.data[j].type = PARAMETER_INPUT;
  2184. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  2185. {
  2186. carla_stderr("Plugin has latency input port, this should not happen!");
  2187. }
  2188. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  2189. {
  2190. def = sampleRate;
  2191. step = 1.0f;
  2192. stepSmall = 1.0f;
  2193. stepLarge = 1.0f;
  2194. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  2195. }
  2196. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  2197. {
  2198. pData->param.special[j] = PARAMETER_SPECIAL_FREEWHEEL;
  2199. }
  2200. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  2201. {
  2202. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  2203. }
  2204. else
  2205. {
  2206. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  2207. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  2208. needsCtrlIn = true;
  2209. if (! LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) &&
  2210. ! LV2_IS_PORT_ENUMERATION(portProps) &&
  2211. ! LV2_IS_PORT_EXPENSIVE(portProps) &&
  2212. ! LV2_IS_PORT_NOT_AUTOMATIC(portProps) &&
  2213. ! LV2_IS_PORT_NOT_ON_GUI(portProps) &&
  2214. ! LV2_IS_PORT_TRIGGER(portProps))
  2215. {
  2216. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  2217. }
  2218. }
  2219. // MIDI CC value
  2220. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  2221. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  2222. {
  2223. if (portMidiMap.Number < MAX_MIDI_CONTROL && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  2224. pData->param.data[j].mappedControlIndex = static_cast<int16_t>(portMidiMap.Number);
  2225. }
  2226. }
  2227. else if (LV2_IS_PORT_OUTPUT(portTypes))
  2228. {
  2229. pData->param.data[j].type = PARAMETER_OUTPUT;
  2230. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  2231. {
  2232. min = 0.0f;
  2233. max = sampleRate;
  2234. def = 0.0f;
  2235. step = 1.0f;
  2236. stepSmall = 1.0f;
  2237. stepLarge = 1.0f;
  2238. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  2239. CARLA_SAFE_ASSERT_INT2(fLatencyIndex == static_cast<int32_t>(j), fLatencyIndex, j);
  2240. }
  2241. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  2242. {
  2243. def = sampleRate;
  2244. step = 1.0f;
  2245. stepSmall = 1.0f;
  2246. stepLarge = 1.0f;
  2247. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  2248. }
  2249. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  2250. {
  2251. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  2252. }
  2253. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  2254. {
  2255. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  2256. }
  2257. else
  2258. {
  2259. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  2260. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  2261. needsCtrlOut = true;
  2262. }
  2263. }
  2264. else
  2265. {
  2266. pData->param.data[j].type = PARAMETER_UNKNOWN;
  2267. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  2268. }
  2269. // extra parameter hints
  2270. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  2271. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  2272. if (LV2_IS_PORT_TRIGGER(portProps))
  2273. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  2274. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  2275. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  2276. if (LV2_IS_PORT_ENUMERATION(portProps))
  2277. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  2278. // check if parameter is not enabled or automable
  2279. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  2280. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  2281. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps))
  2282. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  2283. else if (LV2_IS_PORT_NOT_AUTOMATIC(portProps) || LV2_IS_PORT_NON_AUTOMABLE(portProps))
  2284. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  2285. pData->param.ranges[j].min = min;
  2286. pData->param.ranges[j].max = max;
  2287. pData->param.ranges[j].def = def;
  2288. pData->param.ranges[j].step = step;
  2289. pData->param.ranges[j].stepSmall = stepSmall;
  2290. pData->param.ranges[j].stepLarge = stepLarge;
  2291. // Start parameters in their default values (except freewheel, which is off by default)
  2292. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_FREEWHEEL)
  2293. fParamBuffers[j] = min;
  2294. else
  2295. fParamBuffers[j] = def;
  2296. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  2297. if (fHandle2 != nullptr)
  2298. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  2299. }
  2300. else
  2301. {
  2302. // Port Type not supported, but it's optional anyway
  2303. fDescriptor->connect_port(fHandle, i, nullptr);
  2304. if (fHandle2 != nullptr)
  2305. fDescriptor->connect_port(fHandle2, i, nullptr);
  2306. }
  2307. }
  2308. for (uint32_t i=0; i < fRdfDescriptor->ParameterCount; ++i)
  2309. {
  2310. const LV2_RDF_Parameter& rdfParam(fRdfDescriptor->Parameters[i]);
  2311. switch (rdfParam.Type)
  2312. {
  2313. case LV2_PARAMETER_BOOL:
  2314. case LV2_PARAMETER_INT:
  2315. // case LV2_PARAMETER_LONG:
  2316. case LV2_PARAMETER_FLOAT:
  2317. case LV2_PARAMETER_DOUBLE:
  2318. break;
  2319. default:
  2320. continue;
  2321. }
  2322. const LV2_RDF_PortPoints& portPoints(rdfParam.Points);
  2323. const uint32_t j = iCtrl++;
  2324. pData->param.data[j].index = static_cast<int32_t>(j);
  2325. pData->param.data[j].rindex = static_cast<int32_t>(fRdfDescriptor->PortCount + i);
  2326. float min, max, def, step, stepSmall, stepLarge;
  2327. // min value
  2328. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  2329. min = portPoints.Minimum;
  2330. else
  2331. min = 0.0f;
  2332. // max value
  2333. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  2334. max = portPoints.Maximum;
  2335. else
  2336. max = 1.0f;
  2337. if (min >= max)
  2338. {
  2339. carla_stderr2("WARNING - Broken plugin parameter '%s': min >= max", rdfParam.Label);
  2340. max = min + 0.1f;
  2341. }
  2342. // default value
  2343. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  2344. {
  2345. def = portPoints.Default;
  2346. }
  2347. else
  2348. {
  2349. // no default value
  2350. if (min < 0.0f && max > 0.0f)
  2351. def = 0.0f;
  2352. else
  2353. def = min;
  2354. }
  2355. if (def < min)
  2356. def = min;
  2357. else if (def > max)
  2358. def = max;
  2359. switch (rdfParam.Type)
  2360. {
  2361. case LV2_PARAMETER_BOOL:
  2362. step = max - min;
  2363. stepSmall = step;
  2364. stepLarge = step;
  2365. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  2366. break;
  2367. case LV2_PARAMETER_INT:
  2368. case LV2_PARAMETER_LONG:
  2369. step = 1.0f;
  2370. stepSmall = 1.0f;
  2371. stepLarge = 10.0f;
  2372. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  2373. break;
  2374. default:
  2375. const float range = max - min;
  2376. step = range/100.0f;
  2377. stepSmall = range/1000.0f;
  2378. stepLarge = range/10.0f;
  2379. break;
  2380. }
  2381. if (rdfParam.Input)
  2382. {
  2383. pData->param.data[j].type = PARAMETER_INPUT;
  2384. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  2385. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  2386. needsCtrlIn = true;
  2387. }
  2388. else
  2389. {
  2390. pData->param.data[j].type = PARAMETER_OUTPUT;
  2391. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  2392. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  2393. needsCtrlOut = true;
  2394. }
  2395. pData->param.ranges[j].min = min;
  2396. pData->param.ranges[j].max = max;
  2397. pData->param.ranges[j].def = def;
  2398. pData->param.ranges[j].step = step;
  2399. pData->param.ranges[j].stepSmall = stepSmall;
  2400. pData->param.ranges[j].stepLarge = stepLarge;
  2401. fParamBuffers[j] = def;
  2402. }
  2403. if (needsCtrlIn)
  2404. {
  2405. portName.clear();
  2406. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  2407. {
  2408. portName = pData->name;
  2409. portName += ":";
  2410. }
  2411. portName += "events-in";
  2412. portName.truncate(portNameSize);
  2413. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  2414. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2415. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  2416. #endif
  2417. }
  2418. if (needsCtrlOut)
  2419. {
  2420. portName.clear();
  2421. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  2422. {
  2423. portName = pData->name;
  2424. portName += ":";
  2425. }
  2426. portName += "events-out";
  2427. portName.truncate(portNameSize);
  2428. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  2429. }
  2430. if (fExt.worker != nullptr && fEventsIn.ctrl != nullptr)
  2431. {
  2432. fAtomBufferWorkerIn.createBuffer(eventBufferSize);
  2433. fAtomBufferWorkerResp.createBuffer(eventBufferSize);
  2434. fAtomBufferWorkerInTmpData = new uint8_t[fAtomBufferWorkerIn.getSize()];
  2435. }
  2436. if (fRdfDescriptor->ParameterCount > 0 ||
  2437. (fUI.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  2438. fAtomBufferEvIn.createBuffer(eventBufferSize);
  2439. if (fUI.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0)
  2440. {
  2441. fAtomBufferUiOut.createBuffer(std::min(eventBufferSize*32, 1638400U));
  2442. fAtomBufferUiOutTmpData = new uint8_t[fAtomBufferUiOut.getSize()];
  2443. }
  2444. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  2445. fEventsIn.ctrl->port = pData->event.portIn;
  2446. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  2447. fEventsOut.ctrl->port = pData->event.portOut;
  2448. if (fEventsIn.ctrl != nullptr && fExt.midnam != nullptr)
  2449. {
  2450. if (char* const midnam = fExt.midnam->midnam(fHandle))
  2451. {
  2452. fEventsIn.ctrl->port->setMetaData("http://www.midi.org/dtds/MIDINameDocument10.dtd",
  2453. midnam, "text/xml");
  2454. if (fExt.midnam->free != nullptr)
  2455. fExt.midnam->free(midnam);
  2456. }
  2457. }
  2458. if (forcedStereoIn || forcedStereoOut)
  2459. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2460. else
  2461. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  2462. // plugin hints
  2463. pData->hints = (pData->hints & PLUGIN_HAS_INLINE_DISPLAY) ? PLUGIN_HAS_INLINE_DISPLAY : 0
  2464. | (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD) ? PLUGIN_NEEDS_UI_MAIN_THREAD : 0;
  2465. if (isRealtimeSafe())
  2466. pData->hints |= PLUGIN_IS_RTSAFE;
  2467. if (fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty())
  2468. {
  2469. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  2470. if (fUI.type == UI::TYPE_EMBED || fUI.type == UI::TYPE_EXTERNAL)
  2471. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  2472. }
  2473. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  2474. pData->hints |= PLUGIN_IS_SYNTH;
  2475. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  2476. pData->hints |= PLUGIN_CAN_DRYWET;
  2477. if (aOuts > 0)
  2478. pData->hints |= PLUGIN_CAN_VOLUME;
  2479. if (aOuts >= 2 && aOuts % 2 == 0)
  2480. pData->hints |= PLUGIN_CAN_BALANCE;
  2481. // extra plugin hints
  2482. pData->extraHints = 0x0;
  2483. // check initial latency
  2484. findInitialLatencyValue(aIns, aOuts);
  2485. bufferSizeChanged(pData->engine->getBufferSize());
  2486. reloadPrograms(true);
  2487. evIns.clear();
  2488. evOuts.clear();
  2489. if (pData->active)
  2490. activate();
  2491. carla_debug("CarlaPluginLV2::reload() - end");
  2492. }
  2493. void findInitialLatencyValue(const uint32_t aIns, const uint32_t aOuts) const
  2494. {
  2495. if (fLatencyIndex < 0)
  2496. return;
  2497. // we need to pre-run the plugin so it can update its latency control-port
  2498. const uint32_t bufferSize = static_cast<uint32_t>(fLv2Options.nominalBufferSize);
  2499. float tmpIn [(aIns > 0) ? aIns : 1][bufferSize];
  2500. float tmpOut[(aOuts > 0) ? aOuts : 1][bufferSize];
  2501. for (uint32_t j=0; j < aIns; ++j)
  2502. {
  2503. carla_zeroFloats(tmpIn[j], bufferSize);
  2504. try {
  2505. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  2506. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency input");
  2507. }
  2508. for (uint32_t j=0; j < aOuts; ++j)
  2509. {
  2510. carla_zeroFloats(tmpOut[j], bufferSize);
  2511. try {
  2512. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  2513. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency output");
  2514. }
  2515. if (fDescriptor->activate != nullptr)
  2516. {
  2517. try {
  2518. fDescriptor->activate(fHandle);
  2519. } CARLA_SAFE_EXCEPTION("LV2 latency activate");
  2520. }
  2521. try {
  2522. fDescriptor->run(fHandle, bufferSize);
  2523. } CARLA_SAFE_EXCEPTION("LV2 latency run");
  2524. if (fDescriptor->deactivate != nullptr)
  2525. {
  2526. try {
  2527. fDescriptor->deactivate(fHandle);
  2528. } CARLA_SAFE_EXCEPTION("LV2 latency deactivate");
  2529. }
  2530. // done, let's get the value
  2531. if (const uint32_t latency = getLatencyInFrames())
  2532. {
  2533. pData->client->setLatency(latency);
  2534. #ifndef BUILD_BRIDGE
  2535. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  2536. #endif
  2537. }
  2538. }
  2539. void reloadPrograms(const bool doInit) override
  2540. {
  2541. carla_debug("CarlaPluginLV2::reloadPrograms(%s)", bool2str(doInit));
  2542. const uint32_t oldCount = pData->midiprog.count;
  2543. const int32_t current = pData->midiprog.current;
  2544. // special LV2 programs handling
  2545. if (doInit)
  2546. {
  2547. pData->prog.clear();
  2548. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  2549. if (presetCount > 0)
  2550. {
  2551. pData->prog.createNew(presetCount);
  2552. for (uint32_t i=0; i < presetCount; ++i)
  2553. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  2554. }
  2555. }
  2556. // Delete old programs
  2557. pData->midiprog.clear();
  2558. // Query new programs
  2559. uint32_t newCount = 0;
  2560. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  2561. {
  2562. for (; fExt.programs->get_program(fHandle, newCount);)
  2563. ++newCount;
  2564. }
  2565. if (newCount > 0)
  2566. {
  2567. pData->midiprog.createNew(newCount);
  2568. // Update data
  2569. for (uint32_t i=0; i < newCount; ++i)
  2570. {
  2571. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  2572. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  2573. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  2574. pData->midiprog.data[i].bank = pdesc->bank;
  2575. pData->midiprog.data[i].program = pdesc->program;
  2576. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  2577. }
  2578. }
  2579. if (doInit)
  2580. {
  2581. if (newCount > 0)
  2582. {
  2583. setMidiProgram(0, false, false, false, true);
  2584. }
  2585. else
  2586. {
  2587. // load default state
  2588. if (LilvState* const state = Lv2WorldClass::getInstance().getStateFromURI(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  2589. {
  2590. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  2591. if (fHandle2 != nullptr)
  2592. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  2593. lilv_state_free(state);
  2594. }
  2595. }
  2596. }
  2597. else
  2598. {
  2599. // Check if current program is invalid
  2600. bool programChanged = false;
  2601. if (newCount == oldCount+1)
  2602. {
  2603. // one midi program added, probably created by user
  2604. pData->midiprog.current = static_cast<int32_t>(oldCount);
  2605. programChanged = true;
  2606. }
  2607. else if (current < 0 && newCount > 0)
  2608. {
  2609. // programs exist now, but not before
  2610. pData->midiprog.current = 0;
  2611. programChanged = true;
  2612. }
  2613. else if (current >= 0 && newCount == 0)
  2614. {
  2615. // programs existed before, but not anymore
  2616. pData->midiprog.current = -1;
  2617. programChanged = true;
  2618. }
  2619. else if (current >= static_cast<int32_t>(newCount))
  2620. {
  2621. // current midi program > count
  2622. pData->midiprog.current = 0;
  2623. programChanged = true;
  2624. }
  2625. else
  2626. {
  2627. // no change
  2628. pData->midiprog.current = current;
  2629. }
  2630. if (programChanged)
  2631. setMidiProgram(pData->midiprog.current, true, true, true, false);
  2632. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  2633. }
  2634. }
  2635. // -------------------------------------------------------------------
  2636. // Plugin processing
  2637. void activate() noexcept override
  2638. {
  2639. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2640. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2641. if (fDescriptor->activate != nullptr)
  2642. {
  2643. try {
  2644. fDescriptor->activate(fHandle);
  2645. } CARLA_SAFE_EXCEPTION("LV2 activate");
  2646. if (fHandle2 != nullptr)
  2647. {
  2648. try {
  2649. fDescriptor->activate(fHandle2);
  2650. } CARLA_SAFE_EXCEPTION("LV2 activate #2");
  2651. }
  2652. }
  2653. fFirstActive = true;
  2654. }
  2655. void deactivate() noexcept override
  2656. {
  2657. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2658. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2659. if (fDescriptor->deactivate != nullptr)
  2660. {
  2661. try {
  2662. fDescriptor->deactivate(fHandle);
  2663. } CARLA_SAFE_EXCEPTION("LV2 deactivate");
  2664. if (fHandle2 != nullptr)
  2665. {
  2666. try {
  2667. fDescriptor->deactivate(fHandle2);
  2668. } CARLA_SAFE_EXCEPTION("LV2 deactivate #2");
  2669. }
  2670. }
  2671. }
  2672. void process(const float* const* const audioIn, float** const audioOut,
  2673. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  2674. {
  2675. // --------------------------------------------------------------------------------------------------------
  2676. // Check if active
  2677. if (! pData->active)
  2678. {
  2679. // disable any output sound
  2680. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2681. carla_zeroFloats(audioOut[i], frames);
  2682. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2683. carla_zeroFloats(cvOut[i], frames);
  2684. return;
  2685. }
  2686. // --------------------------------------------------------------------------------------------------------
  2687. // Event itenerators from different APIs (input)
  2688. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  2689. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  2690. LV2_MIDIState evInMidiStates[fEventsIn.count];
  2691. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2692. {
  2693. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2694. {
  2695. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  2696. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  2697. }
  2698. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2699. {
  2700. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  2701. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  2702. }
  2703. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2704. {
  2705. fEventsIn.data[i].midi.event_count = 0;
  2706. fEventsIn.data[i].midi.size = 0;
  2707. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  2708. evInMidiStates[i].frame_count = frames;
  2709. evInMidiStates[i].position = 0;
  2710. }
  2711. }
  2712. for (uint32_t i=0; i < fEventsOut.count; ++i)
  2713. {
  2714. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2715. {
  2716. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  2717. }
  2718. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2719. {
  2720. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  2721. }
  2722. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2723. {
  2724. // not needed
  2725. }
  2726. }
  2727. // --------------------------------------------------------------------------------------------------------
  2728. // Check if needs reset
  2729. if (pData->needsReset)
  2730. {
  2731. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  2732. {
  2733. const uint32_t j = fEventsIn.ctrlIndex;
  2734. CARLA_ASSERT(j < fEventsIn.count);
  2735. uint8_t midiData[3] = { 0, 0, 0 };
  2736. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2737. {
  2738. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  2739. {
  2740. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2741. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2742. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2743. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2744. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2745. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2746. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2747. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2748. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2749. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2750. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2751. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2752. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2753. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2754. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2755. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2756. }
  2757. }
  2758. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2759. {
  2760. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2761. {
  2762. midiData[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  2763. midiData[1] = k;
  2764. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2765. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2766. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2767. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2768. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2769. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2770. }
  2771. }
  2772. }
  2773. pData->needsReset = false;
  2774. }
  2775. // --------------------------------------------------------------------------------------------------------
  2776. // TimeInfo
  2777. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  2778. if (fFirstActive || fLastTimeInfo != timeInfo)
  2779. {
  2780. bool doPostRt;
  2781. int32_t rindex;
  2782. const double barBeat = static_cast<double>(timeInfo.bbt.beat - 1)
  2783. + (timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat);
  2784. // update input ports
  2785. for (uint32_t k=0; k < pData->param.count; ++k)
  2786. {
  2787. if (pData->param.data[k].type != PARAMETER_INPUT)
  2788. continue;
  2789. if (pData->param.special[k] != PARAMETER_SPECIAL_TIME)
  2790. continue;
  2791. doPostRt = false;
  2792. rindex = pData->param.data[k].rindex;
  2793. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2794. switch (fRdfDescriptor->Ports[rindex].Designation)
  2795. {
  2796. // Non-BBT
  2797. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2798. if (fLastTimeInfo.playing != timeInfo.playing)
  2799. {
  2800. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2801. doPostRt = true;
  2802. }
  2803. break;
  2804. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2805. if (fLastTimeInfo.frame != timeInfo.frame)
  2806. {
  2807. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2808. doPostRt = true;
  2809. }
  2810. break;
  2811. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2812. break;
  2813. // BBT
  2814. case LV2_PORT_DESIGNATION_TIME_BAR:
  2815. if (timeInfo.bbt.valid && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2816. {
  2817. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2818. doPostRt = true;
  2819. }
  2820. break;
  2821. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2822. if (timeInfo.bbt.valid && (carla_isNotEqual(fLastTimeInfo.bbt.tick, timeInfo.bbt.tick) ||
  2823. fLastTimeInfo.bbt.beat != timeInfo.bbt.beat))
  2824. {
  2825. fParamBuffers[k] = static_cast<float>(barBeat);
  2826. doPostRt = true;
  2827. }
  2828. break;
  2829. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2830. if (timeInfo.bbt.valid && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2831. {
  2832. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2833. doPostRt = true;
  2834. }
  2835. break;
  2836. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2837. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.beatType, timeInfo.bbt.beatType))
  2838. {
  2839. fParamBuffers[k] = timeInfo.bbt.beatType;
  2840. doPostRt = true;
  2841. }
  2842. break;
  2843. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2844. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.beatsPerBar, timeInfo.bbt.beatsPerBar))
  2845. {
  2846. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2847. doPostRt = true;
  2848. }
  2849. break;
  2850. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2851. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.beatsPerMinute, timeInfo.bbt.beatsPerMinute))
  2852. {
  2853. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2854. doPostRt = true;
  2855. }
  2856. break;
  2857. case LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT:
  2858. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat))
  2859. {
  2860. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.ticksPerBeat);
  2861. doPostRt = true;
  2862. }
  2863. break;
  2864. }
  2865. if (doPostRt)
  2866. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  2867. true,
  2868. static_cast<int32_t>(k),
  2869. 0,
  2870. 0,
  2871. fParamBuffers[k]);
  2872. }
  2873. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2874. {
  2875. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2876. continue;
  2877. uint8_t timeInfoBuf[256];
  2878. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2879. LV2_Atom_Forge_Frame forgeFrame;
  2880. lv2_atom_forge_object(&fAtomForge, &forgeFrame, kUridNull, kUridTimePosition);
  2881. lv2_atom_forge_key(&fAtomForge, kUridTimeSpeed);
  2882. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2883. lv2_atom_forge_key(&fAtomForge, kUridTimeFrame);
  2884. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2885. if (timeInfo.bbt.valid)
  2886. {
  2887. lv2_atom_forge_key(&fAtomForge, kUridTimeBar);
  2888. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2889. lv2_atom_forge_key(&fAtomForge, kUridTimeBarBeat);
  2890. lv2_atom_forge_float(&fAtomForge, static_cast<float>(barBeat));
  2891. lv2_atom_forge_key(&fAtomForge, kUridTimeBeat);
  2892. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat - 1);
  2893. lv2_atom_forge_key(&fAtomForge, kUridTimeBeatUnit);
  2894. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2895. lv2_atom_forge_key(&fAtomForge, kUridTimeBeatsPerBar);
  2896. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2897. lv2_atom_forge_key(&fAtomForge, kUridTimeBeatsPerMinute);
  2898. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2899. lv2_atom_forge_key(&fAtomForge, kUridTimeTicksPerBeat);
  2900. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.ticksPerBeat);
  2901. }
  2902. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2903. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2904. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2905. // send only deprecated blank object for now
  2906. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, kUridAtomBlank, atom->size, LV2_ATOM_BODY_CONST(atom));
  2907. // for atom:object
  2908. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2909. }
  2910. pData->postRtEvents.trySplice();
  2911. fLastTimeInfo = timeInfo;
  2912. }
  2913. // --------------------------------------------------------------------------------------------------------
  2914. // Event Input and Processing
  2915. if (fEventsIn.ctrl != nullptr)
  2916. {
  2917. // ----------------------------------------------------------------------------------------------------
  2918. // Message Input
  2919. if (fAtomBufferEvIn.tryLock())
  2920. {
  2921. if (fAtomBufferEvIn.isDataAvailableForReading())
  2922. {
  2923. const LV2_Atom* atom;
  2924. uint32_t j, portIndex;
  2925. for (; fAtomBufferEvIn.get(atom, portIndex);)
  2926. {
  2927. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2928. if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2929. {
  2930. carla_stderr2("Event input buffer full, at least 1 message lost");
  2931. continue;
  2932. }
  2933. }
  2934. }
  2935. fAtomBufferEvIn.unlock();
  2936. }
  2937. if (fExt.worker != nullptr && fAtomBufferWorkerIn.tryLock())
  2938. {
  2939. if (fAtomBufferWorkerIn.isDataAvailableForReading())
  2940. {
  2941. const LV2_Atom* atom;
  2942. uint32_t portIndex;
  2943. for (; fAtomBufferWorkerIn.get(atom, portIndex);)
  2944. {
  2945. CARLA_SAFE_ASSERT_CONTINUE(atom->type == kUridCarlaAtomWorkerIn);
  2946. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  2947. }
  2948. }
  2949. fAtomBufferWorkerIn.unlock();
  2950. }
  2951. // ----------------------------------------------------------------------------------------------------
  2952. // MIDI Input (External)
  2953. if (pData->extNotes.mutex.tryLock())
  2954. {
  2955. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2956. {
  2957. // does not handle MIDI
  2958. pData->extNotes.data.clear();
  2959. }
  2960. else
  2961. {
  2962. const uint32_t j = fEventsIn.ctrlIndex;
  2963. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  2964. {
  2965. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  2966. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2967. uint8_t midiEvent[3];
  2968. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2969. midiEvent[1] = note.note;
  2970. midiEvent[2] = note.velo;
  2971. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2972. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiEvent);
  2973. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2974. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiEvent);
  2975. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2976. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2977. }
  2978. pData->extNotes.data.clear();
  2979. }
  2980. pData->extNotes.mutex.unlock();
  2981. } // End of MIDI Input (External)
  2982. // ----------------------------------------------------------------------------------------------------
  2983. // Event Input (System)
  2984. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2985. bool allNotesOffSent = false;
  2986. #endif
  2987. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2988. uint32_t startTime = 0;
  2989. uint32_t timeOffset = 0;
  2990. uint32_t nextBankId;
  2991. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2992. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2993. else
  2994. nextBankId = 0;
  2995. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2996. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  2997. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  2998. #endif
  2999. const uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  3000. for (uint32_t i=0; i < numEvents; ++i)
  3001. {
  3002. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  3003. uint32_t eventTime = event.time;
  3004. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  3005. if (eventTime < timeOffset)
  3006. {
  3007. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  3008. eventTime, timeOffset, pData->name);
  3009. eventTime = timeOffset;
  3010. }
  3011. if (isSampleAccurate && eventTime > timeOffset)
  3012. {
  3013. if (processSingle(audioIn, audioOut, cvIn, cvOut, eventTime - timeOffset, timeOffset))
  3014. {
  3015. startTime = 0;
  3016. timeOffset = eventTime;
  3017. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  3018. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  3019. else
  3020. nextBankId = 0;
  3021. for (uint32_t j=0; j < fEventsIn.count; ++j)
  3022. {
  3023. if (fEventsIn.data[j].type & CARLA_EVENT_DATA_ATOM)
  3024. {
  3025. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  3026. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  3027. }
  3028. else if (fEventsIn.data[j].type & CARLA_EVENT_DATA_EVENT)
  3029. {
  3030. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  3031. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  3032. }
  3033. else if (fEventsIn.data[j].type & CARLA_EVENT_DATA_MIDI_LL)
  3034. {
  3035. fEventsIn.data[j].midi.event_count = 0;
  3036. fEventsIn.data[j].midi.size = 0;
  3037. evInMidiStates[j].position = eventTime;
  3038. }
  3039. }
  3040. for (uint32_t j=0; j < fEventsOut.count; ++j)
  3041. {
  3042. if (fEventsOut.data[j].type & CARLA_EVENT_DATA_ATOM)
  3043. {
  3044. lv2_atom_buffer_reset(fEventsOut.data[j].atom, false);
  3045. }
  3046. else if (fEventsOut.data[j].type & CARLA_EVENT_DATA_EVENT)
  3047. {
  3048. lv2_event_buffer_reset(fEventsOut.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[j].event->data);
  3049. }
  3050. else if (fEventsOut.data[j].type & CARLA_EVENT_DATA_MIDI_LL)
  3051. {
  3052. // not needed
  3053. }
  3054. }
  3055. }
  3056. else
  3057. {
  3058. startTime += timeOffset;
  3059. }
  3060. }
  3061. switch (event.type)
  3062. {
  3063. case kEngineEventTypeNull:
  3064. break;
  3065. case kEngineEventTypeControl: {
  3066. const EngineControlEvent& ctrlEvent(event.ctrl);
  3067. switch (ctrlEvent.type)
  3068. {
  3069. case kEngineControlEventTypeNull:
  3070. break;
  3071. case kEngineControlEventTypeParameter: {
  3072. float value;
  3073. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3074. // non-midi
  3075. if (event.channel == kEngineEventNonMidiChannel)
  3076. {
  3077. const uint32_t k = ctrlEvent.param;
  3078. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  3079. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  3080. setParameterValueRT(k, value, true);
  3081. continue;
  3082. }
  3083. // Control backend stuff
  3084. if (event.channel == pData->ctrlChannel)
  3085. {
  3086. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  3087. {
  3088. value = ctrlEvent.value;
  3089. setDryWetRT(value, true);
  3090. }
  3091. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  3092. {
  3093. value = ctrlEvent.value*127.0f/100.0f;
  3094. setVolumeRT(value, true);
  3095. }
  3096. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  3097. {
  3098. float left, right;
  3099. value = ctrlEvent.value/0.5f - 1.0f;
  3100. if (value < 0.0f)
  3101. {
  3102. left = -1.0f;
  3103. right = (value*2.0f)+1.0f;
  3104. }
  3105. else if (value > 0.0f)
  3106. {
  3107. left = (value*2.0f)-1.0f;
  3108. right = 1.0f;
  3109. }
  3110. else
  3111. {
  3112. left = -1.0f;
  3113. right = 1.0f;
  3114. }
  3115. setBalanceLeftRT(left, true);
  3116. setBalanceRightRT(right, true);
  3117. }
  3118. }
  3119. #endif
  3120. // Control plugin parameters
  3121. uint32_t k;
  3122. for (k=0; k < pData->param.count; ++k)
  3123. {
  3124. if (pData->param.data[k].midiChannel != event.channel)
  3125. continue;
  3126. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  3127. continue;
  3128. if (pData->param.data[k].type != PARAMETER_INPUT)
  3129. continue;
  3130. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  3131. continue;
  3132. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  3133. setParameterValueRT(k, value, true);
  3134. }
  3135. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  3136. {
  3137. uint8_t midiData[3];
  3138. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3139. midiData[1] = uint8_t(ctrlEvent.param);
  3140. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  3141. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3142. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3143. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3144. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3145. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3146. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3147. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3148. }
  3149. break;
  3150. } // case kEngineControlEventTypeParameter
  3151. case kEngineControlEventTypeMidiBank:
  3152. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  3153. {
  3154. if (event.channel == pData->ctrlChannel)
  3155. nextBankId = ctrlEvent.param;
  3156. }
  3157. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  3158. {
  3159. uint8_t midiData[3];
  3160. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3161. midiData[1] = MIDI_CONTROL_BANK_SELECT;
  3162. midiData[2] = uint8_t(ctrlEvent.param);
  3163. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3164. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3165. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3166. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3167. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3168. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3169. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3170. }
  3171. break;
  3172. case kEngineControlEventTypeMidiProgram:
  3173. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  3174. {
  3175. if (event.channel == pData->ctrlChannel)
  3176. {
  3177. const uint32_t nextProgramId(ctrlEvent.param);
  3178. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  3179. {
  3180. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  3181. {
  3182. setMidiProgramRT(k, true);
  3183. break;
  3184. }
  3185. }
  3186. }
  3187. }
  3188. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  3189. {
  3190. uint8_t midiData[2];
  3191. midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3192. midiData[1] = uint8_t(ctrlEvent.param);
  3193. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3194. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3195. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 2, midiData);
  3196. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3197. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 2, midiData);
  3198. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3199. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 2, midiData);
  3200. }
  3201. break;
  3202. case kEngineControlEventTypeAllSoundOff:
  3203. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  3204. {
  3205. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3206. uint8_t midiData[3];
  3207. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3208. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  3209. midiData[2] = 0;
  3210. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3211. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3212. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3213. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3214. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3215. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3216. }
  3217. break;
  3218. case kEngineControlEventTypeAllNotesOff:
  3219. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  3220. {
  3221. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3222. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  3223. {
  3224. allNotesOffSent = true;
  3225. postponeRtAllNotesOff();
  3226. }
  3227. #endif
  3228. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3229. uint8_t midiData[3];
  3230. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3231. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  3232. midiData[2] = 0;
  3233. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3234. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3235. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3236. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3237. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3238. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3239. }
  3240. break;
  3241. } // switch (ctrlEvent.type)
  3242. break;
  3243. } // case kEngineEventTypeControl
  3244. case kEngineEventTypeMidi: {
  3245. const EngineMidiEvent& midiEvent(event.midi);
  3246. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  3247. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  3248. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  3249. continue;
  3250. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  3251. continue;
  3252. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  3253. continue;
  3254. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  3255. continue;
  3256. // Fix bad note-off (per LV2 spec)
  3257. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  3258. status = MIDI_STATUS_NOTE_OFF;
  3259. const uint32_t j = fEventsIn.ctrlIndex;
  3260. const uint32_t mtime = isSampleAccurate ? startTime : eventTime;
  3261. // put back channel in data
  3262. uint8_t midiData2[midiEvent.size];
  3263. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  3264. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  3265. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3266. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, kUridMidiEvent, midiEvent.size, midiData2);
  3267. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3268. lv2_event_write(&evInEventIters[j], mtime, 0, kUridMidiEvent, midiEvent.size, midiData2);
  3269. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3270. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiData2);
  3271. if (status == MIDI_STATUS_NOTE_ON)
  3272. {
  3273. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  3274. true,
  3275. event.channel,
  3276. midiData[1],
  3277. midiData[2],
  3278. 0.0f);
  3279. }
  3280. else if (status == MIDI_STATUS_NOTE_OFF)
  3281. {
  3282. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  3283. true,
  3284. event.channel,
  3285. midiData[1],
  3286. 0, 0.0f);
  3287. }
  3288. } break;
  3289. } // switch (event.type)
  3290. }
  3291. pData->postRtEvents.trySplice();
  3292. if (frames > timeOffset)
  3293. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  3294. } // End of Event Input and Processing
  3295. // --------------------------------------------------------------------------------------------------------
  3296. // Plugin processing (no events)
  3297. else
  3298. {
  3299. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  3300. } // End of Plugin processing (no events)
  3301. // --------------------------------------------------------------------------------------------------------
  3302. // Events/MIDI Output
  3303. for (uint32_t i=0; i < fEventsOut.count; ++i)
  3304. {
  3305. uint32_t lastFrame = 0;
  3306. Lv2EventData& evData(fEventsOut.data[i]);
  3307. if (evData.type & CARLA_EVENT_DATA_ATOM)
  3308. {
  3309. const LV2_Atom_Event* ev;
  3310. LV2_Atom_Buffer_Iterator iter;
  3311. uint8_t* data;
  3312. lv2_atom_buffer_begin(&iter, evData.atom);
  3313. for (;;)
  3314. {
  3315. data = nullptr;
  3316. ev = lv2_atom_buffer_get(&iter, &data);
  3317. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  3318. break;
  3319. if (ev->body.type == kUridMidiEvent)
  3320. {
  3321. if (evData.port != nullptr)
  3322. {
  3323. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  3324. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  3325. uint32_t currentFrame = static_cast<uint32_t>(ev->time.frames);
  3326. if (currentFrame < lastFrame)
  3327. currentFrame = lastFrame;
  3328. else if (currentFrame >= frames)
  3329. currentFrame = frames - 1;
  3330. evData.port->writeMidiEvent(currentFrame, static_cast<uint8_t>(ev->body.size), data);
  3331. }
  3332. }
  3333. else if (fAtomBufferUiOutTmpData != nullptr)
  3334. {
  3335. fAtomBufferUiOut.put(&ev->body, evData.rindex);
  3336. }
  3337. lv2_atom_buffer_increment(&iter);
  3338. }
  3339. }
  3340. else if ((evData.type & CARLA_EVENT_DATA_EVENT) != 0 && evData.port != nullptr)
  3341. {
  3342. const LV2_Event* ev;
  3343. LV2_Event_Iterator iter;
  3344. uint8_t* data;
  3345. lv2_event_begin(&iter, evData.event);
  3346. for (;;)
  3347. {
  3348. data = nullptr;
  3349. ev = lv2_event_get(&iter, &data);
  3350. if (ev == nullptr || data == nullptr)
  3351. break;
  3352. uint32_t currentFrame = ev->frames;
  3353. if (currentFrame < lastFrame)
  3354. currentFrame = lastFrame;
  3355. else if (currentFrame >= frames)
  3356. currentFrame = frames - 1;
  3357. if (ev->type == kUridMidiEvent)
  3358. {
  3359. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  3360. evData.port->writeMidiEvent(currentFrame, static_cast<uint8_t>(ev->size), data);
  3361. }
  3362. lv2_event_increment(&iter);
  3363. }
  3364. }
  3365. else if ((evData.type & CARLA_EVENT_DATA_MIDI_LL) != 0 && evData.port != nullptr)
  3366. {
  3367. LV2_MIDIState state = { &evData.midi, frames, 0 };
  3368. uint32_t eventSize;
  3369. double eventTime;
  3370. uchar* eventData;
  3371. for (;;)
  3372. {
  3373. eventSize = 0;
  3374. eventTime = 0.0;
  3375. eventData = nullptr;
  3376. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  3377. if (eventData == nullptr || eventSize == 0)
  3378. break;
  3379. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  3380. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  3381. evData.port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  3382. lv2midi_step(&state);
  3383. }
  3384. }
  3385. }
  3386. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3387. // --------------------------------------------------------------------------------------------------------
  3388. // Control Output
  3389. if (pData->event.portOut != nullptr)
  3390. {
  3391. uint8_t channel;
  3392. uint16_t param;
  3393. float value;
  3394. for (uint32_t k=0; k < pData->param.count; ++k)
  3395. {
  3396. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  3397. continue;
  3398. if (fStrictBounds >= 0 && (pData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS) != 0)
  3399. // plugin is responsible to ensure correct bounds
  3400. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  3401. if (pData->param.data[k].mappedControlIndex > 0)
  3402. {
  3403. channel = pData->param.data[k].midiChannel;
  3404. param = static_cast<uint16_t>(pData->param.data[k].mappedControlIndex);
  3405. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  3406. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  3407. }
  3408. }
  3409. } // End of Control Output
  3410. #endif
  3411. // --------------------------------------------------------------------------------------------------------
  3412. // Final work
  3413. if (fEventsIn.ctrl != nullptr && fExt.worker != nullptr && fAtomBufferWorkerResp.tryLock())
  3414. {
  3415. if (fAtomBufferWorkerResp.isDataAvailableForReading())
  3416. {
  3417. const LV2_Atom* atom;
  3418. uint32_t portIndex;
  3419. for (; fAtomBufferWorkerResp.get(atom, portIndex);)
  3420. {
  3421. CARLA_SAFE_ASSERT_CONTINUE(atom->type == kUridCarlaAtomWorkerResp);
  3422. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  3423. }
  3424. }
  3425. fAtomBufferWorkerResp.unlock();
  3426. }
  3427. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  3428. {
  3429. fExt.worker->end_run(fHandle);
  3430. if (fHandle2 != nullptr)
  3431. fExt.worker->end_run(fHandle2);
  3432. }
  3433. fFirstActive = false;
  3434. // --------------------------------------------------------------------------------------------------------
  3435. }
  3436. bool processSingle(const float* const* const audioIn, float** const audioOut,
  3437. const float* const* const cvIn, float** const cvOut,
  3438. const uint32_t frames, const uint32_t timeOffset)
  3439. {
  3440. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  3441. if (pData->audioIn.count > 0)
  3442. {
  3443. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  3444. CARLA_SAFE_ASSERT_RETURN(fAudioInBuffers != nullptr, false);
  3445. }
  3446. if (pData->audioOut.count > 0)
  3447. {
  3448. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  3449. CARLA_SAFE_ASSERT_RETURN(fAudioOutBuffers != nullptr, false);
  3450. }
  3451. if (pData->cvIn.count > 0)
  3452. {
  3453. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  3454. }
  3455. if (pData->cvOut.count > 0)
  3456. {
  3457. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  3458. }
  3459. // --------------------------------------------------------------------------------------------------------
  3460. // Try lock, silence otherwise
  3461. #ifndef STOAT_TEST_BUILD
  3462. if (pData->engine->isOffline())
  3463. {
  3464. pData->singleMutex.lock();
  3465. }
  3466. else
  3467. #endif
  3468. if (! pData->singleMutex.tryLock())
  3469. {
  3470. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3471. {
  3472. for (uint32_t k=0; k < frames; ++k)
  3473. audioOut[i][k+timeOffset] = 0.0f;
  3474. }
  3475. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3476. {
  3477. for (uint32_t k=0; k < frames; ++k)
  3478. cvOut[i][k+timeOffset] = 0.0f;
  3479. }
  3480. return false;
  3481. }
  3482. // --------------------------------------------------------------------------------------------------------
  3483. // Set audio buffers
  3484. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3485. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  3486. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3487. carla_zeroFloats(fAudioOutBuffers[i], frames);
  3488. // --------------------------------------------------------------------------------------------------------
  3489. // Set CV buffers
  3490. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3491. carla_copyFloats(fCvInBuffers[i], cvIn[i]+timeOffset, frames);
  3492. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3493. carla_zeroFloats(fCvOutBuffers[i], frames);
  3494. // --------------------------------------------------------------------------------------------------------
  3495. // Run plugin
  3496. fDescriptor->run(fHandle, frames);
  3497. if (fHandle2 != nullptr)
  3498. fDescriptor->run(fHandle2, frames);
  3499. // --------------------------------------------------------------------------------------------------------
  3500. // Handle trigger parameters
  3501. for (uint32_t k=0; k < pData->param.count; ++k)
  3502. {
  3503. if (pData->param.data[k].type != PARAMETER_INPUT)
  3504. continue;
  3505. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  3506. {
  3507. if (carla_isNotEqual(fParamBuffers[k], pData->param.ranges[k].def))
  3508. {
  3509. fParamBuffers[k] = pData->param.ranges[k].def;
  3510. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  3511. true,
  3512. static_cast<int32_t>(k),
  3513. 1, 0,
  3514. fParamBuffers[k]);
  3515. }
  3516. }
  3517. }
  3518. pData->postRtEvents.trySplice();
  3519. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3520. // --------------------------------------------------------------------------------------------------------
  3521. // Post-processing (dry/wet, volume and balance)
  3522. {
  3523. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  3524. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  3525. const bool isMono = (pData->audioIn.count == 1);
  3526. bool isPair;
  3527. float bufValue, oldBufLeft[doBalance ? frames : 1];
  3528. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3529. {
  3530. // Dry/Wet
  3531. if (doDryWet)
  3532. {
  3533. const uint32_t c = isMono ? 0 : i;
  3534. for (uint32_t k=0; k < frames; ++k)
  3535. {
  3536. # ifndef BUILD_BRIDGE
  3537. if (k < pData->latency.frames && pData->latency.buffers != nullptr)
  3538. bufValue = pData->latency.buffers[c][k];
  3539. else if (pData->latency.frames < frames)
  3540. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  3541. else
  3542. # endif
  3543. bufValue = fAudioInBuffers[c][k];
  3544. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  3545. }
  3546. }
  3547. // Balance
  3548. if (doBalance)
  3549. {
  3550. isPair = (i % 2 == 0);
  3551. if (isPair)
  3552. {
  3553. CARLA_ASSERT(i+1 < pData->audioOut.count);
  3554. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  3555. }
  3556. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  3557. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  3558. for (uint32_t k=0; k < frames; ++k)
  3559. {
  3560. if (isPair)
  3561. {
  3562. // left
  3563. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  3564. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  3565. }
  3566. else
  3567. {
  3568. // right
  3569. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  3570. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  3571. }
  3572. }
  3573. }
  3574. // Volume (and buffer copy)
  3575. {
  3576. for (uint32_t k=0; k < frames; ++k)
  3577. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  3578. }
  3579. }
  3580. } // End of Post-processing
  3581. # ifndef BUILD_BRIDGE
  3582. // --------------------------------------------------------------------------------------------------------
  3583. // Save latency values for next callback
  3584. if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
  3585. {
  3586. CARLA_SAFE_ASSERT(timeOffset == 0);
  3587. const uint32_t latframes = pData->latency.frames;
  3588. if (latframes <= frames)
  3589. {
  3590. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3591. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  3592. }
  3593. else
  3594. {
  3595. const uint32_t diff = latframes - frames;
  3596. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  3597. {
  3598. // push back buffer by 'frames'
  3599. for (k=0; k < diff; ++k)
  3600. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  3601. // put current input at the end
  3602. for (uint32_t j=0; k < latframes; ++j, ++k)
  3603. pData->latency.buffers[i][k] = audioIn[i][j];
  3604. }
  3605. }
  3606. }
  3607. # endif
  3608. #else // BUILD_BRIDGE_ALTERNATIVE_ARCH
  3609. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3610. {
  3611. for (uint32_t k=0; k < frames; ++k)
  3612. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  3613. }
  3614. #endif
  3615. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3616. {
  3617. for (uint32_t k=0; k < frames; ++k)
  3618. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  3619. }
  3620. // --------------------------------------------------------------------------------------------------------
  3621. pData->singleMutex.unlock();
  3622. return true;
  3623. }
  3624. void bufferSizeChanged(const uint32_t newBufferSize) override
  3625. {
  3626. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  3627. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - start", newBufferSize);
  3628. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3629. {
  3630. if (fAudioInBuffers[i] != nullptr)
  3631. delete[] fAudioInBuffers[i];
  3632. fAudioInBuffers[i] = new float[newBufferSize];
  3633. }
  3634. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3635. {
  3636. if (fAudioOutBuffers[i] != nullptr)
  3637. delete[] fAudioOutBuffers[i];
  3638. fAudioOutBuffers[i] = new float[newBufferSize];
  3639. }
  3640. if (fHandle2 == nullptr)
  3641. {
  3642. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3643. {
  3644. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  3645. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  3646. }
  3647. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3648. {
  3649. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  3650. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  3651. }
  3652. }
  3653. else
  3654. {
  3655. if (pData->audioIn.count > 0)
  3656. {
  3657. CARLA_ASSERT(pData->audioIn.count == 2);
  3658. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  3659. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  3660. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  3661. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  3662. }
  3663. if (pData->audioOut.count > 0)
  3664. {
  3665. CARLA_ASSERT(pData->audioOut.count == 2);
  3666. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  3667. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  3668. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  3669. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  3670. }
  3671. }
  3672. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3673. {
  3674. if (fCvInBuffers[i] != nullptr)
  3675. delete[] fCvInBuffers[i];
  3676. fCvInBuffers[i] = new float[newBufferSize];
  3677. fDescriptor->connect_port(fHandle, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3678. if (fHandle2 != nullptr)
  3679. fDescriptor->connect_port(fHandle2, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3680. }
  3681. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3682. {
  3683. if (fCvOutBuffers[i] != nullptr)
  3684. delete[] fCvOutBuffers[i];
  3685. fCvOutBuffers[i] = new float[newBufferSize];
  3686. fDescriptor->connect_port(fHandle, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3687. if (fHandle2 != nullptr)
  3688. fDescriptor->connect_port(fHandle2, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3689. }
  3690. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  3691. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  3692. {
  3693. fLv2Options.maxBufferSize = fLv2Options.nominalBufferSize = newBufferSizeInt;
  3694. if (fLv2Options.minBufferSize != 1)
  3695. fLv2Options.minBufferSize = newBufferSizeInt;
  3696. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3697. {
  3698. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MaxBlockLenth]);
  3699. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::NominalBlockLenth]);
  3700. if (fLv2Options.minBufferSize != 1)
  3701. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MinBlockLenth]);
  3702. }
  3703. }
  3704. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - end", newBufferSize);
  3705. }
  3706. void sampleRateChanged(const double newSampleRate) override
  3707. {
  3708. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  3709. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - start", newSampleRate);
  3710. const float sampleRatef = static_cast<float>(newSampleRate);
  3711. if (carla_isNotEqual(fLv2Options.sampleRate, sampleRatef))
  3712. {
  3713. fLv2Options.sampleRate = sampleRatef;
  3714. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3715. {
  3716. LV2_Options_Option options[2];
  3717. carla_zeroStructs(options, 2);
  3718. LV2_Options_Option& optSampleRate(options[0]);
  3719. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  3720. optSampleRate.subject = 0;
  3721. optSampleRate.key = kUridParamSampleRate;
  3722. optSampleRate.size = sizeof(float);
  3723. optSampleRate.type = kUridAtomFloat;
  3724. optSampleRate.value = &fLv2Options.sampleRate;
  3725. fExt.options->set(fHandle, options);
  3726. }
  3727. }
  3728. for (uint32_t k=0; k < pData->param.count; ++k)
  3729. {
  3730. if (pData->param.data[k].type != PARAMETER_INPUT)
  3731. continue;
  3732. if (pData->param.special[k] != PARAMETER_SPECIAL_SAMPLE_RATE)
  3733. continue;
  3734. fParamBuffers[k] = sampleRatef;
  3735. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  3736. true,
  3737. static_cast<int32_t>(k),
  3738. 0,
  3739. 0,
  3740. fParamBuffers[k]);
  3741. break;
  3742. }
  3743. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - end", newSampleRate);
  3744. }
  3745. void offlineModeChanged(const bool isOffline) override
  3746. {
  3747. for (uint32_t k=0; k < pData->param.count; ++k)
  3748. {
  3749. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_FREEWHEEL)
  3750. {
  3751. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  3752. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  3753. true,
  3754. static_cast<int32_t>(k),
  3755. 0,
  3756. 0,
  3757. fParamBuffers[k]);
  3758. break;
  3759. }
  3760. }
  3761. }
  3762. // -------------------------------------------------------------------
  3763. // Plugin buffers
  3764. void initBuffers() const noexcept override
  3765. {
  3766. fEventsIn.initBuffers();
  3767. fEventsOut.initBuffers();
  3768. CarlaPlugin::initBuffers();
  3769. }
  3770. void clearBuffers() noexcept override
  3771. {
  3772. carla_debug("CarlaPluginLV2::clearBuffers() - start");
  3773. if (fAudioInBuffers != nullptr)
  3774. {
  3775. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3776. {
  3777. if (fAudioInBuffers[i] != nullptr)
  3778. {
  3779. delete[] fAudioInBuffers[i];
  3780. fAudioInBuffers[i] = nullptr;
  3781. }
  3782. }
  3783. delete[] fAudioInBuffers;
  3784. fAudioInBuffers = nullptr;
  3785. }
  3786. if (fAudioOutBuffers != nullptr)
  3787. {
  3788. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3789. {
  3790. if (fAudioOutBuffers[i] != nullptr)
  3791. {
  3792. delete[] fAudioOutBuffers[i];
  3793. fAudioOutBuffers[i] = nullptr;
  3794. }
  3795. }
  3796. delete[] fAudioOutBuffers;
  3797. fAudioOutBuffers = nullptr;
  3798. }
  3799. if (fCvInBuffers != nullptr)
  3800. {
  3801. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3802. {
  3803. if (fCvInBuffers[i] != nullptr)
  3804. {
  3805. delete[] fCvInBuffers[i];
  3806. fCvInBuffers[i] = nullptr;
  3807. }
  3808. }
  3809. delete[] fCvInBuffers;
  3810. fCvInBuffers = nullptr;
  3811. }
  3812. if (fCvOutBuffers != nullptr)
  3813. {
  3814. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3815. {
  3816. if (fCvOutBuffers[i] != nullptr)
  3817. {
  3818. delete[] fCvOutBuffers[i];
  3819. fCvOutBuffers[i] = nullptr;
  3820. }
  3821. }
  3822. delete[] fCvOutBuffers;
  3823. fCvOutBuffers = nullptr;
  3824. }
  3825. if (fParamBuffers != nullptr)
  3826. {
  3827. delete[] fParamBuffers;
  3828. fParamBuffers = nullptr;
  3829. }
  3830. fEventsIn.clear();
  3831. fEventsOut.clear();
  3832. CarlaPlugin::clearBuffers();
  3833. carla_debug("CarlaPluginLV2::clearBuffers() - end");
  3834. }
  3835. // -------------------------------------------------------------------
  3836. // Post-poned UI Stuff
  3837. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3838. {
  3839. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3840. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3841. if (fUI.type == UI::TYPE_BRIDGE)
  3842. {
  3843. if (fPipeServer.isPipeRunning())
  3844. fPipeServer.writeControlMessage(static_cast<uint32_t>(pData->param.data[index].rindex), value);
  3845. }
  3846. else
  3847. {
  3848. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && ! fNeedsUiClose)
  3849. {
  3850. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3851. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), kUridNull, &value);
  3852. }
  3853. }
  3854. }
  3855. void uiMidiProgramChange(const uint32_t index) noexcept override
  3856. {
  3857. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3858. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3859. if (fUI.type == UI::TYPE_BRIDGE)
  3860. {
  3861. if (fPipeServer.isPipeRunning())
  3862. fPipeServer.writeMidiProgramMessage(pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3863. }
  3864. else
  3865. {
  3866. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr && ! fNeedsUiClose)
  3867. fExt.uiprograms->select_program(fUI.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3868. }
  3869. }
  3870. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3871. {
  3872. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3873. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3874. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3875. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3876. #if 0
  3877. if (fUI.type == UI::TYPE_BRIDGE)
  3878. {
  3879. if (fPipeServer.isPipeRunning())
  3880. fPipeServer.writeMidiNoteMessage(false, channel, note, velo);
  3881. }
  3882. else
  3883. {
  3884. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3885. {
  3886. LV2_Atom_MidiEvent midiEv;
  3887. midiEv.atom.type = kUridMidiEvent;
  3888. midiEv.atom.size = 3;
  3889. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3890. midiEv.data[1] = note;
  3891. midiEv.data[2] = velo;
  3892. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), kUridAtomTransferEvent, &midiEv);
  3893. }
  3894. }
  3895. #endif
  3896. }
  3897. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3898. {
  3899. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3900. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3901. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3902. #if 0
  3903. if (fUI.type == UI::TYPE_BRIDGE)
  3904. {
  3905. if (fPipeServer.isPipeRunning())
  3906. fPipeServer.writeMidiNoteMessage(false, channel, note, 0);
  3907. }
  3908. else
  3909. {
  3910. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3911. {
  3912. LV2_Atom_MidiEvent midiEv;
  3913. midiEv.atom.type = kUridMidiEvent;
  3914. midiEv.atom.size = 3;
  3915. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3916. midiEv.data[1] = note;
  3917. midiEv.data[2] = 0;
  3918. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), kUridAtomTransferEvent, &midiEv);
  3919. }
  3920. }
  3921. #endif
  3922. }
  3923. // -------------------------------------------------------------------
  3924. // Internal helper functions
  3925. void restoreLV2State() noexcept override
  3926. {
  3927. if (fExt.state == nullptr)
  3928. return;
  3929. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  3930. {
  3931. const ScopedSingleProcessLocker spl(this, true);
  3932. try {
  3933. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  3934. } catch(...) {}
  3935. if (fHandle2 != nullptr)
  3936. {
  3937. try {
  3938. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  3939. } catch(...) {}
  3940. }
  3941. }
  3942. switch (status)
  3943. {
  3944. case LV2_STATE_SUCCESS:
  3945. carla_debug("CarlaPluginLV2::updateLV2State() - success");
  3946. break;
  3947. case LV2_STATE_ERR_UNKNOWN:
  3948. carla_stderr("CarlaPluginLV2::updateLV2State() - unknown error");
  3949. break;
  3950. case LV2_STATE_ERR_BAD_TYPE:
  3951. carla_stderr("CarlaPluginLV2::updateLV2State() - error, bad type");
  3952. break;
  3953. case LV2_STATE_ERR_BAD_FLAGS:
  3954. carla_stderr("CarlaPluginLV2::updateLV2State() - error, bad flags");
  3955. break;
  3956. case LV2_STATE_ERR_NO_FEATURE:
  3957. carla_stderr("CarlaPluginLV2::updateLV2State() - error, missing feature");
  3958. break;
  3959. case LV2_STATE_ERR_NO_PROPERTY:
  3960. carla_stderr("CarlaPluginLV2::updateLV2State() - error, missing property");
  3961. break;
  3962. case LV2_STATE_ERR_NO_SPACE:
  3963. carla_stderr("CarlaPluginLV2::updateLV2State() - error, insufficient space");
  3964. break;
  3965. }
  3966. }
  3967. // -------------------------------------------------------------------
  3968. bool isRealtimeSafe() const noexcept
  3969. {
  3970. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3971. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3972. {
  3973. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3974. return true;
  3975. }
  3976. return false;
  3977. }
  3978. // -------------------------------------------------------------------
  3979. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3980. {
  3981. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3982. #ifndef LV2_UIS_ONLY_INPROCESS
  3983. const LV2_RDF_UI* const rdfUI(&fRdfDescriptor->UIs[uiId]);
  3984. for (uint32_t i=0; i < rdfUI->FeatureCount; ++i)
  3985. {
  3986. const LV2_RDF_Feature& feat(rdfUI->Features[i]);
  3987. if (! feat.Required)
  3988. continue;
  3989. if (std::strcmp(feat.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3990. return false;
  3991. if (std::strcmp(feat.URI, LV2_DATA_ACCESS_URI) == 0)
  3992. return false;
  3993. }
  3994. // Calf UIs are mostly useless without their special graphs
  3995. // but they can be crashy under certain conditions, so follow user preferences
  3996. if (std::strstr(rdfUI->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3997. return pData->engine->getOptions().preferUiBridges;
  3998. // LSP-Plugins UIs make heavy use of URIDs, for which carla right now is very slow
  3999. // FIXME after some optimization, remove this
  4000. if (std::strstr(rdfUI->URI, "http://lsp-plug.in/ui/lv2/") != nullptr)
  4001. return false;
  4002. return true;
  4003. #else
  4004. return false;
  4005. #endif
  4006. }
  4007. bool isUiResizable() const noexcept
  4008. {
  4009. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr, false);
  4010. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  4011. {
  4012. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  4013. return false;
  4014. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  4015. return false;
  4016. }
  4017. return true;
  4018. }
  4019. const char* getUiBridgeBinary(const LV2_Property type) const
  4020. {
  4021. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  4022. if (bridgeBinary.isEmpty())
  4023. return nullptr;
  4024. switch (type)
  4025. {
  4026. case LV2_UI_GTK2:
  4027. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk2";
  4028. break;
  4029. case LV2_UI_GTK3:
  4030. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk3";
  4031. break;
  4032. case LV2_UI_QT4:
  4033. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt4";
  4034. break;
  4035. case LV2_UI_QT5:
  4036. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt5";
  4037. break;
  4038. case LV2_UI_COCOA:
  4039. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-cocoa";
  4040. break;
  4041. case LV2_UI_WINDOWS:
  4042. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-windows";
  4043. break;
  4044. case LV2_UI_X11:
  4045. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-x11";
  4046. break;
  4047. case LV2_UI_MOD:
  4048. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-modgui";
  4049. break;
  4050. #if 0
  4051. case LV2_UI_EXTERNAL:
  4052. case LV2_UI_OLD_EXTERNAL:
  4053. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-external";
  4054. break;
  4055. #endif
  4056. default:
  4057. return nullptr;
  4058. }
  4059. #ifdef CARLA_OS_WIN
  4060. bridgeBinary += ".exe";
  4061. #endif
  4062. if (! File(bridgeBinary.buffer()).existsAsFile())
  4063. return nullptr;
  4064. return bridgeBinary.dupSafe();
  4065. }
  4066. // -------------------------------------------------------------------
  4067. void recheckExtensions()
  4068. {
  4069. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  4070. carla_debug("CarlaPluginLV2::recheckExtensions()");
  4071. fExt.options = nullptr;
  4072. fExt.programs = nullptr;
  4073. fExt.state = nullptr;
  4074. fExt.worker = nullptr;
  4075. fExt.inlineDisplay = nullptr;
  4076. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  4077. {
  4078. const char* const extension = fRdfDescriptor->Extensions[i];
  4079. CARLA_SAFE_ASSERT_CONTINUE(extension != nullptr);
  4080. /**/ if (std::strcmp(extension, LV2_OPTIONS__interface) == 0)
  4081. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  4082. else if (std::strcmp(extension, LV2_PROGRAMS__Interface) == 0)
  4083. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  4084. else if (std::strcmp(extension, LV2_STATE__interface) == 0)
  4085. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  4086. else if (std::strcmp(extension, LV2_WORKER__interface) == 0)
  4087. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  4088. else if (std::strcmp(extension, LV2_INLINEDISPLAY__interface) == 0)
  4089. pData->hints |= PLUGIN_HAS_EXTENSION_INLINE_DISPLAY;
  4090. else if (std::strcmp(extension, LV2_MIDNAM__interface) == 0)
  4091. pData->hints |= PLUGIN_HAS_EXTENSION_MIDNAM;
  4092. else
  4093. carla_stdout("Plugin '%s' has non-supported extension: '%s'", fRdfDescriptor->URI, extension);
  4094. }
  4095. // Fix for broken plugins, nasty!
  4096. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  4097. {
  4098. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[i]);
  4099. if (std::strcmp(feature.URI, LV2_INLINEDISPLAY__queue_draw) == 0)
  4100. {
  4101. if (pData->hints & PLUGIN_HAS_EXTENSION_INLINE_DISPLAY)
  4102. break;
  4103. carla_stdout("Plugin '%s' uses inline-display but does not set extension data, nasty!", fRdfDescriptor->URI);
  4104. pData->hints |= PLUGIN_HAS_EXTENSION_INLINE_DISPLAY;
  4105. }
  4106. else if (std::strcmp(feature.URI, LV2_MIDNAM__update) == 0)
  4107. {
  4108. if (pData->hints & PLUGIN_HAS_EXTENSION_MIDNAM)
  4109. break;
  4110. carla_stdout("Plugin '%s' uses midnam but does not set extension data, nasty!", fRdfDescriptor->URI);
  4111. pData->hints |= PLUGIN_HAS_EXTENSION_MIDNAM;
  4112. }
  4113. }
  4114. if (fDescriptor->extension_data != nullptr)
  4115. {
  4116. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  4117. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  4118. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  4119. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  4120. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  4121. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  4122. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  4123. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  4124. if (pData->hints & PLUGIN_HAS_EXTENSION_INLINE_DISPLAY)
  4125. fExt.inlineDisplay = (const LV2_Inline_Display_Interface*)fDescriptor->extension_data(LV2_INLINEDISPLAY__interface);
  4126. if (pData->hints & PLUGIN_HAS_EXTENSION_MIDNAM)
  4127. fExt.midnam = (const LV2_Midnam_Interface*)fDescriptor->extension_data(LV2_MIDNAM__interface);
  4128. // check if invalid
  4129. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  4130. fExt.options = nullptr;
  4131. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  4132. fExt.programs = nullptr;
  4133. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  4134. fExt.state = nullptr;
  4135. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  4136. fExt.worker = nullptr;
  4137. if (fExt.inlineDisplay != nullptr)
  4138. {
  4139. if (fExt.inlineDisplay->render != nullptr)
  4140. {
  4141. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  4142. pData->setCanDeleteLib(false);
  4143. }
  4144. else
  4145. {
  4146. fExt.inlineDisplay = nullptr;
  4147. }
  4148. }
  4149. if (fExt.midnam != nullptr && fExt.midnam->midnam == nullptr)
  4150. fExt.midnam = nullptr;
  4151. }
  4152. CARLA_SAFE_ASSERT_RETURN(fLatencyIndex == -1,);
  4153. int32_t iCtrl=0;
  4154. for (uint32_t i=0, count=fRdfDescriptor->PortCount; i<count; ++i)
  4155. {
  4156. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  4157. if (! LV2_IS_PORT_CONTROL(portTypes))
  4158. continue;
  4159. const CarlaScopedValueSetter<int32_t> svs(iCtrl, iCtrl, iCtrl+1);
  4160. if (! LV2_IS_PORT_OUTPUT(portTypes))
  4161. continue;
  4162. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  4163. if (! LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  4164. continue;
  4165. fLatencyIndex = iCtrl;
  4166. break;
  4167. }
  4168. }
  4169. // -------------------------------------------------------------------
  4170. void updateUi()
  4171. {
  4172. CARLA_SAFE_ASSERT_RETURN(fUI.handle != nullptr,);
  4173. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  4174. carla_debug("CarlaPluginLV2::updateUi()");
  4175. // update midi program
  4176. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  4177. {
  4178. const MidiProgramData& curData(pData->midiprog.getCurrent());
  4179. fExt.uiprograms->select_program(fUI.handle, curData.bank, curData.program);
  4180. }
  4181. // update control ports
  4182. if (fUI.descriptor->port_event != nullptr)
  4183. {
  4184. float value;
  4185. for (uint32_t i=0; i < pData->param.count; ++i)
  4186. {
  4187. value = getParameterValue(i);
  4188. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), kUridNull, &value);
  4189. }
  4190. }
  4191. }
  4192. // -------------------------------------------------------------------
  4193. LV2_URID getCustomURID(const char* const uri)
  4194. {
  4195. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', kUridNull);
  4196. carla_debug("CarlaPluginLV2::getCustomURID(\"%s\")", uri);
  4197. const std::string s_uri(uri);
  4198. const std::ptrdiff_t s_pos(std::find(fCustomURIDs.begin(), fCustomURIDs.end(), s_uri) - fCustomURIDs.begin());
  4199. if (s_pos <= 0 || s_pos >= INT32_MAX)
  4200. return kUridNull;
  4201. const LV2_URID urid = static_cast<LV2_URID>(s_pos);
  4202. const LV2_URID uriCount = static_cast<LV2_URID>(fCustomURIDs.size());
  4203. if (urid < uriCount)
  4204. return urid;
  4205. CARLA_SAFE_ASSERT(urid == uriCount);
  4206. fCustomURIDs.push_back(uri);
  4207. if (fUI.type == UI::TYPE_BRIDGE && fPipeServer.isPipeRunning())
  4208. fPipeServer.writeLv2UridMessage(urid, uri);
  4209. return urid;
  4210. }
  4211. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  4212. {
  4213. CARLA_SAFE_ASSERT_RETURN(urid != kUridNull, kUnmapFallback);
  4214. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.size(), kUnmapFallback);
  4215. carla_debug("CarlaPluginLV2::getCustomURIString(%i)", urid);
  4216. return fCustomURIDs[urid].c_str();
  4217. }
  4218. // -------------------------------------------------------------------
  4219. void handleProgramChanged(const int32_t index)
  4220. {
  4221. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  4222. carla_debug("CarlaPluginLV2::handleProgramChanged(%i)", index);
  4223. if (index == -1)
  4224. {
  4225. const ScopedSingleProcessLocker spl(this, true);
  4226. return reloadPrograms(false);
  4227. }
  4228. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  4229. {
  4230. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  4231. {
  4232. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  4233. if (pData->midiprog.data[index].name != nullptr)
  4234. delete[] pData->midiprog.data[index].name;
  4235. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  4236. if (index == pData->midiprog.current)
  4237. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0, nullptr);
  4238. else
  4239. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0, nullptr);
  4240. }
  4241. }
  4242. }
  4243. // -------------------------------------------------------------------
  4244. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  4245. {
  4246. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4247. carla_debug("CarlaPluginLV2::handleResizePort(%i, " P_SIZE ")", index, size);
  4248. // TODO
  4249. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  4250. (void)index;
  4251. }
  4252. // -------------------------------------------------------------------
  4253. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  4254. {
  4255. CARLA_SAFE_ASSERT_RETURN(key != kUridNull, LV2_STATE_ERR_NO_PROPERTY);
  4256. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  4257. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  4258. CARLA_SAFE_ASSERT_RETURN(type != kUridNull, LV2_STATE_ERR_BAD_TYPE);
  4259. // FIXME linuxsampler does not set POD flag
  4260. // CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  4261. carla_debug("CarlaPluginLV2::handleStateStore(%i:\"%s\", %p, " P_SIZE ", %i:\"%s\", %i)",
  4262. key, carla_lv2_urid_unmap(this, key), value, size, type, carla_lv2_urid_unmap(this, type), flags);
  4263. const char* const skey(carla_lv2_urid_unmap(this, key));
  4264. const char* const stype(carla_lv2_urid_unmap(this, type));
  4265. CARLA_SAFE_ASSERT_RETURN(skey != nullptr && skey != kUnmapFallback, LV2_STATE_ERR_BAD_TYPE);
  4266. CARLA_SAFE_ASSERT_RETURN(stype != nullptr && stype != kUnmapFallback, LV2_STATE_ERR_BAD_TYPE);
  4267. // Check if we already have this key
  4268. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  4269. {
  4270. CustomData& cData(it.getValue(kCustomDataFallbackNC));
  4271. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  4272. if (std::strcmp(cData.key, skey) == 0)
  4273. {
  4274. // found it
  4275. delete[] cData.value;
  4276. if (type == kUridAtomString || type == kUridAtomPath)
  4277. cData.value = carla_strdup((const char*)value);
  4278. else
  4279. cData.value = CarlaString::asBase64(value, size).dup();
  4280. return LV2_STATE_SUCCESS;
  4281. }
  4282. }
  4283. // Otherwise store it
  4284. CustomData newData;
  4285. newData.type = carla_strdup(stype);
  4286. newData.key = carla_strdup(skey);
  4287. if (type == kUridAtomString || type == kUridAtomPath)
  4288. newData.value = carla_strdup((const char*)value);
  4289. else
  4290. newData.value = CarlaString::asBase64(value, size).dup();
  4291. pData->custom.append(newData);
  4292. return LV2_STATE_SUCCESS;
  4293. // unused
  4294. (void)flags;
  4295. }
  4296. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  4297. {
  4298. CARLA_SAFE_ASSERT_RETURN(key != kUridNull, nullptr);
  4299. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  4300. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  4301. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  4302. carla_debug("CarlaPluginLV2::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  4303. const char* const skey(carla_lv2_urid_unmap(this, key));
  4304. CARLA_SAFE_ASSERT_RETURN(skey != nullptr && skey != kUnmapFallback, nullptr);
  4305. const char* stype = nullptr;
  4306. const char* stringData = nullptr;
  4307. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  4308. {
  4309. const CustomData& cData(it.getValue(kCustomDataFallback));
  4310. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  4311. if (std::strcmp(cData.key, skey) == 0)
  4312. {
  4313. stype = cData.type;
  4314. stringData = cData.value;
  4315. break;
  4316. }
  4317. }
  4318. if (stype == nullptr || stringData == nullptr)
  4319. {
  4320. carla_stderr("Plugin requested value for '%s' which is not available", skey);
  4321. *size = *type = *flags = 0;
  4322. return nullptr;
  4323. }
  4324. *type = carla_lv2_urid_map(this, stype);
  4325. *flags = LV2_STATE_IS_POD;
  4326. if (*type == kUridAtomString || *type == kUridAtomPath)
  4327. {
  4328. *size = std::strlen(stringData);
  4329. return stringData;
  4330. }
  4331. if (fLastStateChunk != nullptr)
  4332. {
  4333. std::free(fLastStateChunk);
  4334. fLastStateChunk = nullptr;
  4335. }
  4336. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stringData));
  4337. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  4338. fLastStateChunk = std::malloc(chunk.size());
  4339. CARLA_SAFE_ASSERT_RETURN(fLastStateChunk != nullptr, nullptr);
  4340. #ifdef CARLA_PROPER_CPP11_SUPPORT
  4341. std::memcpy(fLastStateChunk, chunk.data(), chunk.size());
  4342. #else
  4343. std::memcpy(fLastStateChunk, &chunk.front(), chunk.size());
  4344. #endif
  4345. *size = chunk.size();
  4346. return fLastStateChunk;
  4347. }
  4348. // -------------------------------------------------------------------
  4349. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  4350. {
  4351. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4352. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4353. carla_debug("CarlaPluginLV2::handleWorkerSchedule(%i, %p)", size, data);
  4354. if (pData->engine->isOffline())
  4355. {
  4356. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  4357. return LV2_WORKER_SUCCESS;
  4358. }
  4359. LV2_Atom atom;
  4360. atom.size = size;
  4361. atom.type = kUridCarlaAtomWorkerIn;
  4362. return fAtomBufferWorkerIn.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  4363. }
  4364. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  4365. {
  4366. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work_response != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4367. carla_debug("CarlaPluginLV2::handleWorkerRespond(%i, %p)", size, data);
  4368. LV2_Atom atom;
  4369. atom.size = size;
  4370. atom.type = kUridCarlaAtomWorkerResp;
  4371. return fAtomBufferWorkerResp.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  4372. }
  4373. // -------------------------------------------------------------------
  4374. void handleInlineDisplayQueueRedraw()
  4375. {
  4376. fInlineDisplayNeedsRedraw = true;
  4377. }
  4378. const LV2_Inline_Display_Image_Surface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  4379. {
  4380. CARLA_SAFE_ASSERT_RETURN(fExt.inlineDisplay != nullptr && fExt.inlineDisplay->render != nullptr, nullptr);
  4381. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  4382. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  4383. return fExt.inlineDisplay->render(fHandle, width, height);
  4384. }
  4385. // -------------------------------------------------------------------
  4386. void handleMidnamUpdate()
  4387. {
  4388. CARLA_SAFE_ASSERT_RETURN(fExt.midnam != nullptr,);
  4389. if (fEventsIn.ctrl == nullptr)
  4390. return;
  4391. char* const midnam = fExt.midnam->midnam(fHandle);
  4392. CARLA_SAFE_ASSERT_RETURN(midnam != nullptr,);
  4393. fEventsIn.ctrl->port->setMetaData("http://www.midi.org/dtds/MIDINameDocument10.dtd", midnam, "text/xml");
  4394. if (fExt.midnam->free != nullptr)
  4395. fExt.midnam->free(midnam);
  4396. }
  4397. // -------------------------------------------------------------------
  4398. void handleExternalUIClosed()
  4399. {
  4400. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EXTERNAL,);
  4401. carla_debug("CarlaPluginLV2::handleExternalUIClosed()");
  4402. fNeedsUiClose = true;
  4403. }
  4404. void handlePluginUIClosed() override
  4405. {
  4406. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  4407. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  4408. carla_debug("CarlaPluginLV2::handlePluginUIClosed()");
  4409. fNeedsUiClose = true;
  4410. }
  4411. void handlePluginUIResized(const uint width, const uint height) override
  4412. {
  4413. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  4414. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  4415. carla_debug("CarlaPluginLV2::handlePluginUIResized(%u, %u)", width, height);
  4416. if (fUI.handle != nullptr && fExt.uiresize != nullptr)
  4417. fExt.uiresize->ui_resize(fUI.handle, static_cast<int>(width), static_cast<int>(height));
  4418. }
  4419. // -------------------------------------------------------------------
  4420. uint32_t handleUIPortMap(const char* const symbol) const noexcept
  4421. {
  4422. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  4423. carla_debug("CarlaPluginLV2::handleUIPortMap(\"%s\")", symbol);
  4424. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  4425. {
  4426. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  4427. return i;
  4428. }
  4429. return LV2UI_INVALID_PORT_INDEX;
  4430. }
  4431. LV2UI_Request_Value_Status handleUIRequestValue(const LV2_URID key,
  4432. const LV2_URID type,
  4433. const LV2_Feature* const* features)
  4434. {
  4435. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL, LV2UI_REQUEST_VALUE_ERR_UNKNOWN);
  4436. carla_debug("CarlaPluginLV2::handleUIRequestValue(%u, %u, %p)", key, type, features);
  4437. if (type != kUridAtomPath)
  4438. return LV2UI_REQUEST_VALUE_ERR_UNSUPPORTED;
  4439. const char* const uri = getCustomURIDString(key);
  4440. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri != kUnmapFallback, LV2UI_REQUEST_VALUE_ERR_UNKNOWN);
  4441. // check if a file browser is already open
  4442. if (fUI.fileNeededForURI != nullptr || fUI.fileBrowserOpen)
  4443. return LV2UI_REQUEST_VALUE_BUSY;
  4444. for (uint32_t i=0; i < fRdfDescriptor->ParameterCount; ++i)
  4445. {
  4446. if (fRdfDescriptor->Parameters[i].Type != LV2_PARAMETER_PATH)
  4447. continue;
  4448. if (std::strcmp(fRdfDescriptor->Parameters[i].URI, uri) != 0)
  4449. continue;
  4450. // TODO file browser filters, also store label to use for title
  4451. fUI.fileNeededForURI = uri;
  4452. return LV2UI_REQUEST_VALUE_SUCCESS;
  4453. }
  4454. return LV2UI_REQUEST_VALUE_ERR_UNSUPPORTED;
  4455. // may be unused
  4456. (void)features;
  4457. }
  4458. int handleUIResize(const int width, const int height)
  4459. {
  4460. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr, 1);
  4461. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  4462. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  4463. carla_debug("CarlaPluginLV2::handleUIResize(%i, %i)", width, height);
  4464. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  4465. return 0;
  4466. }
  4467. void handleUITouch(const uint32_t rindex, const bool touch)
  4468. {
  4469. carla_debug("CarlaPluginLV2::handleUITouch(%u, %s)", rindex, bool2str(touch));
  4470. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  4471. for (uint32_t i=0; i < pData->param.count; ++i)
  4472. {
  4473. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  4474. continue;
  4475. index = i;
  4476. break;
  4477. }
  4478. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  4479. pData->engine->touchPluginParameter(pData->id, index, touch);
  4480. }
  4481. void handleUIWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  4482. {
  4483. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  4484. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  4485. carla_debug("CarlaPluginLV2::handleUIWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  4486. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  4487. switch (format)
  4488. {
  4489. case kUridNull: {
  4490. CARLA_SAFE_ASSERT_RETURN(rindex < fRdfDescriptor->PortCount,);
  4491. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  4492. for (uint32_t i=0; i < pData->param.count; ++i)
  4493. {
  4494. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  4495. continue;
  4496. index = i;
  4497. break;
  4498. }
  4499. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  4500. const float value(*(const float*)buffer);
  4501. // check if we should feedback message back to UI
  4502. bool sendGui = false;
  4503. if (const uint32_t notifCount = fUI.rdfDescriptor->PortNotificationCount)
  4504. {
  4505. const char* const portSymbol = fRdfDescriptor->Ports[rindex].Symbol;
  4506. for (uint32_t i=0; i < notifCount; ++i)
  4507. {
  4508. const LV2_RDF_UI_PortNotification& portNotif(fUI.rdfDescriptor->PortNotifications[i]);
  4509. if (portNotif.Protocol != LV2_UI_PORT_PROTOCOL_FLOAT)
  4510. continue;
  4511. if (portNotif.Symbol != nullptr)
  4512. {
  4513. if (std::strcmp(portNotif.Symbol, portSymbol) != 0)
  4514. continue;
  4515. }
  4516. else if (portNotif.Index != rindex)
  4517. {
  4518. continue;
  4519. }
  4520. sendGui = true;
  4521. break;
  4522. }
  4523. }
  4524. setParameterValue(index, value, sendGui, true, true);
  4525. } break;
  4526. case kUridAtomTransferAtom:
  4527. case kUridAtomTransferEvent: {
  4528. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  4529. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  4530. // plugins sometimes fail on this, not good...
  4531. const uint32_t totalSize = lv2_atom_total_size(atom);
  4532. const uint32_t paddedSize = lv2_atom_pad_size(totalSize);
  4533. if (bufferSize != totalSize && bufferSize != paddedSize)
  4534. carla_stderr2("Warning: LV2 UI sending atom with invalid size %u! size: %u, padded-size: %u",
  4535. bufferSize, totalSize, paddedSize);
  4536. for (uint32_t i=0; i < fEventsIn.count; ++i)
  4537. {
  4538. if (fEventsIn.data[i].rindex != rindex)
  4539. continue;
  4540. index = i;
  4541. break;
  4542. }
  4543. // for bad plugins
  4544. if (index == LV2UI_INVALID_PORT_INDEX)
  4545. {
  4546. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  4547. index = fEventsIn.ctrlIndex;
  4548. }
  4549. fAtomBufferEvIn.put(atom, index);
  4550. } break;
  4551. default:
  4552. carla_stdout("CarlaPluginLV2::handleUIWrite(%i, %i, %i:\"%s\", %p) - unknown format",
  4553. rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  4554. break;
  4555. }
  4556. }
  4557. // -------------------------------------------------------------------
  4558. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  4559. {
  4560. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  4561. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  4562. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  4563. CARLA_SAFE_ASSERT_RETURN(type != kUridNull,);
  4564. carla_debug("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  4565. int32_t rindex = -1;
  4566. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  4567. {
  4568. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  4569. {
  4570. rindex = static_cast<int32_t>(i);
  4571. break;
  4572. }
  4573. }
  4574. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  4575. float paramValue;
  4576. switch (type)
  4577. {
  4578. case kUridAtomBool:
  4579. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  4580. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  4581. break;
  4582. case kUridAtomDouble:
  4583. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  4584. paramValue = static_cast<float>((*(const double*)value));
  4585. break;
  4586. case kUridAtomFloat:
  4587. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  4588. paramValue = (*(const float*)value);
  4589. break;
  4590. case kUridAtomInt:
  4591. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  4592. paramValue = static_cast<float>((*(const int32_t*)value));
  4593. break;
  4594. case kUridAtomLong:
  4595. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  4596. paramValue = static_cast<float>((*(const int64_t*)value));
  4597. break;
  4598. default:
  4599. carla_stdout("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type",
  4600. portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  4601. return;
  4602. }
  4603. for (uint32_t i=0; i < pData->param.count; ++i)
  4604. {
  4605. if (pData->param.data[i].rindex == rindex)
  4606. {
  4607. setParameterValueRT(i, paramValue, true);
  4608. break;
  4609. }
  4610. }
  4611. }
  4612. // -------------------------------------------------------------------
  4613. void* getNativeHandle() const noexcept override
  4614. {
  4615. return fHandle;
  4616. }
  4617. const void* getNativeDescriptor() const noexcept override
  4618. {
  4619. return fDescriptor;
  4620. }
  4621. uintptr_t getUiBridgeProcessId() const noexcept override
  4622. {
  4623. return fPipeServer.isPipeRunning() ? fPipeServer.getPID() : 0;
  4624. }
  4625. // -------------------------------------------------------------------
  4626. public:
  4627. bool init(const char* const name, const char* const uri, const uint options)
  4628. {
  4629. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  4630. // ---------------------------------------------------------------
  4631. // first checks
  4632. if (pData->client != nullptr)
  4633. {
  4634. pData->engine->setLastError("Plugin client is already registered");
  4635. return false;
  4636. }
  4637. if (uri == nullptr || uri[0] == '\0')
  4638. {
  4639. pData->engine->setLastError("null uri");
  4640. return false;
  4641. }
  4642. const EngineOptions& opts(pData->engine->getOptions());
  4643. // ---------------------------------------------------------------
  4644. // Init LV2 World if needed, sets LV2_PATH for lilv
  4645. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  4646. if (opts.pathLV2 != nullptr && opts.pathLV2[0] != '\0')
  4647. lv2World.initIfNeeded(opts.pathLV2);
  4648. else if (const char* const LV2_PATH = std::getenv("LV2_PATH"))
  4649. lv2World.initIfNeeded(LV2_PATH);
  4650. else
  4651. lv2World.initIfNeeded(LILV_DEFAULT_LV2_PATH);
  4652. // ---------------------------------------------------------------
  4653. // get plugin from lv2_rdf (lilv)
  4654. fRdfDescriptor = lv2_rdf_new(uri, true);
  4655. if (fRdfDescriptor == nullptr)
  4656. {
  4657. pData->engine->setLastError("Failed to find the requested plugin");
  4658. return false;
  4659. }
  4660. // ---------------------------------------------------------------
  4661. // open DLL
  4662. if (! pData->libOpen(fRdfDescriptor->Binary))
  4663. {
  4664. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  4665. return false;
  4666. }
  4667. // ---------------------------------------------------------------
  4668. // try to get DLL main entry via new mode
  4669. if (const LV2_Lib_Descriptor_Function libDescFn = pData->libSymbol<LV2_Lib_Descriptor_Function>("lv2_lib_descriptor"))
  4670. {
  4671. // -----------------------------------------------------------
  4672. // all ok, get lib descriptor
  4673. const LV2_Lib_Descriptor* const libDesc = libDescFn(fRdfDescriptor->Bundle, nullptr);
  4674. if (libDesc == nullptr)
  4675. {
  4676. pData->engine->setLastError("Could not find the LV2 Descriptor");
  4677. return false;
  4678. }
  4679. // -----------------------------------------------------------
  4680. // get descriptor that matches URI (new mode)
  4681. uint32_t i = 0;
  4682. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  4683. {
  4684. if (std::strcmp(fDescriptor->URI, uri) == 0)
  4685. break;
  4686. }
  4687. }
  4688. else
  4689. {
  4690. // -----------------------------------------------------------
  4691. // get DLL main entry (old mode)
  4692. const LV2_Descriptor_Function descFn = pData->libSymbol<LV2_Descriptor_Function>("lv2_descriptor");
  4693. if (descFn == nullptr)
  4694. {
  4695. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  4696. return false;
  4697. }
  4698. // -----------------------------------------------------------
  4699. // get descriptor that matches URI (old mode)
  4700. uint32_t i = 0;
  4701. while ((fDescriptor = descFn(i++)))
  4702. {
  4703. if (std::strcmp(fDescriptor->URI, uri) == 0)
  4704. break;
  4705. }
  4706. }
  4707. if (fDescriptor == nullptr)
  4708. {
  4709. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  4710. return false;
  4711. }
  4712. // ---------------------------------------------------------------
  4713. // check supported port-types and features
  4714. bool canContinue = true;
  4715. // Check supported ports
  4716. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  4717. {
  4718. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  4719. if (! is_lv2_port_supported(portTypes))
  4720. {
  4721. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  4722. {
  4723. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  4724. canContinue = false;
  4725. break;
  4726. }
  4727. }
  4728. }
  4729. // Check supported features
  4730. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  4731. {
  4732. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[j]);
  4733. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  4734. {
  4735. carla_stderr("Plugin DSP wants UI feature '%s', ignoring this", feature.URI);
  4736. }
  4737. else if (std::strcmp(feature.URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  4738. {
  4739. fNeedsFixedBuffers = true;
  4740. }
  4741. else if (std::strcmp(feature.URI, LV2_PORT_PROPS__supportsStrictBounds) == 0)
  4742. {
  4743. fStrictBounds = feature.Required ? 1 : 0;
  4744. }
  4745. else if (feature.Required && ! is_lv2_feature_supported(feature.URI))
  4746. {
  4747. CarlaString msg("Plugin wants a feature that is not supported:\n");
  4748. msg += feature.URI;
  4749. canContinue = false;
  4750. pData->engine->setLastError(msg);
  4751. break;
  4752. }
  4753. }
  4754. if (! canContinue)
  4755. {
  4756. // error already set
  4757. return false;
  4758. }
  4759. if (fNeedsFixedBuffers && ! pData->engine->usesConstantBufferSize())
  4760. {
  4761. pData->engine->setLastError("Cannot use this plugin under the current engine.\n"
  4762. "The plugin requires a fixed block size which is not possible right now.");
  4763. return false;
  4764. }
  4765. // ---------------------------------------------------------------
  4766. // set icon
  4767. if (std::strncmp(fDescriptor->URI, "http://distrho.sf.net/", 22) == 0)
  4768. pData->iconName = carla_strdup_safe("distrho");
  4769. // ---------------------------------------------------------------
  4770. // set info
  4771. if (name != nullptr && name[0] != '\0')
  4772. pData->name = pData->engine->getUniquePluginName(name);
  4773. else
  4774. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  4775. // ---------------------------------------------------------------
  4776. // register client
  4777. pData->client = pData->engine->addClient(this);
  4778. if (pData->client == nullptr || ! pData->client->isOk())
  4779. {
  4780. pData->engine->setLastError("Failed to register plugin client");
  4781. return false;
  4782. }
  4783. // ---------------------------------------------------------------
  4784. // initialize options
  4785. const int bufferSize = static_cast<int>(pData->engine->getBufferSize());
  4786. fLv2Options.minBufferSize = fNeedsFixedBuffers ? bufferSize : 1;
  4787. fLv2Options.maxBufferSize = bufferSize;
  4788. fLv2Options.nominalBufferSize = bufferSize;
  4789. fLv2Options.sampleRate = static_cast<float>(pData->engine->getSampleRate());
  4790. fLv2Options.transientWinId = static_cast<int64_t>(opts.frontendWinId);
  4791. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  4792. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  4793. {
  4794. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  4795. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  4796. {
  4797. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  4798. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  4799. }
  4800. }
  4801. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  4802. fLv2Options.bgColor = opts.bgColor;
  4803. fLv2Options.fgColor = opts.fgColor;
  4804. fLv2Options.uiScale = opts.uiScale;
  4805. // ---------------------------------------------------------------
  4806. // initialize features (part 1)
  4807. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  4808. eventFt->callback_data = this;
  4809. eventFt->lv2_event_ref = carla_lv2_event_ref;
  4810. eventFt->lv2_event_unref = carla_lv2_event_unref;
  4811. LV2_Log_Log* const logFt = new LV2_Log_Log;
  4812. logFt->handle = this;
  4813. logFt->printf = carla_lv2_log_printf;
  4814. logFt->vprintf = carla_lv2_log_vprintf;
  4815. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  4816. stateMakePathFt->handle = this;
  4817. stateMakePathFt->path = carla_lv2_state_make_path;
  4818. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  4819. stateMapPathFt->handle = this;
  4820. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  4821. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  4822. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  4823. programsFt->handle = this;
  4824. programsFt->program_changed = carla_lv2_program_changed;
  4825. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  4826. rsPortFt->data = this;
  4827. rsPortFt->resize = carla_lv2_resize_port;
  4828. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  4829. lv2_rtmempool_init(rtMemPoolFt);
  4830. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  4831. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  4832. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  4833. uriMapFt->callback_data = this;
  4834. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  4835. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  4836. uridMapFt->handle = this;
  4837. uridMapFt->map = carla_lv2_urid_map;
  4838. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  4839. uridUnmapFt->handle = this;
  4840. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  4841. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  4842. workerFt->handle = this;
  4843. workerFt->schedule_work = carla_lv2_worker_schedule;
  4844. LV2_Inline_Display* const inlineDisplay = new LV2_Inline_Display;
  4845. inlineDisplay->handle = this;
  4846. inlineDisplay->queue_draw = carla_lv2_inline_display_queue_draw;
  4847. LV2_Midnam* const midnam = new LV2_Midnam;
  4848. midnam->handle = this;
  4849. midnam->update = carla_lv2_midnam_update;
  4850. // ---------------------------------------------------------------
  4851. // initialize features (part 2)
  4852. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  4853. fFeatures[j] = new LV2_Feature;
  4854. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  4855. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  4856. fFeatures[kFeatureIdBufSizeFixed]->URI = fNeedsFixedBuffers
  4857. ? LV2_BUF_SIZE__fixedBlockLength
  4858. : LV2_BUF_SIZE__boundedBlockLength;
  4859. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  4860. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  4861. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  4862. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  4863. fFeatures[kFeatureIdEvent]->data = eventFt;
  4864. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  4865. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  4866. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  4867. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  4868. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  4869. fFeatures[kFeatureIdIsLive]->data = nullptr;
  4870. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  4871. fFeatures[kFeatureIdLogs]->data = logFt;
  4872. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  4873. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  4874. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  4875. fFeatures[kFeatureIdPrograms]->data = programsFt;
  4876. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  4877. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  4878. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  4879. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  4880. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  4881. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  4882. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  4883. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  4884. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  4885. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  4886. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  4887. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  4888. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  4889. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  4890. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  4891. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  4892. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  4893. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  4894. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  4895. fFeatures[kFeatureIdWorker]->data = workerFt;
  4896. fFeatures[kFeatureIdInlineDisplay]->URI = LV2_INLINEDISPLAY__queue_draw;
  4897. fFeatures[kFeatureIdInlineDisplay]->data = inlineDisplay;
  4898. fFeatures[kFeatureIdMidnam]->URI = LV2_MIDNAM__update;
  4899. fFeatures[kFeatureIdMidnam]->data = midnam;
  4900. // ---------------------------------------------------------------
  4901. // initialize plugin
  4902. try {
  4903. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  4904. } catch(...) {}
  4905. if (fHandle == nullptr)
  4906. {
  4907. pData->engine->setLastError("Plugin failed to initialize");
  4908. return false;
  4909. }
  4910. recheckExtensions();
  4911. // ---------------------------------------------------------------
  4912. // set options
  4913. pData->options = 0x0;
  4914. if (fLatencyIndex >= 0 || getMidiOutCount() != 0 || fNeedsFixedBuffers)
  4915. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4916. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  4917. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4918. if (opts.forceStereo)
  4919. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4920. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  4921. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4922. if (getMidiInCount() != 0)
  4923. {
  4924. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  4925. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  4926. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  4927. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  4928. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  4929. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  4930. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  4931. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  4932. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  4933. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  4934. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  4935. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  4936. }
  4937. if (fExt.programs != nullptr && (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0)
  4938. {
  4939. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  4940. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  4941. }
  4942. // ---------------------------------------------------------------
  4943. // gui stuff
  4944. if (fRdfDescriptor->UICount != 0)
  4945. initUi();
  4946. return true;
  4947. }
  4948. // -------------------------------------------------------------------
  4949. void initUi()
  4950. {
  4951. // ---------------------------------------------------------------
  4952. // find the most appropriate ui
  4953. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eMod, iCocoa, iWindows, iX11, iExt, iFinal;
  4954. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eMod = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  4955. #if defined(LV2_UIS_ONLY_BRIDGES)
  4956. const bool preferUiBridges = true;
  4957. #elif defined(BUILD_BRIDGE)
  4958. const bool preferUiBridges = false;
  4959. #else
  4960. const bool preferUiBridges = pData->engine->getOptions().preferUiBridges;
  4961. #endif
  4962. bool hasShowInterface = false;
  4963. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  4964. {
  4965. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  4966. const int ii(static_cast<int>(i));
  4967. switch (fRdfDescriptor->UIs[i].Type)
  4968. {
  4969. case LV2_UI_QT4:
  4970. if (isUiBridgeable(i))
  4971. eQt4 = ii;
  4972. break;
  4973. case LV2_UI_QT5:
  4974. if (isUiBridgeable(i))
  4975. eQt5 = ii;
  4976. break;
  4977. case LV2_UI_GTK2:
  4978. if (isUiBridgeable(i))
  4979. eGtk2 = ii;
  4980. break;
  4981. case LV2_UI_GTK3:
  4982. if (isUiBridgeable(i))
  4983. eGtk3 = ii;
  4984. break;
  4985. #ifdef CARLA_OS_MAC
  4986. case LV2_UI_COCOA:
  4987. if (isUiBridgeable(i) && preferUiBridges)
  4988. eCocoa = ii;
  4989. iCocoa = ii;
  4990. break;
  4991. #endif
  4992. #ifdef CARLA_OS_WIN
  4993. case LV2_UI_WINDOWS:
  4994. if (isUiBridgeable(i) && preferUiBridges)
  4995. eWindows = ii;
  4996. iWindows = ii;
  4997. break;
  4998. #endif
  4999. case LV2_UI_X11:
  5000. if (isUiBridgeable(i) && preferUiBridges)
  5001. eX11 = ii;
  5002. iX11 = ii;
  5003. break;
  5004. case LV2_UI_EXTERNAL:
  5005. case LV2_UI_OLD_EXTERNAL:
  5006. iExt = ii;
  5007. break;
  5008. case LV2_UI_MOD:
  5009. eMod = ii;
  5010. break;
  5011. default:
  5012. break;
  5013. }
  5014. }
  5015. /**/ if (eQt4 >= 0)
  5016. iFinal = eQt4;
  5017. else if (eQt5 >= 0)
  5018. iFinal = eQt5;
  5019. else if (eGtk2 >= 0)
  5020. iFinal = eGtk2;
  5021. else if (eGtk3 >= 0)
  5022. iFinal = eGtk3;
  5023. #ifdef CARLA_OS_MAC
  5024. else if (eCocoa >= 0)
  5025. iFinal = eCocoa;
  5026. #endif
  5027. #ifdef CARLA_OS_WIN
  5028. else if (eWindows >= 0)
  5029. iFinal = eWindows;
  5030. #endif
  5031. #ifdef HAVE_X11
  5032. else if (eX11 >= 0)
  5033. iFinal = eX11;
  5034. #endif
  5035. #ifndef LV2_UIS_ONLY_BRIDGES
  5036. # ifdef CARLA_OS_MAC
  5037. else if (iCocoa >= 0)
  5038. iFinal = iCocoa;
  5039. # endif
  5040. # ifdef CARLA_OS_WIN
  5041. else if (iWindows >= 0)
  5042. iFinal = iWindows;
  5043. # endif
  5044. # ifdef HAVE_X11
  5045. else if (iX11 >= 0)
  5046. iFinal = iX11;
  5047. # endif
  5048. #endif
  5049. else if (iExt >= 0)
  5050. iFinal = iExt;
  5051. #ifndef BUILD_BRIDGE
  5052. if (iFinal < 0)
  5053. #endif
  5054. {
  5055. // no suitable UI found, see if there's one which supports ui:showInterface
  5056. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  5057. {
  5058. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  5059. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  5060. {
  5061. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  5062. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  5063. continue;
  5064. iFinal = static_cast<int>(i);
  5065. hasShowInterface = true;
  5066. break;
  5067. }
  5068. }
  5069. if (iFinal < 0)
  5070. {
  5071. if (eMod < 0)
  5072. {
  5073. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  5074. return;
  5075. }
  5076. // use MODGUI as last resort
  5077. iFinal = eMod;
  5078. }
  5079. }
  5080. fUI.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  5081. // ---------------------------------------------------------------
  5082. // check supported ui features
  5083. bool canContinue = true;
  5084. bool canDelete = true;
  5085. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  5086. {
  5087. const char* const uri(fUI.rdfDescriptor->Features[i].URI);
  5088. CARLA_SAFE_ASSERT_CONTINUE(uri != nullptr && uri[0] != '\0');
  5089. if (! is_lv2_ui_feature_supported(uri))
  5090. {
  5091. if (fUI.rdfDescriptor->Features[i].Required)
  5092. {
  5093. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", uri);
  5094. canContinue = false;
  5095. break;
  5096. }
  5097. carla_stderr("Plugin UI wants a feature that is not supported (ignored):\n%s", uri);
  5098. }
  5099. if (std::strcmp(uri, LV2_UI__makeResident) == 0 || std::strcmp(uri, LV2_UI__makeSONameResident) == 0)
  5100. canDelete = false;
  5101. else if (std::strcmp(uri, LV2_UI__requestValue) == 0)
  5102. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  5103. }
  5104. if (! canContinue)
  5105. {
  5106. fUI.rdfDescriptor = nullptr;
  5107. return;
  5108. }
  5109. // ---------------------------------------------------------------
  5110. // initialize ui according to type
  5111. const LV2_Property uiType(fUI.rdfDescriptor->Type);
  5112. if (
  5113. (iFinal == eQt4 ||
  5114. iFinal == eQt5 ||
  5115. iFinal == eGtk2 ||
  5116. iFinal == eGtk3 ||
  5117. iFinal == eCocoa ||
  5118. iFinal == eWindows ||
  5119. iFinal == eX11 ||
  5120. iFinal == eMod)
  5121. #ifdef BUILD_BRIDGE
  5122. && ! hasShowInterface
  5123. #endif
  5124. )
  5125. {
  5126. // -----------------------------------------------------------
  5127. // initialize ui-bridge
  5128. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  5129. {
  5130. carla_stdout("Will use UI-Bridge for '%s', binary: \"%s\"", pData->name, bridgeBinary);
  5131. CarlaString guiTitle(pData->name);
  5132. guiTitle += " (GUI)";
  5133. fLv2Options.windowTitle = guiTitle.dup();
  5134. fUI.type = UI::TYPE_BRIDGE;
  5135. fPipeServer.setData(bridgeBinary, fRdfDescriptor->URI, fUI.rdfDescriptor->URI);
  5136. delete[] bridgeBinary;
  5137. return;
  5138. }
  5139. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eMod)
  5140. {
  5141. carla_stderr2("Failed to find UI bridge binary for '%s', cannot use UI", pData->name);
  5142. fUI.rdfDescriptor = nullptr;
  5143. return;
  5144. }
  5145. }
  5146. #ifdef LV2_UIS_ONLY_BRIDGES
  5147. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  5148. fUI.rdfDescriptor = nullptr;
  5149. return;
  5150. #endif
  5151. // ---------------------------------------------------------------
  5152. // open UI DLL
  5153. if (! pData->uiLibOpen(fUI.rdfDescriptor->Binary, canDelete))
  5154. {
  5155. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUI.rdfDescriptor->Binary));
  5156. fUI.rdfDescriptor = nullptr;
  5157. return;
  5158. }
  5159. // ---------------------------------------------------------------
  5160. // get UI DLL main entry
  5161. LV2UI_DescriptorFunction uiDescFn = pData->uiLibSymbol<LV2UI_DescriptorFunction>("lv2ui_descriptor");
  5162. if (uiDescFn == nullptr)
  5163. {
  5164. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  5165. pData->uiLibClose();
  5166. fUI.rdfDescriptor = nullptr;
  5167. return;
  5168. }
  5169. // ---------------------------------------------------------------
  5170. // get UI descriptor that matches UI URI
  5171. uint32_t i = 0;
  5172. while ((fUI.descriptor = uiDescFn(i++)))
  5173. {
  5174. if (std::strcmp(fUI.descriptor->URI, fUI.rdfDescriptor->URI) == 0)
  5175. break;
  5176. }
  5177. if (fUI.descriptor == nullptr)
  5178. {
  5179. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  5180. pData->uiLibClose();
  5181. fUI.rdfDescriptor = nullptr;
  5182. return;
  5183. }
  5184. // ---------------------------------------------------------------
  5185. // check if ui is usable
  5186. switch (uiType)
  5187. {
  5188. case LV2_UI_NONE:
  5189. carla_stdout("Will use LV2 Show Interface for '%s'", pData->name);
  5190. fUI.type = UI::TYPE_EMBED;
  5191. break;
  5192. case LV2_UI_QT4:
  5193. carla_stdout("Will use LV2 Qt4 UI for '%s', NOT!", pData->name);
  5194. fUI.type = UI::TYPE_EMBED;
  5195. break;
  5196. case LV2_UI_QT5:
  5197. carla_stdout("Will use LV2 Qt5 UI for '%s', NOT!", pData->name);
  5198. fUI.type = UI::TYPE_EMBED;
  5199. break;
  5200. case LV2_UI_GTK2:
  5201. carla_stdout("Will use LV2 Gtk2 UI for '%s', NOT!", pData->name);
  5202. fUI.type = UI::TYPE_EMBED;
  5203. break;
  5204. case LV2_UI_GTK3:
  5205. carla_stdout("Will use LV2 Gtk3 UI for '%s', NOT!", pData->name);
  5206. fUI.type = UI::TYPE_EMBED;
  5207. break;
  5208. #ifdef CARLA_OS_MAC
  5209. case LV2_UI_COCOA:
  5210. carla_stdout("Will use LV2 Cocoa UI for '%s'", pData->name);
  5211. fUI.type = UI::TYPE_EMBED;
  5212. break;
  5213. #endif
  5214. #ifdef CARLA_OS_WIN
  5215. case LV2_UI_WINDOWS:
  5216. carla_stdout("Will use LV2 Windows UI for '%s'", pData->name);
  5217. fUI.type = UI::TYPE_EMBED;
  5218. break;
  5219. #endif
  5220. case LV2_UI_X11:
  5221. #ifdef HAVE_X11
  5222. carla_stdout("Will use LV2 X11 UI for '%s'", pData->name);
  5223. #else
  5224. carla_stdout("Will use LV2 X11 UI for '%s', NOT!", pData->name);
  5225. #endif
  5226. fUI.type = UI::TYPE_EMBED;
  5227. break;
  5228. case LV2_UI_EXTERNAL:
  5229. case LV2_UI_OLD_EXTERNAL:
  5230. carla_stdout("Will use LV2 External UI for '%s'", pData->name);
  5231. fUI.type = UI::TYPE_EXTERNAL;
  5232. break;
  5233. }
  5234. if (fUI.type == UI::TYPE_NULL)
  5235. {
  5236. pData->uiLibClose();
  5237. fUI.descriptor = nullptr;
  5238. fUI.rdfDescriptor = nullptr;
  5239. return;
  5240. }
  5241. // ---------------------------------------------------------------
  5242. // initialize ui data
  5243. CarlaString guiTitle(pData->name);
  5244. guiTitle += " (GUI)";
  5245. fLv2Options.windowTitle = guiTitle.dup();
  5246. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  5247. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].value = fLv2Options.windowTitle;
  5248. // ---------------------------------------------------------------
  5249. // initialize ui features (part 1)
  5250. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  5251. uiDataFt->data_access = fDescriptor->extension_data;
  5252. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  5253. uiPortMapFt->handle = this;
  5254. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  5255. LV2UI_Request_Value* const uiRequestValueFt = new LV2UI_Request_Value;
  5256. uiRequestValueFt->handle = this;
  5257. uiRequestValueFt->request = carla_lv2_ui_request_value;
  5258. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  5259. uiResizeFt->handle = this;
  5260. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  5261. LV2UI_Touch* const uiTouchFt = new LV2UI_Touch;
  5262. uiTouchFt->handle = this;
  5263. uiTouchFt->touch = carla_lv2_ui_touch;
  5264. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  5265. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  5266. uiExternalHostFt->plugin_human_id = fLv2Options.windowTitle;
  5267. // ---------------------------------------------------------------
  5268. // initialize ui features (part 2)
  5269. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  5270. fFeatures[j] = new LV2_Feature;
  5271. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  5272. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  5273. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  5274. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  5275. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  5276. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  5277. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  5278. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  5279. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  5280. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  5281. fFeatures[kFeatureIdUiMakeResident2]->URI = LV2_UI__makeSONameResident;
  5282. fFeatures[kFeatureIdUiMakeResident2]->data = nullptr;
  5283. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  5284. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  5285. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  5286. fFeatures[kFeatureIdUiParent]->data = nullptr;
  5287. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  5288. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  5289. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  5290. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  5291. fFeatures[kFeatureIdUiRequestValue]->URI = LV2_UI__requestValue;
  5292. fFeatures[kFeatureIdUiRequestValue]->data = uiRequestValueFt;
  5293. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  5294. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  5295. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  5296. fFeatures[kFeatureIdUiTouch]->data = uiTouchFt;
  5297. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  5298. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  5299. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  5300. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  5301. // ---------------------------------------------------------------
  5302. // initialize ui extensions
  5303. if (fUI.descriptor->extension_data == nullptr)
  5304. return;
  5305. fExt.uiidle = (const LV2UI_Idle_Interface*)fUI.descriptor->extension_data(LV2_UI__idleInterface);
  5306. fExt.uishow = (const LV2UI_Show_Interface*)fUI.descriptor->extension_data(LV2_UI__showInterface);
  5307. fExt.uiresize = (const LV2UI_Resize*)fUI.descriptor->extension_data(LV2_UI__resize);
  5308. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUI.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  5309. // check if invalid
  5310. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  5311. fExt.uiidle = nullptr;
  5312. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  5313. fExt.uishow = nullptr;
  5314. if (fExt.uiresize != nullptr && fExt.uiresize->ui_resize == nullptr)
  5315. fExt.uiresize = nullptr;
  5316. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  5317. fExt.uiprograms = nullptr;
  5318. // don't use uiidle if external
  5319. if (fUI.type == UI::TYPE_EXTERNAL)
  5320. fExt.uiidle = nullptr;
  5321. }
  5322. // -------------------------------------------------------------------
  5323. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  5324. {
  5325. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  5326. carla_debug("CarlaPluginLV2::handleTransferAtom(%i, %p)", portIndex, atom);
  5327. fAtomBufferEvIn.put(atom, portIndex);
  5328. }
  5329. void handleUridMap(const LV2_URID urid, const char* const uri)
  5330. {
  5331. CARLA_SAFE_ASSERT_RETURN(urid != kUridNull,);
  5332. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  5333. carla_debug("CarlaPluginLV2::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.size()-1, uri);
  5334. const std::size_t uriCount(fCustomURIDs.size());
  5335. if (urid < uriCount)
  5336. {
  5337. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  5338. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr && ourURI != kUnmapFallback,);
  5339. if (std::strcmp(ourURI, uri) != 0)
  5340. {
  5341. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  5342. }
  5343. }
  5344. else
  5345. {
  5346. CARLA_SAFE_ASSERT_RETURN(urid == uriCount,);
  5347. fCustomURIDs.push_back(uri);
  5348. }
  5349. }
  5350. // -------------------------------------------------------------------
  5351. void writeAtomPath(const char* const path, const LV2_URID urid)
  5352. {
  5353. uint8_t atomBuf[4096];
  5354. lv2_atom_forge_set_buffer(&fAtomForge, atomBuf, sizeof(atomBuf));
  5355. LV2_Atom_Forge_Frame forgeFrame;
  5356. lv2_atom_forge_object(&fAtomForge, &forgeFrame, kUridNull, kUridPatchSet);
  5357. lv2_atom_forge_key(&fAtomForge, kUridPatchPoperty);
  5358. lv2_atom_forge_urid(&fAtomForge, urid);
  5359. lv2_atom_forge_key(&fAtomForge, kUridPatchValue);
  5360. lv2_atom_forge_path(&fAtomForge, path, static_cast<uint32_t>(std::strlen(path)));
  5361. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  5362. LV2_Atom* const atom((LV2_Atom*)atomBuf);
  5363. CARLA_SAFE_ASSERT(atom->size < sizeof(atomBuf));
  5364. fAtomBufferEvIn.put(atom, fEventsIn.ctrlIndex);
  5365. }
  5366. // -------------------------------------------------------------------
  5367. private:
  5368. LV2_Handle fHandle;
  5369. LV2_Handle fHandle2;
  5370. LV2_Feature* fFeatures[kFeatureCountAll+1];
  5371. const LV2_Descriptor* fDescriptor;
  5372. const LV2_RDF_Descriptor* fRdfDescriptor;
  5373. float** fAudioInBuffers;
  5374. float** fAudioOutBuffers;
  5375. float** fCvInBuffers;
  5376. float** fCvOutBuffers;
  5377. float* fParamBuffers;
  5378. bool fNeedsFixedBuffers;
  5379. bool fNeedsUiClose;
  5380. bool fInlineDisplayNeedsRedraw;
  5381. int64_t fInlineDisplayLastRedrawTime;
  5382. int32_t fLatencyIndex; // -1 if invalid
  5383. int fStrictBounds; // -1 unsupported, 0 optional, 1 required
  5384. Lv2AtomRingBuffer fAtomBufferEvIn;
  5385. Lv2AtomRingBuffer fAtomBufferUiOut;
  5386. Lv2AtomRingBuffer fAtomBufferWorkerIn;
  5387. Lv2AtomRingBuffer fAtomBufferWorkerResp;
  5388. LV2_Atom_Forge fAtomForge;
  5389. uint8_t* fAtomBufferUiOutTmpData;
  5390. uint8_t* fAtomBufferWorkerInTmpData;
  5391. CarlaPluginLV2EventData fEventsIn;
  5392. CarlaPluginLV2EventData fEventsOut;
  5393. CarlaPluginLV2Options fLv2Options;
  5394. CarlaPipeServerLV2 fPipeServer;
  5395. std::vector<std::string> fCustomURIDs;
  5396. bool fFirstActive; // first process() call after activate()
  5397. void* fLastStateChunk;
  5398. EngineTimeInfo fLastTimeInfo;
  5399. // if plugin provides path parameter, use it as fake "gui"
  5400. CarlaString fFilePathURI;
  5401. struct Extensions {
  5402. const LV2_Options_Interface* options;
  5403. const LV2_State_Interface* state;
  5404. const LV2_Worker_Interface* worker;
  5405. const LV2_Inline_Display_Interface* inlineDisplay;
  5406. const LV2_Midnam_Interface* midnam;
  5407. const LV2_Programs_Interface* programs;
  5408. const LV2UI_Idle_Interface* uiidle;
  5409. const LV2UI_Show_Interface* uishow;
  5410. const LV2UI_Resize* uiresize;
  5411. const LV2_Programs_UI_Interface* uiprograms;
  5412. Extensions()
  5413. : options(nullptr),
  5414. state(nullptr),
  5415. worker(nullptr),
  5416. inlineDisplay(nullptr),
  5417. midnam(nullptr),
  5418. programs(nullptr),
  5419. uiidle(nullptr),
  5420. uishow(nullptr),
  5421. uiresize(nullptr),
  5422. uiprograms(nullptr) {}
  5423. CARLA_DECLARE_NON_COPY_STRUCT(Extensions);
  5424. } fExt;
  5425. struct UI {
  5426. enum Type {
  5427. TYPE_NULL = 0,
  5428. TYPE_BRIDGE,
  5429. TYPE_EMBED,
  5430. TYPE_EXTERNAL
  5431. };
  5432. Type type;
  5433. LV2UI_Handle handle;
  5434. LV2UI_Widget widget;
  5435. const LV2UI_Descriptor* descriptor;
  5436. const LV2_RDF_UI* rdfDescriptor;
  5437. bool fileBrowserOpen;
  5438. const char* fileNeededForURI;
  5439. CarlaPluginUI* window;
  5440. UI()
  5441. : type(TYPE_NULL),
  5442. handle(nullptr),
  5443. widget(nullptr),
  5444. descriptor(nullptr),
  5445. rdfDescriptor(nullptr),
  5446. fileBrowserOpen(false),
  5447. fileNeededForURI(nullptr),
  5448. window(nullptr) {}
  5449. ~UI()
  5450. {
  5451. CARLA_SAFE_ASSERT(handle == nullptr);
  5452. CARLA_SAFE_ASSERT(widget == nullptr);
  5453. CARLA_SAFE_ASSERT(descriptor == nullptr);
  5454. CARLA_SAFE_ASSERT(rdfDescriptor == nullptr);
  5455. CARLA_SAFE_ASSERT(! fileBrowserOpen);
  5456. CARLA_SAFE_ASSERT(fileNeededForURI == nullptr);
  5457. CARLA_SAFE_ASSERT(window == nullptr);
  5458. }
  5459. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  5460. } fUI;
  5461. // -------------------------------------------------------------------
  5462. // Event Feature
  5463. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  5464. {
  5465. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  5466. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  5467. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  5468. return 0;
  5469. }
  5470. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  5471. {
  5472. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  5473. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  5474. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  5475. return 0;
  5476. }
  5477. // -------------------------------------------------------------------
  5478. // Logs Feature
  5479. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  5480. {
  5481. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  5482. CARLA_SAFE_ASSERT_RETURN(type != kUridNull, 0);
  5483. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  5484. #ifndef DEBUG
  5485. if (type == kUridLogTrace)
  5486. return 0;
  5487. #endif
  5488. va_list args;
  5489. va_start(args, fmt);
  5490. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  5491. va_end(args);
  5492. return ret;
  5493. }
  5494. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  5495. {
  5496. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  5497. CARLA_SAFE_ASSERT_RETURN(type != kUridNull, 0);
  5498. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  5499. int ret = 0;
  5500. switch (type)
  5501. {
  5502. case kUridLogError:
  5503. std::fprintf(stderr, "\x1b[31m");
  5504. ret = std::vfprintf(stderr, fmt, ap);
  5505. std::fprintf(stderr, "\x1b[0m");
  5506. break;
  5507. case kUridLogNote:
  5508. ret = std::vfprintf(stdout, fmt, ap);
  5509. break;
  5510. case kUridLogTrace:
  5511. #ifdef DEBUG
  5512. std::fprintf(stdout, "\x1b[30;1m");
  5513. ret = std::vfprintf(stdout, fmt, ap);
  5514. std::fprintf(stdout, "\x1b[0m");
  5515. #endif
  5516. break;
  5517. case kUridLogWarning:
  5518. ret = std::vfprintf(stderr, fmt, ap);
  5519. break;
  5520. default:
  5521. break;
  5522. }
  5523. return ret;
  5524. }
  5525. // -------------------------------------------------------------------
  5526. // Programs Feature
  5527. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  5528. {
  5529. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5530. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  5531. ((CarlaPluginLV2*)handle)->handleProgramChanged(index);
  5532. }
  5533. // -------------------------------------------------------------------
  5534. // Resize Port Feature
  5535. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  5536. {
  5537. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  5538. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  5539. return ((CarlaPluginLV2*)data)->handleResizePort(index, size);
  5540. }
  5541. // -------------------------------------------------------------------
  5542. // State Feature
  5543. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  5544. {
  5545. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  5546. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  5547. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  5548. File file;
  5549. if (File::isAbsolutePath(path))
  5550. file = File(path);
  5551. else
  5552. file = File::getCurrentWorkingDirectory().getChildFile(path);
  5553. file.getParentDirectory().createDirectory();
  5554. return strdup(file.getFullPathName().toRawUTF8());
  5555. }
  5556. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  5557. {
  5558. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, strdup(""));
  5559. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', strdup(""));
  5560. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  5561. // may already be an abstract path
  5562. if (! File::isAbsolutePath(absolute_path))
  5563. return strdup(absolute_path);
  5564. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  5565. }
  5566. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  5567. {
  5568. const char* const cwd(File::getCurrentWorkingDirectory().getFullPathName().toRawUTF8());
  5569. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, strdup(cwd));
  5570. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', strdup(cwd));
  5571. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  5572. // may already be an absolute path
  5573. if (File::isAbsolutePath(abstract_path))
  5574. return strdup(abstract_path);
  5575. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  5576. }
  5577. 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)
  5578. {
  5579. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  5580. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  5581. return ((CarlaPluginLV2*)handle)->handleStateStore(key, value, size, type, flags);
  5582. }
  5583. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  5584. {
  5585. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  5586. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  5587. return ((CarlaPluginLV2*)handle)->handleStateRetrieve(key, size, type, flags);
  5588. }
  5589. // -------------------------------------------------------------------
  5590. // URI-Map Feature
  5591. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  5592. {
  5593. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  5594. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  5595. // unused
  5596. (void)map;
  5597. }
  5598. // -------------------------------------------------------------------
  5599. // URID Feature
  5600. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  5601. {
  5602. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, kUridNull);
  5603. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', kUridNull);
  5604. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  5605. // Atom types
  5606. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  5607. return kUridAtomBlank;
  5608. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  5609. return kUridAtomBool;
  5610. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  5611. return kUridAtomChunk;
  5612. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  5613. return kUridAtomDouble;
  5614. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  5615. return kUridAtomEvent;
  5616. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  5617. return kUridAtomFloat;
  5618. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  5619. return kUridAtomInt;
  5620. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  5621. return kUridAtomLiteral;
  5622. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  5623. return kUridAtomLong;
  5624. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  5625. return kUridAtomNumber;
  5626. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  5627. return kUridAtomObject;
  5628. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  5629. return kUridAtomPath;
  5630. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  5631. return kUridAtomProperty;
  5632. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  5633. return kUridAtomResource;
  5634. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  5635. return kUridAtomSequence;
  5636. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  5637. return kUridAtomSound;
  5638. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  5639. return kUridAtomString;
  5640. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  5641. return kUridAtomTuple;
  5642. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  5643. return kUridAtomURI;
  5644. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  5645. return kUridAtomURID;
  5646. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  5647. return kUridAtomVector;
  5648. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  5649. return kUridAtomTransferAtom;
  5650. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  5651. return kUridAtomTransferEvent;
  5652. // BufSize types
  5653. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  5654. return kUridBufMaxLength;
  5655. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  5656. return kUridBufMinLength;
  5657. if (std::strcmp(uri, LV2_BUF_SIZE__nominalBlockLength) == 0)
  5658. return kUridBufNominalLength;
  5659. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  5660. return kUridBufSequenceSize;
  5661. // Log types
  5662. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  5663. return kUridLogError;
  5664. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  5665. return kUridLogNote;
  5666. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  5667. return kUridLogTrace;
  5668. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  5669. return kUridLogWarning;
  5670. // Patch types
  5671. if (std::strcmp(uri, LV2_PATCH__Set) == 0)
  5672. return kUridPatchSet;
  5673. if (std::strcmp(uri, LV2_PATCH__property) == 0)
  5674. return kUridPatchPoperty;
  5675. if (std::strcmp(uri, LV2_PATCH__value) == 0)
  5676. return kUridPatchValue;
  5677. // Time types
  5678. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  5679. return kUridTimePosition;
  5680. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  5681. return kUridTimeBar;
  5682. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  5683. return kUridTimeBarBeat;
  5684. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  5685. return kUridTimeBeat;
  5686. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  5687. return kUridTimeBeatUnit;
  5688. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  5689. return kUridTimeBeatsPerBar;
  5690. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  5691. return kUridTimeBeatsPerMinute;
  5692. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  5693. return kUridTimeFrame;
  5694. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  5695. return kUridTimeFramesPerSecond;
  5696. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  5697. return kUridTimeSpeed;
  5698. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  5699. return kUridTimeTicksPerBeat;
  5700. // Others
  5701. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  5702. return kUridMidiEvent;
  5703. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  5704. return kUridParamSampleRate;
  5705. if (std::strcmp(uri, LV2_UI__backgroundColor) == 0)
  5706. return kUridBackgroundColor;
  5707. if (std::strcmp(uri, LV2_UI__foregroundColor) == 0)
  5708. return kUridForegroundColor;
  5709. if (std::strcmp(uri, LV2_UI__scaleFactor) == 0)
  5710. return kUridScaleFactor;
  5711. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  5712. return kUridWindowTitle;
  5713. // Custom Carla types
  5714. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER_IN) == 0)
  5715. return kUridCarlaAtomWorkerIn;
  5716. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER_RESP) == 0)
  5717. return kUridCarlaAtomWorkerResp;
  5718. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  5719. return kUridCarlaTransientWindowId;
  5720. // Custom plugin types
  5721. return ((CarlaPluginLV2*)handle)->getCustomURID(uri);
  5722. }
  5723. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  5724. {
  5725. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  5726. CARLA_SAFE_ASSERT_RETURN(urid != kUridNull, nullptr);
  5727. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  5728. switch (urid)
  5729. {
  5730. // Atom types
  5731. case kUridAtomBlank:
  5732. return LV2_ATOM__Blank;
  5733. case kUridAtomBool:
  5734. return LV2_ATOM__Bool;
  5735. case kUridAtomChunk:
  5736. return LV2_ATOM__Chunk;
  5737. case kUridAtomDouble:
  5738. return LV2_ATOM__Double;
  5739. case kUridAtomEvent:
  5740. return LV2_ATOM__Event;
  5741. case kUridAtomFloat:
  5742. return LV2_ATOM__Float;
  5743. case kUridAtomInt:
  5744. return LV2_ATOM__Int;
  5745. case kUridAtomLiteral:
  5746. return LV2_ATOM__Literal;
  5747. case kUridAtomLong:
  5748. return LV2_ATOM__Long;
  5749. case kUridAtomNumber:
  5750. return LV2_ATOM__Number;
  5751. case kUridAtomObject:
  5752. return LV2_ATOM__Object;
  5753. case kUridAtomPath:
  5754. return LV2_ATOM__Path;
  5755. case kUridAtomProperty:
  5756. return LV2_ATOM__Property;
  5757. case kUridAtomResource:
  5758. return LV2_ATOM__Resource;
  5759. case kUridAtomSequence:
  5760. return LV2_ATOM__Sequence;
  5761. case kUridAtomSound:
  5762. return LV2_ATOM__Sound;
  5763. case kUridAtomString:
  5764. return LV2_ATOM__String;
  5765. case kUridAtomTuple:
  5766. return LV2_ATOM__Tuple;
  5767. case kUridAtomURI:
  5768. return LV2_ATOM__URI;
  5769. case kUridAtomURID:
  5770. return LV2_ATOM__URID;
  5771. case kUridAtomVector:
  5772. return LV2_ATOM__Vector;
  5773. case kUridAtomTransferAtom:
  5774. return LV2_ATOM__atomTransfer;
  5775. case kUridAtomTransferEvent:
  5776. return LV2_ATOM__eventTransfer;
  5777. // BufSize types
  5778. case kUridBufMaxLength:
  5779. return LV2_BUF_SIZE__maxBlockLength;
  5780. case kUridBufMinLength:
  5781. return LV2_BUF_SIZE__minBlockLength;
  5782. case kUridBufNominalLength:
  5783. return LV2_BUF_SIZE__nominalBlockLength;
  5784. case kUridBufSequenceSize:
  5785. return LV2_BUF_SIZE__sequenceSize;
  5786. // Log types
  5787. case kUridLogError:
  5788. return LV2_LOG__Error;
  5789. case kUridLogNote:
  5790. return LV2_LOG__Note;
  5791. case kUridLogTrace:
  5792. return LV2_LOG__Trace;
  5793. case kUridLogWarning:
  5794. return LV2_LOG__Warning;
  5795. // Patch types
  5796. case kUridPatchSet:
  5797. return LV2_PATCH__Set;
  5798. case kUridPatchPoperty:
  5799. return LV2_PATCH__property;
  5800. case kUridPatchValue:
  5801. return LV2_PATCH__value;
  5802. // Time types
  5803. case kUridTimePosition:
  5804. return LV2_TIME__Position;
  5805. case kUridTimeBar:
  5806. return LV2_TIME__bar;
  5807. case kUridTimeBarBeat:
  5808. return LV2_TIME__barBeat;
  5809. case kUridTimeBeat:
  5810. return LV2_TIME__beat;
  5811. case kUridTimeBeatUnit:
  5812. return LV2_TIME__beatUnit;
  5813. case kUridTimeBeatsPerBar:
  5814. return LV2_TIME__beatsPerBar;
  5815. case kUridTimeBeatsPerMinute:
  5816. return LV2_TIME__beatsPerMinute;
  5817. case kUridTimeFrame:
  5818. return LV2_TIME__frame;
  5819. case kUridTimeFramesPerSecond:
  5820. return LV2_TIME__framesPerSecond;
  5821. case kUridTimeSpeed:
  5822. return LV2_TIME__speed;
  5823. case kUridTimeTicksPerBeat:
  5824. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  5825. // Others
  5826. case kUridMidiEvent:
  5827. return LV2_MIDI__MidiEvent;
  5828. case kUridParamSampleRate:
  5829. return LV2_PARAMETERS__sampleRate;
  5830. case kUridBackgroundColor:
  5831. return LV2_UI__backgroundColor;
  5832. case kUridForegroundColor:
  5833. return LV2_UI__foregroundColor;
  5834. case kUridScaleFactor:
  5835. return LV2_UI__scaleFactor;
  5836. case kUridWindowTitle:
  5837. return LV2_UI__windowTitle;
  5838. // Custom Carla types
  5839. case kUridCarlaAtomWorkerIn:
  5840. return URI_CARLA_ATOM_WORKER_IN;
  5841. case kUridCarlaAtomWorkerResp:
  5842. return URI_CARLA_ATOM_WORKER_RESP;
  5843. case kUridCarlaTransientWindowId:
  5844. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  5845. }
  5846. // Custom plugin types
  5847. return ((CarlaPluginLV2*)handle)->getCustomURIDString(urid);
  5848. }
  5849. // -------------------------------------------------------------------
  5850. // Worker Feature
  5851. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  5852. {
  5853. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  5854. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  5855. return ((CarlaPluginLV2*)handle)->handleWorkerSchedule(size, data);
  5856. }
  5857. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  5858. {
  5859. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  5860. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  5861. return ((CarlaPluginLV2*)handle)->handleWorkerRespond(size, data);
  5862. }
  5863. // -------------------------------------------------------------------
  5864. // Inline Display Feature
  5865. static void carla_lv2_inline_display_queue_draw(LV2_Inline_Display_Handle handle)
  5866. {
  5867. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5868. // carla_debug("carla_lv2_inline_display_queue_draw(%p)", handle);
  5869. ((CarlaPluginLV2*)handle)->handleInlineDisplayQueueRedraw();
  5870. }
  5871. // -------------------------------------------------------------------
  5872. // Midnam Feature
  5873. static void carla_lv2_midnam_update(LV2_Midnam_Handle handle)
  5874. {
  5875. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5876. carla_stdout("carla_lv2_midnam_update(%p)", handle);
  5877. ((CarlaPluginLV2*)handle)->handleMidnamUpdate();
  5878. }
  5879. // -------------------------------------------------------------------
  5880. // External UI Feature
  5881. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  5882. {
  5883. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  5884. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  5885. ((CarlaPluginLV2*)controller)->handleExternalUIClosed();
  5886. }
  5887. // -------------------------------------------------------------------
  5888. // UI Port-Map Feature
  5889. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  5890. {
  5891. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  5892. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  5893. return ((CarlaPluginLV2*)handle)->handleUIPortMap(symbol);
  5894. }
  5895. // ----------------------------------------------------------------------------------------------------------------
  5896. // UI Request Parameter Feature
  5897. static LV2UI_Request_Value_Status carla_lv2_ui_request_value(LV2UI_Feature_Handle handle,
  5898. LV2_URID key,
  5899. LV2_URID type,
  5900. const LV2_Feature* const* features)
  5901. {
  5902. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_REQUEST_VALUE_ERR_UNKNOWN);
  5903. carla_debug("carla_lv2_ui_request_value(%p, %u, %u, %p)", handle, key, type, features);
  5904. return ((CarlaPluginLV2*)handle)->handleUIRequestValue(key, type, features);
  5905. }
  5906. // -------------------------------------------------------------------
  5907. // UI Resize Feature
  5908. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  5909. {
  5910. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  5911. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  5912. return ((CarlaPluginLV2*)handle)->handleUIResize(width, height);
  5913. }
  5914. // -------------------------------------------------------------------
  5915. // UI Touch Feature
  5916. static void carla_lv2_ui_touch(LV2UI_Feature_Handle handle, uint32_t port_index, bool touch)
  5917. {
  5918. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5919. carla_debug("carla_lv2_ui_touch(%p, %u, %s)", handle, port_index, bool2str(touch));
  5920. ((CarlaPluginLV2*)handle)->handleUITouch(port_index, touch);
  5921. }
  5922. // -------------------------------------------------------------------
  5923. // UI Extension
  5924. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  5925. {
  5926. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  5927. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  5928. ((CarlaPluginLV2*)controller)->handleUIWrite(port_index, buffer_size, format, buffer);
  5929. }
  5930. // -------------------------------------------------------------------
  5931. // Lilv State
  5932. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  5933. {
  5934. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  5935. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  5936. ((CarlaPluginLV2*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  5937. }
  5938. // -------------------------------------------------------------------
  5939. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLV2)
  5940. };
  5941. // -------------------------------------------------------------------------------------------------------------------
  5942. bool CarlaPipeServerLV2::msgReceived(const char* const msg) noexcept
  5943. {
  5944. if (std::strcmp(msg, "exiting") == 0)
  5945. {
  5946. closePipeServer();
  5947. fUiState = UiHide;
  5948. return true;
  5949. }
  5950. if (std::strcmp(msg, "control") == 0)
  5951. {
  5952. uint32_t index;
  5953. float value;
  5954. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5955. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  5956. try {
  5957. kPlugin->handleUIWrite(index, sizeof(float), kUridNull, &value);
  5958. } CARLA_SAFE_EXCEPTION("magReceived control");
  5959. return true;
  5960. }
  5961. if (std::strcmp(msg, "atom") == 0)
  5962. {
  5963. uint32_t index, atomTotalSize, base64Size;
  5964. const char* base64atom;
  5965. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5966. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  5967. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(base64Size), true);
  5968. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom, false, base64Size), true);
  5969. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  5970. CARLA_SAFE_ASSERT_UINT2_RETURN(chunk.size() >= sizeof(LV2_Atom), chunk.size(), sizeof(LV2_Atom), true);
  5971. #ifdef CARLA_PROPER_CPP11_SUPPORT
  5972. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  5973. #else
  5974. const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
  5975. #endif
  5976. CARLA_SAFE_ASSERT_RETURN(lv2_atom_total_size(atom) == chunk.size(), true);
  5977. try {
  5978. kPlugin->handleUIWrite(index, lv2_atom_total_size(atom), kUridAtomTransferEvent, atom);
  5979. } CARLA_SAFE_EXCEPTION("magReceived atom");
  5980. return true;
  5981. }
  5982. if (std::strcmp(msg, "program") == 0)
  5983. {
  5984. uint32_t index;
  5985. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5986. try {
  5987. kPlugin->setMidiProgram(static_cast<int32_t>(index), false, true, true, false);
  5988. } CARLA_SAFE_EXCEPTION("msgReceived program");
  5989. return true;
  5990. }
  5991. if (std::strcmp(msg, "urid") == 0)
  5992. {
  5993. uint32_t urid, size;
  5994. const char* uri;
  5995. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  5996. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), true);
  5997. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri, false, size), true);
  5998. if (urid != 0)
  5999. {
  6000. try {
  6001. kPlugin->handleUridMap(urid, uri);
  6002. } CARLA_SAFE_EXCEPTION("msgReceived urid");
  6003. }
  6004. return true;
  6005. }
  6006. if (std::strcmp(msg, "reloadprograms") == 0)
  6007. {
  6008. int32_t index;
  6009. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index), true);
  6010. try {
  6011. kPlugin->handleProgramChanged(index);
  6012. } CARLA_SAFE_EXCEPTION("handleProgramChanged");
  6013. return true;
  6014. }
  6015. if (std::strcmp(msg, "requestvalue") == 0)
  6016. {
  6017. uint32_t key, type;
  6018. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(key), true);
  6019. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(type), true);
  6020. if (key != 0)
  6021. {
  6022. try {
  6023. kPlugin->handleUIRequestValue(key, type, nullptr);
  6024. } CARLA_SAFE_EXCEPTION("msgReceived requestvalue");
  6025. }
  6026. return true;
  6027. }
  6028. return false;
  6029. }
  6030. // -------------------------------------------------------------------------------------------------------------------
  6031. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  6032. {
  6033. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  6034. CarlaPluginLV2* const plugin(new CarlaPluginLV2(init.engine, init.id));
  6035. if (! plugin->init(init.name, init.label, init.options))
  6036. {
  6037. delete plugin;
  6038. return nullptr;
  6039. }
  6040. return plugin;
  6041. }
  6042. // used in CarlaStandalone.cpp
  6043. const void* carla_render_inline_display_lv2(CarlaPlugin* plugin, uint32_t width, uint32_t height);
  6044. const void* carla_render_inline_display_lv2(CarlaPlugin* plugin, uint32_t width, uint32_t height)
  6045. {
  6046. CarlaPluginLV2* const lv2Plugin = (CarlaPluginLV2*)plugin;
  6047. return lv2Plugin->renderInlineDisplay(width, height);
  6048. }
  6049. // -------------------------------------------------------------------------------------------------------------------
  6050. CARLA_BACKEND_END_NAMESPACE