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.

6221 lines
234KB

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