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
236KB

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