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.

6214 lines
233KB

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