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.

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