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.

6994 lines
256KB

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