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.

7461 lines
273KB

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