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.

6869 lines
251KB

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