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.

7502 lines
274KB

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