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.

6233 lines
234KB

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