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.

6931 lines
253KB

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