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.

6201 lines
232KB

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