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.

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