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.

6166 lines
231KB

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