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.

6198 lines
233KB

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