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.

7944 lines
292KB

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