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.

6190 lines
232KB

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