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.

6829 lines
249KB

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