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.

7628 lines
280KB

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