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.

6262 lines
236KB

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