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.

7407 lines
272KB

  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, cvIns, aOuts, cvOuts);
  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,
  2494. const uint32_t cvIns,
  2495. const uint32_t aOuts,
  2496. const uint32_t cvOuts) const
  2497. {
  2498. if (fLatencyIndex < 0)
  2499. return;
  2500. // we need to pre-run the plugin so it can update its latency control-port
  2501. const uint32_t bufferSize = static_cast<uint32_t>(fLv2Options.nominalBufferSize);
  2502. float tmpIn [( aIns+cvIns > 0) ? aIns+cvIns : 1][bufferSize];
  2503. float tmpOut[(aOuts+cvOuts > 0) ? aOuts+cvOuts : 1][bufferSize];
  2504. {
  2505. uint32_t i=0;
  2506. for (; i < aIns; ++i)
  2507. {
  2508. carla_zeroFloats(tmpIn[i], bufferSize);
  2509. try {
  2510. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, tmpIn[i]);
  2511. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency audio input");
  2512. }
  2513. for (uint32_t j=0; j < cvIns; ++i, ++j)
  2514. {
  2515. carla_zeroFloats(tmpIn[i], bufferSize);
  2516. try {
  2517. fDescriptor->connect_port(fHandle, pData->cvIn.ports[j].rindex, tmpIn[i]);
  2518. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency cv input");
  2519. }
  2520. }
  2521. {
  2522. uint32_t i=0;
  2523. for (; i < aOuts; ++i)
  2524. {
  2525. carla_zeroFloats(tmpOut[i], bufferSize);
  2526. try {
  2527. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, tmpOut[i]);
  2528. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency audio output");
  2529. }
  2530. for (uint32_t j=0; j < cvOuts; ++i, ++j)
  2531. {
  2532. carla_zeroFloats(tmpOut[i], bufferSize);
  2533. try {
  2534. fDescriptor->connect_port(fHandle, pData->cvOut.ports[j].rindex, tmpOut[i]);
  2535. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency cv output");
  2536. }
  2537. }
  2538. if (fDescriptor->activate != nullptr)
  2539. {
  2540. try {
  2541. fDescriptor->activate(fHandle);
  2542. } CARLA_SAFE_EXCEPTION("LV2 latency activate");
  2543. }
  2544. try {
  2545. fDescriptor->run(fHandle, bufferSize);
  2546. } CARLA_SAFE_EXCEPTION("LV2 latency run");
  2547. if (fDescriptor->deactivate != nullptr)
  2548. {
  2549. try {
  2550. fDescriptor->deactivate(fHandle);
  2551. } CARLA_SAFE_EXCEPTION("LV2 latency deactivate");
  2552. }
  2553. // done, let's get the value
  2554. if (const uint32_t latency = getLatencyInFrames())
  2555. {
  2556. pData->client->setLatency(latency);
  2557. #ifndef BUILD_BRIDGE
  2558. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  2559. #endif
  2560. }
  2561. }
  2562. void reloadPrograms(const bool doInit) override
  2563. {
  2564. carla_debug("CarlaPluginLV2::reloadPrograms(%s)", bool2str(doInit));
  2565. const uint32_t oldCount = pData->midiprog.count;
  2566. const int32_t current = pData->midiprog.current;
  2567. // special LV2 programs handling
  2568. if (doInit)
  2569. {
  2570. pData->prog.clear();
  2571. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  2572. if (presetCount > 0)
  2573. {
  2574. pData->prog.createNew(presetCount);
  2575. for (uint32_t i=0; i < presetCount; ++i)
  2576. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  2577. }
  2578. }
  2579. // Delete old programs
  2580. pData->midiprog.clear();
  2581. // Query new programs
  2582. uint32_t newCount = 0;
  2583. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  2584. {
  2585. for (; fExt.programs->get_program(fHandle, newCount);)
  2586. ++newCount;
  2587. }
  2588. if (newCount > 0)
  2589. {
  2590. pData->midiprog.createNew(newCount);
  2591. // Update data
  2592. for (uint32_t i=0; i < newCount; ++i)
  2593. {
  2594. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  2595. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  2596. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  2597. pData->midiprog.data[i].bank = pdesc->bank;
  2598. pData->midiprog.data[i].program = pdesc->program;
  2599. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  2600. }
  2601. }
  2602. if (doInit)
  2603. {
  2604. if (newCount > 0)
  2605. {
  2606. setMidiProgram(0, false, false, false, true);
  2607. }
  2608. else
  2609. {
  2610. // load default state
  2611. if (LilvState* const state = Lv2WorldClass::getInstance().getStateFromURI(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  2612. {
  2613. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  2614. if (fHandle2 != nullptr)
  2615. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  2616. lilv_state_free(state);
  2617. }
  2618. }
  2619. }
  2620. else
  2621. {
  2622. // Check if current program is invalid
  2623. bool programChanged = false;
  2624. if (newCount == oldCount+1)
  2625. {
  2626. // one midi program added, probably created by user
  2627. pData->midiprog.current = static_cast<int32_t>(oldCount);
  2628. programChanged = true;
  2629. }
  2630. else if (current < 0 && newCount > 0)
  2631. {
  2632. // programs exist now, but not before
  2633. pData->midiprog.current = 0;
  2634. programChanged = true;
  2635. }
  2636. else if (current >= 0 && newCount == 0)
  2637. {
  2638. // programs existed before, but not anymore
  2639. pData->midiprog.current = -1;
  2640. programChanged = true;
  2641. }
  2642. else if (current >= static_cast<int32_t>(newCount))
  2643. {
  2644. // current midi program > count
  2645. pData->midiprog.current = 0;
  2646. programChanged = true;
  2647. }
  2648. else
  2649. {
  2650. // no change
  2651. pData->midiprog.current = current;
  2652. }
  2653. if (programChanged)
  2654. setMidiProgram(pData->midiprog.current, true, true, true, false);
  2655. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  2656. }
  2657. }
  2658. // -------------------------------------------------------------------
  2659. // Plugin processing
  2660. void activate() noexcept override
  2661. {
  2662. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2663. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2664. if (fDescriptor->activate != nullptr)
  2665. {
  2666. try {
  2667. fDescriptor->activate(fHandle);
  2668. } CARLA_SAFE_EXCEPTION("LV2 activate");
  2669. if (fHandle2 != nullptr)
  2670. {
  2671. try {
  2672. fDescriptor->activate(fHandle2);
  2673. } CARLA_SAFE_EXCEPTION("LV2 activate #2");
  2674. }
  2675. }
  2676. fFirstActive = true;
  2677. }
  2678. void deactivate() noexcept override
  2679. {
  2680. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2681. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2682. if (fDescriptor->deactivate != nullptr)
  2683. {
  2684. try {
  2685. fDescriptor->deactivate(fHandle);
  2686. } CARLA_SAFE_EXCEPTION("LV2 deactivate");
  2687. if (fHandle2 != nullptr)
  2688. {
  2689. try {
  2690. fDescriptor->deactivate(fHandle2);
  2691. } CARLA_SAFE_EXCEPTION("LV2 deactivate #2");
  2692. }
  2693. }
  2694. }
  2695. void process(const float* const* const audioIn, float** const audioOut,
  2696. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  2697. {
  2698. // --------------------------------------------------------------------------------------------------------
  2699. // Check if active
  2700. if (! pData->active)
  2701. {
  2702. // disable any output sound
  2703. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2704. carla_zeroFloats(audioOut[i], frames);
  2705. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2706. carla_zeroFloats(cvOut[i], frames);
  2707. return;
  2708. }
  2709. // --------------------------------------------------------------------------------------------------------
  2710. // Event itenerators from different APIs (input)
  2711. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  2712. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  2713. LV2_MIDIState evInMidiStates[fEventsIn.count];
  2714. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2715. {
  2716. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2717. {
  2718. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  2719. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  2720. }
  2721. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2722. {
  2723. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  2724. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  2725. }
  2726. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2727. {
  2728. fEventsIn.data[i].midi.event_count = 0;
  2729. fEventsIn.data[i].midi.size = 0;
  2730. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  2731. evInMidiStates[i].frame_count = frames;
  2732. evInMidiStates[i].position = 0;
  2733. }
  2734. }
  2735. for (uint32_t i=0; i < fEventsOut.count; ++i)
  2736. {
  2737. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2738. {
  2739. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  2740. }
  2741. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2742. {
  2743. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  2744. }
  2745. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2746. {
  2747. // not needed
  2748. }
  2749. }
  2750. // --------------------------------------------------------------------------------------------------------
  2751. // Check if needs reset
  2752. if (pData->needsReset)
  2753. {
  2754. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  2755. {
  2756. const uint32_t j = fEventsIn.ctrlIndex;
  2757. CARLA_ASSERT(j < fEventsIn.count);
  2758. uint8_t midiData[3] = { 0, 0, 0 };
  2759. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2760. {
  2761. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  2762. {
  2763. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2764. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2765. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2766. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2767. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2768. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2769. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2770. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2771. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2772. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2773. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2774. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2775. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2776. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2777. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2778. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2779. }
  2780. }
  2781. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2782. {
  2783. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2784. {
  2785. midiData[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  2786. midiData[1] = k;
  2787. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2788. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2789. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2790. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiData);
  2791. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2792. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2793. }
  2794. }
  2795. }
  2796. pData->needsReset = false;
  2797. }
  2798. // --------------------------------------------------------------------------------------------------------
  2799. // TimeInfo
  2800. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  2801. if (fFirstActive || fLastTimeInfo != timeInfo)
  2802. {
  2803. bool doPostRt;
  2804. int32_t rindex;
  2805. const double barBeat = static_cast<double>(timeInfo.bbt.beat - 1)
  2806. + (timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat);
  2807. // update input ports
  2808. for (uint32_t k=0; k < pData->param.count; ++k)
  2809. {
  2810. if (pData->param.data[k].type != PARAMETER_INPUT)
  2811. continue;
  2812. if (pData->param.special[k] != PARAMETER_SPECIAL_TIME)
  2813. continue;
  2814. doPostRt = false;
  2815. rindex = pData->param.data[k].rindex;
  2816. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2817. switch (fRdfDescriptor->Ports[rindex].Designation)
  2818. {
  2819. // Non-BBT
  2820. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2821. if (fLastTimeInfo.playing != timeInfo.playing)
  2822. {
  2823. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2824. doPostRt = true;
  2825. }
  2826. break;
  2827. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2828. if (fLastTimeInfo.frame != timeInfo.frame)
  2829. {
  2830. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2831. doPostRt = true;
  2832. }
  2833. break;
  2834. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2835. break;
  2836. // BBT
  2837. case LV2_PORT_DESIGNATION_TIME_BAR:
  2838. if (timeInfo.bbt.valid && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2839. {
  2840. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2841. doPostRt = true;
  2842. }
  2843. break;
  2844. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2845. if (timeInfo.bbt.valid && (carla_isNotEqual(fLastTimeInfo.bbt.tick, timeInfo.bbt.tick) ||
  2846. fLastTimeInfo.bbt.beat != timeInfo.bbt.beat))
  2847. {
  2848. fParamBuffers[k] = static_cast<float>(barBeat);
  2849. doPostRt = true;
  2850. }
  2851. break;
  2852. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2853. if (timeInfo.bbt.valid && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2854. {
  2855. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2856. doPostRt = true;
  2857. }
  2858. break;
  2859. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2860. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.beatType, timeInfo.bbt.beatType))
  2861. {
  2862. fParamBuffers[k] = timeInfo.bbt.beatType;
  2863. doPostRt = true;
  2864. }
  2865. break;
  2866. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2867. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.beatsPerBar, timeInfo.bbt.beatsPerBar))
  2868. {
  2869. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2870. doPostRt = true;
  2871. }
  2872. break;
  2873. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2874. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.beatsPerMinute, timeInfo.bbt.beatsPerMinute))
  2875. {
  2876. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2877. doPostRt = true;
  2878. }
  2879. break;
  2880. case LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT:
  2881. if (timeInfo.bbt.valid && carla_isNotEqual(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat))
  2882. {
  2883. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.ticksPerBeat);
  2884. doPostRt = true;
  2885. }
  2886. break;
  2887. }
  2888. if (doPostRt)
  2889. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  2890. true,
  2891. static_cast<int32_t>(k),
  2892. 0,
  2893. 0,
  2894. fParamBuffers[k]);
  2895. }
  2896. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2897. {
  2898. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2899. continue;
  2900. uint8_t timeInfoBuf[256];
  2901. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2902. LV2_Atom_Forge_Frame forgeFrame;
  2903. lv2_atom_forge_object(&fAtomForge, &forgeFrame, kUridNull, kUridTimePosition);
  2904. lv2_atom_forge_key(&fAtomForge, kUridTimeSpeed);
  2905. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2906. lv2_atom_forge_key(&fAtomForge, kUridTimeFrame);
  2907. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2908. if (timeInfo.bbt.valid)
  2909. {
  2910. lv2_atom_forge_key(&fAtomForge, kUridTimeBar);
  2911. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2912. lv2_atom_forge_key(&fAtomForge, kUridTimeBarBeat);
  2913. lv2_atom_forge_float(&fAtomForge, static_cast<float>(barBeat));
  2914. lv2_atom_forge_key(&fAtomForge, kUridTimeBeat);
  2915. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat - 1);
  2916. lv2_atom_forge_key(&fAtomForge, kUridTimeBeatUnit);
  2917. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2918. lv2_atom_forge_key(&fAtomForge, kUridTimeBeatsPerBar);
  2919. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2920. lv2_atom_forge_key(&fAtomForge, kUridTimeBeatsPerMinute);
  2921. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2922. lv2_atom_forge_key(&fAtomForge, kUridTimeTicksPerBeat);
  2923. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.ticksPerBeat);
  2924. }
  2925. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2926. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2927. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2928. // send only deprecated blank object for now
  2929. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, kUridAtomBlank, atom->size, LV2_ATOM_BODY_CONST(atom));
  2930. // for atom:object
  2931. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2932. }
  2933. pData->postRtEvents.trySplice();
  2934. fLastTimeInfo = timeInfo;
  2935. }
  2936. // --------------------------------------------------------------------------------------------------------
  2937. // Event Input and Processing
  2938. if (fEventsIn.ctrl != nullptr)
  2939. {
  2940. // ----------------------------------------------------------------------------------------------------
  2941. // Message Input
  2942. if (fAtomBufferEvIn.tryLock())
  2943. {
  2944. if (fAtomBufferEvIn.isDataAvailableForReading())
  2945. {
  2946. const LV2_Atom* atom;
  2947. uint32_t j, portIndex;
  2948. for (; fAtomBufferEvIn.get(atom, portIndex);)
  2949. {
  2950. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2951. if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2952. {
  2953. carla_stderr2("Event input buffer full, at least 1 message lost");
  2954. continue;
  2955. }
  2956. }
  2957. }
  2958. fAtomBufferEvIn.unlock();
  2959. }
  2960. if (fExt.worker != nullptr && fAtomBufferWorkerIn.tryLock())
  2961. {
  2962. if (fAtomBufferWorkerIn.isDataAvailableForReading())
  2963. {
  2964. const LV2_Atom* atom;
  2965. uint32_t portIndex;
  2966. for (; fAtomBufferWorkerIn.get(atom, portIndex);)
  2967. {
  2968. CARLA_SAFE_ASSERT_CONTINUE(atom->type == kUridCarlaAtomWorkerIn);
  2969. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  2970. }
  2971. }
  2972. fAtomBufferWorkerIn.unlock();
  2973. }
  2974. // ----------------------------------------------------------------------------------------------------
  2975. // MIDI Input (External)
  2976. if (pData->extNotes.mutex.tryLock())
  2977. {
  2978. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2979. {
  2980. // does not handle MIDI
  2981. pData->extNotes.data.clear();
  2982. }
  2983. else
  2984. {
  2985. const uint32_t j = fEventsIn.ctrlIndex;
  2986. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  2987. {
  2988. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  2989. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2990. uint8_t midiEvent[3];
  2991. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2992. midiEvent[1] = note.note;
  2993. midiEvent[2] = note.velo;
  2994. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2995. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, kUridMidiEvent, 3, midiEvent);
  2996. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2997. lv2_event_write(&evInEventIters[j], 0, 0, kUridMidiEvent, 3, midiEvent);
  2998. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2999. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  3000. }
  3001. pData->extNotes.data.clear();
  3002. }
  3003. pData->extNotes.mutex.unlock();
  3004. } // End of MIDI Input (External)
  3005. // ----------------------------------------------------------------------------------------------------
  3006. // Event Input (System)
  3007. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3008. bool allNotesOffSent = false;
  3009. #endif
  3010. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  3011. uint32_t startTime = 0;
  3012. uint32_t timeOffset = 0;
  3013. uint32_t nextBankId;
  3014. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  3015. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  3016. else
  3017. nextBankId = 0;
  3018. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3019. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  3020. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  3021. #endif
  3022. const uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  3023. for (uint32_t i=0; i < numEvents; ++i)
  3024. {
  3025. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  3026. uint32_t eventTime = event.time;
  3027. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  3028. if (eventTime < timeOffset)
  3029. {
  3030. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  3031. eventTime, timeOffset, pData->name);
  3032. eventTime = timeOffset;
  3033. }
  3034. if (isSampleAccurate && eventTime > timeOffset)
  3035. {
  3036. if (processSingle(audioIn, audioOut, cvIn, cvOut, eventTime - timeOffset, timeOffset))
  3037. {
  3038. startTime = 0;
  3039. timeOffset = eventTime;
  3040. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  3041. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  3042. else
  3043. nextBankId = 0;
  3044. for (uint32_t j=0; j < fEventsIn.count; ++j)
  3045. {
  3046. if (fEventsIn.data[j].type & CARLA_EVENT_DATA_ATOM)
  3047. {
  3048. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  3049. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  3050. }
  3051. else if (fEventsIn.data[j].type & CARLA_EVENT_DATA_EVENT)
  3052. {
  3053. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  3054. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  3055. }
  3056. else if (fEventsIn.data[j].type & CARLA_EVENT_DATA_MIDI_LL)
  3057. {
  3058. fEventsIn.data[j].midi.event_count = 0;
  3059. fEventsIn.data[j].midi.size = 0;
  3060. evInMidiStates[j].position = eventTime;
  3061. }
  3062. }
  3063. for (uint32_t j=0; j < fEventsOut.count; ++j)
  3064. {
  3065. if (fEventsOut.data[j].type & CARLA_EVENT_DATA_ATOM)
  3066. {
  3067. lv2_atom_buffer_reset(fEventsOut.data[j].atom, false);
  3068. }
  3069. else if (fEventsOut.data[j].type & CARLA_EVENT_DATA_EVENT)
  3070. {
  3071. lv2_event_buffer_reset(fEventsOut.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[j].event->data);
  3072. }
  3073. else if (fEventsOut.data[j].type & CARLA_EVENT_DATA_MIDI_LL)
  3074. {
  3075. // not needed
  3076. }
  3077. }
  3078. }
  3079. else
  3080. {
  3081. startTime += timeOffset;
  3082. }
  3083. }
  3084. switch (event.type)
  3085. {
  3086. case kEngineEventTypeNull:
  3087. break;
  3088. case kEngineEventTypeControl: {
  3089. const EngineControlEvent& ctrlEvent(event.ctrl);
  3090. switch (ctrlEvent.type)
  3091. {
  3092. case kEngineControlEventTypeNull:
  3093. break;
  3094. case kEngineControlEventTypeParameter: {
  3095. float value;
  3096. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3097. // non-midi
  3098. if (event.channel == kEngineEventNonMidiChannel)
  3099. {
  3100. const uint32_t k = ctrlEvent.param;
  3101. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  3102. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  3103. setParameterValueRT(k, value, true);
  3104. continue;
  3105. }
  3106. // Control backend stuff
  3107. if (event.channel == pData->ctrlChannel)
  3108. {
  3109. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  3110. {
  3111. value = ctrlEvent.value;
  3112. setDryWetRT(value, true);
  3113. }
  3114. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  3115. {
  3116. value = ctrlEvent.value*127.0f/100.0f;
  3117. setVolumeRT(value, true);
  3118. }
  3119. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  3120. {
  3121. float left, right;
  3122. value = ctrlEvent.value/0.5f - 1.0f;
  3123. if (value < 0.0f)
  3124. {
  3125. left = -1.0f;
  3126. right = (value*2.0f)+1.0f;
  3127. }
  3128. else if (value > 0.0f)
  3129. {
  3130. left = (value*2.0f)-1.0f;
  3131. right = 1.0f;
  3132. }
  3133. else
  3134. {
  3135. left = -1.0f;
  3136. right = 1.0f;
  3137. }
  3138. setBalanceLeftRT(left, true);
  3139. setBalanceRightRT(right, true);
  3140. }
  3141. }
  3142. #endif
  3143. // Control plugin parameters
  3144. uint32_t k;
  3145. for (k=0; k < pData->param.count; ++k)
  3146. {
  3147. if (pData->param.data[k].midiChannel != event.channel)
  3148. continue;
  3149. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  3150. continue;
  3151. if (pData->param.data[k].type != PARAMETER_INPUT)
  3152. continue;
  3153. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  3154. continue;
  3155. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.value);
  3156. setParameterValueRT(k, value, true);
  3157. }
  3158. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  3159. {
  3160. uint8_t midiData[3];
  3161. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3162. midiData[1] = uint8_t(ctrlEvent.param);
  3163. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  3164. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3165. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3166. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3167. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3168. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3169. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3170. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3171. }
  3172. break;
  3173. } // case kEngineControlEventTypeParameter
  3174. case kEngineControlEventTypeMidiBank:
  3175. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  3176. {
  3177. if (event.channel == pData->ctrlChannel)
  3178. nextBankId = ctrlEvent.param;
  3179. }
  3180. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  3181. {
  3182. uint8_t midiData[3];
  3183. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3184. midiData[1] = MIDI_CONTROL_BANK_SELECT;
  3185. midiData[2] = uint8_t(ctrlEvent.param);
  3186. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3187. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3188. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3189. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3190. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3191. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3192. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3193. }
  3194. break;
  3195. case kEngineControlEventTypeMidiProgram:
  3196. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  3197. {
  3198. if (event.channel == pData->ctrlChannel)
  3199. {
  3200. const uint32_t nextProgramId(ctrlEvent.param);
  3201. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  3202. {
  3203. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  3204. {
  3205. setMidiProgramRT(k, true);
  3206. break;
  3207. }
  3208. }
  3209. }
  3210. }
  3211. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  3212. {
  3213. uint8_t midiData[2];
  3214. midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3215. midiData[1] = uint8_t(ctrlEvent.param);
  3216. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3217. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3218. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 2, midiData);
  3219. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3220. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 2, midiData);
  3221. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3222. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 2, midiData);
  3223. }
  3224. break;
  3225. case kEngineControlEventTypeAllSoundOff:
  3226. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  3227. {
  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_SOUND_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. case kEngineControlEventTypeAllNotesOff:
  3242. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  3243. {
  3244. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3245. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  3246. {
  3247. allNotesOffSent = true;
  3248. postponeRtAllNotesOff();
  3249. }
  3250. #endif
  3251. const uint32_t mtime(isSampleAccurate ? startTime : eventTime);
  3252. uint8_t midiData[3];
  3253. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  3254. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  3255. midiData[2] = 0;
  3256. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3257. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3258. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3259. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, kUridMidiEvent, 3, midiData);
  3260. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3261. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  3262. }
  3263. break;
  3264. } // switch (ctrlEvent.type)
  3265. break;
  3266. } // case kEngineEventTypeControl
  3267. case kEngineEventTypeMidi: {
  3268. const EngineMidiEvent& midiEvent(event.midi);
  3269. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  3270. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  3271. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  3272. continue;
  3273. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  3274. continue;
  3275. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  3276. continue;
  3277. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  3278. continue;
  3279. // Fix bad note-off (per LV2 spec)
  3280. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  3281. status = MIDI_STATUS_NOTE_OFF;
  3282. const uint32_t j = fEventsIn.ctrlIndex;
  3283. const uint32_t mtime = isSampleAccurate ? startTime : eventTime;
  3284. // put back channel in data
  3285. uint8_t midiData2[midiEvent.size];
  3286. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  3287. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  3288. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  3289. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, kUridMidiEvent, midiEvent.size, midiData2);
  3290. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  3291. lv2_event_write(&evInEventIters[j], mtime, 0, kUridMidiEvent, midiEvent.size, midiData2);
  3292. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  3293. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiData2);
  3294. if (status == MIDI_STATUS_NOTE_ON)
  3295. {
  3296. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  3297. true,
  3298. event.channel,
  3299. midiData[1],
  3300. midiData[2],
  3301. 0.0f);
  3302. }
  3303. else if (status == MIDI_STATUS_NOTE_OFF)
  3304. {
  3305. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  3306. true,
  3307. event.channel,
  3308. midiData[1],
  3309. 0, 0.0f);
  3310. }
  3311. } break;
  3312. } // switch (event.type)
  3313. }
  3314. pData->postRtEvents.trySplice();
  3315. if (frames > timeOffset)
  3316. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  3317. } // End of Event Input and Processing
  3318. // --------------------------------------------------------------------------------------------------------
  3319. // Plugin processing (no events)
  3320. else
  3321. {
  3322. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  3323. } // End of Plugin processing (no events)
  3324. // --------------------------------------------------------------------------------------------------------
  3325. // Events/MIDI Output
  3326. for (uint32_t i=0; i < fEventsOut.count; ++i)
  3327. {
  3328. uint32_t lastFrame = 0;
  3329. Lv2EventData& evData(fEventsOut.data[i]);
  3330. if (evData.type & CARLA_EVENT_DATA_ATOM)
  3331. {
  3332. const LV2_Atom_Event* ev;
  3333. LV2_Atom_Buffer_Iterator iter;
  3334. uint8_t* data;
  3335. lv2_atom_buffer_begin(&iter, evData.atom);
  3336. for (;;)
  3337. {
  3338. data = nullptr;
  3339. ev = lv2_atom_buffer_get(&iter, &data);
  3340. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  3341. break;
  3342. if (ev->body.type == kUridMidiEvent)
  3343. {
  3344. if (evData.port != nullptr)
  3345. {
  3346. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  3347. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  3348. uint32_t currentFrame = static_cast<uint32_t>(ev->time.frames);
  3349. if (currentFrame < lastFrame)
  3350. currentFrame = lastFrame;
  3351. else if (currentFrame >= frames)
  3352. currentFrame = frames - 1;
  3353. evData.port->writeMidiEvent(currentFrame, static_cast<uint8_t>(ev->body.size), data);
  3354. }
  3355. }
  3356. else if (fAtomBufferUiOutTmpData != nullptr)
  3357. {
  3358. fAtomBufferUiOut.put(&ev->body, evData.rindex);
  3359. }
  3360. lv2_atom_buffer_increment(&iter);
  3361. }
  3362. }
  3363. else if ((evData.type & CARLA_EVENT_DATA_EVENT) != 0 && evData.port != nullptr)
  3364. {
  3365. const LV2_Event* ev;
  3366. LV2_Event_Iterator iter;
  3367. uint8_t* data;
  3368. lv2_event_begin(&iter, evData.event);
  3369. for (;;)
  3370. {
  3371. data = nullptr;
  3372. ev = lv2_event_get(&iter, &data);
  3373. if (ev == nullptr || data == nullptr)
  3374. break;
  3375. uint32_t currentFrame = ev->frames;
  3376. if (currentFrame < lastFrame)
  3377. currentFrame = lastFrame;
  3378. else if (currentFrame >= frames)
  3379. currentFrame = frames - 1;
  3380. if (ev->type == kUridMidiEvent)
  3381. {
  3382. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  3383. evData.port->writeMidiEvent(currentFrame, static_cast<uint8_t>(ev->size), data);
  3384. }
  3385. lv2_event_increment(&iter);
  3386. }
  3387. }
  3388. else if ((evData.type & CARLA_EVENT_DATA_MIDI_LL) != 0 && evData.port != nullptr)
  3389. {
  3390. LV2_MIDIState state = { &evData.midi, frames, 0 };
  3391. uint32_t eventSize;
  3392. double eventTime;
  3393. uchar* eventData;
  3394. for (;;)
  3395. {
  3396. eventSize = 0;
  3397. eventTime = 0.0;
  3398. eventData = nullptr;
  3399. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  3400. if (eventData == nullptr || eventSize == 0)
  3401. break;
  3402. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  3403. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  3404. evData.port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  3405. lv2midi_step(&state);
  3406. }
  3407. }
  3408. }
  3409. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3410. // --------------------------------------------------------------------------------------------------------
  3411. // Control Output
  3412. if (pData->event.portOut != nullptr)
  3413. {
  3414. uint8_t channel;
  3415. uint16_t param;
  3416. float value;
  3417. for (uint32_t k=0; k < pData->param.count; ++k)
  3418. {
  3419. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  3420. continue;
  3421. if (fStrictBounds >= 0 && (pData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS) != 0)
  3422. // plugin is responsible to ensure correct bounds
  3423. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  3424. if (pData->param.data[k].mappedControlIndex > 0)
  3425. {
  3426. channel = pData->param.data[k].midiChannel;
  3427. param = static_cast<uint16_t>(pData->param.data[k].mappedControlIndex);
  3428. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  3429. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  3430. }
  3431. }
  3432. } // End of Control Output
  3433. #endif
  3434. // --------------------------------------------------------------------------------------------------------
  3435. // Final work
  3436. if (fEventsIn.ctrl != nullptr && fExt.worker != nullptr && fAtomBufferWorkerResp.tryLock())
  3437. {
  3438. if (fAtomBufferWorkerResp.isDataAvailableForReading())
  3439. {
  3440. const LV2_Atom* atom;
  3441. uint32_t portIndex;
  3442. for (; fAtomBufferWorkerResp.get(atom, portIndex);)
  3443. {
  3444. CARLA_SAFE_ASSERT_CONTINUE(atom->type == kUridCarlaAtomWorkerResp);
  3445. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  3446. }
  3447. }
  3448. fAtomBufferWorkerResp.unlock();
  3449. }
  3450. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  3451. {
  3452. fExt.worker->end_run(fHandle);
  3453. if (fHandle2 != nullptr)
  3454. fExt.worker->end_run(fHandle2);
  3455. }
  3456. fFirstActive = false;
  3457. // --------------------------------------------------------------------------------------------------------
  3458. }
  3459. bool processSingle(const float* const* const audioIn, float** const audioOut,
  3460. const float* const* const cvIn, float** const cvOut,
  3461. const uint32_t frames, const uint32_t timeOffset)
  3462. {
  3463. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  3464. if (pData->audioIn.count > 0)
  3465. {
  3466. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  3467. CARLA_SAFE_ASSERT_RETURN(fAudioInBuffers != nullptr, false);
  3468. }
  3469. if (pData->audioOut.count > 0)
  3470. {
  3471. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  3472. CARLA_SAFE_ASSERT_RETURN(fAudioOutBuffers != nullptr, false);
  3473. }
  3474. if (pData->cvIn.count > 0)
  3475. {
  3476. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  3477. }
  3478. if (pData->cvOut.count > 0)
  3479. {
  3480. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  3481. }
  3482. // --------------------------------------------------------------------------------------------------------
  3483. // Try lock, silence otherwise
  3484. #ifndef STOAT_TEST_BUILD
  3485. if (pData->engine->isOffline())
  3486. {
  3487. pData->singleMutex.lock();
  3488. }
  3489. else
  3490. #endif
  3491. if (! pData->singleMutex.tryLock())
  3492. {
  3493. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3494. {
  3495. for (uint32_t k=0; k < frames; ++k)
  3496. audioOut[i][k+timeOffset] = 0.0f;
  3497. }
  3498. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3499. {
  3500. for (uint32_t k=0; k < frames; ++k)
  3501. cvOut[i][k+timeOffset] = 0.0f;
  3502. }
  3503. return false;
  3504. }
  3505. // --------------------------------------------------------------------------------------------------------
  3506. // Set audio buffers
  3507. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3508. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  3509. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3510. carla_zeroFloats(fAudioOutBuffers[i], frames);
  3511. // --------------------------------------------------------------------------------------------------------
  3512. // Set CV buffers
  3513. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3514. carla_copyFloats(fCvInBuffers[i], cvIn[i]+timeOffset, frames);
  3515. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3516. carla_zeroFloats(fCvOutBuffers[i], frames);
  3517. // --------------------------------------------------------------------------------------------------------
  3518. // Run plugin
  3519. fDescriptor->run(fHandle, frames);
  3520. if (fHandle2 != nullptr)
  3521. fDescriptor->run(fHandle2, frames);
  3522. // --------------------------------------------------------------------------------------------------------
  3523. // Handle trigger parameters
  3524. for (uint32_t k=0; k < pData->param.count; ++k)
  3525. {
  3526. if (pData->param.data[k].type != PARAMETER_INPUT)
  3527. continue;
  3528. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  3529. {
  3530. if (carla_isNotEqual(fParamBuffers[k], pData->param.ranges[k].def))
  3531. {
  3532. fParamBuffers[k] = pData->param.ranges[k].def;
  3533. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  3534. true,
  3535. static_cast<int32_t>(k),
  3536. 1, 0,
  3537. fParamBuffers[k]);
  3538. }
  3539. }
  3540. }
  3541. pData->postRtEvents.trySplice();
  3542. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3543. // --------------------------------------------------------------------------------------------------------
  3544. // Post-processing (dry/wet, volume and balance)
  3545. {
  3546. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  3547. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  3548. const bool isMono = (pData->audioIn.count == 1);
  3549. bool isPair;
  3550. float bufValue, oldBufLeft[doBalance ? frames : 1];
  3551. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3552. {
  3553. // Dry/Wet
  3554. if (doDryWet)
  3555. {
  3556. const uint32_t c = isMono ? 0 : i;
  3557. for (uint32_t k=0; k < frames; ++k)
  3558. {
  3559. # ifndef BUILD_BRIDGE
  3560. if (k < pData->latency.frames && pData->latency.buffers != nullptr)
  3561. bufValue = pData->latency.buffers[c][k];
  3562. else if (pData->latency.frames < frames)
  3563. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  3564. else
  3565. # endif
  3566. bufValue = fAudioInBuffers[c][k];
  3567. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  3568. }
  3569. }
  3570. // Balance
  3571. if (doBalance)
  3572. {
  3573. isPair = (i % 2 == 0);
  3574. if (isPair)
  3575. {
  3576. CARLA_ASSERT(i+1 < pData->audioOut.count);
  3577. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  3578. }
  3579. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  3580. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  3581. for (uint32_t k=0; k < frames; ++k)
  3582. {
  3583. if (isPair)
  3584. {
  3585. // left
  3586. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  3587. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  3588. }
  3589. else
  3590. {
  3591. // right
  3592. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  3593. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  3594. }
  3595. }
  3596. }
  3597. // Volume (and buffer copy)
  3598. {
  3599. for (uint32_t k=0; k < frames; ++k)
  3600. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  3601. }
  3602. }
  3603. } // End of Post-processing
  3604. # ifndef BUILD_BRIDGE
  3605. // --------------------------------------------------------------------------------------------------------
  3606. // Save latency values for next callback
  3607. if (pData->latency.frames != 0 && pData->latency.buffers != nullptr)
  3608. {
  3609. CARLA_SAFE_ASSERT(timeOffset == 0);
  3610. const uint32_t latframes = pData->latency.frames;
  3611. if (latframes <= frames)
  3612. {
  3613. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3614. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  3615. }
  3616. else
  3617. {
  3618. const uint32_t diff = latframes - frames;
  3619. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  3620. {
  3621. // push back buffer by 'frames'
  3622. for (k=0; k < diff; ++k)
  3623. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  3624. // put current input at the end
  3625. for (uint32_t j=0; k < latframes; ++j, ++k)
  3626. pData->latency.buffers[i][k] = audioIn[i][j];
  3627. }
  3628. }
  3629. }
  3630. # endif
  3631. #else // BUILD_BRIDGE_ALTERNATIVE_ARCH
  3632. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3633. {
  3634. for (uint32_t k=0; k < frames; ++k)
  3635. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  3636. }
  3637. #endif
  3638. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3639. {
  3640. for (uint32_t k=0; k < frames; ++k)
  3641. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  3642. }
  3643. // --------------------------------------------------------------------------------------------------------
  3644. pData->singleMutex.unlock();
  3645. return true;
  3646. }
  3647. void bufferSizeChanged(const uint32_t newBufferSize) override
  3648. {
  3649. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  3650. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - start", newBufferSize);
  3651. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3652. {
  3653. if (fAudioInBuffers[i] != nullptr)
  3654. delete[] fAudioInBuffers[i];
  3655. fAudioInBuffers[i] = new float[newBufferSize];
  3656. }
  3657. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3658. {
  3659. if (fAudioOutBuffers[i] != nullptr)
  3660. delete[] fAudioOutBuffers[i];
  3661. fAudioOutBuffers[i] = new float[newBufferSize];
  3662. }
  3663. if (fHandle2 == nullptr)
  3664. {
  3665. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3666. {
  3667. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  3668. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  3669. }
  3670. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3671. {
  3672. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  3673. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  3674. }
  3675. }
  3676. else
  3677. {
  3678. if (pData->audioIn.count > 0)
  3679. {
  3680. CARLA_ASSERT(pData->audioIn.count == 2);
  3681. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  3682. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  3683. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  3684. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  3685. }
  3686. if (pData->audioOut.count > 0)
  3687. {
  3688. CARLA_ASSERT(pData->audioOut.count == 2);
  3689. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  3690. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  3691. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  3692. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  3693. }
  3694. }
  3695. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3696. {
  3697. if (fCvInBuffers[i] != nullptr)
  3698. delete[] fCvInBuffers[i];
  3699. fCvInBuffers[i] = new float[newBufferSize];
  3700. fDescriptor->connect_port(fHandle, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3701. if (fHandle2 != nullptr)
  3702. fDescriptor->connect_port(fHandle2, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3703. }
  3704. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3705. {
  3706. if (fCvOutBuffers[i] != nullptr)
  3707. delete[] fCvOutBuffers[i];
  3708. fCvOutBuffers[i] = new float[newBufferSize];
  3709. fDescriptor->connect_port(fHandle, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3710. if (fHandle2 != nullptr)
  3711. fDescriptor->connect_port(fHandle2, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3712. }
  3713. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  3714. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  3715. {
  3716. fLv2Options.maxBufferSize = fLv2Options.nominalBufferSize = newBufferSizeInt;
  3717. if (fLv2Options.minBufferSize != 1)
  3718. fLv2Options.minBufferSize = newBufferSizeInt;
  3719. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3720. {
  3721. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MaxBlockLenth]);
  3722. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::NominalBlockLenth]);
  3723. if (fLv2Options.minBufferSize != 1)
  3724. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MinBlockLenth]);
  3725. }
  3726. }
  3727. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - end", newBufferSize);
  3728. }
  3729. void sampleRateChanged(const double newSampleRate) override
  3730. {
  3731. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  3732. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - start", newSampleRate);
  3733. const float sampleRatef = static_cast<float>(newSampleRate);
  3734. if (carla_isNotEqual(fLv2Options.sampleRate, sampleRatef))
  3735. {
  3736. fLv2Options.sampleRate = sampleRatef;
  3737. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3738. {
  3739. LV2_Options_Option options[2];
  3740. carla_zeroStructs(options, 2);
  3741. LV2_Options_Option& optSampleRate(options[0]);
  3742. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  3743. optSampleRate.subject = 0;
  3744. optSampleRate.key = kUridParamSampleRate;
  3745. optSampleRate.size = sizeof(float);
  3746. optSampleRate.type = kUridAtomFloat;
  3747. optSampleRate.value = &fLv2Options.sampleRate;
  3748. fExt.options->set(fHandle, options);
  3749. }
  3750. }
  3751. for (uint32_t k=0; k < pData->param.count; ++k)
  3752. {
  3753. if (pData->param.data[k].type != PARAMETER_INPUT)
  3754. continue;
  3755. if (pData->param.special[k] != PARAMETER_SPECIAL_SAMPLE_RATE)
  3756. continue;
  3757. fParamBuffers[k] = sampleRatef;
  3758. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  3759. true,
  3760. static_cast<int32_t>(k),
  3761. 0,
  3762. 0,
  3763. fParamBuffers[k]);
  3764. break;
  3765. }
  3766. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - end", newSampleRate);
  3767. }
  3768. void offlineModeChanged(const bool isOffline) override
  3769. {
  3770. for (uint32_t k=0; k < pData->param.count; ++k)
  3771. {
  3772. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_FREEWHEEL)
  3773. {
  3774. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  3775. pData->postponeRtEvent(kPluginPostRtEventParameterChange,
  3776. true,
  3777. static_cast<int32_t>(k),
  3778. 0,
  3779. 0,
  3780. fParamBuffers[k]);
  3781. break;
  3782. }
  3783. }
  3784. }
  3785. // -------------------------------------------------------------------
  3786. // Plugin buffers
  3787. void initBuffers() const noexcept override
  3788. {
  3789. fEventsIn.initBuffers();
  3790. fEventsOut.initBuffers();
  3791. CarlaPlugin::initBuffers();
  3792. }
  3793. void clearBuffers() noexcept override
  3794. {
  3795. carla_debug("CarlaPluginLV2::clearBuffers() - start");
  3796. if (fAudioInBuffers != nullptr)
  3797. {
  3798. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3799. {
  3800. if (fAudioInBuffers[i] != nullptr)
  3801. {
  3802. delete[] fAudioInBuffers[i];
  3803. fAudioInBuffers[i] = nullptr;
  3804. }
  3805. }
  3806. delete[] fAudioInBuffers;
  3807. fAudioInBuffers = nullptr;
  3808. }
  3809. if (fAudioOutBuffers != nullptr)
  3810. {
  3811. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3812. {
  3813. if (fAudioOutBuffers[i] != nullptr)
  3814. {
  3815. delete[] fAudioOutBuffers[i];
  3816. fAudioOutBuffers[i] = nullptr;
  3817. }
  3818. }
  3819. delete[] fAudioOutBuffers;
  3820. fAudioOutBuffers = nullptr;
  3821. }
  3822. if (fCvInBuffers != nullptr)
  3823. {
  3824. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3825. {
  3826. if (fCvInBuffers[i] != nullptr)
  3827. {
  3828. delete[] fCvInBuffers[i];
  3829. fCvInBuffers[i] = nullptr;
  3830. }
  3831. }
  3832. delete[] fCvInBuffers;
  3833. fCvInBuffers = nullptr;
  3834. }
  3835. if (fCvOutBuffers != nullptr)
  3836. {
  3837. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3838. {
  3839. if (fCvOutBuffers[i] != nullptr)
  3840. {
  3841. delete[] fCvOutBuffers[i];
  3842. fCvOutBuffers[i] = nullptr;
  3843. }
  3844. }
  3845. delete[] fCvOutBuffers;
  3846. fCvOutBuffers = nullptr;
  3847. }
  3848. if (fParamBuffers != nullptr)
  3849. {
  3850. delete[] fParamBuffers;
  3851. fParamBuffers = nullptr;
  3852. }
  3853. fEventsIn.clear();
  3854. fEventsOut.clear();
  3855. CarlaPlugin::clearBuffers();
  3856. carla_debug("CarlaPluginLV2::clearBuffers() - end");
  3857. }
  3858. // -------------------------------------------------------------------
  3859. // Post-poned UI Stuff
  3860. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3861. {
  3862. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3863. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3864. if (fUI.type == UI::TYPE_BRIDGE)
  3865. {
  3866. if (fPipeServer.isPipeRunning())
  3867. fPipeServer.writeControlMessage(static_cast<uint32_t>(pData->param.data[index].rindex), value);
  3868. }
  3869. else
  3870. {
  3871. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && ! fNeedsUiClose)
  3872. {
  3873. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3874. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), kUridNull, &value);
  3875. }
  3876. }
  3877. }
  3878. void uiMidiProgramChange(const uint32_t index) noexcept override
  3879. {
  3880. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3881. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3882. if (fUI.type == UI::TYPE_BRIDGE)
  3883. {
  3884. if (fPipeServer.isPipeRunning())
  3885. fPipeServer.writeMidiProgramMessage(pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3886. }
  3887. else
  3888. {
  3889. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr && ! fNeedsUiClose)
  3890. fExt.uiprograms->select_program(fUI.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3891. }
  3892. }
  3893. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3894. {
  3895. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3896. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3897. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3898. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3899. #if 0
  3900. if (fUI.type == UI::TYPE_BRIDGE)
  3901. {
  3902. if (fPipeServer.isPipeRunning())
  3903. fPipeServer.writeMidiNoteMessage(false, channel, note, velo);
  3904. }
  3905. else
  3906. {
  3907. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3908. {
  3909. LV2_Atom_MidiEvent midiEv;
  3910. midiEv.atom.type = kUridMidiEvent;
  3911. midiEv.atom.size = 3;
  3912. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3913. midiEv.data[1] = note;
  3914. midiEv.data[2] = velo;
  3915. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), kUridAtomTransferEvent, &midiEv);
  3916. }
  3917. }
  3918. #endif
  3919. }
  3920. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3921. {
  3922. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL || fFilePathURI.isNotEmpty(),);
  3923. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3924. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3925. #if 0
  3926. if (fUI.type == UI::TYPE_BRIDGE)
  3927. {
  3928. if (fPipeServer.isPipeRunning())
  3929. fPipeServer.writeMidiNoteMessage(false, channel, note, 0);
  3930. }
  3931. else
  3932. {
  3933. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3934. {
  3935. LV2_Atom_MidiEvent midiEv;
  3936. midiEv.atom.type = kUridMidiEvent;
  3937. midiEv.atom.size = 3;
  3938. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3939. midiEv.data[1] = note;
  3940. midiEv.data[2] = 0;
  3941. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), kUridAtomTransferEvent, &midiEv);
  3942. }
  3943. }
  3944. #endif
  3945. }
  3946. // -------------------------------------------------------------------
  3947. // Internal helper functions
  3948. void restoreLV2State() noexcept override
  3949. {
  3950. if (fExt.state == nullptr)
  3951. return;
  3952. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  3953. {
  3954. const ScopedSingleProcessLocker spl(this, true);
  3955. try {
  3956. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  3957. } catch(...) {}
  3958. if (fHandle2 != nullptr)
  3959. {
  3960. try {
  3961. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  3962. } catch(...) {}
  3963. }
  3964. }
  3965. switch (status)
  3966. {
  3967. case LV2_STATE_SUCCESS:
  3968. carla_debug("CarlaPluginLV2::updateLV2State() - success");
  3969. break;
  3970. case LV2_STATE_ERR_UNKNOWN:
  3971. carla_stderr("CarlaPluginLV2::updateLV2State() - unknown error");
  3972. break;
  3973. case LV2_STATE_ERR_BAD_TYPE:
  3974. carla_stderr("CarlaPluginLV2::updateLV2State() - error, bad type");
  3975. break;
  3976. case LV2_STATE_ERR_BAD_FLAGS:
  3977. carla_stderr("CarlaPluginLV2::updateLV2State() - error, bad flags");
  3978. break;
  3979. case LV2_STATE_ERR_NO_FEATURE:
  3980. carla_stderr("CarlaPluginLV2::updateLV2State() - error, missing feature");
  3981. break;
  3982. case LV2_STATE_ERR_NO_PROPERTY:
  3983. carla_stderr("CarlaPluginLV2::updateLV2State() - error, missing property");
  3984. break;
  3985. case LV2_STATE_ERR_NO_SPACE:
  3986. carla_stderr("CarlaPluginLV2::updateLV2State() - error, insufficient space");
  3987. break;
  3988. }
  3989. }
  3990. // -------------------------------------------------------------------
  3991. bool isRealtimeSafe() const noexcept
  3992. {
  3993. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3994. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3995. {
  3996. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3997. return true;
  3998. }
  3999. return false;
  4000. }
  4001. // -------------------------------------------------------------------
  4002. bool isUiBridgeable(const uint32_t uiId) const noexcept
  4003. {
  4004. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  4005. #ifndef LV2_UIS_ONLY_INPROCESS
  4006. const LV2_RDF_UI* const rdfUI(&fRdfDescriptor->UIs[uiId]);
  4007. for (uint32_t i=0; i < rdfUI->FeatureCount; ++i)
  4008. {
  4009. const LV2_RDF_Feature& feat(rdfUI->Features[i]);
  4010. if (! feat.Required)
  4011. continue;
  4012. if (std::strcmp(feat.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  4013. return false;
  4014. if (std::strcmp(feat.URI, LV2_DATA_ACCESS_URI) == 0)
  4015. return false;
  4016. }
  4017. // Calf UIs are mostly useless without their special graphs
  4018. // but they can be crashy under certain conditions, so follow user preferences
  4019. if (std::strstr(rdfUI->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  4020. return pData->engine->getOptions().preferUiBridges;
  4021. // LSP-Plugins UIs make heavy use of URIDs, for which carla right now is very slow
  4022. // FIXME after some optimization, remove this
  4023. if (std::strstr(rdfUI->URI, "http://lsp-plug.in/ui/lv2/") != nullptr)
  4024. return false;
  4025. return true;
  4026. #else
  4027. return false;
  4028. #endif
  4029. }
  4030. bool isUiResizable() const noexcept
  4031. {
  4032. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr, false);
  4033. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  4034. {
  4035. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  4036. return false;
  4037. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  4038. return false;
  4039. }
  4040. return true;
  4041. }
  4042. const char* getUiBridgeBinary(const LV2_Property type) const
  4043. {
  4044. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  4045. if (bridgeBinary.isEmpty())
  4046. return nullptr;
  4047. switch (type)
  4048. {
  4049. case LV2_UI_GTK2:
  4050. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk2";
  4051. break;
  4052. case LV2_UI_GTK3:
  4053. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk3";
  4054. break;
  4055. case LV2_UI_QT4:
  4056. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt4";
  4057. break;
  4058. case LV2_UI_QT5:
  4059. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt5";
  4060. break;
  4061. case LV2_UI_COCOA:
  4062. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-cocoa";
  4063. break;
  4064. case LV2_UI_WINDOWS:
  4065. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-windows";
  4066. break;
  4067. case LV2_UI_X11:
  4068. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-x11";
  4069. break;
  4070. case LV2_UI_MOD:
  4071. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-modgui";
  4072. break;
  4073. #if 0
  4074. case LV2_UI_EXTERNAL:
  4075. case LV2_UI_OLD_EXTERNAL:
  4076. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-external";
  4077. break;
  4078. #endif
  4079. default:
  4080. return nullptr;
  4081. }
  4082. #ifdef CARLA_OS_WIN
  4083. bridgeBinary += ".exe";
  4084. #endif
  4085. if (! File(bridgeBinary.buffer()).existsAsFile())
  4086. return nullptr;
  4087. return bridgeBinary.dupSafe();
  4088. }
  4089. // -------------------------------------------------------------------
  4090. void recheckExtensions()
  4091. {
  4092. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  4093. carla_debug("CarlaPluginLV2::recheckExtensions()");
  4094. fExt.options = nullptr;
  4095. fExt.programs = nullptr;
  4096. fExt.state = nullptr;
  4097. fExt.worker = nullptr;
  4098. fExt.inlineDisplay = nullptr;
  4099. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  4100. {
  4101. const char* const extension = fRdfDescriptor->Extensions[i];
  4102. CARLA_SAFE_ASSERT_CONTINUE(extension != nullptr);
  4103. /**/ if (std::strcmp(extension, LV2_OPTIONS__interface) == 0)
  4104. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  4105. else if (std::strcmp(extension, LV2_PROGRAMS__Interface) == 0)
  4106. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  4107. else if (std::strcmp(extension, LV2_STATE__interface) == 0)
  4108. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  4109. else if (std::strcmp(extension, LV2_WORKER__interface) == 0)
  4110. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  4111. else if (std::strcmp(extension, LV2_INLINEDISPLAY__interface) == 0)
  4112. pData->hints |= PLUGIN_HAS_EXTENSION_INLINE_DISPLAY;
  4113. else if (std::strcmp(extension, LV2_MIDNAM__interface) == 0)
  4114. pData->hints |= PLUGIN_HAS_EXTENSION_MIDNAM;
  4115. else
  4116. carla_stdout("Plugin '%s' has non-supported extension: '%s'", fRdfDescriptor->URI, extension);
  4117. }
  4118. // Fix for broken plugins, nasty!
  4119. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  4120. {
  4121. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[i]);
  4122. if (std::strcmp(feature.URI, LV2_INLINEDISPLAY__queue_draw) == 0)
  4123. {
  4124. if (pData->hints & PLUGIN_HAS_EXTENSION_INLINE_DISPLAY)
  4125. break;
  4126. carla_stdout("Plugin '%s' uses inline-display but does not set extension data, nasty!", fRdfDescriptor->URI);
  4127. pData->hints |= PLUGIN_HAS_EXTENSION_INLINE_DISPLAY;
  4128. }
  4129. else if (std::strcmp(feature.URI, LV2_MIDNAM__update) == 0)
  4130. {
  4131. if (pData->hints & PLUGIN_HAS_EXTENSION_MIDNAM)
  4132. break;
  4133. carla_stdout("Plugin '%s' uses midnam but does not set extension data, nasty!", fRdfDescriptor->URI);
  4134. pData->hints |= PLUGIN_HAS_EXTENSION_MIDNAM;
  4135. }
  4136. }
  4137. if (fDescriptor->extension_data != nullptr)
  4138. {
  4139. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  4140. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  4141. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  4142. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  4143. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  4144. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  4145. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  4146. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  4147. if (pData->hints & PLUGIN_HAS_EXTENSION_INLINE_DISPLAY)
  4148. fExt.inlineDisplay = (const LV2_Inline_Display_Interface*)fDescriptor->extension_data(LV2_INLINEDISPLAY__interface);
  4149. if (pData->hints & PLUGIN_HAS_EXTENSION_MIDNAM)
  4150. fExt.midnam = (const LV2_Midnam_Interface*)fDescriptor->extension_data(LV2_MIDNAM__interface);
  4151. // check if invalid
  4152. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  4153. fExt.options = nullptr;
  4154. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  4155. fExt.programs = nullptr;
  4156. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  4157. fExt.state = nullptr;
  4158. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  4159. fExt.worker = nullptr;
  4160. if (fExt.inlineDisplay != nullptr)
  4161. {
  4162. if (fExt.inlineDisplay->render != nullptr)
  4163. {
  4164. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  4165. pData->setCanDeleteLib(false);
  4166. }
  4167. else
  4168. {
  4169. fExt.inlineDisplay = nullptr;
  4170. }
  4171. }
  4172. if (fExt.midnam != nullptr && fExt.midnam->midnam == nullptr)
  4173. fExt.midnam = nullptr;
  4174. }
  4175. CARLA_SAFE_ASSERT_RETURN(fLatencyIndex == -1,);
  4176. int32_t iCtrl=0;
  4177. for (uint32_t i=0, count=fRdfDescriptor->PortCount; i<count; ++i)
  4178. {
  4179. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  4180. if (! LV2_IS_PORT_CONTROL(portTypes))
  4181. continue;
  4182. const CarlaScopedValueSetter<int32_t> svs(iCtrl, iCtrl, iCtrl+1);
  4183. if (! LV2_IS_PORT_OUTPUT(portTypes))
  4184. continue;
  4185. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  4186. if (! LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  4187. continue;
  4188. fLatencyIndex = iCtrl;
  4189. break;
  4190. }
  4191. }
  4192. // -------------------------------------------------------------------
  4193. void updateUi()
  4194. {
  4195. CARLA_SAFE_ASSERT_RETURN(fUI.handle != nullptr,);
  4196. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  4197. carla_debug("CarlaPluginLV2::updateUi()");
  4198. // update midi program
  4199. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  4200. {
  4201. const MidiProgramData& curData(pData->midiprog.getCurrent());
  4202. fExt.uiprograms->select_program(fUI.handle, curData.bank, curData.program);
  4203. }
  4204. // update control ports
  4205. if (fUI.descriptor->port_event != nullptr)
  4206. {
  4207. float value;
  4208. for (uint32_t i=0; i < pData->param.count; ++i)
  4209. {
  4210. value = getParameterValue(i);
  4211. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), kUridNull, &value);
  4212. }
  4213. }
  4214. }
  4215. // -------------------------------------------------------------------
  4216. LV2_URID getCustomURID(const char* const uri)
  4217. {
  4218. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', kUridNull);
  4219. carla_debug("CarlaPluginLV2::getCustomURID(\"%s\")", uri);
  4220. const std::string s_uri(uri);
  4221. const std::ptrdiff_t s_pos(std::find(fCustomURIDs.begin(), fCustomURIDs.end(), s_uri) - fCustomURIDs.begin());
  4222. if (s_pos <= 0 || s_pos >= INT32_MAX)
  4223. return kUridNull;
  4224. const LV2_URID urid = static_cast<LV2_URID>(s_pos);
  4225. const LV2_URID uriCount = static_cast<LV2_URID>(fCustomURIDs.size());
  4226. if (urid < uriCount)
  4227. return urid;
  4228. CARLA_SAFE_ASSERT(urid == uriCount);
  4229. fCustomURIDs.push_back(uri);
  4230. if (fUI.type == UI::TYPE_BRIDGE && fPipeServer.isPipeRunning())
  4231. fPipeServer.writeLv2UridMessage(urid, uri);
  4232. return urid;
  4233. }
  4234. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  4235. {
  4236. CARLA_SAFE_ASSERT_RETURN(urid != kUridNull, kUnmapFallback);
  4237. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.size(), kUnmapFallback);
  4238. carla_debug("CarlaPluginLV2::getCustomURIString(%i)", urid);
  4239. return fCustomURIDs[urid].c_str();
  4240. }
  4241. // -------------------------------------------------------------------
  4242. void handleProgramChanged(const int32_t index)
  4243. {
  4244. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  4245. carla_debug("CarlaPluginLV2::handleProgramChanged(%i)", index);
  4246. if (index == -1)
  4247. {
  4248. const ScopedSingleProcessLocker spl(this, true);
  4249. return reloadPrograms(false);
  4250. }
  4251. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  4252. {
  4253. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  4254. {
  4255. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  4256. if (pData->midiprog.data[index].name != nullptr)
  4257. delete[] pData->midiprog.data[index].name;
  4258. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  4259. if (index == pData->midiprog.current)
  4260. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0, nullptr);
  4261. else
  4262. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0, nullptr);
  4263. }
  4264. }
  4265. }
  4266. // -------------------------------------------------------------------
  4267. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  4268. {
  4269. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4270. carla_debug("CarlaPluginLV2::handleResizePort(%i, " P_SIZE ")", index, size);
  4271. // TODO
  4272. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  4273. (void)index;
  4274. }
  4275. // -------------------------------------------------------------------
  4276. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  4277. {
  4278. CARLA_SAFE_ASSERT_RETURN(key != kUridNull, LV2_STATE_ERR_NO_PROPERTY);
  4279. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  4280. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  4281. CARLA_SAFE_ASSERT_RETURN(type != kUridNull, LV2_STATE_ERR_BAD_TYPE);
  4282. // FIXME linuxsampler does not set POD flag
  4283. // CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  4284. carla_debug("CarlaPluginLV2::handleStateStore(%i:\"%s\", %p, " P_SIZE ", %i:\"%s\", %i)",
  4285. key, carla_lv2_urid_unmap(this, key), value, size, type, carla_lv2_urid_unmap(this, type), flags);
  4286. const char* const skey(carla_lv2_urid_unmap(this, key));
  4287. const char* const stype(carla_lv2_urid_unmap(this, type));
  4288. CARLA_SAFE_ASSERT_RETURN(skey != nullptr && skey != kUnmapFallback, LV2_STATE_ERR_BAD_TYPE);
  4289. CARLA_SAFE_ASSERT_RETURN(stype != nullptr && stype != kUnmapFallback, LV2_STATE_ERR_BAD_TYPE);
  4290. // Check if we already have this key
  4291. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  4292. {
  4293. CustomData& cData(it.getValue(kCustomDataFallbackNC));
  4294. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  4295. if (std::strcmp(cData.key, skey) == 0)
  4296. {
  4297. // found it
  4298. delete[] cData.value;
  4299. if (type == kUridAtomString || type == kUridAtomPath)
  4300. cData.value = carla_strdup((const char*)value);
  4301. else
  4302. cData.value = CarlaString::asBase64(value, size).dup();
  4303. return LV2_STATE_SUCCESS;
  4304. }
  4305. }
  4306. // Otherwise store it
  4307. CustomData newData;
  4308. newData.type = carla_strdup(stype);
  4309. newData.key = carla_strdup(skey);
  4310. if (type == kUridAtomString || type == kUridAtomPath)
  4311. newData.value = carla_strdup((const char*)value);
  4312. else
  4313. newData.value = CarlaString::asBase64(value, size).dup();
  4314. pData->custom.append(newData);
  4315. return LV2_STATE_SUCCESS;
  4316. // unused
  4317. (void)flags;
  4318. }
  4319. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  4320. {
  4321. CARLA_SAFE_ASSERT_RETURN(key != kUridNull, nullptr);
  4322. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  4323. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  4324. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  4325. carla_debug("CarlaPluginLV2::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  4326. const char* const skey(carla_lv2_urid_unmap(this, key));
  4327. CARLA_SAFE_ASSERT_RETURN(skey != nullptr && skey != kUnmapFallback, nullptr);
  4328. const char* stype = nullptr;
  4329. const char* stringData = nullptr;
  4330. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  4331. {
  4332. const CustomData& cData(it.getValue(kCustomDataFallback));
  4333. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  4334. if (std::strcmp(cData.key, skey) == 0)
  4335. {
  4336. stype = cData.type;
  4337. stringData = cData.value;
  4338. break;
  4339. }
  4340. }
  4341. if (stype == nullptr || stringData == nullptr)
  4342. {
  4343. carla_stderr("Plugin requested value for '%s' which is not available", skey);
  4344. *size = *type = *flags = 0;
  4345. return nullptr;
  4346. }
  4347. *type = carla_lv2_urid_map(this, stype);
  4348. *flags = LV2_STATE_IS_POD;
  4349. if (*type == kUridAtomString || *type == kUridAtomPath)
  4350. {
  4351. *size = std::strlen(stringData);
  4352. return stringData;
  4353. }
  4354. if (fLastStateChunk != nullptr)
  4355. {
  4356. std::free(fLastStateChunk);
  4357. fLastStateChunk = nullptr;
  4358. }
  4359. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stringData));
  4360. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  4361. fLastStateChunk = std::malloc(chunk.size());
  4362. CARLA_SAFE_ASSERT_RETURN(fLastStateChunk != nullptr, nullptr);
  4363. #ifdef CARLA_PROPER_CPP11_SUPPORT
  4364. std::memcpy(fLastStateChunk, chunk.data(), chunk.size());
  4365. #else
  4366. std::memcpy(fLastStateChunk, &chunk.front(), chunk.size());
  4367. #endif
  4368. *size = chunk.size();
  4369. return fLastStateChunk;
  4370. }
  4371. // -------------------------------------------------------------------
  4372. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  4373. {
  4374. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4375. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4376. carla_debug("CarlaPluginLV2::handleWorkerSchedule(%i, %p)", size, data);
  4377. if (pData->engine->isOffline())
  4378. {
  4379. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  4380. return LV2_WORKER_SUCCESS;
  4381. }
  4382. LV2_Atom atom;
  4383. atom.size = size;
  4384. atom.type = kUridCarlaAtomWorkerIn;
  4385. return fAtomBufferWorkerIn.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  4386. }
  4387. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  4388. {
  4389. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work_response != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4390. carla_debug("CarlaPluginLV2::handleWorkerRespond(%i, %p)", size, data);
  4391. LV2_Atom atom;
  4392. atom.size = size;
  4393. atom.type = kUridCarlaAtomWorkerResp;
  4394. return fAtomBufferWorkerResp.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  4395. }
  4396. // -------------------------------------------------------------------
  4397. void handleInlineDisplayQueueRedraw()
  4398. {
  4399. fInlineDisplayNeedsRedraw = true;
  4400. }
  4401. const LV2_Inline_Display_Image_Surface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  4402. {
  4403. CARLA_SAFE_ASSERT_RETURN(fExt.inlineDisplay != nullptr && fExt.inlineDisplay->render != nullptr, nullptr);
  4404. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  4405. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  4406. return fExt.inlineDisplay->render(fHandle, width, height);
  4407. }
  4408. // -------------------------------------------------------------------
  4409. void handleMidnamUpdate()
  4410. {
  4411. CARLA_SAFE_ASSERT_RETURN(fExt.midnam != nullptr,);
  4412. if (fEventsIn.ctrl == nullptr)
  4413. return;
  4414. char* const midnam = fExt.midnam->midnam(fHandle);
  4415. CARLA_SAFE_ASSERT_RETURN(midnam != nullptr,);
  4416. fEventsIn.ctrl->port->setMetaData("http://www.midi.org/dtds/MIDINameDocument10.dtd", midnam, "text/xml");
  4417. if (fExt.midnam->free != nullptr)
  4418. fExt.midnam->free(midnam);
  4419. }
  4420. // -------------------------------------------------------------------
  4421. void handleExternalUIClosed()
  4422. {
  4423. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EXTERNAL,);
  4424. carla_debug("CarlaPluginLV2::handleExternalUIClosed()");
  4425. fNeedsUiClose = true;
  4426. }
  4427. void handlePluginUIClosed() override
  4428. {
  4429. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  4430. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  4431. carla_debug("CarlaPluginLV2::handlePluginUIClosed()");
  4432. fNeedsUiClose = true;
  4433. }
  4434. void handlePluginUIResized(const uint width, const uint height) override
  4435. {
  4436. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  4437. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  4438. carla_debug("CarlaPluginLV2::handlePluginUIResized(%u, %u)", width, height);
  4439. if (fUI.handle != nullptr && fExt.uiresize != nullptr)
  4440. fExt.uiresize->ui_resize(fUI.handle, static_cast<int>(width), static_cast<int>(height));
  4441. }
  4442. // -------------------------------------------------------------------
  4443. uint32_t handleUIPortMap(const char* const symbol) const noexcept
  4444. {
  4445. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  4446. carla_debug("CarlaPluginLV2::handleUIPortMap(\"%s\")", symbol);
  4447. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  4448. {
  4449. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  4450. return i;
  4451. }
  4452. return LV2UI_INVALID_PORT_INDEX;
  4453. }
  4454. LV2UI_Request_Value_Status handleUIRequestValue(const LV2_URID key,
  4455. const LV2_URID type,
  4456. const LV2_Feature* const* features)
  4457. {
  4458. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL, LV2UI_REQUEST_VALUE_ERR_UNKNOWN);
  4459. carla_debug("CarlaPluginLV2::handleUIRequestValue(%u, %u, %p)", key, type, features);
  4460. if (type != kUridAtomPath)
  4461. return LV2UI_REQUEST_VALUE_ERR_UNSUPPORTED;
  4462. const char* const uri = getCustomURIDString(key);
  4463. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri != kUnmapFallback, LV2UI_REQUEST_VALUE_ERR_UNKNOWN);
  4464. // check if a file browser is already open
  4465. if (fUI.fileNeededForURI != nullptr || fUI.fileBrowserOpen)
  4466. return LV2UI_REQUEST_VALUE_BUSY;
  4467. for (uint32_t i=0; i < fRdfDescriptor->ParameterCount; ++i)
  4468. {
  4469. if (fRdfDescriptor->Parameters[i].Type != LV2_PARAMETER_PATH)
  4470. continue;
  4471. if (std::strcmp(fRdfDescriptor->Parameters[i].URI, uri) != 0)
  4472. continue;
  4473. // TODO file browser filters, also store label to use for title
  4474. fUI.fileNeededForURI = uri;
  4475. return LV2UI_REQUEST_VALUE_SUCCESS;
  4476. }
  4477. return LV2UI_REQUEST_VALUE_ERR_UNSUPPORTED;
  4478. // may be unused
  4479. (void)features;
  4480. }
  4481. int handleUIResize(const int width, const int height)
  4482. {
  4483. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr, 1);
  4484. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  4485. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  4486. carla_debug("CarlaPluginLV2::handleUIResize(%i, %i)", width, height);
  4487. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  4488. return 0;
  4489. }
  4490. void handleUITouch(const uint32_t rindex, const bool touch)
  4491. {
  4492. carla_debug("CarlaPluginLV2::handleUITouch(%u, %s)", rindex, bool2str(touch));
  4493. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  4494. for (uint32_t i=0; i < pData->param.count; ++i)
  4495. {
  4496. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  4497. continue;
  4498. index = i;
  4499. break;
  4500. }
  4501. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  4502. pData->engine->touchPluginParameter(pData->id, index, touch);
  4503. }
  4504. void handleUIWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  4505. {
  4506. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  4507. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  4508. carla_debug("CarlaPluginLV2::handleUIWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  4509. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  4510. switch (format)
  4511. {
  4512. case kUridNull: {
  4513. CARLA_SAFE_ASSERT_RETURN(rindex < fRdfDescriptor->PortCount,);
  4514. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  4515. for (uint32_t i=0; i < pData->param.count; ++i)
  4516. {
  4517. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  4518. continue;
  4519. index = i;
  4520. break;
  4521. }
  4522. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  4523. const float value(*(const float*)buffer);
  4524. // check if we should feedback message back to UI
  4525. bool sendGui = false;
  4526. if (const uint32_t notifCount = fUI.rdfDescriptor->PortNotificationCount)
  4527. {
  4528. const char* const portSymbol = fRdfDescriptor->Ports[rindex].Symbol;
  4529. for (uint32_t i=0; i < notifCount; ++i)
  4530. {
  4531. const LV2_RDF_UI_PortNotification& portNotif(fUI.rdfDescriptor->PortNotifications[i]);
  4532. if (portNotif.Protocol != LV2_UI_PORT_PROTOCOL_FLOAT)
  4533. continue;
  4534. if (portNotif.Symbol != nullptr)
  4535. {
  4536. if (std::strcmp(portNotif.Symbol, portSymbol) != 0)
  4537. continue;
  4538. }
  4539. else if (portNotif.Index != rindex)
  4540. {
  4541. continue;
  4542. }
  4543. sendGui = true;
  4544. break;
  4545. }
  4546. }
  4547. setParameterValue(index, value, sendGui, true, true);
  4548. } break;
  4549. case kUridAtomTransferAtom:
  4550. case kUridAtomTransferEvent: {
  4551. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  4552. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  4553. // plugins sometimes fail on this, not good...
  4554. const uint32_t totalSize = lv2_atom_total_size(atom);
  4555. const uint32_t paddedSize = lv2_atom_pad_size(totalSize);
  4556. if (bufferSize != totalSize && bufferSize != paddedSize)
  4557. carla_stderr2("Warning: LV2 UI sending atom with invalid size %u! size: %u, padded-size: %u",
  4558. bufferSize, totalSize, paddedSize);
  4559. for (uint32_t i=0; i < fEventsIn.count; ++i)
  4560. {
  4561. if (fEventsIn.data[i].rindex != rindex)
  4562. continue;
  4563. index = i;
  4564. break;
  4565. }
  4566. // for bad plugins
  4567. if (index == LV2UI_INVALID_PORT_INDEX)
  4568. {
  4569. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  4570. index = fEventsIn.ctrlIndex;
  4571. }
  4572. fAtomBufferEvIn.put(atom, index);
  4573. } break;
  4574. default:
  4575. carla_stdout("CarlaPluginLV2::handleUIWrite(%i, %i, %i:\"%s\", %p) - unknown format",
  4576. rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  4577. break;
  4578. }
  4579. }
  4580. // -------------------------------------------------------------------
  4581. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  4582. {
  4583. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  4584. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  4585. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  4586. CARLA_SAFE_ASSERT_RETURN(type != kUridNull,);
  4587. carla_debug("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  4588. int32_t rindex = -1;
  4589. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  4590. {
  4591. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  4592. {
  4593. rindex = static_cast<int32_t>(i);
  4594. break;
  4595. }
  4596. }
  4597. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  4598. float paramValue;
  4599. switch (type)
  4600. {
  4601. case kUridAtomBool:
  4602. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  4603. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  4604. break;
  4605. case kUridAtomDouble:
  4606. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  4607. paramValue = static_cast<float>((*(const double*)value));
  4608. break;
  4609. case kUridAtomFloat:
  4610. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  4611. paramValue = (*(const float*)value);
  4612. break;
  4613. case kUridAtomInt:
  4614. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  4615. paramValue = static_cast<float>((*(const int32_t*)value));
  4616. break;
  4617. case kUridAtomLong:
  4618. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  4619. paramValue = static_cast<float>((*(const int64_t*)value));
  4620. break;
  4621. default:
  4622. carla_stdout("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type",
  4623. portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  4624. return;
  4625. }
  4626. for (uint32_t i=0; i < pData->param.count; ++i)
  4627. {
  4628. if (pData->param.data[i].rindex == rindex)
  4629. {
  4630. setParameterValueRT(i, paramValue, true);
  4631. break;
  4632. }
  4633. }
  4634. }
  4635. // -------------------------------------------------------------------
  4636. void* getNativeHandle() const noexcept override
  4637. {
  4638. return fHandle;
  4639. }
  4640. const void* getNativeDescriptor() const noexcept override
  4641. {
  4642. return fDescriptor;
  4643. }
  4644. uintptr_t getUiBridgeProcessId() const noexcept override
  4645. {
  4646. return fPipeServer.isPipeRunning() ? fPipeServer.getPID() : 0;
  4647. }
  4648. // -------------------------------------------------------------------
  4649. public:
  4650. bool init(const char* const name, const char* const uri, const uint options)
  4651. {
  4652. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  4653. // ---------------------------------------------------------------
  4654. // first checks
  4655. if (pData->client != nullptr)
  4656. {
  4657. pData->engine->setLastError("Plugin client is already registered");
  4658. return false;
  4659. }
  4660. if (uri == nullptr || uri[0] == '\0')
  4661. {
  4662. pData->engine->setLastError("null uri");
  4663. return false;
  4664. }
  4665. const EngineOptions& opts(pData->engine->getOptions());
  4666. // ---------------------------------------------------------------
  4667. // Init LV2 World if needed, sets LV2_PATH for lilv
  4668. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  4669. if (opts.pathLV2 != nullptr && opts.pathLV2[0] != '\0')
  4670. lv2World.initIfNeeded(opts.pathLV2);
  4671. else if (const char* const LV2_PATH = std::getenv("LV2_PATH"))
  4672. lv2World.initIfNeeded(LV2_PATH);
  4673. else
  4674. lv2World.initIfNeeded(LILV_DEFAULT_LV2_PATH);
  4675. // ---------------------------------------------------------------
  4676. // get plugin from lv2_rdf (lilv)
  4677. fRdfDescriptor = lv2_rdf_new(uri, true);
  4678. if (fRdfDescriptor == nullptr)
  4679. {
  4680. pData->engine->setLastError("Failed to find the requested plugin");
  4681. return false;
  4682. }
  4683. // ---------------------------------------------------------------
  4684. // open DLL
  4685. if (! pData->libOpen(fRdfDescriptor->Binary))
  4686. {
  4687. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  4688. return false;
  4689. }
  4690. // ---------------------------------------------------------------
  4691. // try to get DLL main entry via new mode
  4692. if (const LV2_Lib_Descriptor_Function libDescFn = pData->libSymbol<LV2_Lib_Descriptor_Function>("lv2_lib_descriptor"))
  4693. {
  4694. // -----------------------------------------------------------
  4695. // all ok, get lib descriptor
  4696. const LV2_Lib_Descriptor* const libDesc = libDescFn(fRdfDescriptor->Bundle, nullptr);
  4697. if (libDesc == nullptr)
  4698. {
  4699. pData->engine->setLastError("Could not find the LV2 Descriptor");
  4700. return false;
  4701. }
  4702. // -----------------------------------------------------------
  4703. // get descriptor that matches URI (new mode)
  4704. uint32_t i = 0;
  4705. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  4706. {
  4707. if (std::strcmp(fDescriptor->URI, uri) == 0)
  4708. break;
  4709. }
  4710. }
  4711. else
  4712. {
  4713. // -----------------------------------------------------------
  4714. // get DLL main entry (old mode)
  4715. const LV2_Descriptor_Function descFn = pData->libSymbol<LV2_Descriptor_Function>("lv2_descriptor");
  4716. if (descFn == nullptr)
  4717. {
  4718. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  4719. return false;
  4720. }
  4721. // -----------------------------------------------------------
  4722. // get descriptor that matches URI (old mode)
  4723. uint32_t i = 0;
  4724. while ((fDescriptor = descFn(i++)))
  4725. {
  4726. if (std::strcmp(fDescriptor->URI, uri) == 0)
  4727. break;
  4728. }
  4729. }
  4730. if (fDescriptor == nullptr)
  4731. {
  4732. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  4733. return false;
  4734. }
  4735. // ---------------------------------------------------------------
  4736. // check supported port-types and features
  4737. bool canContinue = true;
  4738. // Check supported ports
  4739. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  4740. {
  4741. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  4742. if (! is_lv2_port_supported(portTypes))
  4743. {
  4744. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  4745. {
  4746. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  4747. canContinue = false;
  4748. break;
  4749. }
  4750. }
  4751. }
  4752. // Check supported features
  4753. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  4754. {
  4755. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[j]);
  4756. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  4757. {
  4758. carla_stderr("Plugin DSP wants UI feature '%s', ignoring this", feature.URI);
  4759. }
  4760. else if (std::strcmp(feature.URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  4761. {
  4762. fNeedsFixedBuffers = true;
  4763. }
  4764. else if (std::strcmp(feature.URI, LV2_PORT_PROPS__supportsStrictBounds) == 0)
  4765. {
  4766. fStrictBounds = feature.Required ? 1 : 0;
  4767. }
  4768. else if (feature.Required && ! is_lv2_feature_supported(feature.URI))
  4769. {
  4770. CarlaString msg("Plugin wants a feature that is not supported:\n");
  4771. msg += feature.URI;
  4772. canContinue = false;
  4773. pData->engine->setLastError(msg);
  4774. break;
  4775. }
  4776. }
  4777. if (! canContinue)
  4778. {
  4779. // error already set
  4780. return false;
  4781. }
  4782. if (fNeedsFixedBuffers && ! pData->engine->usesConstantBufferSize())
  4783. {
  4784. pData->engine->setLastError("Cannot use this plugin under the current engine.\n"
  4785. "The plugin requires a fixed block size which is not possible right now.");
  4786. return false;
  4787. }
  4788. // ---------------------------------------------------------------
  4789. // set icon
  4790. if (std::strncmp(fDescriptor->URI, "http://distrho.sf.net/", 22) == 0)
  4791. pData->iconName = carla_strdup_safe("distrho");
  4792. // ---------------------------------------------------------------
  4793. // set info
  4794. if (name != nullptr && name[0] != '\0')
  4795. pData->name = pData->engine->getUniquePluginName(name);
  4796. else
  4797. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  4798. // ---------------------------------------------------------------
  4799. // register client
  4800. pData->client = pData->engine->addClient(this);
  4801. if (pData->client == nullptr || ! pData->client->isOk())
  4802. {
  4803. pData->engine->setLastError("Failed to register plugin client");
  4804. return false;
  4805. }
  4806. // ---------------------------------------------------------------
  4807. // initialize options
  4808. const int bufferSize = static_cast<int>(pData->engine->getBufferSize());
  4809. fLv2Options.minBufferSize = fNeedsFixedBuffers ? bufferSize : 1;
  4810. fLv2Options.maxBufferSize = bufferSize;
  4811. fLv2Options.nominalBufferSize = bufferSize;
  4812. fLv2Options.sampleRate = static_cast<float>(pData->engine->getSampleRate());
  4813. fLv2Options.transientWinId = static_cast<int64_t>(opts.frontendWinId);
  4814. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  4815. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  4816. {
  4817. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  4818. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  4819. {
  4820. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  4821. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  4822. }
  4823. }
  4824. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  4825. fLv2Options.bgColor = opts.bgColor;
  4826. fLv2Options.fgColor = opts.fgColor;
  4827. fLv2Options.uiScale = opts.uiScale;
  4828. // ---------------------------------------------------------------
  4829. // initialize features (part 1)
  4830. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  4831. eventFt->callback_data = this;
  4832. eventFt->lv2_event_ref = carla_lv2_event_ref;
  4833. eventFt->lv2_event_unref = carla_lv2_event_unref;
  4834. LV2_Log_Log* const logFt = new LV2_Log_Log;
  4835. logFt->handle = this;
  4836. logFt->printf = carla_lv2_log_printf;
  4837. logFt->vprintf = carla_lv2_log_vprintf;
  4838. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  4839. stateMakePathFt->handle = this;
  4840. stateMakePathFt->path = carla_lv2_state_make_path;
  4841. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  4842. stateMapPathFt->handle = this;
  4843. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  4844. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  4845. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  4846. programsFt->handle = this;
  4847. programsFt->program_changed = carla_lv2_program_changed;
  4848. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  4849. rsPortFt->data = this;
  4850. rsPortFt->resize = carla_lv2_resize_port;
  4851. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  4852. lv2_rtmempool_init(rtMemPoolFt);
  4853. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  4854. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  4855. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  4856. uriMapFt->callback_data = this;
  4857. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  4858. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  4859. uridMapFt->handle = this;
  4860. uridMapFt->map = carla_lv2_urid_map;
  4861. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  4862. uridUnmapFt->handle = this;
  4863. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  4864. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  4865. workerFt->handle = this;
  4866. workerFt->schedule_work = carla_lv2_worker_schedule;
  4867. LV2_Inline_Display* const inlineDisplay = new LV2_Inline_Display;
  4868. inlineDisplay->handle = this;
  4869. inlineDisplay->queue_draw = carla_lv2_inline_display_queue_draw;
  4870. LV2_Midnam* const midnam = new LV2_Midnam;
  4871. midnam->handle = this;
  4872. midnam->update = carla_lv2_midnam_update;
  4873. // ---------------------------------------------------------------
  4874. // initialize features (part 2)
  4875. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  4876. fFeatures[j] = new LV2_Feature;
  4877. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  4878. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  4879. fFeatures[kFeatureIdBufSizeFixed]->URI = fNeedsFixedBuffers
  4880. ? LV2_BUF_SIZE__fixedBlockLength
  4881. : LV2_BUF_SIZE__boundedBlockLength;
  4882. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  4883. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  4884. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  4885. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  4886. fFeatures[kFeatureIdEvent]->data = eventFt;
  4887. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  4888. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  4889. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  4890. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  4891. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  4892. fFeatures[kFeatureIdIsLive]->data = nullptr;
  4893. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  4894. fFeatures[kFeatureIdLogs]->data = logFt;
  4895. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  4896. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  4897. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  4898. fFeatures[kFeatureIdPrograms]->data = programsFt;
  4899. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  4900. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  4901. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  4902. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  4903. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  4904. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  4905. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  4906. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  4907. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  4908. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  4909. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  4910. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  4911. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  4912. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  4913. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  4914. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  4915. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  4916. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  4917. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  4918. fFeatures[kFeatureIdWorker]->data = workerFt;
  4919. fFeatures[kFeatureIdInlineDisplay]->URI = LV2_INLINEDISPLAY__queue_draw;
  4920. fFeatures[kFeatureIdInlineDisplay]->data = inlineDisplay;
  4921. fFeatures[kFeatureIdMidnam]->URI = LV2_MIDNAM__update;
  4922. fFeatures[kFeatureIdMidnam]->data = midnam;
  4923. // ---------------------------------------------------------------
  4924. // initialize plugin
  4925. try {
  4926. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  4927. } catch(...) {}
  4928. if (fHandle == nullptr)
  4929. {
  4930. pData->engine->setLastError("Plugin failed to initialize");
  4931. return false;
  4932. }
  4933. recheckExtensions();
  4934. // ---------------------------------------------------------------
  4935. // set options
  4936. pData->options = 0x0;
  4937. if (fLatencyIndex >= 0 || getMidiOutCount() != 0 || fNeedsFixedBuffers)
  4938. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4939. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  4940. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4941. if (opts.forceStereo)
  4942. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4943. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  4944. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4945. if (getMidiInCount() != 0)
  4946. {
  4947. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  4948. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  4949. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  4950. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  4951. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  4952. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  4953. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  4954. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  4955. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  4956. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  4957. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  4958. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  4959. }
  4960. if (fExt.programs != nullptr && (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0)
  4961. {
  4962. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  4963. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  4964. }
  4965. // ---------------------------------------------------------------
  4966. // gui stuff
  4967. if (fRdfDescriptor->UICount != 0)
  4968. initUi();
  4969. return true;
  4970. }
  4971. // -------------------------------------------------------------------
  4972. void initUi()
  4973. {
  4974. // ---------------------------------------------------------------
  4975. // find the most appropriate ui
  4976. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eMod, iCocoa, iWindows, iX11, iExt, iFinal;
  4977. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eMod = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  4978. #if defined(LV2_UIS_ONLY_BRIDGES)
  4979. const bool preferUiBridges = true;
  4980. #elif defined(BUILD_BRIDGE)
  4981. const bool preferUiBridges = false;
  4982. #else
  4983. const bool preferUiBridges = pData->engine->getOptions().preferUiBridges;
  4984. #endif
  4985. bool hasShowInterface = false;
  4986. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  4987. {
  4988. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  4989. const int ii(static_cast<int>(i));
  4990. switch (fRdfDescriptor->UIs[i].Type)
  4991. {
  4992. case LV2_UI_QT4:
  4993. if (isUiBridgeable(i))
  4994. eQt4 = ii;
  4995. break;
  4996. case LV2_UI_QT5:
  4997. if (isUiBridgeable(i))
  4998. eQt5 = ii;
  4999. break;
  5000. case LV2_UI_GTK2:
  5001. if (isUiBridgeable(i))
  5002. eGtk2 = ii;
  5003. break;
  5004. case LV2_UI_GTK3:
  5005. if (isUiBridgeable(i))
  5006. eGtk3 = ii;
  5007. break;
  5008. #ifdef CARLA_OS_MAC
  5009. case LV2_UI_COCOA:
  5010. if (isUiBridgeable(i) && preferUiBridges)
  5011. eCocoa = ii;
  5012. iCocoa = ii;
  5013. break;
  5014. #endif
  5015. #ifdef CARLA_OS_WIN
  5016. case LV2_UI_WINDOWS:
  5017. if (isUiBridgeable(i) && preferUiBridges)
  5018. eWindows = ii;
  5019. iWindows = ii;
  5020. break;
  5021. #endif
  5022. case LV2_UI_X11:
  5023. if (isUiBridgeable(i) && preferUiBridges)
  5024. eX11 = ii;
  5025. iX11 = ii;
  5026. break;
  5027. case LV2_UI_EXTERNAL:
  5028. case LV2_UI_OLD_EXTERNAL:
  5029. iExt = ii;
  5030. break;
  5031. case LV2_UI_MOD:
  5032. eMod = ii;
  5033. break;
  5034. default:
  5035. break;
  5036. }
  5037. }
  5038. /**/ if (eQt4 >= 0)
  5039. iFinal = eQt4;
  5040. else if (eQt5 >= 0)
  5041. iFinal = eQt5;
  5042. else if (eGtk2 >= 0)
  5043. iFinal = eGtk2;
  5044. else if (eGtk3 >= 0)
  5045. iFinal = eGtk3;
  5046. #ifdef CARLA_OS_MAC
  5047. else if (eCocoa >= 0)
  5048. iFinal = eCocoa;
  5049. #endif
  5050. #ifdef CARLA_OS_WIN
  5051. else if (eWindows >= 0)
  5052. iFinal = eWindows;
  5053. #endif
  5054. #ifdef HAVE_X11
  5055. else if (eX11 >= 0)
  5056. iFinal = eX11;
  5057. #endif
  5058. #ifndef LV2_UIS_ONLY_BRIDGES
  5059. # ifdef CARLA_OS_MAC
  5060. else if (iCocoa >= 0)
  5061. iFinal = iCocoa;
  5062. # endif
  5063. # ifdef CARLA_OS_WIN
  5064. else if (iWindows >= 0)
  5065. iFinal = iWindows;
  5066. # endif
  5067. # ifdef HAVE_X11
  5068. else if (iX11 >= 0)
  5069. iFinal = iX11;
  5070. # endif
  5071. #endif
  5072. else if (iExt >= 0)
  5073. iFinal = iExt;
  5074. #ifndef BUILD_BRIDGE
  5075. if (iFinal < 0)
  5076. #endif
  5077. {
  5078. // no suitable UI found, see if there's one which supports ui:showInterface
  5079. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  5080. {
  5081. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  5082. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  5083. {
  5084. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  5085. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  5086. continue;
  5087. iFinal = static_cast<int>(i);
  5088. hasShowInterface = true;
  5089. break;
  5090. }
  5091. }
  5092. if (iFinal < 0)
  5093. {
  5094. if (eMod < 0)
  5095. {
  5096. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  5097. return;
  5098. }
  5099. // use MODGUI as last resort
  5100. iFinal = eMod;
  5101. }
  5102. }
  5103. fUI.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  5104. // ---------------------------------------------------------------
  5105. // check supported ui features
  5106. bool canContinue = true;
  5107. bool canDelete = true;
  5108. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  5109. {
  5110. const char* const uri(fUI.rdfDescriptor->Features[i].URI);
  5111. CARLA_SAFE_ASSERT_CONTINUE(uri != nullptr && uri[0] != '\0');
  5112. if (! is_lv2_ui_feature_supported(uri))
  5113. {
  5114. if (fUI.rdfDescriptor->Features[i].Required)
  5115. {
  5116. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", uri);
  5117. canContinue = false;
  5118. break;
  5119. }
  5120. carla_stderr("Plugin UI wants a feature that is not supported (ignored):\n%s", uri);
  5121. }
  5122. if (std::strcmp(uri, LV2_UI__makeResident) == 0 || std::strcmp(uri, LV2_UI__makeSONameResident) == 0)
  5123. canDelete = false;
  5124. else if (std::strcmp(uri, LV2_UI__requestValue) == 0)
  5125. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  5126. }
  5127. if (! canContinue)
  5128. {
  5129. fUI.rdfDescriptor = nullptr;
  5130. return;
  5131. }
  5132. // ---------------------------------------------------------------
  5133. // initialize ui according to type
  5134. const LV2_Property uiType(fUI.rdfDescriptor->Type);
  5135. if (
  5136. (iFinal == eQt4 ||
  5137. iFinal == eQt5 ||
  5138. iFinal == eGtk2 ||
  5139. iFinal == eGtk3 ||
  5140. iFinal == eCocoa ||
  5141. iFinal == eWindows ||
  5142. iFinal == eX11 ||
  5143. iFinal == eMod)
  5144. #ifdef BUILD_BRIDGE
  5145. && ! hasShowInterface
  5146. #endif
  5147. )
  5148. {
  5149. // -----------------------------------------------------------
  5150. // initialize ui-bridge
  5151. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  5152. {
  5153. carla_stdout("Will use UI-Bridge for '%s', binary: \"%s\"", pData->name, bridgeBinary);
  5154. CarlaString guiTitle(pData->name);
  5155. guiTitle += " (GUI)";
  5156. fLv2Options.windowTitle = guiTitle.dup();
  5157. fUI.type = UI::TYPE_BRIDGE;
  5158. fPipeServer.setData(bridgeBinary, fRdfDescriptor->URI, fUI.rdfDescriptor->URI);
  5159. delete[] bridgeBinary;
  5160. return;
  5161. }
  5162. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eMod)
  5163. {
  5164. carla_stderr2("Failed to find UI bridge binary for '%s', cannot use UI", pData->name);
  5165. fUI.rdfDescriptor = nullptr;
  5166. return;
  5167. }
  5168. }
  5169. #ifdef LV2_UIS_ONLY_BRIDGES
  5170. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  5171. fUI.rdfDescriptor = nullptr;
  5172. return;
  5173. #endif
  5174. // ---------------------------------------------------------------
  5175. // open UI DLL
  5176. if (! pData->uiLibOpen(fUI.rdfDescriptor->Binary, canDelete))
  5177. {
  5178. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUI.rdfDescriptor->Binary));
  5179. fUI.rdfDescriptor = nullptr;
  5180. return;
  5181. }
  5182. // ---------------------------------------------------------------
  5183. // get UI DLL main entry
  5184. LV2UI_DescriptorFunction uiDescFn = pData->uiLibSymbol<LV2UI_DescriptorFunction>("lv2ui_descriptor");
  5185. if (uiDescFn == nullptr)
  5186. {
  5187. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  5188. pData->uiLibClose();
  5189. fUI.rdfDescriptor = nullptr;
  5190. return;
  5191. }
  5192. // ---------------------------------------------------------------
  5193. // get UI descriptor that matches UI URI
  5194. uint32_t i = 0;
  5195. while ((fUI.descriptor = uiDescFn(i++)))
  5196. {
  5197. if (std::strcmp(fUI.descriptor->URI, fUI.rdfDescriptor->URI) == 0)
  5198. break;
  5199. }
  5200. if (fUI.descriptor == nullptr)
  5201. {
  5202. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  5203. pData->uiLibClose();
  5204. fUI.rdfDescriptor = nullptr;
  5205. return;
  5206. }
  5207. // ---------------------------------------------------------------
  5208. // check if ui is usable
  5209. switch (uiType)
  5210. {
  5211. case LV2_UI_NONE:
  5212. carla_stdout("Will use LV2 Show Interface for '%s'", pData->name);
  5213. fUI.type = UI::TYPE_EMBED;
  5214. break;
  5215. case LV2_UI_QT4:
  5216. carla_stdout("Will use LV2 Qt4 UI for '%s', NOT!", pData->name);
  5217. fUI.type = UI::TYPE_EMBED;
  5218. break;
  5219. case LV2_UI_QT5:
  5220. carla_stdout("Will use LV2 Qt5 UI for '%s', NOT!", pData->name);
  5221. fUI.type = UI::TYPE_EMBED;
  5222. break;
  5223. case LV2_UI_GTK2:
  5224. carla_stdout("Will use LV2 Gtk2 UI for '%s', NOT!", pData->name);
  5225. fUI.type = UI::TYPE_EMBED;
  5226. break;
  5227. case LV2_UI_GTK3:
  5228. carla_stdout("Will use LV2 Gtk3 UI for '%s', NOT!", pData->name);
  5229. fUI.type = UI::TYPE_EMBED;
  5230. break;
  5231. #ifdef CARLA_OS_MAC
  5232. case LV2_UI_COCOA:
  5233. carla_stdout("Will use LV2 Cocoa UI for '%s'", pData->name);
  5234. fUI.type = UI::TYPE_EMBED;
  5235. break;
  5236. #endif
  5237. #ifdef CARLA_OS_WIN
  5238. case LV2_UI_WINDOWS:
  5239. carla_stdout("Will use LV2 Windows UI for '%s'", pData->name);
  5240. fUI.type = UI::TYPE_EMBED;
  5241. break;
  5242. #endif
  5243. case LV2_UI_X11:
  5244. #ifdef HAVE_X11
  5245. carla_stdout("Will use LV2 X11 UI for '%s'", pData->name);
  5246. #else
  5247. carla_stdout("Will use LV2 X11 UI for '%s', NOT!", pData->name);
  5248. #endif
  5249. fUI.type = UI::TYPE_EMBED;
  5250. break;
  5251. case LV2_UI_EXTERNAL:
  5252. case LV2_UI_OLD_EXTERNAL:
  5253. carla_stdout("Will use LV2 External UI for '%s'", pData->name);
  5254. fUI.type = UI::TYPE_EXTERNAL;
  5255. break;
  5256. }
  5257. if (fUI.type == UI::TYPE_NULL)
  5258. {
  5259. pData->uiLibClose();
  5260. fUI.descriptor = nullptr;
  5261. fUI.rdfDescriptor = nullptr;
  5262. return;
  5263. }
  5264. // ---------------------------------------------------------------
  5265. // initialize ui data
  5266. CarlaString guiTitle(pData->name);
  5267. guiTitle += " (GUI)";
  5268. fLv2Options.windowTitle = guiTitle.dup();
  5269. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  5270. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].value = fLv2Options.windowTitle;
  5271. // ---------------------------------------------------------------
  5272. // initialize ui features (part 1)
  5273. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  5274. uiDataFt->data_access = fDescriptor->extension_data;
  5275. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  5276. uiPortMapFt->handle = this;
  5277. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  5278. LV2UI_Request_Value* const uiRequestValueFt = new LV2UI_Request_Value;
  5279. uiRequestValueFt->handle = this;
  5280. uiRequestValueFt->request = carla_lv2_ui_request_value;
  5281. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  5282. uiResizeFt->handle = this;
  5283. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  5284. LV2UI_Touch* const uiTouchFt = new LV2UI_Touch;
  5285. uiTouchFt->handle = this;
  5286. uiTouchFt->touch = carla_lv2_ui_touch;
  5287. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  5288. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  5289. uiExternalHostFt->plugin_human_id = fLv2Options.windowTitle;
  5290. // ---------------------------------------------------------------
  5291. // initialize ui features (part 2)
  5292. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  5293. fFeatures[j] = new LV2_Feature;
  5294. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  5295. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  5296. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  5297. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  5298. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  5299. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  5300. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  5301. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  5302. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  5303. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  5304. fFeatures[kFeatureIdUiMakeResident2]->URI = LV2_UI__makeSONameResident;
  5305. fFeatures[kFeatureIdUiMakeResident2]->data = nullptr;
  5306. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  5307. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  5308. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  5309. fFeatures[kFeatureIdUiParent]->data = nullptr;
  5310. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  5311. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  5312. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  5313. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  5314. fFeatures[kFeatureIdUiRequestValue]->URI = LV2_UI__requestValue;
  5315. fFeatures[kFeatureIdUiRequestValue]->data = uiRequestValueFt;
  5316. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  5317. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  5318. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  5319. fFeatures[kFeatureIdUiTouch]->data = uiTouchFt;
  5320. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  5321. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  5322. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  5323. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  5324. // ---------------------------------------------------------------
  5325. // initialize ui extensions
  5326. if (fUI.descriptor->extension_data == nullptr)
  5327. return;
  5328. fExt.uiidle = (const LV2UI_Idle_Interface*)fUI.descriptor->extension_data(LV2_UI__idleInterface);
  5329. fExt.uishow = (const LV2UI_Show_Interface*)fUI.descriptor->extension_data(LV2_UI__showInterface);
  5330. fExt.uiresize = (const LV2UI_Resize*)fUI.descriptor->extension_data(LV2_UI__resize);
  5331. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUI.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  5332. // check if invalid
  5333. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  5334. fExt.uiidle = nullptr;
  5335. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  5336. fExt.uishow = nullptr;
  5337. if (fExt.uiresize != nullptr && fExt.uiresize->ui_resize == nullptr)
  5338. fExt.uiresize = nullptr;
  5339. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  5340. fExt.uiprograms = nullptr;
  5341. // don't use uiidle if external
  5342. if (fUI.type == UI::TYPE_EXTERNAL)
  5343. fExt.uiidle = nullptr;
  5344. }
  5345. // -------------------------------------------------------------------
  5346. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  5347. {
  5348. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  5349. carla_debug("CarlaPluginLV2::handleTransferAtom(%i, %p)", portIndex, atom);
  5350. fAtomBufferEvIn.put(atom, portIndex);
  5351. }
  5352. void handleUridMap(const LV2_URID urid, const char* const uri)
  5353. {
  5354. CARLA_SAFE_ASSERT_RETURN(urid != kUridNull,);
  5355. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  5356. carla_debug("CarlaPluginLV2::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.size()-1, uri);
  5357. const std::size_t uriCount(fCustomURIDs.size());
  5358. if (urid < uriCount)
  5359. {
  5360. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  5361. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr && ourURI != kUnmapFallback,);
  5362. if (std::strcmp(ourURI, uri) != 0)
  5363. {
  5364. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  5365. }
  5366. }
  5367. else
  5368. {
  5369. CARLA_SAFE_ASSERT_RETURN(urid == uriCount,);
  5370. fCustomURIDs.push_back(uri);
  5371. }
  5372. }
  5373. // -------------------------------------------------------------------
  5374. void writeAtomPath(const char* const path, const LV2_URID urid)
  5375. {
  5376. uint8_t atomBuf[4096];
  5377. lv2_atom_forge_set_buffer(&fAtomForge, atomBuf, sizeof(atomBuf));
  5378. LV2_Atom_Forge_Frame forgeFrame;
  5379. lv2_atom_forge_object(&fAtomForge, &forgeFrame, kUridNull, kUridPatchSet);
  5380. lv2_atom_forge_key(&fAtomForge, kUridPatchPoperty);
  5381. lv2_atom_forge_urid(&fAtomForge, urid);
  5382. lv2_atom_forge_key(&fAtomForge, kUridPatchValue);
  5383. lv2_atom_forge_path(&fAtomForge, path, static_cast<uint32_t>(std::strlen(path)));
  5384. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  5385. LV2_Atom* const atom((LV2_Atom*)atomBuf);
  5386. CARLA_SAFE_ASSERT(atom->size < sizeof(atomBuf));
  5387. fAtomBufferEvIn.put(atom, fEventsIn.ctrlIndex);
  5388. }
  5389. // -------------------------------------------------------------------
  5390. private:
  5391. LV2_Handle fHandle;
  5392. LV2_Handle fHandle2;
  5393. LV2_Feature* fFeatures[kFeatureCountAll+1];
  5394. const LV2_Descriptor* fDescriptor;
  5395. const LV2_RDF_Descriptor* fRdfDescriptor;
  5396. float** fAudioInBuffers;
  5397. float** fAudioOutBuffers;
  5398. float** fCvInBuffers;
  5399. float** fCvOutBuffers;
  5400. float* fParamBuffers;
  5401. bool fNeedsFixedBuffers;
  5402. bool fNeedsUiClose;
  5403. bool fInlineDisplayNeedsRedraw;
  5404. int64_t fInlineDisplayLastRedrawTime;
  5405. int32_t fLatencyIndex; // -1 if invalid
  5406. int fStrictBounds; // -1 unsupported, 0 optional, 1 required
  5407. Lv2AtomRingBuffer fAtomBufferEvIn;
  5408. Lv2AtomRingBuffer fAtomBufferUiOut;
  5409. Lv2AtomRingBuffer fAtomBufferWorkerIn;
  5410. Lv2AtomRingBuffer fAtomBufferWorkerResp;
  5411. LV2_Atom_Forge fAtomForge;
  5412. uint8_t* fAtomBufferUiOutTmpData;
  5413. uint8_t* fAtomBufferWorkerInTmpData;
  5414. CarlaPluginLV2EventData fEventsIn;
  5415. CarlaPluginLV2EventData fEventsOut;
  5416. CarlaPluginLV2Options fLv2Options;
  5417. CarlaPipeServerLV2 fPipeServer;
  5418. std::vector<std::string> fCustomURIDs;
  5419. bool fFirstActive; // first process() call after activate()
  5420. void* fLastStateChunk;
  5421. EngineTimeInfo fLastTimeInfo;
  5422. // if plugin provides path parameter, use it as fake "gui"
  5423. CarlaString fFilePathURI;
  5424. struct Extensions {
  5425. const LV2_Options_Interface* options;
  5426. const LV2_State_Interface* state;
  5427. const LV2_Worker_Interface* worker;
  5428. const LV2_Inline_Display_Interface* inlineDisplay;
  5429. const LV2_Midnam_Interface* midnam;
  5430. const LV2_Programs_Interface* programs;
  5431. const LV2UI_Idle_Interface* uiidle;
  5432. const LV2UI_Show_Interface* uishow;
  5433. const LV2UI_Resize* uiresize;
  5434. const LV2_Programs_UI_Interface* uiprograms;
  5435. Extensions()
  5436. : options(nullptr),
  5437. state(nullptr),
  5438. worker(nullptr),
  5439. inlineDisplay(nullptr),
  5440. midnam(nullptr),
  5441. programs(nullptr),
  5442. uiidle(nullptr),
  5443. uishow(nullptr),
  5444. uiresize(nullptr),
  5445. uiprograms(nullptr) {}
  5446. CARLA_DECLARE_NON_COPY_STRUCT(Extensions);
  5447. } fExt;
  5448. struct UI {
  5449. enum Type {
  5450. TYPE_NULL = 0,
  5451. TYPE_BRIDGE,
  5452. TYPE_EMBED,
  5453. TYPE_EXTERNAL
  5454. };
  5455. Type type;
  5456. LV2UI_Handle handle;
  5457. LV2UI_Widget widget;
  5458. const LV2UI_Descriptor* descriptor;
  5459. const LV2_RDF_UI* rdfDescriptor;
  5460. bool fileBrowserOpen;
  5461. const char* fileNeededForURI;
  5462. CarlaPluginUI* window;
  5463. UI()
  5464. : type(TYPE_NULL),
  5465. handle(nullptr),
  5466. widget(nullptr),
  5467. descriptor(nullptr),
  5468. rdfDescriptor(nullptr),
  5469. fileBrowserOpen(false),
  5470. fileNeededForURI(nullptr),
  5471. window(nullptr) {}
  5472. ~UI()
  5473. {
  5474. CARLA_SAFE_ASSERT(handle == nullptr);
  5475. CARLA_SAFE_ASSERT(widget == nullptr);
  5476. CARLA_SAFE_ASSERT(descriptor == nullptr);
  5477. CARLA_SAFE_ASSERT(rdfDescriptor == nullptr);
  5478. CARLA_SAFE_ASSERT(! fileBrowserOpen);
  5479. CARLA_SAFE_ASSERT(fileNeededForURI == nullptr);
  5480. CARLA_SAFE_ASSERT(window == nullptr);
  5481. }
  5482. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  5483. } fUI;
  5484. // -------------------------------------------------------------------
  5485. // Event Feature
  5486. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  5487. {
  5488. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  5489. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  5490. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  5491. return 0;
  5492. }
  5493. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  5494. {
  5495. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  5496. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  5497. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  5498. return 0;
  5499. }
  5500. // -------------------------------------------------------------------
  5501. // Logs Feature
  5502. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  5503. {
  5504. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  5505. CARLA_SAFE_ASSERT_RETURN(type != kUridNull, 0);
  5506. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  5507. #ifndef DEBUG
  5508. if (type == kUridLogTrace)
  5509. return 0;
  5510. #endif
  5511. va_list args;
  5512. va_start(args, fmt);
  5513. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  5514. va_end(args);
  5515. return ret;
  5516. }
  5517. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  5518. {
  5519. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  5520. CARLA_SAFE_ASSERT_RETURN(type != kUridNull, 0);
  5521. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  5522. int ret = 0;
  5523. switch (type)
  5524. {
  5525. case kUridLogError:
  5526. std::fprintf(stderr, "\x1b[31m");
  5527. ret = std::vfprintf(stderr, fmt, ap);
  5528. std::fprintf(stderr, "\x1b[0m");
  5529. break;
  5530. case kUridLogNote:
  5531. ret = std::vfprintf(stdout, fmt, ap);
  5532. break;
  5533. case kUridLogTrace:
  5534. #ifdef DEBUG
  5535. std::fprintf(stdout, "\x1b[30;1m");
  5536. ret = std::vfprintf(stdout, fmt, ap);
  5537. std::fprintf(stdout, "\x1b[0m");
  5538. #endif
  5539. break;
  5540. case kUridLogWarning:
  5541. ret = std::vfprintf(stderr, fmt, ap);
  5542. break;
  5543. default:
  5544. break;
  5545. }
  5546. return ret;
  5547. }
  5548. // -------------------------------------------------------------------
  5549. // Programs Feature
  5550. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  5551. {
  5552. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5553. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  5554. ((CarlaPluginLV2*)handle)->handleProgramChanged(index);
  5555. }
  5556. // -------------------------------------------------------------------
  5557. // Resize Port Feature
  5558. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  5559. {
  5560. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  5561. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  5562. return ((CarlaPluginLV2*)data)->handleResizePort(index, size);
  5563. }
  5564. // -------------------------------------------------------------------
  5565. // State Feature
  5566. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  5567. {
  5568. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  5569. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  5570. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  5571. File file;
  5572. if (File::isAbsolutePath(path))
  5573. file = File(path);
  5574. else
  5575. file = File::getCurrentWorkingDirectory().getChildFile(path);
  5576. file.getParentDirectory().createDirectory();
  5577. return strdup(file.getFullPathName().toRawUTF8());
  5578. }
  5579. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  5580. {
  5581. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, strdup(""));
  5582. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', strdup(""));
  5583. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  5584. // may already be an abstract path
  5585. if (! File::isAbsolutePath(absolute_path))
  5586. return strdup(absolute_path);
  5587. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  5588. }
  5589. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  5590. {
  5591. const char* const cwd(File::getCurrentWorkingDirectory().getFullPathName().toRawUTF8());
  5592. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, strdup(cwd));
  5593. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', strdup(cwd));
  5594. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  5595. // may already be an absolute path
  5596. if (File::isAbsolutePath(abstract_path))
  5597. return strdup(abstract_path);
  5598. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  5599. }
  5600. 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)
  5601. {
  5602. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  5603. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  5604. return ((CarlaPluginLV2*)handle)->handleStateStore(key, value, size, type, flags);
  5605. }
  5606. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  5607. {
  5608. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  5609. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  5610. return ((CarlaPluginLV2*)handle)->handleStateRetrieve(key, size, type, flags);
  5611. }
  5612. // -------------------------------------------------------------------
  5613. // URI-Map Feature
  5614. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  5615. {
  5616. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  5617. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  5618. // unused
  5619. (void)map;
  5620. }
  5621. // -------------------------------------------------------------------
  5622. // URID Feature
  5623. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  5624. {
  5625. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, kUridNull);
  5626. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', kUridNull);
  5627. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  5628. // Atom types
  5629. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  5630. return kUridAtomBlank;
  5631. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  5632. return kUridAtomBool;
  5633. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  5634. return kUridAtomChunk;
  5635. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  5636. return kUridAtomDouble;
  5637. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  5638. return kUridAtomEvent;
  5639. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  5640. return kUridAtomFloat;
  5641. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  5642. return kUridAtomInt;
  5643. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  5644. return kUridAtomLiteral;
  5645. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  5646. return kUridAtomLong;
  5647. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  5648. return kUridAtomNumber;
  5649. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  5650. return kUridAtomObject;
  5651. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  5652. return kUridAtomPath;
  5653. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  5654. return kUridAtomProperty;
  5655. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  5656. return kUridAtomResource;
  5657. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  5658. return kUridAtomSequence;
  5659. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  5660. return kUridAtomSound;
  5661. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  5662. return kUridAtomString;
  5663. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  5664. return kUridAtomTuple;
  5665. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  5666. return kUridAtomURI;
  5667. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  5668. return kUridAtomURID;
  5669. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  5670. return kUridAtomVector;
  5671. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  5672. return kUridAtomTransferAtom;
  5673. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  5674. return kUridAtomTransferEvent;
  5675. // BufSize types
  5676. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  5677. return kUridBufMaxLength;
  5678. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  5679. return kUridBufMinLength;
  5680. if (std::strcmp(uri, LV2_BUF_SIZE__nominalBlockLength) == 0)
  5681. return kUridBufNominalLength;
  5682. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  5683. return kUridBufSequenceSize;
  5684. // Log types
  5685. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  5686. return kUridLogError;
  5687. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  5688. return kUridLogNote;
  5689. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  5690. return kUridLogTrace;
  5691. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  5692. return kUridLogWarning;
  5693. // Patch types
  5694. if (std::strcmp(uri, LV2_PATCH__Set) == 0)
  5695. return kUridPatchSet;
  5696. if (std::strcmp(uri, LV2_PATCH__property) == 0)
  5697. return kUridPatchPoperty;
  5698. if (std::strcmp(uri, LV2_PATCH__value) == 0)
  5699. return kUridPatchValue;
  5700. // Time types
  5701. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  5702. return kUridTimePosition;
  5703. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  5704. return kUridTimeBar;
  5705. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  5706. return kUridTimeBarBeat;
  5707. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  5708. return kUridTimeBeat;
  5709. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  5710. return kUridTimeBeatUnit;
  5711. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  5712. return kUridTimeBeatsPerBar;
  5713. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  5714. return kUridTimeBeatsPerMinute;
  5715. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  5716. return kUridTimeFrame;
  5717. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  5718. return kUridTimeFramesPerSecond;
  5719. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  5720. return kUridTimeSpeed;
  5721. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  5722. return kUridTimeTicksPerBeat;
  5723. // Others
  5724. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  5725. return kUridMidiEvent;
  5726. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  5727. return kUridParamSampleRate;
  5728. if (std::strcmp(uri, LV2_UI__backgroundColor) == 0)
  5729. return kUridBackgroundColor;
  5730. if (std::strcmp(uri, LV2_UI__foregroundColor) == 0)
  5731. return kUridForegroundColor;
  5732. if (std::strcmp(uri, LV2_UI__scaleFactor) == 0)
  5733. return kUridScaleFactor;
  5734. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  5735. return kUridWindowTitle;
  5736. // Custom Carla types
  5737. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER_IN) == 0)
  5738. return kUridCarlaAtomWorkerIn;
  5739. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER_RESP) == 0)
  5740. return kUridCarlaAtomWorkerResp;
  5741. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  5742. return kUridCarlaTransientWindowId;
  5743. // Custom plugin types
  5744. return ((CarlaPluginLV2*)handle)->getCustomURID(uri);
  5745. }
  5746. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  5747. {
  5748. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  5749. CARLA_SAFE_ASSERT_RETURN(urid != kUridNull, nullptr);
  5750. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  5751. switch (urid)
  5752. {
  5753. // Atom types
  5754. case kUridAtomBlank:
  5755. return LV2_ATOM__Blank;
  5756. case kUridAtomBool:
  5757. return LV2_ATOM__Bool;
  5758. case kUridAtomChunk:
  5759. return LV2_ATOM__Chunk;
  5760. case kUridAtomDouble:
  5761. return LV2_ATOM__Double;
  5762. case kUridAtomEvent:
  5763. return LV2_ATOM__Event;
  5764. case kUridAtomFloat:
  5765. return LV2_ATOM__Float;
  5766. case kUridAtomInt:
  5767. return LV2_ATOM__Int;
  5768. case kUridAtomLiteral:
  5769. return LV2_ATOM__Literal;
  5770. case kUridAtomLong:
  5771. return LV2_ATOM__Long;
  5772. case kUridAtomNumber:
  5773. return LV2_ATOM__Number;
  5774. case kUridAtomObject:
  5775. return LV2_ATOM__Object;
  5776. case kUridAtomPath:
  5777. return LV2_ATOM__Path;
  5778. case kUridAtomProperty:
  5779. return LV2_ATOM__Property;
  5780. case kUridAtomResource:
  5781. return LV2_ATOM__Resource;
  5782. case kUridAtomSequence:
  5783. return LV2_ATOM__Sequence;
  5784. case kUridAtomSound:
  5785. return LV2_ATOM__Sound;
  5786. case kUridAtomString:
  5787. return LV2_ATOM__String;
  5788. case kUridAtomTuple:
  5789. return LV2_ATOM__Tuple;
  5790. case kUridAtomURI:
  5791. return LV2_ATOM__URI;
  5792. case kUridAtomURID:
  5793. return LV2_ATOM__URID;
  5794. case kUridAtomVector:
  5795. return LV2_ATOM__Vector;
  5796. case kUridAtomTransferAtom:
  5797. return LV2_ATOM__atomTransfer;
  5798. case kUridAtomTransferEvent:
  5799. return LV2_ATOM__eventTransfer;
  5800. // BufSize types
  5801. case kUridBufMaxLength:
  5802. return LV2_BUF_SIZE__maxBlockLength;
  5803. case kUridBufMinLength:
  5804. return LV2_BUF_SIZE__minBlockLength;
  5805. case kUridBufNominalLength:
  5806. return LV2_BUF_SIZE__nominalBlockLength;
  5807. case kUridBufSequenceSize:
  5808. return LV2_BUF_SIZE__sequenceSize;
  5809. // Log types
  5810. case kUridLogError:
  5811. return LV2_LOG__Error;
  5812. case kUridLogNote:
  5813. return LV2_LOG__Note;
  5814. case kUridLogTrace:
  5815. return LV2_LOG__Trace;
  5816. case kUridLogWarning:
  5817. return LV2_LOG__Warning;
  5818. // Patch types
  5819. case kUridPatchSet:
  5820. return LV2_PATCH__Set;
  5821. case kUridPatchPoperty:
  5822. return LV2_PATCH__property;
  5823. case kUridPatchValue:
  5824. return LV2_PATCH__value;
  5825. // Time types
  5826. case kUridTimePosition:
  5827. return LV2_TIME__Position;
  5828. case kUridTimeBar:
  5829. return LV2_TIME__bar;
  5830. case kUridTimeBarBeat:
  5831. return LV2_TIME__barBeat;
  5832. case kUridTimeBeat:
  5833. return LV2_TIME__beat;
  5834. case kUridTimeBeatUnit:
  5835. return LV2_TIME__beatUnit;
  5836. case kUridTimeBeatsPerBar:
  5837. return LV2_TIME__beatsPerBar;
  5838. case kUridTimeBeatsPerMinute:
  5839. return LV2_TIME__beatsPerMinute;
  5840. case kUridTimeFrame:
  5841. return LV2_TIME__frame;
  5842. case kUridTimeFramesPerSecond:
  5843. return LV2_TIME__framesPerSecond;
  5844. case kUridTimeSpeed:
  5845. return LV2_TIME__speed;
  5846. case kUridTimeTicksPerBeat:
  5847. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  5848. // Others
  5849. case kUridMidiEvent:
  5850. return LV2_MIDI__MidiEvent;
  5851. case kUridParamSampleRate:
  5852. return LV2_PARAMETERS__sampleRate;
  5853. case kUridBackgroundColor:
  5854. return LV2_UI__backgroundColor;
  5855. case kUridForegroundColor:
  5856. return LV2_UI__foregroundColor;
  5857. case kUridScaleFactor:
  5858. return LV2_UI__scaleFactor;
  5859. case kUridWindowTitle:
  5860. return LV2_UI__windowTitle;
  5861. // Custom Carla types
  5862. case kUridCarlaAtomWorkerIn:
  5863. return URI_CARLA_ATOM_WORKER_IN;
  5864. case kUridCarlaAtomWorkerResp:
  5865. return URI_CARLA_ATOM_WORKER_RESP;
  5866. case kUridCarlaTransientWindowId:
  5867. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  5868. }
  5869. // Custom plugin types
  5870. return ((CarlaPluginLV2*)handle)->getCustomURIDString(urid);
  5871. }
  5872. // -------------------------------------------------------------------
  5873. // Worker Feature
  5874. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  5875. {
  5876. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  5877. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  5878. return ((CarlaPluginLV2*)handle)->handleWorkerSchedule(size, data);
  5879. }
  5880. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  5881. {
  5882. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  5883. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  5884. return ((CarlaPluginLV2*)handle)->handleWorkerRespond(size, data);
  5885. }
  5886. // -------------------------------------------------------------------
  5887. // Inline Display Feature
  5888. static void carla_lv2_inline_display_queue_draw(LV2_Inline_Display_Handle handle)
  5889. {
  5890. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5891. // carla_debug("carla_lv2_inline_display_queue_draw(%p)", handle);
  5892. ((CarlaPluginLV2*)handle)->handleInlineDisplayQueueRedraw();
  5893. }
  5894. // -------------------------------------------------------------------
  5895. // Midnam Feature
  5896. static void carla_lv2_midnam_update(LV2_Midnam_Handle handle)
  5897. {
  5898. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5899. carla_stdout("carla_lv2_midnam_update(%p)", handle);
  5900. ((CarlaPluginLV2*)handle)->handleMidnamUpdate();
  5901. }
  5902. // -------------------------------------------------------------------
  5903. // External UI Feature
  5904. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  5905. {
  5906. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  5907. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  5908. ((CarlaPluginLV2*)controller)->handleExternalUIClosed();
  5909. }
  5910. // -------------------------------------------------------------------
  5911. // UI Port-Map Feature
  5912. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  5913. {
  5914. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  5915. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  5916. return ((CarlaPluginLV2*)handle)->handleUIPortMap(symbol);
  5917. }
  5918. // ----------------------------------------------------------------------------------------------------------------
  5919. // UI Request Parameter Feature
  5920. static LV2UI_Request_Value_Status carla_lv2_ui_request_value(LV2UI_Feature_Handle handle,
  5921. LV2_URID key,
  5922. LV2_URID type,
  5923. const LV2_Feature* const* features)
  5924. {
  5925. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_REQUEST_VALUE_ERR_UNKNOWN);
  5926. carla_debug("carla_lv2_ui_request_value(%p, %u, %u, %p)", handle, key, type, features);
  5927. return ((CarlaPluginLV2*)handle)->handleUIRequestValue(key, type, features);
  5928. }
  5929. // -------------------------------------------------------------------
  5930. // UI Resize Feature
  5931. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  5932. {
  5933. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  5934. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  5935. return ((CarlaPluginLV2*)handle)->handleUIResize(width, height);
  5936. }
  5937. // -------------------------------------------------------------------
  5938. // UI Touch Feature
  5939. static void carla_lv2_ui_touch(LV2UI_Feature_Handle handle, uint32_t port_index, bool touch)
  5940. {
  5941. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  5942. carla_debug("carla_lv2_ui_touch(%p, %u, %s)", handle, port_index, bool2str(touch));
  5943. ((CarlaPluginLV2*)handle)->handleUITouch(port_index, touch);
  5944. }
  5945. // -------------------------------------------------------------------
  5946. // UI Extension
  5947. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  5948. {
  5949. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  5950. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  5951. ((CarlaPluginLV2*)controller)->handleUIWrite(port_index, buffer_size, format, buffer);
  5952. }
  5953. // -------------------------------------------------------------------
  5954. // Lilv State
  5955. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  5956. {
  5957. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  5958. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  5959. ((CarlaPluginLV2*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  5960. }
  5961. // -------------------------------------------------------------------
  5962. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLV2)
  5963. };
  5964. // -------------------------------------------------------------------------------------------------------------------
  5965. bool CarlaPipeServerLV2::msgReceived(const char* const msg) noexcept
  5966. {
  5967. if (std::strcmp(msg, "exiting") == 0)
  5968. {
  5969. closePipeServer();
  5970. fUiState = UiHide;
  5971. return true;
  5972. }
  5973. if (std::strcmp(msg, "control") == 0)
  5974. {
  5975. uint32_t index;
  5976. float value;
  5977. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5978. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  5979. try {
  5980. kPlugin->handleUIWrite(index, sizeof(float), kUridNull, &value);
  5981. } CARLA_SAFE_EXCEPTION("magReceived control");
  5982. return true;
  5983. }
  5984. if (std::strcmp(msg, "atom") == 0)
  5985. {
  5986. uint32_t index, atomTotalSize, base64Size;
  5987. const char* base64atom;
  5988. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5989. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  5990. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(base64Size), true);
  5991. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom, false, base64Size), true);
  5992. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  5993. CARLA_SAFE_ASSERT_UINT2_RETURN(chunk.size() >= sizeof(LV2_Atom), chunk.size(), sizeof(LV2_Atom), true);
  5994. #ifdef CARLA_PROPER_CPP11_SUPPORT
  5995. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  5996. #else
  5997. const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
  5998. #endif
  5999. CARLA_SAFE_ASSERT_RETURN(lv2_atom_total_size(atom) == chunk.size(), true);
  6000. try {
  6001. kPlugin->handleUIWrite(index, lv2_atom_total_size(atom), kUridAtomTransferEvent, atom);
  6002. } CARLA_SAFE_EXCEPTION("magReceived atom");
  6003. return true;
  6004. }
  6005. if (std::strcmp(msg, "program") == 0)
  6006. {
  6007. uint32_t index;
  6008. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  6009. try {
  6010. kPlugin->setMidiProgram(static_cast<int32_t>(index), false, true, true, false);
  6011. } CARLA_SAFE_EXCEPTION("msgReceived program");
  6012. return true;
  6013. }
  6014. if (std::strcmp(msg, "urid") == 0)
  6015. {
  6016. uint32_t urid, size;
  6017. const char* uri;
  6018. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  6019. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), true);
  6020. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri, false, size), true);
  6021. if (urid != 0)
  6022. {
  6023. try {
  6024. kPlugin->handleUridMap(urid, uri);
  6025. } CARLA_SAFE_EXCEPTION("msgReceived urid");
  6026. }
  6027. return true;
  6028. }
  6029. if (std::strcmp(msg, "reloadprograms") == 0)
  6030. {
  6031. int32_t index;
  6032. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index), true);
  6033. try {
  6034. kPlugin->handleProgramChanged(index);
  6035. } CARLA_SAFE_EXCEPTION("handleProgramChanged");
  6036. return true;
  6037. }
  6038. if (std::strcmp(msg, "requestvalue") == 0)
  6039. {
  6040. uint32_t key, type;
  6041. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(key), true);
  6042. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(type), true);
  6043. if (key != 0)
  6044. {
  6045. try {
  6046. kPlugin->handleUIRequestValue(key, type, nullptr);
  6047. } CARLA_SAFE_EXCEPTION("msgReceived requestvalue");
  6048. }
  6049. return true;
  6050. }
  6051. return false;
  6052. }
  6053. // -------------------------------------------------------------------------------------------------------------------
  6054. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  6055. {
  6056. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  6057. CarlaPluginLV2* const plugin(new CarlaPluginLV2(init.engine, init.id));
  6058. if (! plugin->init(init.name, init.label, init.options))
  6059. {
  6060. delete plugin;
  6061. return nullptr;
  6062. }
  6063. return plugin;
  6064. }
  6065. // used in CarlaStandalone.cpp
  6066. const void* carla_render_inline_display_lv2(CarlaPlugin* plugin, uint32_t width, uint32_t height);
  6067. const void* carla_render_inline_display_lv2(CarlaPlugin* plugin, uint32_t width, uint32_t height)
  6068. {
  6069. CarlaPluginLV2* const lv2Plugin = (CarlaPluginLV2*)plugin;
  6070. return lv2Plugin->renderInlineDisplay(width, height);
  6071. }
  6072. // -------------------------------------------------------------------------------------------------------------------
  6073. CARLA_BACKEND_END_NAMESPACE