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.

6327 lines
239KB

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