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.

6319 lines
239KB

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