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.

6192 lines
232KB

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