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.

6440 lines
236KB

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