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.

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