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.

6217 lines
233KB

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