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.

7483 lines
274KB

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