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.

6192 lines
232KB

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