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.

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