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.

6230 lines
236KB

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