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.

6218 lines
233KB

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