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.

6249 lines
235KB

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