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.

7537 lines
276KB

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