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.

6226 lines
235KB

  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. // Fallback data
  39. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  40. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  41. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  42. // -------------------------------------------------------------------------------------------------------------------
  43. // Maximum default buffer size
  44. const uint MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  45. // Extra Plugin Hints
  46. const uint PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  47. const uint PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  48. const uint PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  49. const uint PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  50. // Extra Parameter Hints
  51. const uint PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  52. const uint PARAMETER_IS_TRIGGER = 0x2000;
  53. // LV2 Event Data/Types
  54. const uint CARLA_EVENT_DATA_ATOM = 0x01;
  55. const uint CARLA_EVENT_DATA_EVENT = 0x02;
  56. const uint CARLA_EVENT_DATA_MIDI_LL = 0x04;
  57. const uint CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  58. const uint CARLA_EVENT_TYPE_MIDI = 0x20;
  59. const uint CARLA_EVENT_TYPE_TIME = 0x40;
  60. // LV2 URI Map Ids
  61. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  77. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  78. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  79. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  80. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  81. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  82. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  83. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 22;
  84. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 23;
  85. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 24;
  86. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 25;
  87. const uint32_t CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH = 26;
  88. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 27;
  89. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 28;
  90. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 29;
  91. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 30;
  92. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 31;
  93. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 32; // base type
  94. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 33; // values
  95. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 34;
  96. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 35;
  97. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 36;
  98. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 37;
  99. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 38;
  100. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 39;
  101. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 40;
  102. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 41;
  103. const uint32_t CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT = 42;
  104. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 43;
  105. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 44;
  106. const uint32_t CARLA_URI_MAP_ID_UI_WINDOW_TITLE = 45;
  107. const uint32_t CARLA_URI_MAP_ID_CARLA_ATOM_WORKER = 46;
  108. const uint32_t CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID = 47;
  109. const uint32_t CARLA_URI_MAP_ID_COUNT = 48;
  110. // LV2 Feature Ids
  111. const uint32_t kFeatureIdBufSizeBounded = 0;
  112. const uint32_t kFeatureIdBufSizeFixed = 1;
  113. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  114. const uint32_t kFeatureIdEvent = 3;
  115. const uint32_t kFeatureIdHardRtCapable = 4;
  116. const uint32_t kFeatureIdInPlaceBroken = 5;
  117. const uint32_t kFeatureIdIsLive = 6;
  118. const uint32_t kFeatureIdLogs = 7;
  119. const uint32_t kFeatureIdOptions = 8;
  120. const uint32_t kFeatureIdPrograms = 9;
  121. const uint32_t kFeatureIdResizePort = 10;
  122. const uint32_t kFeatureIdRtMemPool = 11;
  123. const uint32_t kFeatureIdRtMemPoolOld = 12;
  124. const uint32_t kFeatureIdStateMakePath = 13;
  125. const uint32_t kFeatureIdStateMapPath = 14;
  126. const uint32_t kFeatureIdStrictBounds = 15;
  127. const uint32_t kFeatureIdUriMap = 16;
  128. const uint32_t kFeatureIdUridMap = 17;
  129. const uint32_t kFeatureIdUridUnmap = 18;
  130. const uint32_t kFeatureIdWorker = 19;
  131. const uint32_t kFeatureCountPlugin = 20;
  132. const uint32_t kFeatureIdUiDataAccess = 20;
  133. const uint32_t kFeatureIdUiInstanceAccess = 21;
  134. const uint32_t kFeatureIdUiIdleInterface = 22;
  135. const uint32_t kFeatureIdUiFixedSize = 23;
  136. const uint32_t kFeatureIdUiMakeResident = 24;
  137. const uint32_t kFeatureIdUiMakeResident2 = 25;
  138. const uint32_t kFeatureIdUiNoUserResize = 26;
  139. const uint32_t kFeatureIdUiParent = 27;
  140. const uint32_t kFeatureIdUiPortMap = 28;
  141. const uint32_t kFeatureIdUiPortSubscribe = 29;
  142. const uint32_t kFeatureIdUiResize = 30;
  143. const uint32_t kFeatureIdUiTouch = 31;
  144. const uint32_t kFeatureIdExternalUi = 32;
  145. const uint32_t kFeatureIdExternalUiOld = 33;
  146. const uint32_t kFeatureCountAll = 34;
  147. // -------------------------------------------------------------------------------------------------------------------
  148. struct Lv2EventData {
  149. uint32_t type;
  150. uint32_t rindex;
  151. CarlaEngineEventPort* port;
  152. union {
  153. LV2_Atom_Buffer* atom;
  154. LV2_Event_Buffer* event;
  155. LV2_MIDI midi;
  156. };
  157. Lv2EventData() noexcept
  158. : type(0x0),
  159. rindex(0),
  160. port(nullptr) {}
  161. ~Lv2EventData() noexcept
  162. {
  163. if (port != nullptr)
  164. {
  165. delete port;
  166. port = nullptr;
  167. }
  168. const uint32_t rtype(type);
  169. type = 0x0;
  170. if (rtype & CARLA_EVENT_DATA_ATOM)
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  173. std::free(atom);
  174. atom = nullptr;
  175. }
  176. else if (rtype & CARLA_EVENT_DATA_EVENT)
  177. {
  178. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  179. std::free(event);
  180. event = nullptr;
  181. }
  182. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  183. {
  184. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  185. delete[] midi.data;
  186. midi.data = nullptr;
  187. }
  188. }
  189. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  190. };
  191. struct CarlaPluginLV2EventData {
  192. uint32_t count;
  193. Lv2EventData* data;
  194. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  195. uint32_t ctrlIndex;
  196. CarlaPluginLV2EventData() noexcept
  197. : count(0),
  198. data(nullptr),
  199. ctrl(nullptr),
  200. ctrlIndex(0) {}
  201. ~CarlaPluginLV2EventData() noexcept
  202. {
  203. CARLA_SAFE_ASSERT_INT(count == 0, count);
  204. CARLA_SAFE_ASSERT(data == nullptr);
  205. CARLA_SAFE_ASSERT(ctrl == nullptr);
  206. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  207. }
  208. void createNew(const uint32_t newCount)
  209. {
  210. CARLA_SAFE_ASSERT_INT(count == 0, count);
  211. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  212. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  213. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  214. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  215. data = new Lv2EventData[newCount];
  216. count = newCount;
  217. ctrl = nullptr;
  218. ctrlIndex = 0;
  219. }
  220. void clear() noexcept
  221. {
  222. if (data != nullptr)
  223. {
  224. for (uint32_t i=0; i < count; ++i)
  225. {
  226. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  227. data[i].port = nullptr;
  228. }
  229. delete[] data;
  230. data = nullptr;
  231. }
  232. count = 0;
  233. ctrl = nullptr;
  234. ctrlIndex = 0;
  235. }
  236. void initBuffers() const noexcept
  237. {
  238. for (uint32_t i=0; i < count; ++i)
  239. {
  240. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  241. data[i].port->initBuffer();
  242. }
  243. }
  244. CARLA_DECLARE_NON_COPY_STRUCT(CarlaPluginLV2EventData)
  245. };
  246. // -------------------------------------------------------------------------------------------------------------------
  247. struct CarlaPluginLV2Options {
  248. enum OptIndex {
  249. MaxBlockLenth = 0,
  250. MinBlockLenth,
  251. NominalBlockLenth,
  252. SequenceSize,
  253. SampleRate,
  254. FrontendWinId,
  255. WindowTitle,
  256. Null,
  257. Count
  258. };
  259. int maxBufferSize;
  260. int minBufferSize;
  261. int nominalBufferSize;
  262. int sequenceSize;
  263. double sampleRate;
  264. int64_t frontendWinId;
  265. const char* windowTitle;
  266. LV2_Options_Option opts[Count];
  267. CarlaPluginLV2Options() noexcept
  268. : maxBufferSize(0),
  269. minBufferSize(0),
  270. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  271. sampleRate(0.0),
  272. frontendWinId(0),
  273. windowTitle(nullptr)
  274. {
  275. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  276. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  277. optMaxBlockLenth.subject = 0;
  278. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  279. optMaxBlockLenth.size = sizeof(int);
  280. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  281. optMaxBlockLenth.value = &maxBufferSize;
  282. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  283. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  284. optMinBlockLenth.subject = 0;
  285. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  286. optMinBlockLenth.size = sizeof(int);
  287. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  288. optMinBlockLenth.value = &minBufferSize;
  289. LV2_Options_Option& optNominalBlockLenth(opts[NominalBlockLenth]);
  290. optNominalBlockLenth.context = LV2_OPTIONS_INSTANCE;
  291. optNominalBlockLenth.subject = 0;
  292. optNominalBlockLenth.key = CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH;
  293. optNominalBlockLenth.size = sizeof(int);
  294. optNominalBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  295. optNominalBlockLenth.value = &nominalBufferSize;
  296. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  297. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  298. optSequenceSize.subject = 0;
  299. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  300. optSequenceSize.size = sizeof(int);
  301. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  302. optSequenceSize.value = &sequenceSize;
  303. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  304. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  305. optSampleRate.subject = 0;
  306. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  307. optSampleRate.size = sizeof(double);
  308. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  309. optSampleRate.value = &sampleRate;
  310. LV2_Options_Option& optFrontendWinId(opts[FrontendWinId]);
  311. optFrontendWinId.context = LV2_OPTIONS_INSTANCE;
  312. optFrontendWinId.subject = 0;
  313. optFrontendWinId.key = CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  314. optFrontendWinId.size = sizeof(int64_t);
  315. optFrontendWinId.type = CARLA_URI_MAP_ID_ATOM_LONG;
  316. optFrontendWinId.value = &frontendWinId;
  317. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  318. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  319. optWindowTitle.subject = 0;
  320. optWindowTitle.key = CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  321. optWindowTitle.size = 0;
  322. optWindowTitle.type = CARLA_URI_MAP_ID_ATOM_STRING;
  323. optWindowTitle.value = nullptr;
  324. LV2_Options_Option& optNull(opts[Null]);
  325. optNull.context = LV2_OPTIONS_INSTANCE;
  326. optNull.subject = 0;
  327. optNull.key = CARLA_URI_MAP_ID_NULL;
  328. optNull.size = 0;
  329. optNull.type = CARLA_URI_MAP_ID_NULL;
  330. optNull.value = nullptr;
  331. }
  332. ~CarlaPluginLV2Options() noexcept
  333. {
  334. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  335. optWindowTitle.size = 0;
  336. optWindowTitle.value = nullptr;
  337. if (windowTitle != nullptr)
  338. {
  339. delete[] windowTitle;
  340. windowTitle = nullptr;
  341. }
  342. }
  343. CARLA_DECLARE_NON_COPY_STRUCT(CarlaPluginLV2Options);
  344. };
  345. // -------------------------------------------------------------------------------------------------------------------
  346. class CarlaPluginLV2;
  347. class CarlaPipeServerLV2 : public CarlaPipeServer
  348. {
  349. public:
  350. enum UiState {
  351. UiNone = 0,
  352. UiHide,
  353. UiShow,
  354. UiCrashed
  355. };
  356. CarlaPipeServerLV2(CarlaEngine* const engine, CarlaPluginLV2* const plugin)
  357. : kEngine(engine),
  358. kPlugin(plugin),
  359. fFilename(),
  360. fPluginURI(),
  361. fUiURI(),
  362. fUiState(UiNone) {}
  363. ~CarlaPipeServerLV2() noexcept override
  364. {
  365. CARLA_SAFE_ASSERT_INT(fUiState == UiNone, fUiState);
  366. }
  367. UiState getAndResetUiState() noexcept
  368. {
  369. const UiState uiState(fUiState);
  370. fUiState = UiNone;
  371. return uiState;
  372. }
  373. void setData(const char* const filename, const char* const pluginURI, const char* const uiURI) noexcept
  374. {
  375. fFilename = filename;
  376. fPluginURI = pluginURI;
  377. fUiURI = uiURI;
  378. }
  379. bool startPipeServer() noexcept
  380. {
  381. const ScopedEngineEnvironmentLocker _seel(kEngine);
  382. const ScopedEnvVar _sev1("LV2_PATH", kEngine->getOptions().pathLV2);
  383. #ifdef CARLA_OS_LINUX
  384. const ScopedEnvVar _sev2("LD_PRELOAD", nullptr);
  385. #endif
  386. return CarlaPipeServer::startPipeServer(fFilename, fPluginURI, fUiURI);
  387. }
  388. void writeUiOptionsMessage(const double sampleRate, const bool useTheme, const bool useThemeColors, const char* const windowTitle, uintptr_t transientWindowId) const noexcept
  389. {
  390. char tmpBuf[0xff+1];
  391. tmpBuf[0xff] = '\0';
  392. const CarlaMutexLocker cml(getPipeLock());
  393. const ScopedLocale csl;
  394. _writeMsgBuffer("uiOptions\n", 10);
  395. {
  396. std::snprintf(tmpBuf, 0xff, "%g\n", sampleRate);
  397. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  398. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(useTheme));
  399. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  400. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(useThemeColors));
  401. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  402. writeAndFixMessage(windowTitle != nullptr ? windowTitle : "");
  403. std::snprintf(tmpBuf, 0xff, P_INTPTR "\n", transientWindowId);
  404. _writeMsgBuffer(tmpBuf, std::strlen(tmpBuf));
  405. }
  406. flushMessages();
  407. }
  408. void writeUiTitleMessage(const char* const title) const noexcept
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0',);
  411. const CarlaMutexLocker cml(getPipeLock());
  412. _writeMsgBuffer("uiTitle\n", 8);
  413. writeAndFixMessage(title);
  414. flushMessages();
  415. }
  416. protected:
  417. // returns true if msg was handled
  418. bool msgReceived(const char* const msg) noexcept override;
  419. private:
  420. CarlaEngine* const kEngine;
  421. CarlaPluginLV2* const kPlugin;
  422. CarlaString fFilename;
  423. CarlaString fPluginURI;
  424. CarlaString fUiURI;
  425. UiState fUiState;
  426. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeServerLV2)
  427. };
  428. // -------------------------------------------------------------------------------------------------------------------
  429. class CarlaPluginLV2 : public CarlaPlugin,
  430. private CarlaPluginUI::CloseCallback
  431. {
  432. public:
  433. CarlaPluginLV2(CarlaEngine* const engine, const uint id)
  434. : CarlaPlugin(engine, id),
  435. fHandle(nullptr),
  436. fHandle2(nullptr),
  437. fDescriptor(nullptr),
  438. fRdfDescriptor(nullptr),
  439. fAudioInBuffers(nullptr),
  440. fAudioOutBuffers(nullptr),
  441. fCvInBuffers(nullptr),
  442. fCvOutBuffers(nullptr),
  443. fParamBuffers(nullptr),
  444. fCanInit2(true),
  445. fNeedsUiClose(false),
  446. //fLatencyChanged(false),
  447. fLatencyIndex(-1),
  448. fAtomBufferIn(),
  449. fAtomBufferOut(),
  450. fAtomForge(),
  451. fEventsIn(),
  452. fEventsOut(),
  453. fLv2Options(),
  454. fPipeServer(engine, this),
  455. fCustomURIDs(),
  456. fFirstActive(true),
  457. fLastStateChunk(nullptr),
  458. fLastTimeInfo(),
  459. fExt(),
  460. fUI()
  461. {
  462. carla_debug("CarlaPluginLV2::CarlaPluginLV2(%p, %i)", engine, id);
  463. carla_zeroPointers(fFeatures, kFeatureCountAll+1);
  464. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  465. fCustomURIDs.append(nullptr);
  466. #if defined(__clang__)
  467. # pragma clang diagnostic push
  468. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  469. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  470. # pragma GCC diagnostic push
  471. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  472. #endif
  473. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  474. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  475. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  476. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  477. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  478. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  479. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  480. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  481. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  482. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  483. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  484. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  485. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  486. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  487. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  488. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  489. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  490. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  491. #if defined(__clang__)
  492. # pragma clang diagnostic pop
  493. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  494. # pragma GCC diagnostic pop
  495. #endif
  496. }
  497. ~CarlaPluginLV2() override
  498. {
  499. carla_debug("CarlaPluginLV2::~CarlaPluginLV2()");
  500. // close UI
  501. if (fUI.type != UI::TYPE_NULL)
  502. {
  503. showCustomUI(false);
  504. if (fUI.type == UI::TYPE_BRIDGE)
  505. {
  506. fPipeServer.stopPipeServer(pData->engine->getOptions().uiBridgesTimeout);
  507. }
  508. else
  509. {
  510. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  511. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  512. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  513. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  514. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  515. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  516. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  517. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  518. fUI.descriptor = nullptr;
  519. pData->uiLibClose();
  520. }
  521. #ifndef LV2_UIS_ONLY_BRIDGES
  522. if (fUI.window != nullptr)
  523. {
  524. delete fUI.window;
  525. fUI.window = nullptr;
  526. }
  527. #endif
  528. fUI.rdfDescriptor = nullptr;
  529. }
  530. pData->singleMutex.lock();
  531. pData->masterMutex.lock();
  532. if (pData->client != nullptr && pData->client->isActive())
  533. pData->client->deactivate();
  534. if (pData->active)
  535. {
  536. deactivate();
  537. pData->active = false;
  538. }
  539. if (fDescriptor != nullptr)
  540. {
  541. if (fDescriptor->cleanup != nullptr)
  542. {
  543. if (fHandle != nullptr)
  544. fDescriptor->cleanup(fHandle);
  545. if (fHandle2 != nullptr)
  546. fDescriptor->cleanup(fHandle2);
  547. }
  548. fHandle = nullptr;
  549. fHandle2 = nullptr;
  550. fDescriptor = nullptr;
  551. }
  552. if (fRdfDescriptor != nullptr)
  553. {
  554. delete fRdfDescriptor;
  555. fRdfDescriptor = nullptr;
  556. }
  557. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  558. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  559. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  560. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  561. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  562. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  563. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  564. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  565. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  566. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  567. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  568. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  569. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  570. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  571. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  572. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  573. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  574. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  575. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  576. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  577. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  578. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  579. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  580. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  581. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  582. {
  583. if (fFeatures[i] != nullptr)
  584. {
  585. delete fFeatures[i];
  586. fFeatures[i] = nullptr;
  587. }
  588. }
  589. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin2(); it.valid(); it.next())
  590. {
  591. const char* const uri(it.getValue(nullptr));
  592. if (uri != nullptr)
  593. delete[] uri;
  594. }
  595. fCustomURIDs.clear();
  596. if (fLastStateChunk != nullptr)
  597. {
  598. std::free(fLastStateChunk);
  599. fLastStateChunk = nullptr;
  600. }
  601. clearBuffers();
  602. }
  603. // -------------------------------------------------------------------
  604. // Information (base)
  605. PluginType getType() const noexcept override
  606. {
  607. return PLUGIN_LV2;
  608. }
  609. PluginCategory getCategory() const noexcept override
  610. {
  611. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  612. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  613. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  614. if (LV2_IS_DELAY(cat1, cat2))
  615. return PLUGIN_CATEGORY_DELAY;
  616. if (LV2_IS_DISTORTION(cat1, cat2))
  617. return PLUGIN_CATEGORY_OTHER;
  618. if (LV2_IS_DYNAMICS(cat1, cat2))
  619. return PLUGIN_CATEGORY_DYNAMICS;
  620. if (LV2_IS_EQ(cat1, cat2))
  621. return PLUGIN_CATEGORY_EQ;
  622. if (LV2_IS_FILTER(cat1, cat2))
  623. return PLUGIN_CATEGORY_FILTER;
  624. if (LV2_IS_GENERATOR(cat1, cat2))
  625. return PLUGIN_CATEGORY_SYNTH;
  626. if (LV2_IS_MODULATOR(cat1, cat2))
  627. return PLUGIN_CATEGORY_MODULATOR;
  628. if (LV2_IS_REVERB(cat1, cat2))
  629. return PLUGIN_CATEGORY_DELAY;
  630. if (LV2_IS_SIMULATOR(cat1, cat2))
  631. return PLUGIN_CATEGORY_OTHER;
  632. if (LV2_IS_SPATIAL(cat1, cat2))
  633. return PLUGIN_CATEGORY_OTHER;
  634. if (LV2_IS_SPECTRAL(cat1, cat2))
  635. return PLUGIN_CATEGORY_UTILITY;
  636. if (LV2_IS_UTILITY(cat1, cat2))
  637. return PLUGIN_CATEGORY_UTILITY;
  638. return CarlaPlugin::getCategory();
  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(kExternalMidiNoteFallback));
  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. }
  2574. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2575. {
  2576. value = ctrlEvent.value*127.0f/100.0f;
  2577. setVolume(value, false, false);
  2578. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2579. }
  2580. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2581. {
  2582. float left, right;
  2583. value = ctrlEvent.value/0.5f - 1.0f;
  2584. if (value < 0.0f)
  2585. {
  2586. left = -1.0f;
  2587. right = (value*2.0f)+1.0f;
  2588. }
  2589. else if (value > 0.0f)
  2590. {
  2591. left = (value*2.0f)-1.0f;
  2592. right = 1.0f;
  2593. }
  2594. else
  2595. {
  2596. left = -1.0f;
  2597. right = 1.0f;
  2598. }
  2599. setBalanceLeft(left, false, false);
  2600. setBalanceRight(right, false, false);
  2601. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2602. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2603. }
  2604. }
  2605. #endif
  2606. // Control plugin parameters
  2607. uint32_t k;
  2608. for (k=0; k < pData->param.count; ++k)
  2609. {
  2610. if (pData->param.data[k].midiChannel != event.channel)
  2611. continue;
  2612. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2613. continue;
  2614. if (pData->param.data[k].type != PARAMETER_INPUT)
  2615. continue;
  2616. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2617. continue;
  2618. float value;
  2619. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2620. {
  2621. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2622. }
  2623. else
  2624. {
  2625. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2626. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2627. value = std::rint(value);
  2628. }
  2629. setParameterValue(k, value, false, false, false);
  2630. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2631. }
  2632. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  2633. {
  2634. uint8_t midiData[3];
  2635. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2636. midiData[1] = uint8_t(ctrlEvent.param);
  2637. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2638. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2639. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2640. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2641. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2642. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2643. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2644. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2645. }
  2646. break;
  2647. } // case kEngineControlEventTypeParameter
  2648. case kEngineControlEventTypeMidiBank:
  2649. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2650. {
  2651. if (event.channel == pData->ctrlChannel)
  2652. nextBankId = ctrlEvent.param;
  2653. }
  2654. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2655. {
  2656. uint8_t midiData[3];
  2657. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2658. midiData[1] = MIDI_CONTROL_BANK_SELECT;
  2659. midiData[2] = uint8_t(ctrlEvent.param);
  2660. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2661. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2662. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2663. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2664. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2665. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2666. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2667. }
  2668. break;
  2669. case kEngineControlEventTypeMidiProgram:
  2670. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2671. {
  2672. if (event.channel == pData->ctrlChannel)
  2673. {
  2674. const uint32_t nextProgramId(ctrlEvent.param);
  2675. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2676. {
  2677. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2678. {
  2679. const int32_t index(static_cast<int32_t>(k));
  2680. setMidiProgram(index, false, false, false);
  2681. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2682. break;
  2683. }
  2684. }
  2685. }
  2686. }
  2687. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2688. {
  2689. uint8_t midiData[2];
  2690. midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2691. midiData[1] = uint8_t(ctrlEvent.param);
  2692. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2693. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2694. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiData);
  2695. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2696. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiData);
  2697. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2698. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 2, midiData);
  2699. }
  2700. break;
  2701. case kEngineControlEventTypeAllSoundOff:
  2702. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2703. {
  2704. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2705. uint8_t midiData[3];
  2706. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2707. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2708. midiData[2] = 0;
  2709. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2710. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2711. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2712. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2713. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2714. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2715. }
  2716. break;
  2717. case kEngineControlEventTypeAllNotesOff:
  2718. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2719. {
  2720. #ifndef BUILD_BRIDGE
  2721. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2722. {
  2723. allNotesOffSent = true;
  2724. sendMidiAllNotesOffToCallback();
  2725. }
  2726. #endif
  2727. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2728. uint8_t midiData[3];
  2729. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2730. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2731. midiData[2] = 0;
  2732. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2733. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2734. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2735. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2736. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2737. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2738. }
  2739. break;
  2740. } // switch (ctrlEvent.type)
  2741. break;
  2742. } // case kEngineEventTypeControl
  2743. case kEngineEventTypeMidi: {
  2744. const EngineMidiEvent& midiEvent(event.midi);
  2745. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  2746. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  2747. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2748. continue;
  2749. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2750. continue;
  2751. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2752. continue;
  2753. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2754. continue;
  2755. // Fix bad note-off (per LV2 spec)
  2756. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  2757. status = MIDI_STATUS_NOTE_OFF;
  2758. const uint32_t j = fEventsIn.ctrlIndex;
  2759. const uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2760. // put back channel in data
  2761. uint8_t midiData2[midiEvent.size];
  2762. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  2763. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  2764. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2765. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2766. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2767. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2768. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2769. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiData2);
  2770. if (status == MIDI_STATUS_NOTE_ON)
  2771. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiData[1], midiData[2]);
  2772. else if (status == MIDI_STATUS_NOTE_OFF)
  2773. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiData[1], 0.0f);
  2774. } break;
  2775. } // switch (event.type)
  2776. }
  2777. pData->postRtEvents.trySplice();
  2778. if (frames > timeOffset)
  2779. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  2780. } // End of Event Input and Processing
  2781. // --------------------------------------------------------------------------------------------------------
  2782. // Plugin processing (no events)
  2783. else
  2784. {
  2785. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  2786. } // End of Plugin processing (no events)
  2787. #ifndef BUILD_BRIDGE
  2788. #if 0 // TODO
  2789. // --------------------------------------------------------------------------------------------------------
  2790. // Latency, save values for next callback
  2791. if (fLatencyIndex != -1)
  2792. {
  2793. if (pData->latency != static_cast<uint32_t>(fParamBuffers[fLatencyIndex]))
  2794. {
  2795. fLatencyChanged = true;
  2796. }
  2797. else if (pData->latency > 0)
  2798. {
  2799. if (pData->latency <= frames)
  2800. {
  2801. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2802. FloatVectorOperations::copy(pData->latencyBuffers[i], audioIn[i]+(frames-pData->latency), static_cast<int>(pData->latency));
  2803. }
  2804. else
  2805. {
  2806. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  2807. {
  2808. for (k=0; k < pData->latency-frames; ++k)
  2809. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  2810. for (j=0; k < pData->latency; ++j, ++k)
  2811. pData->latencyBuffers[i][k] = audioIn[i][j];
  2812. }
  2813. }
  2814. }
  2815. }
  2816. #endif
  2817. #endif
  2818. // --------------------------------------------------------------------------------------------------------
  2819. // MIDI Output
  2820. if (fEventsOut.ctrl != nullptr)
  2821. {
  2822. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2823. {
  2824. const LV2_Atom_Event* ev;
  2825. LV2_Atom_Buffer_Iterator iter;
  2826. uint8_t* data;
  2827. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2828. for (;;)
  2829. {
  2830. data = nullptr;
  2831. ev = lv2_atom_buffer_get(&iter, &data);
  2832. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2833. break;
  2834. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2835. {
  2836. if (fEventsOut.ctrl->port != nullptr)
  2837. {
  2838. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2839. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2840. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2841. }
  2842. }
  2843. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2844. {
  2845. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2846. fAtomBufferOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2847. }
  2848. lv2_atom_buffer_increment(&iter);
  2849. }
  2850. }
  2851. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2852. {
  2853. const LV2_Event* ev;
  2854. LV2_Event_Iterator iter;
  2855. uint8_t* data;
  2856. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2857. for (;;)
  2858. {
  2859. data = nullptr;
  2860. ev = lv2_event_get(&iter, &data);
  2861. if (ev == nullptr || data == nullptr)
  2862. break;
  2863. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2864. {
  2865. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2866. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2867. }
  2868. lv2_event_increment(&iter);
  2869. }
  2870. }
  2871. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2872. {
  2873. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2874. uint32_t eventSize;
  2875. double eventTime;
  2876. uchar* eventData;
  2877. for (;;)
  2878. {
  2879. eventSize = 0;
  2880. eventTime = 0.0;
  2881. eventData = nullptr;
  2882. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2883. if (eventData == nullptr || eventSize == 0)
  2884. break;
  2885. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2886. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2887. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2888. lv2midi_step(&state);
  2889. }
  2890. }
  2891. }
  2892. #ifndef BUILD_BRIDGE
  2893. // --------------------------------------------------------------------------------------------------------
  2894. // Control Output
  2895. if (pData->event.portOut != nullptr)
  2896. {
  2897. uint8_t channel;
  2898. uint16_t param;
  2899. float value;
  2900. for (uint32_t k=0; k < pData->param.count; ++k)
  2901. {
  2902. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2903. continue;
  2904. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2905. if (pData->param.data[k].midiCC > 0)
  2906. {
  2907. channel = pData->param.data[k].midiChannel;
  2908. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2909. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2910. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2911. }
  2912. }
  2913. } // End of Control Output
  2914. #endif
  2915. // --------------------------------------------------------------------------------------------------------
  2916. // Final work
  2917. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2918. {
  2919. fExt.worker->end_run(fHandle);
  2920. if (fHandle2 != nullptr)
  2921. fExt.worker->end_run(fHandle2);
  2922. }
  2923. fFirstActive = false;
  2924. // --------------------------------------------------------------------------------------------------------
  2925. }
  2926. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  2927. {
  2928. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2929. if (pData->audioIn.count > 0)
  2930. {
  2931. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  2932. }
  2933. if (pData->audioOut.count > 0)
  2934. {
  2935. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  2936. }
  2937. if (pData->cvIn.count > 0)
  2938. {
  2939. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  2940. }
  2941. if (pData->cvOut.count > 0)
  2942. {
  2943. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  2944. }
  2945. // --------------------------------------------------------------------------------------------------------
  2946. // Try lock, silence otherwise
  2947. if (pData->engine->isOffline())
  2948. {
  2949. pData->singleMutex.lock();
  2950. }
  2951. else if (! pData->singleMutex.tryLock())
  2952. {
  2953. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2954. {
  2955. for (uint32_t k=0; k < frames; ++k)
  2956. audioOut[i][k+timeOffset] = 0.0f;
  2957. }
  2958. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2959. {
  2960. for (uint32_t k=0; k < frames; ++k)
  2961. cvOut[i][k+timeOffset] = 0.0f;
  2962. }
  2963. return false;
  2964. }
  2965. // --------------------------------------------------------------------------------------------------------
  2966. // Set audio buffers
  2967. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2968. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, static_cast<int>(frames));
  2969. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2970. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  2971. // --------------------------------------------------------------------------------------------------------
  2972. // Set CV buffers
  2973. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  2974. FloatVectorOperations::copy(fCvInBuffers[i], cvIn[i]+timeOffset, static_cast<int>(frames));
  2975. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2976. FloatVectorOperations::clear(fCvOutBuffers[i], static_cast<int>(frames));
  2977. // --------------------------------------------------------------------------------------------------------
  2978. // Run plugin
  2979. fDescriptor->run(fHandle, frames);
  2980. if (fHandle2 != nullptr)
  2981. fDescriptor->run(fHandle2, frames);
  2982. // --------------------------------------------------------------------------------------------------------
  2983. // Handle trigger parameters
  2984. for (uint32_t k=0; k < pData->param.count; ++k)
  2985. {
  2986. if (pData->param.data[k].type != PARAMETER_INPUT)
  2987. continue;
  2988. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2989. {
  2990. if (carla_isNotEqual(fParamBuffers[k], pData->param.ranges[k].def))
  2991. {
  2992. fParamBuffers[k] = pData->param.ranges[k].def;
  2993. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2994. }
  2995. }
  2996. }
  2997. pData->postRtEvents.trySplice();
  2998. #ifndef BUILD_BRIDGE
  2999. // --------------------------------------------------------------------------------------------------------
  3000. // Post-processing (dry/wet, volume and balance)
  3001. {
  3002. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  3003. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  3004. const bool isMono = (pData->audioIn.count == 1);
  3005. bool isPair;
  3006. float bufValue, oldBufLeft[doBalance ? frames : 1];
  3007. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3008. {
  3009. // Dry/Wet
  3010. if (doDryWet)
  3011. {
  3012. for (uint32_t k=0; k < frames; ++k)
  3013. {
  3014. #if 0 // TODO
  3015. if (k < pData->latency)
  3016. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  3017. else if (pData->latency < frames)
  3018. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  3019. else
  3020. #endif
  3021. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  3022. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  3023. }
  3024. }
  3025. // Balance
  3026. if (doBalance)
  3027. {
  3028. isPair = (i % 2 == 0);
  3029. if (isPair)
  3030. {
  3031. CARLA_ASSERT(i+1 < pData->audioOut.count);
  3032. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  3033. }
  3034. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  3035. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  3036. for (uint32_t k=0; k < frames; ++k)
  3037. {
  3038. if (isPair)
  3039. {
  3040. // left
  3041. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  3042. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  3043. }
  3044. else
  3045. {
  3046. // right
  3047. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  3048. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  3049. }
  3050. }
  3051. }
  3052. // Volume (and buffer copy)
  3053. {
  3054. for (uint32_t k=0; k < frames; ++k)
  3055. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  3056. }
  3057. }
  3058. } // End of Post-processing
  3059. #else // BUILD_BRIDGE
  3060. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3061. {
  3062. for (uint32_t k=0; k < frames; ++k)
  3063. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  3064. }
  3065. #endif
  3066. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3067. {
  3068. for (uint32_t k=0; k < frames; ++k)
  3069. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  3070. }
  3071. // --------------------------------------------------------------------------------------------------------
  3072. pData->singleMutex.unlock();
  3073. return true;
  3074. }
  3075. void bufferSizeChanged(const uint32_t newBufferSize) override
  3076. {
  3077. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  3078. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - start", newBufferSize);
  3079. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3080. {
  3081. if (fAudioInBuffers[i] != nullptr)
  3082. delete[] fAudioInBuffers[i];
  3083. fAudioInBuffers[i] = new float[newBufferSize];
  3084. }
  3085. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3086. {
  3087. if (fAudioOutBuffers[i] != nullptr)
  3088. delete[] fAudioOutBuffers[i];
  3089. fAudioOutBuffers[i] = new float[newBufferSize];
  3090. }
  3091. if (fHandle2 == nullptr)
  3092. {
  3093. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3094. {
  3095. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  3096. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  3097. }
  3098. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3099. {
  3100. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  3101. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  3102. }
  3103. }
  3104. else
  3105. {
  3106. if (pData->audioIn.count > 0)
  3107. {
  3108. CARLA_ASSERT(pData->audioIn.count == 2);
  3109. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  3110. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  3111. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  3112. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  3113. }
  3114. if (pData->audioOut.count > 0)
  3115. {
  3116. CARLA_ASSERT(pData->audioOut.count == 2);
  3117. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  3118. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  3119. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  3120. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  3121. }
  3122. }
  3123. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3124. {
  3125. if (fCvInBuffers[i] != nullptr)
  3126. delete[] fCvInBuffers[i];
  3127. fCvInBuffers[i] = new float[newBufferSize];
  3128. fDescriptor->connect_port(fHandle, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3129. if (fHandle2 != nullptr)
  3130. fDescriptor->connect_port(fHandle2, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3131. }
  3132. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3133. {
  3134. if (fCvOutBuffers[i] != nullptr)
  3135. delete[] fCvOutBuffers[i];
  3136. fCvOutBuffers[i] = new float[newBufferSize];
  3137. fDescriptor->connect_port(fHandle, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3138. if (fHandle2 != nullptr)
  3139. fDescriptor->connect_port(fHandle2, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3140. }
  3141. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  3142. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  3143. {
  3144. fLv2Options.maxBufferSize = fLv2Options.nominalBufferSize = newBufferSizeInt;
  3145. if (fLv2Options.minBufferSize != 1)
  3146. fLv2Options.minBufferSize = newBufferSizeInt;
  3147. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3148. {
  3149. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MaxBlockLenth]);
  3150. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::NominalBlockLenth]);
  3151. if (fLv2Options.minBufferSize != 1)
  3152. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MinBlockLenth]);
  3153. }
  3154. }
  3155. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - end", newBufferSize);
  3156. }
  3157. void sampleRateChanged(const double newSampleRate) override
  3158. {
  3159. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  3160. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - start", newSampleRate);
  3161. if (carla_isNotEqual(fLv2Options.sampleRate, newSampleRate))
  3162. {
  3163. fLv2Options.sampleRate = newSampleRate;
  3164. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3165. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::SampleRate]);
  3166. }
  3167. for (uint32_t k=0; k < pData->param.count; ++k)
  3168. {
  3169. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  3170. {
  3171. fParamBuffers[k] = static_cast<float>(newSampleRate);
  3172. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3173. break;
  3174. }
  3175. }
  3176. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - end", newSampleRate);
  3177. }
  3178. void offlineModeChanged(const bool isOffline) override
  3179. {
  3180. for (uint32_t k=0; k < pData->param.count; ++k)
  3181. {
  3182. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_FREEWHEEL)
  3183. {
  3184. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  3185. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3186. break;
  3187. }
  3188. }
  3189. }
  3190. // -------------------------------------------------------------------
  3191. // Plugin buffers
  3192. void initBuffers() const noexcept override
  3193. {
  3194. fEventsIn.initBuffers();
  3195. fEventsOut.initBuffers();
  3196. CarlaPlugin::initBuffers();
  3197. }
  3198. void clearBuffers() noexcept override
  3199. {
  3200. carla_debug("CarlaPluginLV2::clearBuffers() - start");
  3201. if (fAudioInBuffers != nullptr)
  3202. {
  3203. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3204. {
  3205. if (fAudioInBuffers[i] != nullptr)
  3206. {
  3207. delete[] fAudioInBuffers[i];
  3208. fAudioInBuffers[i] = nullptr;
  3209. }
  3210. }
  3211. delete[] fAudioInBuffers;
  3212. fAudioInBuffers = nullptr;
  3213. }
  3214. if (fAudioOutBuffers != nullptr)
  3215. {
  3216. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3217. {
  3218. if (fAudioOutBuffers[i] != nullptr)
  3219. {
  3220. delete[] fAudioOutBuffers[i];
  3221. fAudioOutBuffers[i] = nullptr;
  3222. }
  3223. }
  3224. delete[] fAudioOutBuffers;
  3225. fAudioOutBuffers = nullptr;
  3226. }
  3227. if (fCvInBuffers != nullptr)
  3228. {
  3229. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3230. {
  3231. if (fCvInBuffers[i] != nullptr)
  3232. {
  3233. delete[] fCvInBuffers[i];
  3234. fCvInBuffers[i] = nullptr;
  3235. }
  3236. }
  3237. delete[] fCvInBuffers;
  3238. fCvInBuffers = nullptr;
  3239. }
  3240. if (fCvOutBuffers != nullptr)
  3241. {
  3242. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3243. {
  3244. if (fCvOutBuffers[i] != nullptr)
  3245. {
  3246. delete[] fCvOutBuffers[i];
  3247. fCvOutBuffers[i] = nullptr;
  3248. }
  3249. }
  3250. delete[] fCvOutBuffers;
  3251. fCvOutBuffers = nullptr;
  3252. }
  3253. if (fParamBuffers != nullptr)
  3254. {
  3255. delete[] fParamBuffers;
  3256. fParamBuffers = nullptr;
  3257. }
  3258. fEventsIn.clear();
  3259. fEventsOut.clear();
  3260. CarlaPlugin::clearBuffers();
  3261. carla_debug("CarlaPluginLV2::clearBuffers() - end");
  3262. }
  3263. // -------------------------------------------------------------------
  3264. // Post-poned UI Stuff
  3265. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3266. {
  3267. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3268. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3269. if (fUI.type == UI::TYPE_BRIDGE)
  3270. {
  3271. if (fPipeServer.isPipeRunning())
  3272. fPipeServer.writeControlMessage(static_cast<uint32_t>(pData->param.data[index].rindex), value);
  3273. }
  3274. else
  3275. {
  3276. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && ! fNeedsUiClose)
  3277. {
  3278. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3279. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3280. }
  3281. }
  3282. }
  3283. void uiMidiProgramChange(const uint32_t index) noexcept override
  3284. {
  3285. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3286. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3287. if (fUI.type == UI::TYPE_BRIDGE)
  3288. {
  3289. if (fPipeServer.isPipeRunning())
  3290. fPipeServer.writeMidiProgramMessage(pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3291. }
  3292. else
  3293. {
  3294. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr && ! fNeedsUiClose)
  3295. fExt.uiprograms->select_program(fUI.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3296. }
  3297. }
  3298. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3299. {
  3300. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3301. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3302. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3303. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3304. if (fUI.type == UI::TYPE_BRIDGE)
  3305. {
  3306. if (fPipeServer.isPipeRunning())
  3307. fPipeServer.writeMidiNoteMessage(false, channel, note, velo);
  3308. }
  3309. else
  3310. {
  3311. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3312. {
  3313. LV2_Atom_MidiEvent midiEv;
  3314. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3315. midiEv.atom.size = 3;
  3316. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3317. midiEv.data[1] = note;
  3318. midiEv.data[2] = velo;
  3319. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, &midiEv);
  3320. }
  3321. }
  3322. }
  3323. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3324. {
  3325. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3326. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3327. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3328. if (fUI.type == UI::TYPE_BRIDGE)
  3329. {
  3330. if (fPipeServer.isPipeRunning())
  3331. fPipeServer.writeMidiNoteMessage(false, channel, note, 0);
  3332. }
  3333. else
  3334. {
  3335. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3336. {
  3337. LV2_Atom_MidiEvent midiEv;
  3338. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3339. midiEv.atom.size = 3;
  3340. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3341. midiEv.data[1] = note;
  3342. midiEv.data[2] = 0;
  3343. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, &midiEv);
  3344. }
  3345. }
  3346. }
  3347. // -------------------------------------------------------------------
  3348. bool isRealtimeSafe() const noexcept
  3349. {
  3350. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3351. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3352. {
  3353. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3354. return true;
  3355. }
  3356. return false;
  3357. }
  3358. bool needsFixedBuffer() const noexcept
  3359. {
  3360. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3361. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3362. {
  3363. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3364. return true;
  3365. }
  3366. return false;
  3367. }
  3368. // -------------------------------------------------------------------
  3369. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3370. {
  3371. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3372. #ifndef LV2_UIS_ONLY_INPROCESS
  3373. const LV2_RDF_UI* const rdfUI(&fRdfDescriptor->UIs[uiId]);
  3374. for (uint32_t i=0; i < rdfUI->FeatureCount; ++i)
  3375. {
  3376. const LV2_RDF_Feature& feat(rdfUI->Features[i]);
  3377. if (! feat.Required)
  3378. continue;
  3379. if (std::strcmp(feat.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3380. return false;
  3381. if (std::strcmp(feat.URI, LV2_DATA_ACCESS_URI) == 0)
  3382. return false;
  3383. }
  3384. // Calf UIs are mostly useless without their special graphs
  3385. // but they can be crashy under certain conditions, so follow user preferences
  3386. if (std::strstr(rdfUI->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3387. return pData->engine->getOptions().preferUiBridges;
  3388. return true;
  3389. #else
  3390. return false;
  3391. #endif
  3392. }
  3393. bool isUiResizable() const noexcept
  3394. {
  3395. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr, false);
  3396. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  3397. {
  3398. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3399. return false;
  3400. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3401. return false;
  3402. }
  3403. return true;
  3404. }
  3405. const char* getUiBridgeBinary(const LV2_Property type) const
  3406. {
  3407. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3408. if (bridgeBinary.isEmpty())
  3409. return nullptr;
  3410. switch (type)
  3411. {
  3412. case LV2_UI_GTK2:
  3413. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk2";
  3414. break;
  3415. case LV2_UI_GTK3:
  3416. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk3";
  3417. break;
  3418. case LV2_UI_QT4:
  3419. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt4";
  3420. break;
  3421. case LV2_UI_QT5:
  3422. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt5";
  3423. break;
  3424. case LV2_UI_COCOA:
  3425. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-cocoa";
  3426. break;
  3427. case LV2_UI_WINDOWS:
  3428. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-windows";
  3429. break;
  3430. case LV2_UI_X11:
  3431. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-x11";
  3432. break;
  3433. case LV2_UI_EXTERNAL:
  3434. case LV2_UI_OLD_EXTERNAL:
  3435. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-external";
  3436. break;
  3437. case LV2_UI_MOD:
  3438. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-modgui";
  3439. break;
  3440. default:
  3441. return nullptr;
  3442. }
  3443. #ifdef CARLA_OS_WIN
  3444. bridgeBinary += ".exe";
  3445. #endif
  3446. if (! File(bridgeBinary.buffer()).existsAsFile())
  3447. return nullptr;
  3448. return bridgeBinary.dup();
  3449. }
  3450. // -------------------------------------------------------------------
  3451. void recheckExtensions()
  3452. {
  3453. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3454. carla_debug("CarlaPluginLV2::recheckExtensions()");
  3455. fExt.options = nullptr;
  3456. fExt.programs = nullptr;
  3457. fExt.state = nullptr;
  3458. fExt.worker = nullptr;
  3459. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3460. {
  3461. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->Extensions[i] != nullptr);
  3462. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3463. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3464. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3465. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3466. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3467. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3468. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3469. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3470. else
  3471. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3472. }
  3473. if (fDescriptor->extension_data != nullptr)
  3474. {
  3475. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3476. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3477. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3478. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3479. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3480. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3481. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3482. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3483. // check if invalid
  3484. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3485. fExt.options = nullptr;
  3486. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3487. fExt.programs = nullptr;
  3488. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3489. fExt.state = nullptr;
  3490. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3491. fExt.worker = nullptr;
  3492. }
  3493. CARLA_SAFE_ASSERT_RETURN(fLatencyIndex == -1,);
  3494. int32_t iCtrl=0;
  3495. for (uint32_t i=0, count=fRdfDescriptor->PortCount; i<count; ++i)
  3496. {
  3497. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3498. if (! LV2_IS_PORT_CONTROL(portTypes))
  3499. continue;
  3500. const ScopedValueSetter<int32_t> svs(iCtrl, iCtrl, iCtrl+1);
  3501. if (! LV2_IS_PORT_OUTPUT(portTypes))
  3502. continue;
  3503. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  3504. if (! LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  3505. continue;
  3506. fLatencyIndex = iCtrl;
  3507. break;
  3508. }
  3509. }
  3510. // -------------------------------------------------------------------
  3511. void updateUi()
  3512. {
  3513. CARLA_SAFE_ASSERT_RETURN(fUI.handle != nullptr,);
  3514. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  3515. carla_debug("CarlaPluginLV2::updateUi()");
  3516. // update midi program
  3517. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3518. {
  3519. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3520. fExt.uiprograms->select_program(fUI.handle, curData.bank, curData.program);
  3521. }
  3522. // update control ports
  3523. if (fUI.descriptor->port_event != nullptr)
  3524. {
  3525. float value;
  3526. for (uint32_t i=0; i < pData->param.count; ++i)
  3527. {
  3528. value = getParameterValue(i);
  3529. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3530. }
  3531. }
  3532. }
  3533. // -------------------------------------------------------------------
  3534. LV2_URID getCustomURID(const char* const uri)
  3535. {
  3536. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3537. carla_debug("CarlaPluginLV2::getCustomURID(\"%s\")", uri);
  3538. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3539. {
  3540. const char* const thisUri(fCustomURIDs.getAt(i, nullptr));
  3541. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3542. return static_cast<LV2_URID>(i);
  3543. }
  3544. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3545. fCustomURIDs.append(carla_strdup(uri));
  3546. if (fUI.type == UI::TYPE_BRIDGE && fPipeServer.isPipeRunning())
  3547. fPipeServer.writeLv2UridMessage(urid, uri);
  3548. return urid;
  3549. }
  3550. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3551. {
  3552. static const char* const sFallback = "urn:null";
  3553. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, sFallback);
  3554. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), sFallback);
  3555. carla_debug("CarlaPluginLV2::getCustomURIString(%i)", urid);
  3556. return fCustomURIDs.getAt(urid, sFallback);
  3557. }
  3558. // -------------------------------------------------------------------
  3559. void handleProgramChanged(const int32_t index)
  3560. {
  3561. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3562. carla_debug("CarlaPluginLV2::handleProgramChanged(%i)", index);
  3563. if (index == -1)
  3564. {
  3565. const ScopedSingleProcessLocker spl(this, true);
  3566. return reloadPrograms(false);
  3567. }
  3568. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3569. {
  3570. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3571. {
  3572. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3573. if (pData->midiprog.data[index].name != nullptr)
  3574. delete[] pData->midiprog.data[index].name;
  3575. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3576. if (index == pData->midiprog.current)
  3577. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3578. else
  3579. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3580. }
  3581. }
  3582. }
  3583. // -------------------------------------------------------------------
  3584. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3585. {
  3586. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3587. carla_debug("CarlaPluginLV2::handleResizePort(%i, " P_SIZE ")", index, size);
  3588. // TODO
  3589. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3590. (void)index;
  3591. }
  3592. // -------------------------------------------------------------------
  3593. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3594. {
  3595. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3596. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3597. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3598. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3599. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3600. 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);
  3601. const char* const skey(carla_lv2_urid_unmap(this, key));
  3602. const char* const stype(carla_lv2_urid_unmap(this, type));
  3603. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3604. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3605. // Check if we already have this key
  3606. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  3607. {
  3608. CustomData& cData(it.getValue(kCustomDataFallbackNC));
  3609. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  3610. if (std::strcmp(cData.key, skey) == 0)
  3611. {
  3612. // found it
  3613. delete[] cData.value;
  3614. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3615. cData.value = carla_strdup((const char*)value);
  3616. else
  3617. cData.value = CarlaString::asBase64(value, size).dup();
  3618. return LV2_STATE_SUCCESS;
  3619. }
  3620. }
  3621. // Otherwise store it
  3622. CustomData newData;
  3623. newData.type = carla_strdup(stype);
  3624. newData.key = carla_strdup(skey);
  3625. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3626. newData.value = carla_strdup((const char*)value);
  3627. else
  3628. newData.value = CarlaString::asBase64(value, size).dup();
  3629. pData->custom.append(newData);
  3630. return LV2_STATE_SUCCESS;
  3631. }
  3632. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3633. {
  3634. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3635. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3636. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3637. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3638. carla_debug("CarlaPluginLV2::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3639. const char* const skey(carla_lv2_urid_unmap(this, key));
  3640. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3641. const char* stype = nullptr;
  3642. const char* stringData = nullptr;
  3643. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  3644. {
  3645. const CustomData& cData(it.getValue(kCustomDataFallback));
  3646. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  3647. if (std::strcmp(cData.key, skey) == 0)
  3648. {
  3649. stype = cData.type;
  3650. stringData = cData.value;
  3651. break;
  3652. }
  3653. }
  3654. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3655. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3656. *type = carla_lv2_urid_map(this, stype);
  3657. *flags = LV2_STATE_IS_POD;
  3658. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3659. {
  3660. *size = std::strlen(stringData);
  3661. return stringData;
  3662. }
  3663. else
  3664. {
  3665. if (fLastStateChunk != nullptr)
  3666. {
  3667. std::free(fLastStateChunk);
  3668. fLastStateChunk = nullptr;
  3669. }
  3670. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stringData));
  3671. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3672. fLastStateChunk = std::malloc(chunk.size());
  3673. CARLA_SAFE_ASSERT_RETURN(fLastStateChunk != nullptr, nullptr);
  3674. std::memcpy(fLastStateChunk, chunk.data(), chunk.size());
  3675. *size = chunk.size();
  3676. return fLastStateChunk;
  3677. }
  3678. }
  3679. // -------------------------------------------------------------------
  3680. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3681. {
  3682. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3683. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3684. carla_debug("CarlaPluginLV2::handleWorkerSchedule(%i, %p)", size, data);
  3685. if (pData->engine->isOffline())
  3686. {
  3687. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3688. return LV2_WORKER_SUCCESS;
  3689. }
  3690. LV2_Atom atom;
  3691. atom.size = size;
  3692. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3693. return fAtomBufferOut.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3694. }
  3695. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3696. {
  3697. carla_debug("CarlaPluginLV2::handleWorkerRespond(%i, %p)", size, data);
  3698. LV2_Atom atom;
  3699. atom.size = size;
  3700. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3701. return fAtomBufferIn.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3702. }
  3703. // -------------------------------------------------------------------
  3704. void handleExternalUIClosed()
  3705. {
  3706. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EXTERNAL,);
  3707. carla_debug("CarlaPluginLV2::handleExternalUIClosed()");
  3708. fNeedsUiClose = true;
  3709. }
  3710. void handlePluginUIClosed() override
  3711. {
  3712. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  3713. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  3714. carla_debug("CarlaPluginLV2::handlePluginUIClosed()");
  3715. fNeedsUiClose = true;
  3716. }
  3717. void handlePluginUIResized(const uint width, const uint height) override
  3718. {
  3719. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  3720. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  3721. carla_debug("CarlaPluginLV2::handlePluginUIResized(%u, %u)", width, height);
  3722. if (fUI.handle != nullptr && fExt.uiresize != nullptr)
  3723. fExt.uiresize->ui_resize(fUI.handle, static_cast<int>(width), static_cast<int>(height));
  3724. }
  3725. // -------------------------------------------------------------------
  3726. uint32_t handleUIPortMap(const char* const symbol) const noexcept
  3727. {
  3728. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3729. carla_debug("CarlaPluginLV2::handleUIPortMap(\"%s\")", symbol);
  3730. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3731. {
  3732. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3733. return i;
  3734. }
  3735. return LV2UI_INVALID_PORT_INDEX;
  3736. }
  3737. int handleUIResize(const int width, const int height)
  3738. {
  3739. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr, 1);
  3740. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3741. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3742. carla_debug("CarlaPluginLV2::handleUIResize(%i, %i)", width, height);
  3743. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3744. return 0;
  3745. }
  3746. void handleUIWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3747. {
  3748. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3749. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3750. carla_debug("CarlaPluginLV2::handleUIWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3751. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  3752. switch (format)
  3753. {
  3754. case CARLA_URI_MAP_ID_NULL: {
  3755. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3756. for (uint32_t i=0; i < pData->param.count; ++i)
  3757. {
  3758. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  3759. continue;
  3760. index = i;
  3761. break;
  3762. }
  3763. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  3764. const float value(*(const float*)buffer);
  3765. //if (carla_isNotEqual(fParamBuffers[index], value))
  3766. setParameterValue(index, value, false, true, true);
  3767. } break;
  3768. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3769. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT: {
  3770. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  3771. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  3772. // plugins sometimes fail on this, not good...
  3773. CARLA_SAFE_ASSERT_INT2(bufferSize == lv2_atom_total_size(atom), bufferSize, atom->size);
  3774. for (uint32_t i=0; i < fEventsIn.count; ++i)
  3775. {
  3776. if (fEventsIn.data[i].rindex != rindex)
  3777. continue;
  3778. index = i;
  3779. break;
  3780. }
  3781. // for bad plugins
  3782. if (index == LV2UI_INVALID_PORT_INDEX)
  3783. {
  3784. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  3785. index = fEventsIn.ctrlIndex;
  3786. }
  3787. fAtomBufferIn.put(atom, index);
  3788. } break;
  3789. default:
  3790. carla_stdout("CarlaPluginLV2::handleUIWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3791. break;
  3792. }
  3793. }
  3794. // -------------------------------------------------------------------
  3795. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3796. {
  3797. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3798. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3799. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3800. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3801. carla_debug("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3802. int32_t rindex = -1;
  3803. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3804. {
  3805. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3806. {
  3807. rindex = static_cast<int32_t>(i);
  3808. break;
  3809. }
  3810. }
  3811. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3812. float paramValue;
  3813. switch (type)
  3814. {
  3815. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3816. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3817. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3818. break;
  3819. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3820. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3821. paramValue = static_cast<float>((*(const double*)value));
  3822. break;
  3823. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3824. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3825. paramValue = (*(const float*)value);
  3826. break;
  3827. case CARLA_URI_MAP_ID_ATOM_INT:
  3828. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3829. paramValue = static_cast<float>((*(const int32_t*)value));
  3830. break;
  3831. case CARLA_URI_MAP_ID_ATOM_LONG:
  3832. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3833. paramValue = static_cast<float>((*(const int64_t*)value));
  3834. break;
  3835. default:
  3836. carla_stdout("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3837. return;
  3838. }
  3839. for (uint32_t i=0; i < pData->param.count; ++i)
  3840. {
  3841. if (pData->param.data[i].rindex == rindex)
  3842. {
  3843. setParameterValue(i, paramValue, true, true, true);
  3844. break;
  3845. }
  3846. }
  3847. }
  3848. // -------------------------------------------------------------------
  3849. void* getNativeHandle() const noexcept override
  3850. {
  3851. return fHandle;
  3852. }
  3853. const void* getNativeDescriptor() const noexcept override
  3854. {
  3855. return fDescriptor;
  3856. }
  3857. uintptr_t getUiBridgeProcessId() const noexcept override
  3858. {
  3859. return fPipeServer.isPipeRunning() ? fPipeServer.getPID() : 0;
  3860. }
  3861. // -------------------------------------------------------------------
  3862. public:
  3863. bool init(const char* const name, const char* const uri, const uint options)
  3864. {
  3865. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3866. // ---------------------------------------------------------------
  3867. // first checks
  3868. if (pData->client != nullptr)
  3869. {
  3870. pData->engine->setLastError("Plugin client is already registered");
  3871. return false;
  3872. }
  3873. if (uri == nullptr || uri[0] == '\0')
  3874. {
  3875. pData->engine->setLastError("null uri");
  3876. return false;
  3877. }
  3878. // ---------------------------------------------------------------
  3879. // Init LV2 World if needed, sets LV2_PATH for lilv
  3880. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3881. if (pData->engine->getOptions().pathLV2 != nullptr && pData->engine->getOptions().pathLV2[0] != '\0')
  3882. lv2World.initIfNeeded(pData->engine->getOptions().pathLV2);
  3883. else if (const char* const LV2_PATH = std::getenv("LV2_PATH"))
  3884. lv2World.initIfNeeded(LV2_PATH);
  3885. else
  3886. lv2World.initIfNeeded(LILV_DEFAULT_LV2_PATH);
  3887. // ---------------------------------------------------------------
  3888. // get plugin from lv2_rdf (lilv)
  3889. fRdfDescriptor = lv2_rdf_new(uri, true);
  3890. if (fRdfDescriptor == nullptr)
  3891. {
  3892. pData->engine->setLastError("Failed to find the requested plugin");
  3893. return false;
  3894. }
  3895. // ---------------------------------------------------------------
  3896. // open DLL
  3897. if (! pData->libOpen(fRdfDescriptor->Binary))
  3898. {
  3899. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3900. return false;
  3901. }
  3902. // ---------------------------------------------------------------
  3903. // try to get DLL main entry via new mode
  3904. if (const LV2_Lib_Descriptor_Function libDescFn = pData->libSymbol<LV2_Lib_Descriptor_Function>("lv2_lib_descriptor"))
  3905. {
  3906. // -----------------------------------------------------------
  3907. // all ok, get lib descriptor
  3908. const LV2_Lib_Descriptor* const libDesc = libDescFn(fRdfDescriptor->Bundle, nullptr);
  3909. if (libDesc == nullptr)
  3910. {
  3911. pData->engine->setLastError("Could not find the LV2 Descriptor");
  3912. return false;
  3913. }
  3914. // -----------------------------------------------------------
  3915. // get descriptor that matches URI (new mode)
  3916. uint32_t i = 0;
  3917. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3918. {
  3919. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3920. break;
  3921. }
  3922. }
  3923. else
  3924. {
  3925. // -----------------------------------------------------------
  3926. // get DLL main entry (old mode)
  3927. const LV2_Descriptor_Function descFn = pData->libSymbol<LV2_Descriptor_Function>("lv2_descriptor");
  3928. if (descFn == nullptr)
  3929. {
  3930. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3931. return false;
  3932. }
  3933. // -----------------------------------------------------------
  3934. // get descriptor that matches URI (old mode)
  3935. uint32_t i = 0;
  3936. while ((fDescriptor = descFn(i++)))
  3937. {
  3938. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3939. break;
  3940. }
  3941. }
  3942. if (fDescriptor == nullptr)
  3943. {
  3944. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3945. return false;
  3946. }
  3947. // ---------------------------------------------------------------
  3948. // check supported port-types and features
  3949. bool canContinue = true;
  3950. // Check supported ports
  3951. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3952. {
  3953. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3954. if (! is_lv2_port_supported(portTypes))
  3955. {
  3956. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3957. {
  3958. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3959. canContinue = false;
  3960. break;
  3961. }
  3962. }
  3963. }
  3964. // Check supported features
  3965. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3966. {
  3967. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[j]);
  3968. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3969. {
  3970. carla_stderr("Plugin DSP wants UI feature '%s', ignoring this", feature.URI);
  3971. }
  3972. else if (feature.Required && ! is_lv2_feature_supported(feature.URI))
  3973. {
  3974. CarlaString msg("Plugin wants a feature that is not supported:\n");
  3975. msg += feature.URI;
  3976. canContinue = false;
  3977. pData->engine->setLastError(msg);
  3978. break;
  3979. }
  3980. }
  3981. if (! canContinue)
  3982. {
  3983. // error already set
  3984. return false;
  3985. }
  3986. // ---------------------------------------------------------------
  3987. // get info
  3988. if (name != nullptr && name[0] != '\0')
  3989. pData->name = pData->engine->getUniquePluginName(name);
  3990. else
  3991. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3992. // ---------------------------------------------------------------
  3993. // register client
  3994. pData->client = pData->engine->addClient(this);
  3995. if (pData->client == nullptr || ! pData->client->isOk())
  3996. {
  3997. pData->engine->setLastError("Failed to register plugin client");
  3998. return false;
  3999. }
  4000. // ---------------------------------------------------------------
  4001. // initialize options
  4002. fLv2Options.minBufferSize = 1;
  4003. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  4004. fLv2Options.nominalBufferSize = fLv2Options.maxBufferSize;
  4005. fLv2Options.sampleRate = pData->engine->getSampleRate();
  4006. fLv2Options.frontendWinId = static_cast<int64_t>(pData->engine->getOptions().frontendWinId);
  4007. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  4008. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  4009. {
  4010. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  4011. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  4012. {
  4013. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  4014. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  4015. }
  4016. }
  4017. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  4018. // ---------------------------------------------------------------
  4019. // initialize features (part 1)
  4020. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  4021. eventFt->callback_data = this;
  4022. eventFt->lv2_event_ref = carla_lv2_event_ref;
  4023. eventFt->lv2_event_unref = carla_lv2_event_unref;
  4024. LV2_Log_Log* const logFt = new LV2_Log_Log;
  4025. logFt->handle = this;
  4026. logFt->printf = carla_lv2_log_printf;
  4027. logFt->vprintf = carla_lv2_log_vprintf;
  4028. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  4029. stateMakePathFt->handle = this;
  4030. stateMakePathFt->path = carla_lv2_state_make_path;
  4031. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  4032. stateMapPathFt->handle = this;
  4033. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  4034. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  4035. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  4036. programsFt->handle = this;
  4037. programsFt->program_changed = carla_lv2_program_changed;
  4038. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  4039. rsPortFt->data = this;
  4040. rsPortFt->resize = carla_lv2_resize_port;
  4041. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  4042. lv2_rtmempool_init(rtMemPoolFt);
  4043. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  4044. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  4045. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  4046. uriMapFt->callback_data = this;
  4047. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  4048. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  4049. uridMapFt->handle = this;
  4050. uridMapFt->map = carla_lv2_urid_map;
  4051. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  4052. uridUnmapFt->handle = this;
  4053. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  4054. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  4055. workerFt->handle = this;
  4056. workerFt->schedule_work = carla_lv2_worker_schedule;
  4057. // ---------------------------------------------------------------
  4058. // initialize features (part 2)
  4059. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  4060. fFeatures[j] = new LV2_Feature;
  4061. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  4062. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  4063. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  4064. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  4065. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  4066. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  4067. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  4068. fFeatures[kFeatureIdEvent]->data = eventFt;
  4069. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  4070. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  4071. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  4072. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  4073. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  4074. fFeatures[kFeatureIdIsLive]->data = nullptr;
  4075. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  4076. fFeatures[kFeatureIdLogs]->data = logFt;
  4077. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  4078. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  4079. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  4080. fFeatures[kFeatureIdPrograms]->data = programsFt;
  4081. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  4082. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  4083. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  4084. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  4085. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  4086. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  4087. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  4088. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  4089. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  4090. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  4091. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  4092. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  4093. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  4094. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  4095. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  4096. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  4097. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  4098. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  4099. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  4100. fFeatures[kFeatureIdWorker]->data = workerFt;
  4101. // if a fixed buffer is not needed, skip its feature
  4102. if (! needsFixedBuffer())
  4103. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  4104. else
  4105. fLv2Options.minBufferSize = fLv2Options.maxBufferSize;
  4106. // if power of 2 is not possible, skip its feature FIXME
  4107. //if (fLv2Options.maxBufferSize)
  4108. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  4109. // ---------------------------------------------------------------
  4110. // initialize plugin
  4111. try {
  4112. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  4113. } catch(...) {}
  4114. if (fHandle == nullptr)
  4115. {
  4116. pData->engine->setLastError("Plugin failed to initialize");
  4117. return false;
  4118. }
  4119. if (std::strcmp(uri, "http://hyperglitch.com/dev/VocProc") == 0)
  4120. fCanInit2 = false;
  4121. recheckExtensions();
  4122. // ---------------------------------------------------------------
  4123. // set default options
  4124. pData->options = 0x0;
  4125. if (fExt.programs != nullptr && getCategory() == PLUGIN_CATEGORY_SYNTH)
  4126. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  4127. if (fLatencyIndex >= 0 || getMidiInCount() > 0 || needsFixedBuffer())
  4128. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4129. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  4130. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4131. if (fCanInit2)
  4132. {
  4133. if (pData->engine->getOptions().forceStereo)
  4134. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4135. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  4136. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4137. }
  4138. if (getMidiInCount() > 0)
  4139. {
  4140. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  4141. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  4142. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  4143. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  4144. }
  4145. // ---------------------------------------------------------------
  4146. // gui stuff
  4147. if (fRdfDescriptor->UICount != 0)
  4148. initUi();
  4149. return true;
  4150. }
  4151. // -------------------------------------------------------------------
  4152. void initUi()
  4153. {
  4154. // ---------------------------------------------------------------
  4155. // find the most appropriate ui
  4156. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, eMod, iCocoa, iWindows, iX11, iExt, iFinal;
  4157. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = eMod = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  4158. #if defined(BUILD_BRIDGE)
  4159. const bool preferUiBridges(false);
  4160. #elif defined(LV2_UIS_ONLY_BRIDGES)
  4161. const bool preferUiBridges(true);
  4162. #else
  4163. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  4164. #endif
  4165. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  4166. {
  4167. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  4168. const int ii(static_cast<int>(i));
  4169. switch (fRdfDescriptor->UIs[i].Type)
  4170. {
  4171. case LV2_UI_QT4:
  4172. if (isUiBridgeable(i))
  4173. eQt4 = ii;
  4174. break;
  4175. case LV2_UI_QT5:
  4176. if (isUiBridgeable(i))
  4177. eQt5 = ii;
  4178. break;
  4179. case LV2_UI_GTK2:
  4180. if (isUiBridgeable(i))
  4181. eGtk2 = ii;
  4182. break;
  4183. case LV2_UI_GTK3:
  4184. if (isUiBridgeable(i))
  4185. eGtk3 = ii;
  4186. break;
  4187. case LV2_UI_COCOA:
  4188. if (isUiBridgeable(i) && preferUiBridges)
  4189. eCocoa = ii;
  4190. iCocoa = ii;
  4191. break;
  4192. case LV2_UI_WINDOWS:
  4193. if (isUiBridgeable(i) && preferUiBridges)
  4194. eWindows = ii;
  4195. iWindows = ii;
  4196. break;
  4197. case LV2_UI_X11:
  4198. if (isUiBridgeable(i) && preferUiBridges)
  4199. eX11 = ii;
  4200. iX11 = ii;
  4201. break;
  4202. case LV2_UI_EXTERNAL:
  4203. case LV2_UI_OLD_EXTERNAL:
  4204. if (isUiBridgeable(i))
  4205. eExt = ii;
  4206. iExt = ii;
  4207. break;
  4208. case LV2_UI_MOD:
  4209. eMod = ii;
  4210. break;
  4211. default:
  4212. break;
  4213. }
  4214. }
  4215. if (eQt4 >= 0)
  4216. iFinal = eQt4;
  4217. else if (eQt5 >= 0)
  4218. iFinal = eQt5;
  4219. else if (eGtk2 >= 0)
  4220. iFinal = eGtk2;
  4221. else if (eGtk3 >= 0)
  4222. iFinal = eGtk3;
  4223. #ifdef CARLA_OS_MAC
  4224. else if (eCocoa >= 0)
  4225. iFinal = eCocoa;
  4226. #endif
  4227. #ifdef CARLA_OS_WIN
  4228. else if (eWindows >= 0)
  4229. iFinal = eWindows;
  4230. #endif
  4231. #ifdef HAVE_X11
  4232. else if (eX11 >= 0)
  4233. iFinal = eX11;
  4234. #endif
  4235. //else if (eExt >= 0) // TODO
  4236. // iFinal = eExt;
  4237. #ifndef LV2_UIS_ONLY_BRIDGES
  4238. # ifdef CARLA_OS_MAC
  4239. else if (iCocoa >= 0)
  4240. iFinal = iCocoa;
  4241. # endif
  4242. # ifdef CARLA_OS_WIN
  4243. else if (iWindows >= 0)
  4244. iFinal = iWindows;
  4245. # endif
  4246. # ifdef HAVE_X11
  4247. else if (iX11 >= 0)
  4248. iFinal = iX11;
  4249. # endif
  4250. #endif
  4251. else if (iExt >= 0)
  4252. iFinal = iExt;
  4253. if (iFinal < 0)
  4254. {
  4255. // no suitable UI found, see if there's one which supports ui:showInterface
  4256. bool hasShowInterface = false;
  4257. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  4258. {
  4259. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  4260. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  4261. {
  4262. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  4263. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  4264. continue;
  4265. iFinal = static_cast<int>(i);
  4266. hasShowInterface = true;
  4267. break;
  4268. }
  4269. }
  4270. if (! hasShowInterface)
  4271. {
  4272. if (eMod < 0)
  4273. {
  4274. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  4275. return;
  4276. }
  4277. // use MODGUI as last resort
  4278. iFinal = eMod;
  4279. }
  4280. }
  4281. fUI.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  4282. // ---------------------------------------------------------------
  4283. // check supported ui features
  4284. bool canContinue = true;
  4285. bool canDelete = true;
  4286. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  4287. {
  4288. const char* const uri(fUI.rdfDescriptor->Features[i].URI);
  4289. CARLA_SAFE_ASSERT_CONTINUE(uri != nullptr && uri[0] != '\0');
  4290. if (! is_lv2_ui_feature_supported(uri))
  4291. {
  4292. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", uri);
  4293. if (fUI.rdfDescriptor->Features[i].Required)
  4294. {
  4295. canContinue = false;
  4296. break;
  4297. }
  4298. }
  4299. if (std::strcmp(uri, LV2_UI__makeResident) == 0 || std::strcmp(uri, LV2_UI__makeSONameResident) == 0)
  4300. canDelete = false;
  4301. }
  4302. if (! canContinue)
  4303. {
  4304. fUI.rdfDescriptor = nullptr;
  4305. return;
  4306. }
  4307. // ---------------------------------------------------------------
  4308. // initialize ui according to type
  4309. const LV2_Property uiType(fUI.rdfDescriptor->Type);
  4310. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 ||
  4311. iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt || iFinal == eMod)
  4312. {
  4313. // -----------------------------------------------------------
  4314. // initialize ui-bridge
  4315. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  4316. {
  4317. carla_stdout("Will use UI-Bridge, binary: \"%s\"", bridgeBinary);
  4318. CarlaString guiTitle(pData->name);
  4319. guiTitle += " (GUI)";
  4320. fLv2Options.windowTitle = guiTitle.dup();
  4321. fUI.type = UI::TYPE_BRIDGE;
  4322. fPipeServer.setData(bridgeBinary, fRdfDescriptor->URI, fUI.rdfDescriptor->URI);
  4323. delete[] bridgeBinary;
  4324. return;
  4325. }
  4326. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3)
  4327. {
  4328. carla_stderr2("Failed to find UI bridge binary, cannot use UI");
  4329. fUI.rdfDescriptor = nullptr;
  4330. return;
  4331. }
  4332. }
  4333. #ifdef LV2_UIS_ONLY_BRIDGES
  4334. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  4335. fUI.rdfDescriptor = nullptr;
  4336. return;
  4337. #endif
  4338. // ---------------------------------------------------------------
  4339. // open UI DLL
  4340. if (! pData->uiLibOpen(fUI.rdfDescriptor->Binary, canDelete))
  4341. {
  4342. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUI.rdfDescriptor->Binary));
  4343. fUI.rdfDescriptor = nullptr;
  4344. return;
  4345. }
  4346. // ---------------------------------------------------------------
  4347. // get UI DLL main entry
  4348. LV2UI_DescriptorFunction uiDescFn = pData->uiLibSymbol<LV2UI_DescriptorFunction>("lv2ui_descriptor");
  4349. if (uiDescFn == nullptr)
  4350. {
  4351. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  4352. pData->uiLibClose();
  4353. fUI.rdfDescriptor = nullptr;
  4354. return;
  4355. }
  4356. // ---------------------------------------------------------------
  4357. // get UI descriptor that matches UI URI
  4358. uint32_t i = 0;
  4359. while ((fUI.descriptor = uiDescFn(i++)))
  4360. {
  4361. if (std::strcmp(fUI.descriptor->URI, fUI.rdfDescriptor->URI) == 0)
  4362. break;
  4363. }
  4364. if (fUI.descriptor == nullptr)
  4365. {
  4366. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  4367. pData->uiLibClose();
  4368. fUI.rdfDescriptor = nullptr;
  4369. return;
  4370. }
  4371. // ---------------------------------------------------------------
  4372. // check if ui is usable
  4373. switch (uiType)
  4374. {
  4375. case LV2_UI_QT4:
  4376. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  4377. fUI.type = UI::TYPE_EMBED;
  4378. break;
  4379. case LV2_UI_QT5:
  4380. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  4381. fUI.type = UI::TYPE_EMBED;
  4382. break;
  4383. case LV2_UI_GTK2:
  4384. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  4385. fUI.type = UI::TYPE_EMBED;
  4386. break;
  4387. case LV2_UI_GTK3:
  4388. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  4389. fUI.type = UI::TYPE_EMBED;
  4390. break;
  4391. #ifdef CARLA_OS_MAC
  4392. case LV2_UI_COCOA:
  4393. carla_stdout("Will use LV2 Cocoa UI");
  4394. fUI.type = UI::TYPE_EMBED;
  4395. break;
  4396. #endif
  4397. #ifdef CARLA_OS_WIN
  4398. case LV2_UI_WINDOWS:
  4399. carla_stdout("Will use LV2 Windows UI");
  4400. fUI.type = UI::TYPE_EMBED;
  4401. break;
  4402. #endif
  4403. case LV2_UI_X11:
  4404. #ifdef HAVE_X11
  4405. carla_stdout("Will use LV2 X11 UI");
  4406. #else
  4407. carla_stdout("Will use LV2 X11 UI, NOT!");
  4408. #endif
  4409. fUI.type = UI::TYPE_EMBED;
  4410. break;
  4411. case LV2_UI_EXTERNAL:
  4412. case LV2_UI_OLD_EXTERNAL:
  4413. carla_stdout("Will use LV2 External UI");
  4414. fUI.type = UI::TYPE_EXTERNAL;
  4415. break;
  4416. }
  4417. if (fUI.type == UI::TYPE_NULL)
  4418. {
  4419. pData->uiLibClose();
  4420. fUI.descriptor = nullptr;
  4421. fUI.rdfDescriptor = nullptr;
  4422. return;
  4423. }
  4424. // ---------------------------------------------------------------
  4425. // initialize ui data
  4426. CarlaString guiTitle(pData->name);
  4427. guiTitle += " (GUI)";
  4428. fLv2Options.windowTitle = guiTitle.dup();
  4429. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  4430. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].value = fLv2Options.windowTitle;
  4431. // ---------------------------------------------------------------
  4432. // initialize ui features (part 1)
  4433. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  4434. uiDataFt->data_access = fDescriptor->extension_data;
  4435. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  4436. uiPortMapFt->handle = this;
  4437. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  4438. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  4439. uiResizeFt->handle = this;
  4440. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  4441. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  4442. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  4443. uiExternalHostFt->plugin_human_id = fLv2Options.windowTitle;
  4444. // ---------------------------------------------------------------
  4445. // initialize ui features (part 2)
  4446. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  4447. fFeatures[j] = new LV2_Feature;
  4448. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  4449. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  4450. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  4451. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  4452. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  4453. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  4454. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  4455. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  4456. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  4457. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  4458. fFeatures[kFeatureIdUiMakeResident2]->URI = LV2_UI__makeSONameResident;
  4459. fFeatures[kFeatureIdUiMakeResident2]->data = nullptr;
  4460. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  4461. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  4462. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  4463. fFeatures[kFeatureIdUiParent]->data = nullptr;
  4464. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  4465. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  4466. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  4467. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  4468. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  4469. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  4470. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  4471. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  4472. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  4473. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  4474. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  4475. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  4476. // ---------------------------------------------------------------
  4477. // initialize ui extensions
  4478. if (fUI.descriptor->extension_data == nullptr)
  4479. return;
  4480. fExt.uiidle = (const LV2UI_Idle_Interface*)fUI.descriptor->extension_data(LV2_UI__idleInterface);
  4481. fExt.uishow = (const LV2UI_Show_Interface*)fUI.descriptor->extension_data(LV2_UI__showInterface);
  4482. fExt.uiresize = (const LV2UI_Resize*)fUI.descriptor->extension_data(LV2_UI__resize);
  4483. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUI.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  4484. // check if invalid
  4485. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  4486. fExt.uiidle = nullptr;
  4487. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  4488. fExt.uishow = nullptr;
  4489. if (fExt.uiresize != nullptr && fExt.uiresize->ui_resize == nullptr)
  4490. fExt.uiresize = nullptr;
  4491. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  4492. fExt.uiprograms = nullptr;
  4493. // don't use uiidle if external
  4494. if (fUI.type == UI::TYPE_EXTERNAL)
  4495. fExt.uiidle = nullptr;
  4496. }
  4497. // -------------------------------------------------------------------
  4498. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  4499. {
  4500. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  4501. carla_debug("CarlaPluginLV2::handleTransferAtom(%i, %p)", portIndex, atom);
  4502. fAtomBufferIn.put(atom, portIndex);
  4503. }
  4504. void handleUridMap(const LV2_URID urid, const char* const uri)
  4505. {
  4506. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  4507. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  4508. carla_debug("CarlaPluginLV2::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count()-1, uri);
  4509. if (urid < fCustomURIDs.count())
  4510. {
  4511. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  4512. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr,);
  4513. if (std::strcmp(ourURI, uri) != 0)
  4514. {
  4515. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  4516. }
  4517. }
  4518. else
  4519. {
  4520. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  4521. fCustomURIDs.append(carla_strdup(uri));
  4522. }
  4523. }
  4524. // -------------------------------------------------------------------
  4525. private:
  4526. LV2_Handle fHandle;
  4527. LV2_Handle fHandle2;
  4528. LV2_Feature* fFeatures[kFeatureCountAll+1];
  4529. const LV2_Descriptor* fDescriptor;
  4530. const LV2_RDF_Descriptor* fRdfDescriptor;
  4531. float** fAudioInBuffers;
  4532. float** fAudioOutBuffers;
  4533. float** fCvInBuffers;
  4534. float** fCvOutBuffers;
  4535. float* fParamBuffers;
  4536. bool fCanInit2; // some plugins don't like 2 instances
  4537. bool fNeedsUiClose;
  4538. //bool fLatencyChanged;
  4539. int32_t fLatencyIndex; // -1 if invalid
  4540. Lv2AtomRingBuffer fAtomBufferIn;
  4541. Lv2AtomRingBuffer fAtomBufferOut;
  4542. LV2_Atom_Forge fAtomForge;
  4543. CarlaPluginLV2EventData fEventsIn;
  4544. CarlaPluginLV2EventData fEventsOut;
  4545. CarlaPluginLV2Options fLv2Options;
  4546. CarlaPipeServerLV2 fPipeServer;
  4547. LinkedList<const char*> fCustomURIDs;
  4548. bool fFirstActive; // first process() call after activate()
  4549. void* fLastStateChunk;
  4550. EngineTimeInfo fLastTimeInfo;
  4551. struct Extensions {
  4552. const LV2_Options_Interface* options;
  4553. const LV2_State_Interface* state;
  4554. const LV2_Worker_Interface* worker;
  4555. const LV2_Programs_Interface* programs;
  4556. const LV2UI_Idle_Interface* uiidle;
  4557. const LV2UI_Show_Interface* uishow;
  4558. const LV2UI_Resize* uiresize;
  4559. const LV2_Programs_UI_Interface* uiprograms;
  4560. Extensions()
  4561. : options(nullptr),
  4562. state(nullptr),
  4563. worker(nullptr),
  4564. programs(nullptr),
  4565. uiidle(nullptr),
  4566. uishow(nullptr),
  4567. uiresize(nullptr),
  4568. uiprograms(nullptr) {}
  4569. CARLA_DECLARE_NON_COPY_STRUCT(Extensions);
  4570. } fExt;
  4571. struct UI {
  4572. enum Type {
  4573. TYPE_NULL = 0,
  4574. TYPE_BRIDGE,
  4575. TYPE_EMBED,
  4576. TYPE_EXTERNAL
  4577. };
  4578. Type type;
  4579. LV2UI_Handle handle;
  4580. LV2UI_Widget widget;
  4581. const LV2UI_Descriptor* descriptor;
  4582. const LV2_RDF_UI* rdfDescriptor;
  4583. CarlaPluginUI* window;
  4584. UI()
  4585. : type(TYPE_NULL),
  4586. handle(nullptr),
  4587. widget(nullptr),
  4588. descriptor(nullptr),
  4589. rdfDescriptor(nullptr),
  4590. window(nullptr) {}
  4591. ~UI()
  4592. {
  4593. CARLA_ASSERT(handle == nullptr);
  4594. CARLA_ASSERT(widget == nullptr);
  4595. CARLA_ASSERT(descriptor == nullptr);
  4596. CARLA_ASSERT(rdfDescriptor == nullptr);
  4597. CARLA_ASSERT(window == nullptr);
  4598. }
  4599. CARLA_DECLARE_NON_COPY_STRUCT(UI);
  4600. } fUI;
  4601. // -------------------------------------------------------------------
  4602. // Event Feature
  4603. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4604. {
  4605. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4606. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4607. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4608. return 0;
  4609. }
  4610. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4611. {
  4612. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4613. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4614. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4615. return 0;
  4616. }
  4617. // -------------------------------------------------------------------
  4618. // Logs Feature
  4619. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4620. {
  4621. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4622. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4623. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4624. #ifndef DEBUG
  4625. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4626. return 0;
  4627. #endif
  4628. va_list args;
  4629. va_start(args, fmt);
  4630. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4631. va_end(args);
  4632. return ret;
  4633. }
  4634. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4635. {
  4636. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4637. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4638. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4639. #ifndef DEBUG
  4640. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4641. return 0;
  4642. #endif
  4643. int ret = 0;
  4644. switch (type)
  4645. {
  4646. case CARLA_URI_MAP_ID_LOG_ERROR:
  4647. std::fprintf(stderr, "\x1b[31m");
  4648. ret = std::vfprintf(stderr, fmt, ap);
  4649. std::fprintf(stderr, "\x1b[0m");
  4650. break;
  4651. case CARLA_URI_MAP_ID_LOG_NOTE:
  4652. ret = std::vfprintf(stdout, fmt, ap);
  4653. break;
  4654. case CARLA_URI_MAP_ID_LOG_TRACE:
  4655. #ifdef DEBUG
  4656. std::fprintf(stdout, "\x1b[30;1m");
  4657. ret = std::vfprintf(stdout, fmt, ap);
  4658. std::fprintf(stdout, "\x1b[0m");
  4659. #endif
  4660. break;
  4661. case CARLA_URI_MAP_ID_LOG_WARNING:
  4662. ret = std::vfprintf(stderr, fmt, ap);
  4663. break;
  4664. default:
  4665. break;
  4666. }
  4667. return ret;
  4668. }
  4669. // -------------------------------------------------------------------
  4670. // Programs Feature
  4671. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4672. {
  4673. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4674. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4675. ((CarlaPluginLV2*)handle)->handleProgramChanged(index);
  4676. }
  4677. // -------------------------------------------------------------------
  4678. // Resize Port Feature
  4679. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4680. {
  4681. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4682. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4683. return ((CarlaPluginLV2*)data)->handleResizePort(index, size);
  4684. }
  4685. // -------------------------------------------------------------------
  4686. // State Feature
  4687. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4688. {
  4689. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4690. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4691. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4692. File file;
  4693. if (File::isAbsolutePath(path))
  4694. file = File(path);
  4695. else
  4696. file = File::getCurrentWorkingDirectory().getChildFile(path);
  4697. file.getParentDirectory().createDirectory();
  4698. return strdup(file.getFullPathName().toRawUTF8());
  4699. }
  4700. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4701. {
  4702. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, strdup(""));
  4703. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', strdup(""));
  4704. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4705. // may already be an abstract path
  4706. if (! File::isAbsolutePath(absolute_path))
  4707. return strdup(absolute_path);
  4708. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  4709. }
  4710. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4711. {
  4712. const char* const cwd(File::getCurrentWorkingDirectory().getFullPathName().toRawUTF8());
  4713. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, strdup(cwd));
  4714. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', strdup(cwd));
  4715. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4716. // may already be an absolute path
  4717. if (File::isAbsolutePath(abstract_path))
  4718. return strdup(abstract_path);
  4719. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  4720. }
  4721. 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)
  4722. {
  4723. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4724. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4725. return ((CarlaPluginLV2*)handle)->handleStateStore(key, value, size, type, flags);
  4726. }
  4727. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4728. {
  4729. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4730. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4731. return ((CarlaPluginLV2*)handle)->handleStateRetrieve(key, size, type, flags);
  4732. }
  4733. // -------------------------------------------------------------------
  4734. // URI-Map Feature
  4735. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4736. {
  4737. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4738. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4739. // unused
  4740. (void)map;
  4741. }
  4742. // -------------------------------------------------------------------
  4743. // URID Feature
  4744. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4745. {
  4746. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4747. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4748. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4749. // Atom types
  4750. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4751. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4752. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4753. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4754. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4755. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4756. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4757. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4758. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  4759. return CARLA_URI_MAP_ID_ATOM_EVENT;
  4760. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4761. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4762. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4763. return CARLA_URI_MAP_ID_ATOM_INT;
  4764. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4765. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4766. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4767. return CARLA_URI_MAP_ID_ATOM_LONG;
  4768. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  4769. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  4770. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4771. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4772. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4773. return CARLA_URI_MAP_ID_ATOM_PATH;
  4774. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4775. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4776. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4777. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4778. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4779. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4780. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  4781. return CARLA_URI_MAP_ID_ATOM_SOUND;
  4782. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4783. return CARLA_URI_MAP_ID_ATOM_STRING;
  4784. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4785. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4786. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4787. return CARLA_URI_MAP_ID_ATOM_URI;
  4788. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4789. return CARLA_URI_MAP_ID_ATOM_URID;
  4790. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4791. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4792. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4793. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4794. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4795. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4796. // BufSize types
  4797. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4798. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4799. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4800. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4801. if (std::strcmp(uri, LV2_BUF_SIZE__nominalBlockLength) == 0)
  4802. return CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH;
  4803. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4804. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4805. // Log types
  4806. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4807. return CARLA_URI_MAP_ID_LOG_ERROR;
  4808. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4809. return CARLA_URI_MAP_ID_LOG_NOTE;
  4810. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4811. return CARLA_URI_MAP_ID_LOG_TRACE;
  4812. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4813. return CARLA_URI_MAP_ID_LOG_WARNING;
  4814. // Time types
  4815. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4816. return CARLA_URI_MAP_ID_TIME_POSITION;
  4817. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4818. return CARLA_URI_MAP_ID_TIME_BAR;
  4819. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4820. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4821. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4822. return CARLA_URI_MAP_ID_TIME_BEAT;
  4823. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4824. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4825. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4826. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4827. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4828. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4829. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4830. return CARLA_URI_MAP_ID_TIME_FRAME;
  4831. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4832. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4833. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4834. return CARLA_URI_MAP_ID_TIME_SPEED;
  4835. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  4836. return CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT;
  4837. // Others
  4838. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4839. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4840. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4841. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4842. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  4843. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  4844. // Custom
  4845. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  4846. return CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  4847. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  4848. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  4849. // Custom types
  4850. return ((CarlaPluginLV2*)handle)->getCustomURID(uri);
  4851. }
  4852. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4853. {
  4854. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4855. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4856. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4857. // Atom types
  4858. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4859. return LV2_ATOM__Blank;
  4860. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4861. return LV2_ATOM__Bool;
  4862. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4863. return LV2_ATOM__Chunk;
  4864. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4865. return LV2_ATOM__Double;
  4866. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  4867. return LV2_ATOM__Event;
  4868. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4869. return LV2_ATOM__Float;
  4870. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4871. return LV2_ATOM__Int;
  4872. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4873. return LV2_ATOM__Literal;
  4874. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4875. return LV2_ATOM__Long;
  4876. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  4877. return LV2_ATOM__Number;
  4878. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4879. return LV2_ATOM__Object;
  4880. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4881. return LV2_ATOM__Path;
  4882. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4883. return LV2_ATOM__Property;
  4884. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4885. return LV2_ATOM__Resource;
  4886. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4887. return LV2_ATOM__Sequence;
  4888. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  4889. return LV2_ATOM__Sound;
  4890. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4891. return LV2_ATOM__String;
  4892. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4893. return LV2_ATOM__Tuple;
  4894. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4895. return LV2_ATOM__URI;
  4896. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4897. return LV2_ATOM__URID;
  4898. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4899. return LV2_ATOM__Vector;
  4900. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4901. return LV2_ATOM__atomTransfer;
  4902. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4903. return LV2_ATOM__eventTransfer;
  4904. // BufSize types
  4905. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4906. return LV2_BUF_SIZE__maxBlockLength;
  4907. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4908. return LV2_BUF_SIZE__minBlockLength;
  4909. if (urid == CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH)
  4910. return LV2_BUF_SIZE__nominalBlockLength;
  4911. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4912. return LV2_BUF_SIZE__sequenceSize;
  4913. // Log types
  4914. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4915. return LV2_LOG__Error;
  4916. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4917. return LV2_LOG__Note;
  4918. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4919. return LV2_LOG__Trace;
  4920. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4921. return LV2_LOG__Warning;
  4922. // Time types
  4923. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4924. return LV2_TIME__Position;
  4925. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4926. return LV2_TIME__bar;
  4927. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4928. return LV2_TIME__barBeat;
  4929. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4930. return LV2_TIME__beat;
  4931. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4932. return LV2_TIME__beatUnit;
  4933. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4934. return LV2_TIME__beatsPerBar;
  4935. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4936. return LV2_TIME__beatsPerMinute;
  4937. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4938. return LV2_TIME__frame;
  4939. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4940. return LV2_TIME__framesPerSecond;
  4941. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4942. return LV2_TIME__speed;
  4943. if (urid == CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT)
  4944. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  4945. // Others
  4946. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4947. return LV2_MIDI__MidiEvent;
  4948. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4949. return LV2_PARAMETERS__sampleRate;
  4950. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  4951. return LV2_UI__windowTitle;
  4952. // Custom
  4953. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  4954. return URI_CARLA_ATOM_WORKER;
  4955. if (urid == CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID)
  4956. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  4957. // Custom types
  4958. return ((CarlaPluginLV2*)handle)->getCustomURIDString(urid);
  4959. }
  4960. // -------------------------------------------------------------------
  4961. // Worker Feature
  4962. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4963. {
  4964. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4965. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4966. return ((CarlaPluginLV2*)handle)->handleWorkerSchedule(size, data);
  4967. }
  4968. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_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_respond(%p, %i, %p)", handle, size, data);
  4972. return ((CarlaPluginLV2*)handle)->handleWorkerRespond(size, data);
  4973. }
  4974. // -------------------------------------------------------------------
  4975. // External UI Feature
  4976. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4977. {
  4978. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4979. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4980. ((CarlaPluginLV2*)controller)->handleExternalUIClosed();
  4981. }
  4982. // -------------------------------------------------------------------
  4983. // UI Port-Map Feature
  4984. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4985. {
  4986. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4987. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4988. return ((CarlaPluginLV2*)handle)->handleUIPortMap(symbol);
  4989. }
  4990. // -------------------------------------------------------------------
  4991. // UI Resize Feature
  4992. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4993. {
  4994. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4995. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4996. return ((CarlaPluginLV2*)handle)->handleUIResize(width, height);
  4997. }
  4998. // -------------------------------------------------------------------
  4999. // UI Extension
  5000. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  5001. {
  5002. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  5003. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  5004. ((CarlaPluginLV2*)controller)->handleUIWrite(port_index, buffer_size, format, buffer);
  5005. }
  5006. // -------------------------------------------------------------------
  5007. // Lilv State
  5008. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  5009. {
  5010. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  5011. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  5012. ((CarlaPluginLV2*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  5013. }
  5014. // -------------------------------------------------------------------
  5015. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginLV2)
  5016. };
  5017. // -------------------------------------------------------------------------------------------------------------------
  5018. bool CarlaPipeServerLV2::msgReceived(const char* const msg) noexcept
  5019. {
  5020. if (std::strcmp(msg, "exiting") == 0)
  5021. {
  5022. closePipeServer();
  5023. fUiState = UiHide;
  5024. return true;
  5025. }
  5026. if (std::strcmp(msg, "control") == 0)
  5027. {
  5028. uint32_t index;
  5029. float value;
  5030. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5031. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  5032. try {
  5033. kPlugin->handleUIWrite(index, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  5034. } CARLA_SAFE_EXCEPTION("magReceived control");
  5035. return true;
  5036. }
  5037. if (std::strcmp(msg, "atom") == 0)
  5038. {
  5039. uint32_t index, size;
  5040. const char* base64atom;
  5041. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5042. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), true);
  5043. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
  5044. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  5045. delete[] base64atom;
  5046. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  5047. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  5048. CARLA_SAFE_ASSERT_RETURN(lv2_atom_total_size(atom) == chunk.size(), true);
  5049. try {
  5050. kPlugin->handleUIWrite(index, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  5051. } CARLA_SAFE_EXCEPTION("magReceived atom");
  5052. return true;
  5053. }
  5054. if (std::strcmp(msg, "program") == 0)
  5055. {
  5056. uint32_t index;
  5057. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  5058. try {
  5059. kPlugin->setMidiProgram(static_cast<int32_t>(index), false, true, true);
  5060. } CARLA_SAFE_EXCEPTION("msgReceived program");
  5061. return true;
  5062. }
  5063. if (std::strcmp(msg, "urid") == 0)
  5064. {
  5065. uint32_t urid;
  5066. const char* uri;
  5067. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  5068. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  5069. if (urid != 0)
  5070. {
  5071. try {
  5072. kPlugin->handleUridMap(urid, uri);
  5073. } CARLA_SAFE_EXCEPTION("msgReceived urid");
  5074. }
  5075. delete[] uri;
  5076. return true;
  5077. }
  5078. return false;
  5079. }
  5080. // -------------------------------------------------------------------------------------------------------------------
  5081. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  5082. {
  5083. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  5084. CarlaPluginLV2* const plugin(new CarlaPluginLV2(init.engine, init.id));
  5085. if (! plugin->init(init.name, init.label, init.options))
  5086. {
  5087. delete plugin;
  5088. return nullptr;
  5089. }
  5090. return plugin;
  5091. }
  5092. // -------------------------------------------------------------------------------------------------------------------
  5093. CARLA_BACKEND_END_NAMESPACE