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.

6207 lines
232KB

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