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.

6211 lines
235KB

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