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.

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