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. uint options = 0x0;
  675. // can't disable fixed buffers if using latency or MIDI output
  676. if (fLatencyIndex == -1 && getMidiOutCount() == 0 && ! fNeedsFixedBuffers)
  677. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  678. // can't disable forced stereo if enabled in the engine
  679. if (pData->engine->getOptions().forceStereo)
  680. pass();
  681. // if inputs or outputs are just 1, then yes we can force stereo
  682. else if ((pData->audioIn.count == 1 || pData->audioOut.count == 1) && fCanInit2)
  683. options |= PLUGIN_OPTION_FORCE_STEREO;
  684. if (fExt.programs != nullptr)
  685. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  686. if (getMidiInCount() != 0)
  687. {
  688. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  689. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  690. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  691. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  692. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  693. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  694. }
  695. return options;
  696. }
  697. float getParameterValue(const uint32_t parameterId) const noexcept override
  698. {
  699. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  700. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  701. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  702. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  703. return fParamBuffers[parameterId];
  704. }
  705. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  706. {
  707. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  708. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  709. const int32_t rindex(pData->param.data[parameterId].rindex);
  710. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  711. {
  712. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  713. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount, 0.0f);
  714. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  715. return portScalePoint->Value;
  716. }
  717. return 0.0f;
  718. }
  719. void getLabel(char* const strBuf) const noexcept override
  720. {
  721. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  722. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  723. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  724. }
  725. void getMaker(char* const strBuf) const noexcept override
  726. {
  727. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  728. if (fRdfDescriptor->Author != nullptr)
  729. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  730. else
  731. CarlaPlugin::getMaker(strBuf);
  732. }
  733. void getCopyright(char* const strBuf) const noexcept override
  734. {
  735. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  736. if (fRdfDescriptor->License != nullptr)
  737. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  738. else
  739. CarlaPlugin::getCopyright(strBuf);
  740. }
  741. void getRealName(char* const strBuf) const noexcept override
  742. {
  743. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  744. if (fRdfDescriptor->Name != nullptr)
  745. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  746. else
  747. CarlaPlugin::getRealName(strBuf);
  748. }
  749. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  750. {
  751. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  752. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  753. const int32_t rindex(pData->param.data[parameterId].rindex);
  754. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  755. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  756. else
  757. CarlaPlugin::getParameterName(parameterId, strBuf);
  758. }
  759. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  760. {
  761. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  762. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  763. const int32_t rindex(pData->param.data[parameterId].rindex);
  764. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  765. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  766. else
  767. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  768. }
  769. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  770. {
  771. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  772. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  773. const int32_t rindex(pData->param.data[parameterId].rindex);
  774. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  775. {
  776. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  777. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  778. {
  779. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  780. return;
  781. }
  782. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  783. {
  784. switch (port->Unit.Unit)
  785. {
  786. case LV2_PORT_UNIT_BAR:
  787. std::strncpy(strBuf, "bars", STR_MAX);
  788. return;
  789. case LV2_PORT_UNIT_BEAT:
  790. std::strncpy(strBuf, "beats", STR_MAX);
  791. return;
  792. case LV2_PORT_UNIT_BPM:
  793. std::strncpy(strBuf, "BPM", STR_MAX);
  794. return;
  795. case LV2_PORT_UNIT_CENT:
  796. std::strncpy(strBuf, "ct", STR_MAX);
  797. return;
  798. case LV2_PORT_UNIT_CM:
  799. std::strncpy(strBuf, "cm", STR_MAX);
  800. return;
  801. case LV2_PORT_UNIT_COEF:
  802. std::strncpy(strBuf, "(coef)", STR_MAX);
  803. return;
  804. case LV2_PORT_UNIT_DB:
  805. std::strncpy(strBuf, "dB", STR_MAX);
  806. return;
  807. case LV2_PORT_UNIT_DEGREE:
  808. std::strncpy(strBuf, "deg", STR_MAX);
  809. return;
  810. case LV2_PORT_UNIT_FRAME:
  811. std::strncpy(strBuf, "frames", STR_MAX);
  812. return;
  813. case LV2_PORT_UNIT_HZ:
  814. std::strncpy(strBuf, "Hz", STR_MAX);
  815. return;
  816. case LV2_PORT_UNIT_INCH:
  817. std::strncpy(strBuf, "in", STR_MAX);
  818. return;
  819. case LV2_PORT_UNIT_KHZ:
  820. std::strncpy(strBuf, "kHz", STR_MAX);
  821. return;
  822. case LV2_PORT_UNIT_KM:
  823. std::strncpy(strBuf, "km", STR_MAX);
  824. return;
  825. case LV2_PORT_UNIT_M:
  826. std::strncpy(strBuf, "m", STR_MAX);
  827. return;
  828. case LV2_PORT_UNIT_MHZ:
  829. std::strncpy(strBuf, "MHz", STR_MAX);
  830. return;
  831. case LV2_PORT_UNIT_MIDINOTE:
  832. std::strncpy(strBuf, "note", STR_MAX);
  833. return;
  834. case LV2_PORT_UNIT_MILE:
  835. std::strncpy(strBuf, "mi", STR_MAX);
  836. return;
  837. case LV2_PORT_UNIT_MIN:
  838. std::strncpy(strBuf, "min", STR_MAX);
  839. return;
  840. case LV2_PORT_UNIT_MM:
  841. std::strncpy(strBuf, "mm", STR_MAX);
  842. return;
  843. case LV2_PORT_UNIT_MS:
  844. std::strncpy(strBuf, "ms", STR_MAX);
  845. return;
  846. case LV2_PORT_UNIT_OCT:
  847. std::strncpy(strBuf, "oct", STR_MAX);
  848. return;
  849. case LV2_PORT_UNIT_PC:
  850. std::strncpy(strBuf, "%", STR_MAX);
  851. return;
  852. case LV2_PORT_UNIT_S:
  853. std::strncpy(strBuf, "s", STR_MAX);
  854. return;
  855. case LV2_PORT_UNIT_SEMITONE:
  856. std::strncpy(strBuf, "semi", STR_MAX);
  857. return;
  858. }
  859. }
  860. }
  861. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  862. }
  863. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  864. {
  865. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  866. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  867. const int32_t rindex(pData->param.data[parameterId].rindex);
  868. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  869. {
  870. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  871. CARLA_SAFE_ASSERT_RETURN(scalePointId < port->ScalePointCount,);
  872. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  873. if (portScalePoint->Label != nullptr)
  874. {
  875. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  876. return;
  877. }
  878. }
  879. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  880. }
  881. // -------------------------------------------------------------------
  882. // Set data (state)
  883. void prepareForSave() override
  884. {
  885. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  886. if (fExt.state != nullptr && fExt.state->save != nullptr)
  887. {
  888. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  889. if (fHandle2 != nullptr)
  890. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  891. }
  892. }
  893. // -------------------------------------------------------------------
  894. // Set data (internal stuff)
  895. void setName(const char* const newName) override
  896. {
  897. CarlaPlugin::setName(newName);
  898. if (fLv2Options.windowTitle == nullptr)
  899. return;
  900. CarlaString guiTitle(pData->name);
  901. guiTitle += " (GUI)";
  902. delete[] fLv2Options.windowTitle;
  903. fLv2Options.windowTitle = guiTitle.dup();
  904. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  905. fLv2Options.opts[CarlaPluginLV2Options::WindowTitle].value = fLv2Options.windowTitle;
  906. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  907. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fLv2Options.windowTitle;
  908. if (fPipeServer.isPipeRunning())
  909. fPipeServer.writeUiTitleMessage(fLv2Options.windowTitle);
  910. #ifndef LV2_UIS_ONLY_BRIDGES
  911. if (fUI.window != nullptr)
  912. fUI.window->setTitle(fLv2Options.windowTitle);
  913. #endif
  914. }
  915. // -------------------------------------------------------------------
  916. // Set data (plugin-specific stuff)
  917. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  918. {
  919. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  920. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  921. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  922. fParamBuffers[parameterId] = fixedValue;
  923. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  924. }
  925. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  926. {
  927. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  928. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  929. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  930. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  931. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  932. carla_debug("CarlaPluginLV2::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  933. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  934. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  935. // we should only call state restore once
  936. // so inject this in CarlaPlugin::loadSaveState
  937. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "CarlaLoadLv2StateNow") == 0 && std::strcmp(value, "true") == 0)
  938. {
  939. if (fExt.state == nullptr)
  940. return;
  941. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  942. {
  943. const ScopedSingleProcessLocker spl(this, true);
  944. try {
  945. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  946. } catch(...) {}
  947. if (fHandle2 != nullptr)
  948. {
  949. try {
  950. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  951. } catch(...) {}
  952. }
  953. }
  954. switch (status)
  955. {
  956. case LV2_STATE_SUCCESS:
  957. carla_debug("CarlaPluginLV2::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  958. break;
  959. case LV2_STATE_ERR_UNKNOWN:
  960. carla_stderr("CarlaPluginLV2::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  961. break;
  962. case LV2_STATE_ERR_BAD_TYPE:
  963. carla_stderr("CarlaPluginLV2::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  964. break;
  965. case LV2_STATE_ERR_BAD_FLAGS:
  966. carla_stderr("CarlaPluginLV2::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  967. break;
  968. case LV2_STATE_ERR_NO_FEATURE:
  969. carla_stderr("CarlaPluginLV2::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  970. break;
  971. case LV2_STATE_ERR_NO_PROPERTY:
  972. carla_stderr("CarlaPluginLV2::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  973. break;
  974. }
  975. return;
  976. }
  977. CarlaPlugin::setCustomData(type, key, value, sendGui);
  978. }
  979. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  980. {
  981. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  982. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  983. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  984. {
  985. const LV2_URID_Map* const uridMap = (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  986. LilvState* const state = Lv2WorldClass::getInstance().getStateFromURI(fRdfDescriptor->Presets[index].URI,
  987. uridMap);
  988. CARLA_SAFE_ASSERT_RETURN(state != nullptr,);
  989. // invalidate midi-program selection
  990. CarlaPlugin::setMidiProgram(-1, false, false, sendCallback);
  991. if (fExt.state != nullptr)
  992. {
  993. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  994. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  995. if (fHandle2 != nullptr)
  996. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  997. }
  998. else
  999. {
  1000. lilv_state_emit_port_values(state, carla_lilv_set_port_value, this);
  1001. }
  1002. lilv_state_free(state);
  1003. }
  1004. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  1005. }
  1006. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  1007. {
  1008. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1009. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1010. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  1011. {
  1012. const uint32_t bank(pData->midiprog.data[index].bank);
  1013. const uint32_t program(pData->midiprog.data[index].program);
  1014. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  1015. try {
  1016. fExt.programs->select_program(fHandle, bank, program);
  1017. } catch(...) {}
  1018. if (fHandle2 != nullptr)
  1019. {
  1020. try {
  1021. fExt.programs->select_program(fHandle2, bank, program);
  1022. } catch(...) {}
  1023. }
  1024. }
  1025. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  1026. }
  1027. // -------------------------------------------------------------------
  1028. // Set ui stuff
  1029. void showCustomUI(const bool yesNo) override
  1030. {
  1031. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  1032. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  1033. if (! yesNo)
  1034. pData->transientTryCounter = 0;
  1035. if (fUI.type == UI::TYPE_BRIDGE)
  1036. {
  1037. if (yesNo)
  1038. {
  1039. if (fPipeServer.isPipeRunning())
  1040. {
  1041. fPipeServer.writeFocusMessage();
  1042. return;
  1043. }
  1044. if (! fPipeServer.startPipeServer(std::min(fLv2Options.sequenceSize, 819200)))
  1045. {
  1046. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1047. return;
  1048. }
  1049. // manually write messages so we can take the lock for ourselves
  1050. {
  1051. char tmpBuf[0xff+1];
  1052. tmpBuf[0xff] = '\0';
  1053. const CarlaMutexLocker cml(fPipeServer.getPipeLock());
  1054. const ScopedLocale csl;
  1055. // write URI mappings
  1056. uint32_t u = 0;
  1057. for (auto it=fCustomURIDs.begin(), end=fCustomURIDs.end(); it != end; ++it, ++u)
  1058. {
  1059. if (u < CARLA_URI_MAP_ID_COUNT)
  1060. continue;
  1061. const std::string& uri(*it);
  1062. std::snprintf(tmpBuf, 0xff, "%u\n", u);
  1063. fPipeServer.writeMessage("urid\n", 5);
  1064. fPipeServer.writeMessage(tmpBuf);
  1065. fPipeServer.writeAndFixMessage(uri.c_str());
  1066. }
  1067. // write UI options
  1068. fPipeServer.writeMessage("uiOptions\n", 10);
  1069. std::snprintf(tmpBuf, 0xff, "%g\n", pData->engine->getSampleRate());
  1070. fPipeServer.writeMessage(tmpBuf);
  1071. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(true)); // useTheme
  1072. fPipeServer.writeMessage(tmpBuf);
  1073. std::snprintf(tmpBuf, 0xff, "%s\n", bool2str(true)); // useThemeColors
  1074. fPipeServer.writeMessage(tmpBuf);
  1075. fPipeServer.writeAndFixMessage(fLv2Options.windowTitle != nullptr ? fLv2Options.windowTitle : "");
  1076. std::snprintf(tmpBuf, 0xff, P_INTPTR "\n", frontendWinId);
  1077. fPipeServer.writeMessage(tmpBuf);
  1078. // write parameter values
  1079. for (uint32_t i=0; i < pData->param.count; ++i)
  1080. {
  1081. fPipeServer.writeMessage("control\n", 8);
  1082. std::snprintf(tmpBuf, 0xff, "%i\n", pData->param.data[i].rindex);
  1083. fPipeServer.writeMessage(tmpBuf);
  1084. std::snprintf(tmpBuf, 0xff, "%f\n", getParameterValue(i));
  1085. fPipeServer.writeMessage(tmpBuf);
  1086. }
  1087. // ready to show
  1088. fPipeServer.writeMessage("show\n", 5);
  1089. fPipeServer.flushMessages();
  1090. }
  1091. #ifndef BUILD_BRIDGE
  1092. if (fUI.rdfDescriptor->Type == LV2_UI_MOD)
  1093. pData->tryTransient();
  1094. #endif
  1095. }
  1096. else
  1097. {
  1098. fPipeServer.stopPipeServer(pData->engine->getOptions().uiBridgesTimeout);
  1099. }
  1100. return;
  1101. }
  1102. // take some precautions
  1103. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  1104. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr,);
  1105. if (yesNo)
  1106. {
  1107. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->instantiate != nullptr,);
  1108. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor->cleanup != nullptr,);
  1109. }
  1110. else
  1111. {
  1112. if (fUI.handle == nullptr)
  1113. return;
  1114. }
  1115. if (yesNo)
  1116. {
  1117. if (fUI.handle == nullptr)
  1118. {
  1119. #ifndef LV2_UIS_ONLY_BRIDGES
  1120. if (fUI.type == UI::TYPE_EMBED && fUI.window == nullptr)
  1121. {
  1122. const char* msg = nullptr;
  1123. switch (fUI.rdfDescriptor->Type)
  1124. {
  1125. case LV2_UI_GTK2:
  1126. case LV2_UI_GTK3:
  1127. case LV2_UI_QT4:
  1128. case LV2_UI_QT5:
  1129. case LV2_UI_EXTERNAL:
  1130. case LV2_UI_OLD_EXTERNAL:
  1131. msg = "Invalid UI type";
  1132. break;
  1133. case LV2_UI_COCOA:
  1134. # ifdef CARLA_OS_MAC
  1135. fUI.window = CarlaPluginUI::newCocoa(this, frontendWinId, isUiResizable());
  1136. # else
  1137. msg = "UI is for MacOS only";
  1138. # endif
  1139. break;
  1140. case LV2_UI_WINDOWS:
  1141. # ifdef CARLA_OS_WIN
  1142. fUI.window = CarlaPluginUI::newWindows(this, frontendWinId, isUiResizable());
  1143. # else
  1144. msg = "UI is for Windows only";
  1145. # endif
  1146. break;
  1147. case LV2_UI_X11:
  1148. # ifdef HAVE_X11
  1149. fUI.window = CarlaPluginUI::newX11(this, frontendWinId, isUiResizable());
  1150. # else
  1151. msg = "UI is only for systems with X11";
  1152. # endif
  1153. break;
  1154. default:
  1155. msg = "Unknown UI type";
  1156. break;
  1157. }
  1158. if (fUI.window == nullptr && fExt.uishow == nullptr)
  1159. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  1160. if (fUI.window != nullptr)
  1161. fFeatures[kFeatureIdUiParent]->data = fUI.window->getPtr();
  1162. }
  1163. #endif
  1164. if (fUI.window != nullptr)
  1165. fUI.window->setTitle(fLv2Options.windowTitle);
  1166. fUI.widget = nullptr;
  1167. fUI.handle = fUI.descriptor->instantiate(fUI.descriptor, fRdfDescriptor->URI, fUI.rdfDescriptor->Bundle,
  1168. carla_lv2_ui_write_function, this, &fUI.widget, fFeatures);
  1169. }
  1170. CARLA_SAFE_ASSERT(fUI.handle != nullptr);
  1171. CARLA_SAFE_ASSERT(fUI.type != UI::TYPE_EXTERNAL || fUI.widget != nullptr);
  1172. if (fUI.handle == nullptr || (fUI.type == UI::TYPE_EXTERNAL && fUI.widget == nullptr))
  1173. {
  1174. fUI.widget = nullptr;
  1175. if (fUI.handle != nullptr)
  1176. {
  1177. fUI.descriptor->cleanup(fUI.handle);
  1178. fUI.handle = nullptr;
  1179. }
  1180. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  1181. }
  1182. updateUi();
  1183. #ifndef LV2_UIS_ONLY_BRIDGES
  1184. if (fUI.type == UI::TYPE_EMBED)
  1185. {
  1186. if (fUI.window != nullptr)
  1187. {
  1188. fUI.window->show();
  1189. }
  1190. else if (fExt.uishow != nullptr)
  1191. {
  1192. fExt.uishow->show(fUI.handle);
  1193. # ifndef BUILD_BRIDGE
  1194. pData->tryTransient();
  1195. # endif
  1196. }
  1197. }
  1198. else
  1199. #endif
  1200. {
  1201. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUI.widget);
  1202. #ifndef BUILD_BRIDGE
  1203. pData->tryTransient();
  1204. #endif
  1205. }
  1206. }
  1207. else
  1208. {
  1209. #ifndef LV2_UIS_ONLY_BRIDGES
  1210. if (fUI.type == UI::TYPE_EMBED)
  1211. {
  1212. if (fUI.window != nullptr)
  1213. fUI.window->hide();
  1214. else if (fExt.uishow != nullptr)
  1215. fExt.uishow->hide(fUI.handle);
  1216. }
  1217. else
  1218. #endif
  1219. {
  1220. CARLA_SAFE_ASSERT(fUI.widget != nullptr);
  1221. if (fUI.widget != nullptr)
  1222. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUI.widget);
  1223. }
  1224. fUI.descriptor->cleanup(fUI.handle);
  1225. fUI.handle = nullptr;
  1226. fUI.widget = nullptr;
  1227. }
  1228. }
  1229. void uiIdle() override
  1230. {
  1231. if (fAtomBufferOut.isDataAvailableForReading())
  1232. {
  1233. Lv2AtomRingBuffer tmpRingBuffer(fAtomBufferOut, fTmpAtomBuffer);
  1234. CARLA_SAFE_ASSERT(tmpRingBuffer.isDataAvailableForReading());
  1235. uint32_t portIndex;
  1236. const LV2_Atom* atom;
  1237. const bool hasPortEvent(fUI.handle != nullptr && fUI.descriptor != nullptr &&
  1238. fUI.descriptor->port_event != nullptr && ! fNeedsUiClose);
  1239. for (; tmpRingBuffer.get(atom, portIndex);)
  1240. {
  1241. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  1242. {
  1243. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1244. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1245. }
  1246. else if (fUI.type == UI::TYPE_BRIDGE)
  1247. {
  1248. if (fPipeServer.isPipeRunning())
  1249. fPipeServer.writeLv2AtomMessage(portIndex, atom);
  1250. }
  1251. else
  1252. {
  1253. if (hasPortEvent)
  1254. fUI.descriptor->port_event(fUI.handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1255. }
  1256. }
  1257. }
  1258. if (fPipeServer.isPipeRunning())
  1259. {
  1260. fPipeServer.idlePipe();
  1261. switch (fPipeServer.getAndResetUiState())
  1262. {
  1263. case CarlaPipeServerLV2::UiNone:
  1264. case CarlaPipeServerLV2::UiShow:
  1265. break;
  1266. case CarlaPipeServerLV2::UiHide:
  1267. fPipeServer.stopPipeServer(2000);
  1268. // nobreak
  1269. case CarlaPipeServerLV2::UiCrashed:
  1270. pData->transientTryCounter = 0;
  1271. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1272. break;
  1273. }
  1274. }
  1275. else
  1276. {
  1277. // TODO - detect if ui-bridge crashed
  1278. }
  1279. if (fNeedsUiClose)
  1280. {
  1281. fNeedsUiClose = false;
  1282. showCustomUI(false);
  1283. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1284. }
  1285. else if (fUI.handle != nullptr && fUI.descriptor != nullptr)
  1286. {
  1287. if (fUI.type == UI::TYPE_EXTERNAL && fUI.widget != nullptr)
  1288. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUI.widget);
  1289. #ifndef LV2_UIS_ONLY_BRIDGES
  1290. else if (fUI.type == UI::TYPE_EMBED && fUI.window != nullptr)
  1291. fUI.window->idle();
  1292. // note: UI might have been closed by window idle
  1293. if (fNeedsUiClose)
  1294. pass();
  1295. else if (fUI.handle != nullptr && fExt.uiidle != nullptr && fExt.uiidle->idle(fUI.handle) != 0)
  1296. {
  1297. showCustomUI(false);
  1298. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1299. CARLA_SAFE_ASSERT(fUI.handle == nullptr);
  1300. }
  1301. #endif
  1302. }
  1303. CarlaPlugin::uiIdle();
  1304. }
  1305. // -------------------------------------------------------------------
  1306. // Plugin state
  1307. void reload() override
  1308. {
  1309. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1310. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1311. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1312. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1313. carla_debug("CarlaPluginLV2::reload() - start");
  1314. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1315. // Safely disable plugin for reload
  1316. const ScopedDisabler sd(this);
  1317. if (pData->active)
  1318. deactivate();
  1319. clearBuffers();
  1320. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1321. const uint32_t portCount(fRdfDescriptor->PortCount);
  1322. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1323. aIns = aOuts = cvIns = cvOuts = params = 0;
  1324. LinkedList<uint> evIns, evOuts;
  1325. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize)+0xff);
  1326. bool forcedStereoIn, forcedStereoOut;
  1327. forcedStereoIn = forcedStereoOut = false;
  1328. bool needsCtrlIn, needsCtrlOut;
  1329. needsCtrlIn = needsCtrlOut = false;
  1330. for (uint32_t i=0; i < portCount; ++i)
  1331. {
  1332. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1333. if (LV2_IS_PORT_AUDIO(portTypes))
  1334. {
  1335. if (LV2_IS_PORT_INPUT(portTypes))
  1336. aIns += 1;
  1337. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1338. aOuts += 1;
  1339. }
  1340. else if (LV2_IS_PORT_CV(portTypes))
  1341. {
  1342. if (LV2_IS_PORT_INPUT(portTypes))
  1343. cvIns += 1;
  1344. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1345. cvOuts += 1;
  1346. }
  1347. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1348. {
  1349. if (LV2_IS_PORT_INPUT(portTypes))
  1350. evIns.append(CARLA_EVENT_DATA_ATOM);
  1351. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1352. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1353. }
  1354. else if (LV2_IS_PORT_EVENT(portTypes))
  1355. {
  1356. if (LV2_IS_PORT_INPUT(portTypes))
  1357. evIns.append(CARLA_EVENT_DATA_EVENT);
  1358. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1359. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1360. }
  1361. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1362. {
  1363. if (LV2_IS_PORT_INPUT(portTypes))
  1364. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1365. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1366. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1367. }
  1368. else if (LV2_IS_PORT_CONTROL(portTypes))
  1369. params += 1;
  1370. }
  1371. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && aIns <= 1 && aOuts <= 1 && fExt.state == nullptr && fExt.worker == nullptr)
  1372. {
  1373. if (fHandle2 == nullptr)
  1374. {
  1375. try {
  1376. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1377. } catch(...) {}
  1378. }
  1379. if (fHandle2 != nullptr)
  1380. {
  1381. if (aIns == 1)
  1382. {
  1383. aIns = 2;
  1384. forcedStereoIn = true;
  1385. }
  1386. if (aOuts == 1)
  1387. {
  1388. aOuts = 2;
  1389. forcedStereoOut = true;
  1390. }
  1391. }
  1392. }
  1393. if (aIns > 0)
  1394. {
  1395. pData->audioIn.createNew(aIns);
  1396. fAudioInBuffers = new float*[aIns];
  1397. for (uint32_t i=0; i < aIns; ++i)
  1398. fAudioInBuffers[i] = nullptr;
  1399. }
  1400. if (aOuts > 0)
  1401. {
  1402. pData->audioOut.createNew(aOuts);
  1403. fAudioOutBuffers = new float*[aOuts];
  1404. needsCtrlIn = true;
  1405. for (uint32_t i=0; i < aOuts; ++i)
  1406. fAudioOutBuffers[i] = nullptr;
  1407. }
  1408. if (cvIns > 0)
  1409. {
  1410. pData->cvIn.createNew(cvIns);
  1411. fCvInBuffers = new float*[cvIns];
  1412. for (uint32_t i=0; i < cvIns; ++i)
  1413. fCvInBuffers[i] = nullptr;
  1414. }
  1415. if (cvOuts > 0)
  1416. {
  1417. pData->cvOut.createNew(cvOuts);
  1418. fCvOutBuffers = new float*[cvOuts];
  1419. for (uint32_t i=0; i < cvOuts; ++i)
  1420. fCvOutBuffers[i] = nullptr;
  1421. }
  1422. if (params > 0)
  1423. {
  1424. pData->param.createNew(params, true);
  1425. fParamBuffers = new float[params];
  1426. FloatVectorOperations::clear(fParamBuffers, static_cast<int>(params));
  1427. }
  1428. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1429. {
  1430. fEventsIn.createNew(count);
  1431. for (uint32_t i=0; i < count; ++i)
  1432. {
  1433. const uint32_t& type(evIns.getAt(i, 0x0));
  1434. if (type == CARLA_EVENT_DATA_ATOM)
  1435. {
  1436. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1437. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1438. }
  1439. else if (type == CARLA_EVENT_DATA_EVENT)
  1440. {
  1441. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1442. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1443. }
  1444. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1445. {
  1446. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1447. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1448. fEventsIn.data[i].midi.data = new uchar[eventBufferSize];
  1449. }
  1450. }
  1451. }
  1452. else
  1453. {
  1454. fEventsIn.createNew(1);
  1455. fEventsIn.ctrl = &fEventsIn.data[0];
  1456. }
  1457. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1458. {
  1459. fEventsOut.createNew(count);
  1460. for (uint32_t i=0; i < count; ++i)
  1461. {
  1462. const uint32_t& type(evOuts.getAt(i, 0x0));
  1463. if (type == CARLA_EVENT_DATA_ATOM)
  1464. {
  1465. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1466. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1467. }
  1468. else if (type == CARLA_EVENT_DATA_EVENT)
  1469. {
  1470. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1471. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1472. }
  1473. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1474. {
  1475. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1476. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1477. fEventsOut.data[i].midi.data = new uchar[eventBufferSize];
  1478. }
  1479. }
  1480. }
  1481. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1482. CarlaString portName;
  1483. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1484. {
  1485. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1486. portName.clear();
  1487. 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))
  1488. {
  1489. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1490. {
  1491. portName = pData->name;
  1492. portName += ":";
  1493. }
  1494. portName += fRdfDescriptor->Ports[i].Name;
  1495. portName.truncate(portNameSize);
  1496. }
  1497. if (LV2_IS_PORT_AUDIO(portTypes))
  1498. {
  1499. if (LV2_IS_PORT_INPUT(portTypes))
  1500. {
  1501. const uint32_t j = iAudioIn++;
  1502. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  1503. pData->audioIn.ports[j].rindex = i;
  1504. if (forcedStereoIn)
  1505. {
  1506. portName += "_2";
  1507. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  1508. pData->audioIn.ports[1].rindex = i;
  1509. }
  1510. }
  1511. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1512. {
  1513. const uint32_t j = iAudioOut++;
  1514. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  1515. pData->audioOut.ports[j].rindex = i;
  1516. if (forcedStereoOut)
  1517. {
  1518. portName += "_2";
  1519. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  1520. pData->audioOut.ports[1].rindex = i;
  1521. }
  1522. }
  1523. else
  1524. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1525. }
  1526. else if (LV2_IS_PORT_CV(portTypes))
  1527. {
  1528. if (LV2_IS_PORT_INPUT(portTypes))
  1529. {
  1530. const uint32_t j = iCvIn++;
  1531. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  1532. pData->cvIn.ports[j].rindex = i;
  1533. }
  1534. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1535. {
  1536. const uint32_t j = iCvOut++;
  1537. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  1538. pData->cvOut.ports[j].rindex = i;
  1539. }
  1540. else
  1541. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1542. }
  1543. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1544. {
  1545. if (LV2_IS_PORT_INPUT(portTypes))
  1546. {
  1547. const uint32_t j = iEvIn++;
  1548. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1549. if (fHandle2 != nullptr)
  1550. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1551. fEventsIn.data[j].rindex = i;
  1552. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1553. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1554. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1555. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1556. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1557. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1558. if (evIns.count() == 1)
  1559. {
  1560. fEventsIn.ctrl = &fEventsIn.data[j];
  1561. fEventsIn.ctrlIndex = j;
  1562. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1563. needsCtrlIn = true;
  1564. }
  1565. else
  1566. {
  1567. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1568. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1569. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1570. {
  1571. fEventsIn.ctrl = &fEventsIn.data[j];
  1572. fEventsIn.ctrlIndex = j;
  1573. }
  1574. }
  1575. }
  1576. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1577. {
  1578. const uint32_t j = iEvOut++;
  1579. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1580. if (fHandle2 != nullptr)
  1581. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1582. fEventsOut.data[j].rindex = i;
  1583. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1584. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1585. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1586. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1587. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1588. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1589. if (evOuts.count() == 1)
  1590. {
  1591. fEventsOut.ctrl = &fEventsOut.data[j];
  1592. fEventsOut.ctrlIndex = j;
  1593. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1594. needsCtrlOut = true;
  1595. }
  1596. else
  1597. {
  1598. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1599. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1600. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1601. {
  1602. fEventsOut.ctrl = &fEventsOut.data[j];
  1603. fEventsOut.ctrlIndex = j;
  1604. }
  1605. }
  1606. }
  1607. else
  1608. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1609. }
  1610. else if (LV2_IS_PORT_EVENT(portTypes))
  1611. {
  1612. if (LV2_IS_PORT_INPUT(portTypes))
  1613. {
  1614. const uint32_t j = iEvIn++;
  1615. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1616. if (fHandle2 != nullptr)
  1617. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1618. fEventsIn.data[j].rindex = i;
  1619. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1620. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1621. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1622. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1623. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1624. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1625. if (evIns.count() == 1)
  1626. {
  1627. fEventsIn.ctrl = &fEventsIn.data[j];
  1628. fEventsIn.ctrlIndex = j;
  1629. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1630. needsCtrlIn = true;
  1631. }
  1632. else
  1633. {
  1634. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1635. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1636. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1637. {
  1638. fEventsIn.ctrl = &fEventsIn.data[j];
  1639. fEventsIn.ctrlIndex = j;
  1640. }
  1641. }
  1642. }
  1643. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1644. {
  1645. const uint32_t j = iEvOut++;
  1646. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1647. if (fHandle2 != nullptr)
  1648. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1649. fEventsOut.data[j].rindex = i;
  1650. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1651. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1652. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1653. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1654. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1655. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1656. if (evOuts.count() == 1)
  1657. {
  1658. fEventsOut.ctrl = &fEventsOut.data[j];
  1659. fEventsOut.ctrlIndex = j;
  1660. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1661. needsCtrlOut = true;
  1662. }
  1663. else
  1664. {
  1665. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1666. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1667. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1668. {
  1669. fEventsOut.ctrl = &fEventsOut.data[j];
  1670. fEventsOut.ctrlIndex = j;
  1671. }
  1672. }
  1673. }
  1674. else
  1675. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1676. }
  1677. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1678. {
  1679. if (LV2_IS_PORT_INPUT(portTypes))
  1680. {
  1681. const uint32_t j = iEvIn++;
  1682. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1683. if (fHandle2 != nullptr)
  1684. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1685. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1686. fEventsIn.data[j].rindex = i;
  1687. if (evIns.count() == 1)
  1688. {
  1689. needsCtrlIn = true;
  1690. fEventsIn.ctrl = &fEventsIn.data[j];
  1691. fEventsIn.ctrlIndex = j;
  1692. }
  1693. else
  1694. {
  1695. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1696. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1697. {
  1698. fEventsIn.ctrl = &fEventsIn.data[j];
  1699. fEventsIn.ctrlIndex = j;
  1700. }
  1701. }
  1702. }
  1703. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1704. {
  1705. const uint32_t j = iEvOut++;
  1706. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1707. if (fHandle2 != nullptr)
  1708. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1709. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1710. fEventsOut.data[j].rindex = i;
  1711. if (evOuts.count() == 1)
  1712. {
  1713. needsCtrlOut = true;
  1714. fEventsOut.ctrl = &fEventsOut.data[j];
  1715. fEventsOut.ctrlIndex = j;
  1716. }
  1717. else
  1718. {
  1719. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1720. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1721. {
  1722. fEventsOut.ctrl = &fEventsOut.data[j];
  1723. fEventsOut.ctrlIndex = j;
  1724. }
  1725. }
  1726. }
  1727. else
  1728. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1729. }
  1730. else if (LV2_IS_PORT_CONTROL(portTypes))
  1731. {
  1732. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1733. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1734. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1735. const uint32_t j = iCtrl++;
  1736. pData->param.data[j].index = static_cast<int32_t>(j);
  1737. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1738. float min, max, def, step, stepSmall, stepLarge;
  1739. // min value
  1740. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1741. min = portPoints.Minimum;
  1742. else
  1743. min = 0.0f;
  1744. // max value
  1745. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1746. max = portPoints.Maximum;
  1747. else
  1748. max = 1.0f;
  1749. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1750. {
  1751. min *= sampleRate;
  1752. max *= sampleRate;
  1753. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1754. }
  1755. // stupid hack for ir.lv2 (broken plugin)
  1756. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1757. {
  1758. min = 0.0f;
  1759. max = (float)0xffffff;
  1760. }
  1761. if (min >= max)
  1762. {
  1763. carla_stderr2("WARNING - Broken plugin parameter '%s': min >= max", fRdfDescriptor->Ports[i].Name);
  1764. max = min + 0.1f;
  1765. }
  1766. // default value
  1767. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1768. {
  1769. def = portPoints.Default;
  1770. }
  1771. else
  1772. {
  1773. // no default value
  1774. if (min < 0.0f && max > 0.0f)
  1775. def = 0.0f;
  1776. else
  1777. def = min;
  1778. }
  1779. if (def < min)
  1780. def = min;
  1781. else if (def > max)
  1782. def = max;
  1783. if (LV2_IS_PORT_TOGGLED(portProps))
  1784. {
  1785. step = max - min;
  1786. stepSmall = step;
  1787. stepLarge = step;
  1788. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1789. }
  1790. else if (LV2_IS_PORT_INTEGER(portProps))
  1791. {
  1792. step = 1.0f;
  1793. stepSmall = 1.0f;
  1794. stepLarge = 10.0f;
  1795. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1796. }
  1797. else
  1798. {
  1799. float range = max - min;
  1800. step = range/100.0f;
  1801. stepSmall = range/1000.0f;
  1802. stepLarge = range/10.0f;
  1803. }
  1804. if (LV2_IS_PORT_INPUT(portTypes))
  1805. {
  1806. pData->param.data[j].type = PARAMETER_INPUT;
  1807. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1808. {
  1809. carla_stderr("Plugin has latency input port, this should not happen!");
  1810. }
  1811. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1812. {
  1813. def = sampleRate;
  1814. step = 1.0f;
  1815. stepSmall = 1.0f;
  1816. stepLarge = 1.0f;
  1817. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1818. }
  1819. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1820. {
  1821. pData->param.special[j] = PARAMETER_SPECIAL_FREEWHEEL;
  1822. }
  1823. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1824. {
  1825. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  1826. }
  1827. else
  1828. {
  1829. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1830. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1831. needsCtrlIn = true;
  1832. }
  1833. // MIDI CC value
  1834. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1835. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1836. {
  1837. if (portMidiMap.Number < MAX_MIDI_CONTROL && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1838. pData->param.data[j].midiCC = static_cast<int16_t>(portMidiMap.Number);
  1839. }
  1840. }
  1841. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1842. {
  1843. pData->param.data[j].type = PARAMETER_OUTPUT;
  1844. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1845. {
  1846. min = 0.0f;
  1847. max = sampleRate;
  1848. def = 0.0f;
  1849. step = 1.0f;
  1850. stepSmall = 1.0f;
  1851. stepLarge = 1.0f;
  1852. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1853. CARLA_SAFE_ASSERT_INT2(fLatencyIndex == static_cast<int32_t>(j), fLatencyIndex, j);
  1854. }
  1855. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1856. {
  1857. def = sampleRate;
  1858. step = 1.0f;
  1859. stepSmall = 1.0f;
  1860. stepLarge = 1.0f;
  1861. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1862. }
  1863. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1864. {
  1865. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1866. }
  1867. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1868. {
  1869. pData->param.special[j] = PARAMETER_SPECIAL_TIME;
  1870. }
  1871. else
  1872. {
  1873. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1874. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1875. needsCtrlOut = true;
  1876. }
  1877. }
  1878. else
  1879. {
  1880. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1881. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1882. }
  1883. // extra parameter hints
  1884. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1885. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1886. if (LV2_IS_PORT_TRIGGER(portProps))
  1887. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1888. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1889. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1890. if (LV2_IS_PORT_ENUMERATION(portProps))
  1891. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1892. // check if parameter is not enabled or automable
  1893. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1894. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1895. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps))
  1896. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1897. else if (LV2_IS_PORT_NOT_AUTOMATIC(portProps) || LV2_IS_PORT_NON_AUTOMABLE(portProps))
  1898. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1899. pData->param.ranges[j].min = min;
  1900. pData->param.ranges[j].max = max;
  1901. pData->param.ranges[j].def = def;
  1902. pData->param.ranges[j].step = step;
  1903. pData->param.ranges[j].stepSmall = stepSmall;
  1904. pData->param.ranges[j].stepLarge = stepLarge;
  1905. // Start parameters in their default values (except freewheel, which is off by default)
  1906. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_FREEWHEEL)
  1907. fParamBuffers[j] = min;
  1908. else
  1909. fParamBuffers[j] = def;
  1910. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1911. if (fHandle2 != nullptr)
  1912. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1913. }
  1914. else
  1915. {
  1916. // Port Type not supported, but it's optional anyway
  1917. fDescriptor->connect_port(fHandle, i, nullptr);
  1918. if (fHandle2 != nullptr)
  1919. fDescriptor->connect_port(fHandle2, i, nullptr);
  1920. }
  1921. }
  1922. if (needsCtrlIn)
  1923. {
  1924. portName.clear();
  1925. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1926. {
  1927. portName = pData->name;
  1928. portName += ":";
  1929. }
  1930. portName += "events-in";
  1931. portName.truncate(portNameSize);
  1932. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  1933. }
  1934. if (needsCtrlOut)
  1935. {
  1936. portName.clear();
  1937. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1938. {
  1939. portName = pData->name;
  1940. portName += ":";
  1941. }
  1942. portName += "events-out";
  1943. portName.truncate(portNameSize);
  1944. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1945. }
  1946. if (fExt.worker != nullptr || (fUI.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1947. fAtomBufferIn.createBuffer(eventBufferSize);
  1948. if (fExt.worker != nullptr || (fUI.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1949. {
  1950. fAtomBufferOut.createBuffer(eventBufferSize);
  1951. fTmpAtomBuffer = new uint8_t[fAtomBufferOut.getSize()];
  1952. }
  1953. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1954. fEventsIn.ctrl->port = pData->event.portIn;
  1955. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1956. fEventsOut.ctrl->port = pData->event.portOut;
  1957. if (fCanInit2 && (forcedStereoIn || forcedStereoOut))
  1958. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1959. else
  1960. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1961. // plugin hints
  1962. pData->hints = 0x0;
  1963. if (isRealtimeSafe())
  1964. pData->hints |= PLUGIN_IS_RTSAFE;
  1965. if (fUI.type != UI::TYPE_NULL)
  1966. {
  1967. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1968. if (fUI.type == UI::TYPE_EMBED || fUI.type == UI::TYPE_EXTERNAL)
  1969. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  1970. }
  1971. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1972. pData->hints |= PLUGIN_IS_SYNTH;
  1973. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1974. pData->hints |= PLUGIN_CAN_DRYWET;
  1975. if (aOuts > 0)
  1976. pData->hints |= PLUGIN_CAN_VOLUME;
  1977. if (aOuts >= 2 && aOuts % 2 == 0)
  1978. pData->hints |= PLUGIN_CAN_BALANCE;
  1979. // extra plugin hints
  1980. pData->extraHints = 0x0;
  1981. if (! fCanInit2)
  1982. {
  1983. // can't run in rack
  1984. }
  1985. else if (fExt.state != nullptr || fExt.worker != nullptr)
  1986. {
  1987. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1988. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1989. }
  1990. else
  1991. {
  1992. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1993. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1994. }
  1995. // check initial latency
  1996. findInitialLatencyValue(aIns, aOuts);
  1997. bufferSizeChanged(pData->engine->getBufferSize());
  1998. reloadPrograms(true);
  1999. evIns.clear();
  2000. evOuts.clear();
  2001. if (pData->active)
  2002. activate();
  2003. carla_debug("CarlaPluginLV2::reload() - end");
  2004. }
  2005. void findInitialLatencyValue(const uint32_t aIns, const uint32_t aOuts) const
  2006. {
  2007. if (fLatencyIndex < 0)
  2008. return;
  2009. // we need to pre-run the plugin so it can update its latency control-port
  2010. float tmpIn [(aIns > 0) ? aIns : 1][2];
  2011. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  2012. for (uint32_t j=0; j < aIns; ++j)
  2013. {
  2014. tmpIn[j][0] = 0.0f;
  2015. tmpIn[j][1] = 0.0f;
  2016. try {
  2017. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  2018. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency input");
  2019. }
  2020. for (uint32_t j=0; j < aOuts; ++j)
  2021. {
  2022. tmpOut[j][0] = 0.0f;
  2023. tmpOut[j][1] = 0.0f;
  2024. try {
  2025. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  2026. } CARLA_SAFE_EXCEPTION("LV2 connect_port latency output");
  2027. }
  2028. if (fDescriptor->activate != nullptr)
  2029. {
  2030. try {
  2031. fDescriptor->activate(fHandle);
  2032. } CARLA_SAFE_EXCEPTION("LV2 latency activate");
  2033. }
  2034. try {
  2035. fDescriptor->run(fHandle, 2);
  2036. } CARLA_SAFE_EXCEPTION("LV2 latency run");
  2037. if (fDescriptor->deactivate != nullptr)
  2038. {
  2039. try {
  2040. fDescriptor->deactivate(fHandle);
  2041. } CARLA_SAFE_EXCEPTION("LV2 latency deactivate");
  2042. }
  2043. // done, let's get the value
  2044. if (const uint32_t latency = getLatencyInFrames())
  2045. {
  2046. pData->client->setLatency(latency);
  2047. #ifndef BUILD_BRIDGE
  2048. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  2049. #endif
  2050. }
  2051. }
  2052. void reloadPrograms(const bool doInit) override
  2053. {
  2054. carla_debug("CarlaPluginLV2::reloadPrograms(%s)", bool2str(doInit));
  2055. const uint32_t oldCount = pData->midiprog.count;
  2056. const int32_t current = pData->midiprog.current;
  2057. // special LV2 programs handling
  2058. if (doInit)
  2059. {
  2060. pData->prog.clear();
  2061. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  2062. if (presetCount > 0)
  2063. {
  2064. pData->prog.createNew(presetCount);
  2065. for (uint32_t i=0; i < presetCount; ++i)
  2066. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  2067. }
  2068. }
  2069. // Delete old programs
  2070. pData->midiprog.clear();
  2071. // Query new programs
  2072. uint32_t newCount = 0;
  2073. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  2074. {
  2075. for (; fExt.programs->get_program(fHandle, newCount);)
  2076. ++newCount;
  2077. }
  2078. if (newCount > 0)
  2079. {
  2080. pData->midiprog.createNew(newCount);
  2081. // Update data
  2082. for (uint32_t i=0; i < newCount; ++i)
  2083. {
  2084. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  2085. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  2086. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  2087. pData->midiprog.data[i].bank = pdesc->bank;
  2088. pData->midiprog.data[i].program = pdesc->program;
  2089. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  2090. }
  2091. }
  2092. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  2093. // Update OSC Names
  2094. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  2095. {
  2096. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  2097. for (uint32_t i=0; i < newCount; ++i)
  2098. 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);
  2099. }
  2100. #endif
  2101. if (doInit)
  2102. {
  2103. if (newCount > 0)
  2104. {
  2105. setMidiProgram(0, false, false, false);
  2106. }
  2107. else
  2108. {
  2109. // load default state
  2110. if (LilvState* const state = Lv2WorldClass::getInstance().getStateFromURI(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  2111. {
  2112. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  2113. if (fHandle2 != nullptr)
  2114. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  2115. lilv_state_free(state);
  2116. }
  2117. }
  2118. }
  2119. else
  2120. {
  2121. // Check if current program is invalid
  2122. bool programChanged = false;
  2123. if (newCount == oldCount+1)
  2124. {
  2125. // one midi program added, probably created by user
  2126. pData->midiprog.current = static_cast<int32_t>(oldCount);
  2127. programChanged = true;
  2128. }
  2129. else if (current < 0 && newCount > 0)
  2130. {
  2131. // programs exist now, but not before
  2132. pData->midiprog.current = 0;
  2133. programChanged = true;
  2134. }
  2135. else if (current >= 0 && newCount == 0)
  2136. {
  2137. // programs existed before, but not anymore
  2138. pData->midiprog.current = -1;
  2139. programChanged = true;
  2140. }
  2141. else if (current >= static_cast<int32_t>(newCount))
  2142. {
  2143. // current midi program > count
  2144. pData->midiprog.current = 0;
  2145. programChanged = true;
  2146. }
  2147. else
  2148. {
  2149. // no change
  2150. pData->midiprog.current = current;
  2151. }
  2152. if (programChanged)
  2153. setMidiProgram(pData->midiprog.current, true, true, true);
  2154. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  2155. }
  2156. }
  2157. // -------------------------------------------------------------------
  2158. // Plugin processing
  2159. void activate() noexcept override
  2160. {
  2161. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2162. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2163. if (fDescriptor->activate != nullptr)
  2164. {
  2165. try {
  2166. fDescriptor->activate(fHandle);
  2167. } CARLA_SAFE_EXCEPTION("LV2 activate");
  2168. if (fHandle2 != nullptr)
  2169. {
  2170. try {
  2171. fDescriptor->activate(fHandle2);
  2172. } CARLA_SAFE_EXCEPTION("LV2 activate #2");
  2173. }
  2174. }
  2175. fFirstActive = true;
  2176. }
  2177. void deactivate() noexcept override
  2178. {
  2179. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2180. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2181. if (fDescriptor->deactivate != nullptr)
  2182. {
  2183. try {
  2184. fDescriptor->deactivate(fHandle);
  2185. } CARLA_SAFE_EXCEPTION("LV2 deactivate");
  2186. if (fHandle2 != nullptr)
  2187. {
  2188. try {
  2189. fDescriptor->deactivate(fHandle2);
  2190. } CARLA_SAFE_EXCEPTION("LV2 deactivate #2");
  2191. }
  2192. }
  2193. }
  2194. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  2195. {
  2196. // --------------------------------------------------------------------------------------------------------
  2197. // Check if active
  2198. if (! pData->active)
  2199. {
  2200. // disable any output sound
  2201. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2202. FloatVectorOperations::clear(audioOut[i], static_cast<int>(frames));
  2203. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2204. FloatVectorOperations::clear(cvOut[i], static_cast<int>(frames));
  2205. return;
  2206. }
  2207. // --------------------------------------------------------------------------------------------------------
  2208. // Event itenerators from different APIs (input)
  2209. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  2210. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  2211. LV2_MIDIState evInMidiStates[fEventsIn.count];
  2212. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2213. {
  2214. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2215. {
  2216. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  2217. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  2218. }
  2219. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2220. {
  2221. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  2222. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  2223. }
  2224. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2225. {
  2226. fEventsIn.data[i].midi.event_count = 0;
  2227. fEventsIn.data[i].midi.size = 0;
  2228. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  2229. evInMidiStates[i].frame_count = frames;
  2230. evInMidiStates[i].position = 0;
  2231. }
  2232. }
  2233. for (uint32_t i=0; i < fEventsOut.count; ++i)
  2234. {
  2235. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2236. {
  2237. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  2238. }
  2239. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2240. {
  2241. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  2242. }
  2243. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2244. {
  2245. // not needed
  2246. }
  2247. }
  2248. // --------------------------------------------------------------------------------------------------------
  2249. // Check if needs reset
  2250. if (pData->needsReset)
  2251. {
  2252. uint8_t midiData[3] = { 0, 0, 0 };
  2253. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  2254. {
  2255. const uint32_t j = fEventsIn.ctrlIndex;
  2256. CARLA_SAFE_ASSERT(j < fEventsIn.count);
  2257. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2258. {
  2259. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  2260. {
  2261. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2262. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2263. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2264. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2265. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2266. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2267. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2268. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2269. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (i & MIDI_CHANNEL_BIT));
  2270. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2271. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2272. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2273. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2274. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2275. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2276. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2277. }
  2278. }
  2279. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2280. {
  2281. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2282. {
  2283. midiData[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  2284. midiData[1] = k;
  2285. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2286. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2287. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2288. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2289. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2290. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2291. }
  2292. }
  2293. }
  2294. pData->needsReset = false;
  2295. }
  2296. // --------------------------------------------------------------------------------------------------------
  2297. // TimeInfo
  2298. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2299. if (fFirstActive || fLastTimeInfo != timeInfo)
  2300. {
  2301. bool doPostRt;
  2302. int32_t rindex;
  2303. // update input ports
  2304. for (uint32_t k=0; k < pData->param.count; ++k)
  2305. {
  2306. if (pData->param.data[k].type != PARAMETER_INPUT)
  2307. continue;
  2308. if (pData->param.special[k] != PARAMETER_SPECIAL_TIME)
  2309. continue;
  2310. doPostRt = false;
  2311. rindex = pData->param.data[k].rindex;
  2312. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2313. switch (fRdfDescriptor->Ports[rindex].Designation)
  2314. {
  2315. // Non-BBT
  2316. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2317. if (fLastTimeInfo.playing != timeInfo.playing)
  2318. {
  2319. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2320. doPostRt = true;
  2321. }
  2322. break;
  2323. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2324. if (fLastTimeInfo.frame != timeInfo.frame)
  2325. {
  2326. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2327. doPostRt = true;
  2328. }
  2329. break;
  2330. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2331. break;
  2332. // BBT
  2333. case LV2_PORT_DESIGNATION_TIME_BAR:
  2334. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2335. {
  2336. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2337. doPostRt = true;
  2338. }
  2339. break;
  2340. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2341. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2342. fLastTimeInfo.bbt.beat != timeInfo.bbt.beat))
  2343. {
  2344. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2345. doPostRt = true;
  2346. }
  2347. break;
  2348. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2349. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2350. {
  2351. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2352. doPostRt = true;
  2353. }
  2354. break;
  2355. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2356. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && carla_isNotEqual(fLastTimeInfo.bbt.beatType, timeInfo.bbt.beatType))
  2357. {
  2358. fParamBuffers[k] = timeInfo.bbt.beatType;
  2359. doPostRt = true;
  2360. }
  2361. break;
  2362. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2363. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && carla_isNotEqual(fLastTimeInfo.bbt.beatsPerBar, timeInfo.bbt.beatsPerBar))
  2364. {
  2365. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2366. doPostRt = true;
  2367. }
  2368. break;
  2369. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2370. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && carla_isNotEqual(fLastTimeInfo.bbt.beatsPerMinute, timeInfo.bbt.beatsPerMinute))
  2371. {
  2372. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2373. doPostRt = true;
  2374. }
  2375. break;
  2376. case LV2_PORT_DESIGNATION_TIME_TICKS_PER_BEAT:
  2377. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && carla_isNotEqual(fLastTimeInfo.bbt.ticksPerBeat, timeInfo.bbt.ticksPerBeat))
  2378. {
  2379. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.ticksPerBeat);
  2380. doPostRt = true;
  2381. }
  2382. break;
  2383. }
  2384. if (doPostRt)
  2385. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2386. }
  2387. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2388. {
  2389. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2390. continue;
  2391. uint8_t timeInfoBuf[256];
  2392. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2393. LV2_Atom_Forge_Frame forgeFrame;
  2394. lv2_atom_forge_object(&fAtomForge, &forgeFrame, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_TIME_POSITION);
  2395. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2396. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2397. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2398. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2399. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2400. {
  2401. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2402. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2403. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2404. 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)));
  2405. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2406. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat - 1);
  2407. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2408. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2409. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2410. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2411. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2412. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2413. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT);
  2414. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.ticksPerBeat);
  2415. }
  2416. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2417. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2418. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2419. // send only deprecated blank object for now
  2420. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, CARLA_URI_MAP_ID_ATOM_BLANK, atom->size, LV2_ATOM_BODY_CONST(atom));
  2421. // for atom:object
  2422. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2423. }
  2424. pData->postRtEvents.trySplice();
  2425. carla_copyStruct(fLastTimeInfo, timeInfo);
  2426. }
  2427. // --------------------------------------------------------------------------------------------------------
  2428. // Event Input and Processing
  2429. if (fEventsIn.ctrl != nullptr)
  2430. {
  2431. // ----------------------------------------------------------------------------------------------------
  2432. // Message Input
  2433. if (fAtomBufferIn.tryLock())
  2434. {
  2435. if (fAtomBufferIn.isDataAvailableForReading())
  2436. {
  2437. const LV2_Atom* atom;
  2438. uint32_t j, portIndex;
  2439. for (; fAtomBufferIn.get(atom, portIndex);)
  2440. {
  2441. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2442. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  2443. {
  2444. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2445. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2446. }
  2447. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2448. {
  2449. carla_stdout("Event input buffer full, at least 1 message lost");
  2450. continue;
  2451. }
  2452. }
  2453. }
  2454. fAtomBufferIn.unlock();
  2455. }
  2456. // ----------------------------------------------------------------------------------------------------
  2457. // MIDI Input (External)
  2458. if (pData->extNotes.mutex.tryLock())
  2459. {
  2460. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2461. {
  2462. // does not handle MIDI
  2463. pData->extNotes.data.clear();
  2464. }
  2465. else
  2466. {
  2467. const uint32_t j = fEventsIn.ctrlIndex;
  2468. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  2469. {
  2470. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  2471. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2472. uint8_t midiEvent[3];
  2473. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2474. midiEvent[1] = note.note;
  2475. midiEvent[2] = note.velo;
  2476. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2477. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2478. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2479. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2480. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2481. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2482. }
  2483. pData->extNotes.data.clear();
  2484. }
  2485. pData->extNotes.mutex.unlock();
  2486. } // End of MIDI Input (External)
  2487. // ----------------------------------------------------------------------------------------------------
  2488. // Event Input (System)
  2489. #ifndef BUILD_BRIDGE
  2490. bool allNotesOffSent = false;
  2491. #endif
  2492. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2493. uint32_t startTime = 0;
  2494. uint32_t timeOffset = 0;
  2495. uint32_t nextBankId;
  2496. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2497. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2498. else
  2499. nextBankId = 0;
  2500. const uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2501. for (uint32_t i=0; i < numEvents; ++i)
  2502. {
  2503. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2504. if (event.time >= frames)
  2505. continue;
  2506. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2507. if (isSampleAccurate && event.time > timeOffset)
  2508. {
  2509. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  2510. {
  2511. startTime = 0;
  2512. timeOffset = event.time;
  2513. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2514. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2515. else
  2516. nextBankId = 0;
  2517. // reset iters
  2518. const uint32_t j = fEventsIn.ctrlIndex;
  2519. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2520. {
  2521. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2522. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2523. }
  2524. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2525. {
  2526. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2527. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2528. }
  2529. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2530. {
  2531. fEventsIn.data[j].midi.event_count = 0;
  2532. fEventsIn.data[j].midi.size = 0;
  2533. evInMidiStates[j].position = event.time;
  2534. }
  2535. }
  2536. else
  2537. startTime += timeOffset;
  2538. }
  2539. switch (event.type)
  2540. {
  2541. case kEngineEventTypeNull:
  2542. break;
  2543. case kEngineEventTypeControl: {
  2544. const EngineControlEvent& ctrlEvent(event.ctrl);
  2545. switch (ctrlEvent.type)
  2546. {
  2547. case kEngineControlEventTypeNull:
  2548. break;
  2549. case kEngineControlEventTypeParameter: {
  2550. #ifndef BUILD_BRIDGE
  2551. // Control backend stuff
  2552. if (event.channel == pData->ctrlChannel)
  2553. {
  2554. float value;
  2555. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2556. {
  2557. value = ctrlEvent.value;
  2558. setDryWet(value, false, false);
  2559. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2560. }
  2561. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2562. {
  2563. value = ctrlEvent.value*127.0f/100.0f;
  2564. setVolume(value, false, false);
  2565. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2566. }
  2567. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2568. {
  2569. float left, right;
  2570. value = ctrlEvent.value/0.5f - 1.0f;
  2571. if (value < 0.0f)
  2572. {
  2573. left = -1.0f;
  2574. right = (value*2.0f)+1.0f;
  2575. }
  2576. else if (value > 0.0f)
  2577. {
  2578. left = (value*2.0f)-1.0f;
  2579. right = 1.0f;
  2580. }
  2581. else
  2582. {
  2583. left = -1.0f;
  2584. right = 1.0f;
  2585. }
  2586. setBalanceLeft(left, false, false);
  2587. setBalanceRight(right, false, false);
  2588. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2589. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2590. }
  2591. }
  2592. #endif
  2593. // Control plugin parameters
  2594. uint32_t k;
  2595. for (k=0; k < pData->param.count; ++k)
  2596. {
  2597. if (pData->param.data[k].midiChannel != event.channel)
  2598. continue;
  2599. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2600. continue;
  2601. if (pData->param.data[k].type != PARAMETER_INPUT)
  2602. continue;
  2603. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2604. continue;
  2605. float value;
  2606. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2607. {
  2608. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2609. }
  2610. else
  2611. {
  2612. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  2613. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  2614. else
  2615. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2616. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2617. value = std::rint(value);
  2618. }
  2619. setParameterValue(k, value, false, false, false);
  2620. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2621. }
  2622. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  2623. {
  2624. uint8_t midiData[3];
  2625. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2626. midiData[1] = uint8_t(ctrlEvent.param);
  2627. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2628. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2629. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2630. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2631. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2632. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2633. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2634. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2635. }
  2636. break;
  2637. } // case kEngineControlEventTypeParameter
  2638. case kEngineControlEventTypeMidiBank:
  2639. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2640. {
  2641. if (event.channel == pData->ctrlChannel)
  2642. nextBankId = ctrlEvent.param;
  2643. }
  2644. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2645. {
  2646. uint8_t midiData[3];
  2647. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2648. midiData[1] = MIDI_CONTROL_BANK_SELECT;
  2649. midiData[2] = uint8_t(ctrlEvent.param);
  2650. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2651. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2652. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2653. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2654. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2655. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2656. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2657. }
  2658. break;
  2659. case kEngineControlEventTypeMidiProgram:
  2660. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  2661. {
  2662. if (event.channel == pData->ctrlChannel)
  2663. {
  2664. const uint32_t nextProgramId(ctrlEvent.param);
  2665. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2666. {
  2667. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2668. {
  2669. const int32_t index(static_cast<int32_t>(k));
  2670. setMidiProgram(index, false, false, false);
  2671. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2672. break;
  2673. }
  2674. }
  2675. }
  2676. }
  2677. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  2678. {
  2679. uint8_t midiData[2];
  2680. midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2681. midiData[1] = uint8_t(ctrlEvent.param);
  2682. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2683. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2684. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiData);
  2685. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2686. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiData);
  2687. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2688. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 2, midiData);
  2689. }
  2690. break;
  2691. case kEngineControlEventTypeAllSoundOff:
  2692. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2693. {
  2694. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2695. uint8_t midiData[3];
  2696. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2697. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2698. midiData[2] = 0;
  2699. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2700. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2701. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2702. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2703. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2704. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2705. }
  2706. break;
  2707. case kEngineControlEventTypeAllNotesOff:
  2708. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2709. {
  2710. #ifndef BUILD_BRIDGE
  2711. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2712. {
  2713. allNotesOffSent = true;
  2714. sendMidiAllNotesOffToCallback();
  2715. }
  2716. #endif
  2717. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2718. uint8_t midiData[3];
  2719. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  2720. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2721. midiData[2] = 0;
  2722. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2723. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2724. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2725. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2726. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2727. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2728. }
  2729. break;
  2730. } // switch (ctrlEvent.type)
  2731. break;
  2732. } // case kEngineEventTypeControl
  2733. case kEngineEventTypeMidi: {
  2734. const EngineMidiEvent& midiEvent(event.midi);
  2735. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  2736. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  2737. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2738. continue;
  2739. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2740. continue;
  2741. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2742. continue;
  2743. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2744. continue;
  2745. // Fix bad note-off (per LV2 spec)
  2746. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  2747. status = MIDI_STATUS_NOTE_OFF;
  2748. const uint32_t j = fEventsIn.ctrlIndex;
  2749. const uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2750. // put back channel in data
  2751. uint8_t midiData2[midiEvent.size];
  2752. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  2753. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  2754. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2755. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2756. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2757. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiData2);
  2758. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2759. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiData2);
  2760. if (status == MIDI_STATUS_NOTE_ON)
  2761. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiData[1], midiData[2]);
  2762. else if (status == MIDI_STATUS_NOTE_OFF)
  2763. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiData[1], 0.0f);
  2764. } break;
  2765. } // switch (event.type)
  2766. }
  2767. pData->postRtEvents.trySplice();
  2768. if (frames > timeOffset)
  2769. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  2770. } // End of Event Input and Processing
  2771. // --------------------------------------------------------------------------------------------------------
  2772. // Plugin processing (no events)
  2773. else
  2774. {
  2775. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  2776. } // End of Plugin processing (no events)
  2777. // --------------------------------------------------------------------------------------------------------
  2778. // MIDI Output
  2779. if (fEventsOut.ctrl != nullptr)
  2780. {
  2781. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2782. {
  2783. const LV2_Atom_Event* ev;
  2784. LV2_Atom_Buffer_Iterator iter;
  2785. uint8_t* data;
  2786. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2787. for (;;)
  2788. {
  2789. data = nullptr;
  2790. ev = lv2_atom_buffer_get(&iter, &data);
  2791. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2792. break;
  2793. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2794. {
  2795. if (fEventsOut.ctrl->port != nullptr)
  2796. {
  2797. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2798. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2799. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2800. }
  2801. }
  2802. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2803. {
  2804. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2805. fAtomBufferOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2806. }
  2807. lv2_atom_buffer_increment(&iter);
  2808. }
  2809. }
  2810. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2811. {
  2812. const LV2_Event* ev;
  2813. LV2_Event_Iterator iter;
  2814. uint8_t* data;
  2815. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2816. for (;;)
  2817. {
  2818. data = nullptr;
  2819. ev = lv2_event_get(&iter, &data);
  2820. if (ev == nullptr || data == nullptr)
  2821. break;
  2822. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2823. {
  2824. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2825. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2826. }
  2827. lv2_event_increment(&iter);
  2828. }
  2829. }
  2830. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2831. {
  2832. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2833. uint32_t eventSize;
  2834. double eventTime;
  2835. uchar* eventData;
  2836. for (;;)
  2837. {
  2838. eventSize = 0;
  2839. eventTime = 0.0;
  2840. eventData = nullptr;
  2841. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2842. if (eventData == nullptr || eventSize == 0)
  2843. break;
  2844. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2845. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2846. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2847. lv2midi_step(&state);
  2848. }
  2849. }
  2850. }
  2851. #ifndef BUILD_BRIDGE
  2852. // --------------------------------------------------------------------------------------------------------
  2853. // Control Output
  2854. if (pData->event.portOut != nullptr)
  2855. {
  2856. uint8_t channel;
  2857. uint16_t param;
  2858. float value;
  2859. for (uint32_t k=0; k < pData->param.count; ++k)
  2860. {
  2861. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2862. continue;
  2863. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2864. if (pData->param.data[k].midiCC > 0)
  2865. {
  2866. channel = pData->param.data[k].midiChannel;
  2867. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2868. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2869. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2870. }
  2871. }
  2872. } // End of Control Output
  2873. #endif
  2874. // --------------------------------------------------------------------------------------------------------
  2875. // Final work
  2876. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2877. {
  2878. fExt.worker->end_run(fHandle);
  2879. if (fHandle2 != nullptr)
  2880. fExt.worker->end_run(fHandle2);
  2881. }
  2882. fFirstActive = false;
  2883. // --------------------------------------------------------------------------------------------------------
  2884. }
  2885. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  2886. {
  2887. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2888. if (pData->audioIn.count > 0)
  2889. {
  2890. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  2891. }
  2892. if (pData->audioOut.count > 0)
  2893. {
  2894. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  2895. }
  2896. if (pData->cvIn.count > 0)
  2897. {
  2898. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  2899. }
  2900. if (pData->cvOut.count > 0)
  2901. {
  2902. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  2903. }
  2904. // --------------------------------------------------------------------------------------------------------
  2905. // Try lock, silence otherwise
  2906. if (pData->engine->isOffline())
  2907. {
  2908. pData->singleMutex.lock();
  2909. }
  2910. else if (! pData->singleMutex.tryLock())
  2911. {
  2912. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2913. {
  2914. for (uint32_t k=0; k < frames; ++k)
  2915. audioOut[i][k+timeOffset] = 0.0f;
  2916. }
  2917. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2918. {
  2919. for (uint32_t k=0; k < frames; ++k)
  2920. cvOut[i][k+timeOffset] = 0.0f;
  2921. }
  2922. return false;
  2923. }
  2924. const int iframes(static_cast<int>(frames));
  2925. // --------------------------------------------------------------------------------------------------------
  2926. // Set audio buffers
  2927. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2928. FloatVectorOperations::copy(fAudioInBuffers[i], audioIn[i]+timeOffset, iframes);
  2929. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2930. FloatVectorOperations::clear(fAudioOutBuffers[i], iframes);
  2931. // --------------------------------------------------------------------------------------------------------
  2932. // Set CV buffers
  2933. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  2934. FloatVectorOperations::copy(fCvInBuffers[i], cvIn[i]+timeOffset, iframes);
  2935. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  2936. FloatVectorOperations::clear(fCvOutBuffers[i], iframes);
  2937. // --------------------------------------------------------------------------------------------------------
  2938. // Run plugin
  2939. fDescriptor->run(fHandle, frames);
  2940. if (fHandle2 != nullptr)
  2941. fDescriptor->run(fHandle2, frames);
  2942. // --------------------------------------------------------------------------------------------------------
  2943. // Handle trigger parameters
  2944. for (uint32_t k=0; k < pData->param.count; ++k)
  2945. {
  2946. if (pData->param.data[k].type != PARAMETER_INPUT)
  2947. continue;
  2948. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2949. {
  2950. if (carla_isNotEqual(fParamBuffers[k], pData->param.ranges[k].def))
  2951. {
  2952. fParamBuffers[k] = pData->param.ranges[k].def;
  2953. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2954. }
  2955. }
  2956. }
  2957. pData->postRtEvents.trySplice();
  2958. #ifndef BUILD_BRIDGE
  2959. // --------------------------------------------------------------------------------------------------------
  2960. // Post-processing (dry/wet, volume and balance)
  2961. {
  2962. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  2963. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  2964. const bool isMono = (pData->audioIn.count == 1);
  2965. bool isPair;
  2966. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2967. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2968. {
  2969. // Dry/Wet
  2970. if (doDryWet)
  2971. {
  2972. const uint32_t c = isMono ? 0 : i;
  2973. for (uint32_t k=0; k < frames; ++k)
  2974. {
  2975. if (k < pData->latency.frames)
  2976. bufValue = pData->latency.buffers[c][k];
  2977. else if (pData->latency.frames < frames)
  2978. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  2979. else
  2980. bufValue = fAudioInBuffers[c][k];
  2981. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2982. }
  2983. }
  2984. // Balance
  2985. if (doBalance)
  2986. {
  2987. isPair = (i % 2 == 0);
  2988. if (isPair)
  2989. {
  2990. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2991. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], iframes);
  2992. }
  2993. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2994. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2995. for (uint32_t k=0; k < frames; ++k)
  2996. {
  2997. if (isPair)
  2998. {
  2999. // left
  3000. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  3001. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  3002. }
  3003. else
  3004. {
  3005. // right
  3006. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  3007. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  3008. }
  3009. }
  3010. }
  3011. // Volume (and buffer copy)
  3012. {
  3013. for (uint32_t k=0; k < frames; ++k)
  3014. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  3015. }
  3016. }
  3017. } // End of Post-processing
  3018. // --------------------------------------------------------------------------------------------------------
  3019. // Save latency values for next callback
  3020. if (const uint32_t latframes = pData->latency.frames)
  3021. {
  3022. CARLA_SAFE_ASSERT(timeOffset == 0);
  3023. if (latframes <= frames)
  3024. {
  3025. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3026. FloatVectorOperations::copy(pData->latency.buffers[i], audioIn[i]+(frames-latframes), static_cast<int>(latframes));
  3027. }
  3028. else
  3029. {
  3030. const uint32_t diff = pData->latency.frames-frames;
  3031. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  3032. {
  3033. // push back buffer by 'frames'
  3034. for (k=0; k < diff; ++k)
  3035. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  3036. // put current input at the end
  3037. for (uint32_t j=0; k < latframes; ++j, ++k)
  3038. pData->latency.buffers[i][k] = audioIn[i][j];
  3039. }
  3040. }
  3041. }
  3042. #else // BUILD_BRIDGE
  3043. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3044. {
  3045. for (uint32_t k=0; k < frames; ++k)
  3046. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  3047. }
  3048. #endif
  3049. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3050. {
  3051. for (uint32_t k=0; k < frames; ++k)
  3052. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  3053. }
  3054. // --------------------------------------------------------------------------------------------------------
  3055. pData->singleMutex.unlock();
  3056. return true;
  3057. }
  3058. void bufferSizeChanged(const uint32_t newBufferSize) override
  3059. {
  3060. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  3061. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - start", newBufferSize);
  3062. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3063. {
  3064. if (fAudioInBuffers[i] != nullptr)
  3065. delete[] fAudioInBuffers[i];
  3066. fAudioInBuffers[i] = new float[newBufferSize];
  3067. }
  3068. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3069. {
  3070. if (fAudioOutBuffers[i] != nullptr)
  3071. delete[] fAudioOutBuffers[i];
  3072. fAudioOutBuffers[i] = new float[newBufferSize];
  3073. }
  3074. if (fHandle2 == nullptr)
  3075. {
  3076. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3077. {
  3078. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  3079. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  3080. }
  3081. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3082. {
  3083. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  3084. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  3085. }
  3086. }
  3087. else
  3088. {
  3089. if (pData->audioIn.count > 0)
  3090. {
  3091. CARLA_ASSERT(pData->audioIn.count == 2);
  3092. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  3093. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  3094. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  3095. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  3096. }
  3097. if (pData->audioOut.count > 0)
  3098. {
  3099. CARLA_ASSERT(pData->audioOut.count == 2);
  3100. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  3101. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  3102. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  3103. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  3104. }
  3105. }
  3106. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3107. {
  3108. if (fCvInBuffers[i] != nullptr)
  3109. delete[] fCvInBuffers[i];
  3110. fCvInBuffers[i] = new float[newBufferSize];
  3111. fDescriptor->connect_port(fHandle, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3112. if (fHandle2 != nullptr)
  3113. fDescriptor->connect_port(fHandle2, pData->cvIn.ports[i].rindex, fCvInBuffers[i]);
  3114. }
  3115. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3116. {
  3117. if (fCvOutBuffers[i] != nullptr)
  3118. delete[] fCvOutBuffers[i];
  3119. fCvOutBuffers[i] = new float[newBufferSize];
  3120. fDescriptor->connect_port(fHandle, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3121. if (fHandle2 != nullptr)
  3122. fDescriptor->connect_port(fHandle2, pData->cvOut.ports[i].rindex, fCvOutBuffers[i]);
  3123. }
  3124. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  3125. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  3126. {
  3127. fLv2Options.maxBufferSize = fLv2Options.nominalBufferSize = newBufferSizeInt;
  3128. if (fLv2Options.minBufferSize != 1)
  3129. fLv2Options.minBufferSize = newBufferSizeInt;
  3130. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3131. {
  3132. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MaxBlockLenth]);
  3133. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::NominalBlockLenth]);
  3134. if (fLv2Options.minBufferSize != 1)
  3135. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::MinBlockLenth]);
  3136. }
  3137. }
  3138. carla_debug("CarlaPluginLV2::bufferSizeChanged(%i) - end", newBufferSize);
  3139. }
  3140. void sampleRateChanged(const double newSampleRate) override
  3141. {
  3142. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  3143. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - start", newSampleRate);
  3144. if (carla_isNotEqual(fLv2Options.sampleRate, newSampleRate))
  3145. {
  3146. fLv2Options.sampleRate = newSampleRate;
  3147. if (fExt.options != nullptr && fExt.options->set != nullptr)
  3148. fExt.options->set(fHandle, &fLv2Options.opts[CarlaPluginLV2Options::SampleRate]);
  3149. }
  3150. for (uint32_t k=0; k < pData->param.count; ++k)
  3151. {
  3152. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  3153. {
  3154. fParamBuffers[k] = static_cast<float>(newSampleRate);
  3155. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3156. break;
  3157. }
  3158. }
  3159. carla_debug("CarlaPluginLV2::sampleRateChanged(%g) - end", newSampleRate);
  3160. }
  3161. void offlineModeChanged(const bool isOffline) override
  3162. {
  3163. for (uint32_t k=0; k < pData->param.count; ++k)
  3164. {
  3165. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_FREEWHEEL)
  3166. {
  3167. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  3168. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  3169. break;
  3170. }
  3171. }
  3172. }
  3173. // -------------------------------------------------------------------
  3174. // Plugin buffers
  3175. void initBuffers() const noexcept override
  3176. {
  3177. fEventsIn.initBuffers();
  3178. fEventsOut.initBuffers();
  3179. CarlaPlugin::initBuffers();
  3180. }
  3181. void clearBuffers() noexcept override
  3182. {
  3183. carla_debug("CarlaPluginLV2::clearBuffers() - start");
  3184. if (fAudioInBuffers != nullptr)
  3185. {
  3186. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  3187. {
  3188. if (fAudioInBuffers[i] != nullptr)
  3189. {
  3190. delete[] fAudioInBuffers[i];
  3191. fAudioInBuffers[i] = nullptr;
  3192. }
  3193. }
  3194. delete[] fAudioInBuffers;
  3195. fAudioInBuffers = nullptr;
  3196. }
  3197. if (fAudioOutBuffers != nullptr)
  3198. {
  3199. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  3200. {
  3201. if (fAudioOutBuffers[i] != nullptr)
  3202. {
  3203. delete[] fAudioOutBuffers[i];
  3204. fAudioOutBuffers[i] = nullptr;
  3205. }
  3206. }
  3207. delete[] fAudioOutBuffers;
  3208. fAudioOutBuffers = nullptr;
  3209. }
  3210. if (fCvInBuffers != nullptr)
  3211. {
  3212. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  3213. {
  3214. if (fCvInBuffers[i] != nullptr)
  3215. {
  3216. delete[] fCvInBuffers[i];
  3217. fCvInBuffers[i] = nullptr;
  3218. }
  3219. }
  3220. delete[] fCvInBuffers;
  3221. fCvInBuffers = nullptr;
  3222. }
  3223. if (fCvOutBuffers != nullptr)
  3224. {
  3225. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  3226. {
  3227. if (fCvOutBuffers[i] != nullptr)
  3228. {
  3229. delete[] fCvOutBuffers[i];
  3230. fCvOutBuffers[i] = nullptr;
  3231. }
  3232. }
  3233. delete[] fCvOutBuffers;
  3234. fCvOutBuffers = nullptr;
  3235. }
  3236. if (fParamBuffers != nullptr)
  3237. {
  3238. delete[] fParamBuffers;
  3239. fParamBuffers = nullptr;
  3240. }
  3241. fEventsIn.clear();
  3242. fEventsOut.clear();
  3243. CarlaPlugin::clearBuffers();
  3244. carla_debug("CarlaPluginLV2::clearBuffers() - end");
  3245. }
  3246. // -------------------------------------------------------------------
  3247. // Post-poned UI Stuff
  3248. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3249. {
  3250. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3251. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3252. if (fUI.type == UI::TYPE_BRIDGE)
  3253. {
  3254. if (fPipeServer.isPipeRunning())
  3255. fPipeServer.writeControlMessage(static_cast<uint32_t>(pData->param.data[index].rindex), value);
  3256. }
  3257. else
  3258. {
  3259. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && ! fNeedsUiClose)
  3260. {
  3261. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3262. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3263. }
  3264. }
  3265. }
  3266. void uiMidiProgramChange(const uint32_t index) noexcept override
  3267. {
  3268. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3269. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3270. if (fUI.type == UI::TYPE_BRIDGE)
  3271. {
  3272. if (fPipeServer.isPipeRunning())
  3273. fPipeServer.writeMidiProgramMessage(pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3274. }
  3275. else
  3276. {
  3277. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr && ! fNeedsUiClose)
  3278. fExt.uiprograms->select_program(fUI.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3279. }
  3280. }
  3281. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3282. {
  3283. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3284. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3285. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3286. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3287. if (fUI.type == UI::TYPE_BRIDGE)
  3288. {
  3289. if (fPipeServer.isPipeRunning())
  3290. fPipeServer.writeMidiNoteMessage(false, channel, note, velo);
  3291. }
  3292. else
  3293. {
  3294. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3295. {
  3296. LV2_Atom_MidiEvent midiEv;
  3297. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3298. midiEv.atom.size = 3;
  3299. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  3300. midiEv.data[1] = note;
  3301. midiEv.data[2] = velo;
  3302. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, &midiEv);
  3303. }
  3304. }
  3305. }
  3306. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3307. {
  3308. CARLA_SAFE_ASSERT_RETURN(fUI.type != UI::TYPE_NULL,);
  3309. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3310. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3311. if (fUI.type == UI::TYPE_BRIDGE)
  3312. {
  3313. if (fPipeServer.isPipeRunning())
  3314. fPipeServer.writeMidiNoteMessage(false, channel, note, 0);
  3315. }
  3316. else
  3317. {
  3318. if (fUI.handle != nullptr && fUI.descriptor != nullptr && fUI.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr && ! fNeedsUiClose)
  3319. {
  3320. LV2_Atom_MidiEvent midiEv;
  3321. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3322. midiEv.atom.size = 3;
  3323. midiEv.data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  3324. midiEv.data[1] = note;
  3325. midiEv.data[2] = 0;
  3326. fUI.descriptor->port_event(fUI.handle, fEventsIn.ctrl->rindex, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, &midiEv);
  3327. }
  3328. }
  3329. }
  3330. // -------------------------------------------------------------------
  3331. bool isRealtimeSafe() const noexcept
  3332. {
  3333. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3334. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3335. {
  3336. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3337. return true;
  3338. }
  3339. return false;
  3340. }
  3341. // -------------------------------------------------------------------
  3342. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3343. {
  3344. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3345. #ifndef LV2_UIS_ONLY_INPROCESS
  3346. const LV2_RDF_UI* const rdfUI(&fRdfDescriptor->UIs[uiId]);
  3347. for (uint32_t i=0; i < rdfUI->FeatureCount; ++i)
  3348. {
  3349. const LV2_RDF_Feature& feat(rdfUI->Features[i]);
  3350. if (! feat.Required)
  3351. continue;
  3352. if (std::strcmp(feat.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3353. return false;
  3354. if (std::strcmp(feat.URI, LV2_DATA_ACCESS_URI) == 0)
  3355. return false;
  3356. }
  3357. // Calf UIs are mostly useless without their special graphs
  3358. // but they can be crashy under certain conditions, so follow user preferences
  3359. if (std::strstr(rdfUI->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3360. return pData->engine->getOptions().preferUiBridges;
  3361. return true;
  3362. #else
  3363. return false;
  3364. #endif
  3365. }
  3366. bool isUiResizable() const noexcept
  3367. {
  3368. CARLA_SAFE_ASSERT_RETURN(fUI.rdfDescriptor != nullptr, false);
  3369. for (uint32_t i=0; i < fUI.rdfDescriptor->FeatureCount; ++i)
  3370. {
  3371. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3372. return false;
  3373. if (std::strcmp(fUI.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3374. return false;
  3375. }
  3376. return true;
  3377. }
  3378. const char* getUiBridgeBinary(const LV2_Property type) const
  3379. {
  3380. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3381. if (bridgeBinary.isEmpty())
  3382. return nullptr;
  3383. switch (type)
  3384. {
  3385. case LV2_UI_GTK2:
  3386. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk2";
  3387. break;
  3388. case LV2_UI_GTK3:
  3389. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-gtk3";
  3390. break;
  3391. case LV2_UI_QT4:
  3392. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt4";
  3393. break;
  3394. case LV2_UI_QT5:
  3395. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-qt5";
  3396. break;
  3397. case LV2_UI_COCOA:
  3398. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-cocoa";
  3399. break;
  3400. case LV2_UI_WINDOWS:
  3401. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-windows";
  3402. break;
  3403. case LV2_UI_X11:
  3404. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-x11";
  3405. break;
  3406. case LV2_UI_EXTERNAL:
  3407. case LV2_UI_OLD_EXTERNAL:
  3408. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-external";
  3409. break;
  3410. case LV2_UI_MOD:
  3411. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-lv2-modgui";
  3412. break;
  3413. default:
  3414. return nullptr;
  3415. }
  3416. #ifdef CARLA_OS_WIN
  3417. bridgeBinary += ".exe";
  3418. #endif
  3419. if (! File(bridgeBinary.buffer()).existsAsFile())
  3420. return nullptr;
  3421. return bridgeBinary.dup();
  3422. }
  3423. // -------------------------------------------------------------------
  3424. void recheckExtensions()
  3425. {
  3426. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3427. carla_debug("CarlaPluginLV2::recheckExtensions()");
  3428. fExt.options = nullptr;
  3429. fExt.programs = nullptr;
  3430. fExt.state = nullptr;
  3431. fExt.worker = nullptr;
  3432. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3433. {
  3434. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->Extensions[i] != nullptr);
  3435. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3436. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3437. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3438. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3439. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3440. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3441. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3442. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3443. else
  3444. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3445. }
  3446. if (fDescriptor->extension_data != nullptr)
  3447. {
  3448. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3449. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3450. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3451. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3452. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3453. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3454. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3455. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3456. // check if invalid
  3457. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3458. fExt.options = nullptr;
  3459. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3460. fExt.programs = nullptr;
  3461. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3462. fExt.state = nullptr;
  3463. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3464. fExt.worker = nullptr;
  3465. }
  3466. CARLA_SAFE_ASSERT_RETURN(fLatencyIndex == -1,);
  3467. int32_t iCtrl=0;
  3468. for (uint32_t i=0, count=fRdfDescriptor->PortCount; i<count; ++i)
  3469. {
  3470. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3471. if (! LV2_IS_PORT_CONTROL(portTypes))
  3472. continue;
  3473. const ScopedValueSetter<int32_t> svs(iCtrl, iCtrl, iCtrl+1);
  3474. if (! LV2_IS_PORT_OUTPUT(portTypes))
  3475. continue;
  3476. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  3477. if (! LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  3478. continue;
  3479. fLatencyIndex = iCtrl;
  3480. break;
  3481. }
  3482. }
  3483. // -------------------------------------------------------------------
  3484. void updateUi()
  3485. {
  3486. CARLA_SAFE_ASSERT_RETURN(fUI.handle != nullptr,);
  3487. CARLA_SAFE_ASSERT_RETURN(fUI.descriptor != nullptr,);
  3488. carla_debug("CarlaPluginLV2::updateUi()");
  3489. // update midi program
  3490. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3491. {
  3492. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3493. fExt.uiprograms->select_program(fUI.handle, curData.bank, curData.program);
  3494. }
  3495. // update control ports
  3496. if (fUI.descriptor->port_event != nullptr)
  3497. {
  3498. float value;
  3499. for (uint32_t i=0; i < pData->param.count; ++i)
  3500. {
  3501. value = getParameterValue(i);
  3502. fUI.descriptor->port_event(fUI.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3503. }
  3504. }
  3505. }
  3506. // -------------------------------------------------------------------
  3507. LV2_URID getCustomURID(const char* const uri)
  3508. {
  3509. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3510. carla_debug("CarlaPluginLV2::getCustomURID(\"%s\")", uri);
  3511. const std::string s_uri(uri);
  3512. const std::ptrdiff_t s_pos(std::find(fCustomURIDs.begin(), fCustomURIDs.end(), s_uri) - fCustomURIDs.begin());
  3513. if (s_pos <= 0 || s_pos >= UINT32_MAX)
  3514. return CARLA_URI_MAP_ID_NULL;
  3515. const LV2_URID urid = static_cast<LV2_URID>(s_pos);
  3516. const LV2_URID uriCount = static_cast<LV2_URID>(fCustomURIDs.size());
  3517. if (urid < uriCount)
  3518. return urid;
  3519. CARLA_SAFE_ASSERT(urid == uriCount);
  3520. fCustomURIDs.push_back(uri);
  3521. if (fUI.type == UI::TYPE_BRIDGE && fPipeServer.isPipeRunning())
  3522. fPipeServer.writeLv2UridMessage(uriCount, uri);
  3523. return urid;
  3524. }
  3525. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3526. {
  3527. static const char* const sFallback = "urn:null";
  3528. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, sFallback);
  3529. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.size(), sFallback);
  3530. carla_debug("CarlaPluginLV2::getCustomURIString(%i)", urid);
  3531. return fCustomURIDs[urid].c_str();
  3532. }
  3533. // -------------------------------------------------------------------
  3534. void handleProgramChanged(const int32_t index)
  3535. {
  3536. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3537. carla_debug("CarlaPluginLV2::handleProgramChanged(%i)", index);
  3538. if (index == -1)
  3539. {
  3540. const ScopedSingleProcessLocker spl(this, true);
  3541. return reloadPrograms(false);
  3542. }
  3543. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3544. {
  3545. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3546. {
  3547. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3548. if (pData->midiprog.data[index].name != nullptr)
  3549. delete[] pData->midiprog.data[index].name;
  3550. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3551. if (index == pData->midiprog.current)
  3552. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3553. else
  3554. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3555. }
  3556. }
  3557. }
  3558. // -------------------------------------------------------------------
  3559. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3560. {
  3561. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3562. carla_debug("CarlaPluginLV2::handleResizePort(%i, " P_SIZE ")", index, size);
  3563. // TODO
  3564. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3565. (void)index;
  3566. }
  3567. // -------------------------------------------------------------------
  3568. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3569. {
  3570. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3571. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3572. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3573. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3574. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3575. 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);
  3576. const char* const skey(carla_lv2_urid_unmap(this, key));
  3577. const char* const stype(carla_lv2_urid_unmap(this, type));
  3578. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3579. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3580. // Check if we already have this key
  3581. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  3582. {
  3583. CustomData& cData(it.getValue(kCustomDataFallbackNC));
  3584. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  3585. if (std::strcmp(cData.key, skey) == 0)
  3586. {
  3587. // found it
  3588. delete[] cData.value;
  3589. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3590. cData.value = carla_strdup((const char*)value);
  3591. else
  3592. cData.value = CarlaString::asBase64(value, size).dup();
  3593. return LV2_STATE_SUCCESS;
  3594. }
  3595. }
  3596. // Otherwise store it
  3597. CustomData newData;
  3598. newData.type = carla_strdup(stype);
  3599. newData.key = carla_strdup(skey);
  3600. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3601. newData.value = carla_strdup((const char*)value);
  3602. else
  3603. newData.value = CarlaString::asBase64(value, size).dup();
  3604. pData->custom.append(newData);
  3605. return LV2_STATE_SUCCESS;
  3606. }
  3607. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3608. {
  3609. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3610. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3611. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3612. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3613. carla_debug("CarlaPluginLV2::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3614. const char* const skey(carla_lv2_urid_unmap(this, key));
  3615. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3616. const char* stype = nullptr;
  3617. const char* stringData = nullptr;
  3618. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  3619. {
  3620. const CustomData& cData(it.getValue(kCustomDataFallback));
  3621. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  3622. if (std::strcmp(cData.key, skey) == 0)
  3623. {
  3624. stype = cData.type;
  3625. stringData = cData.value;
  3626. break;
  3627. }
  3628. }
  3629. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3630. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3631. *type = carla_lv2_urid_map(this, stype);
  3632. *flags = LV2_STATE_IS_POD;
  3633. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3634. {
  3635. *size = std::strlen(stringData);
  3636. return stringData;
  3637. }
  3638. else
  3639. {
  3640. if (fLastStateChunk != nullptr)
  3641. {
  3642. std::free(fLastStateChunk);
  3643. fLastStateChunk = nullptr;
  3644. }
  3645. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stringData));
  3646. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3647. fLastStateChunk = std::malloc(chunk.size());
  3648. CARLA_SAFE_ASSERT_RETURN(fLastStateChunk != nullptr, nullptr);
  3649. std::memcpy(fLastStateChunk, chunk.data(), chunk.size());
  3650. *size = chunk.size();
  3651. return fLastStateChunk;
  3652. }
  3653. }
  3654. // -------------------------------------------------------------------
  3655. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3656. {
  3657. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3658. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3659. carla_debug("CarlaPluginLV2::handleWorkerSchedule(%i, %p)", size, data);
  3660. if (pData->engine->isOffline())
  3661. {
  3662. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3663. return LV2_WORKER_SUCCESS;
  3664. }
  3665. LV2_Atom atom;
  3666. atom.size = size;
  3667. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3668. return fAtomBufferOut.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3669. }
  3670. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3671. {
  3672. carla_debug("CarlaPluginLV2::handleWorkerRespond(%i, %p)", size, data);
  3673. LV2_Atom atom;
  3674. atom.size = size;
  3675. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3676. return fAtomBufferIn.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3677. }
  3678. // -------------------------------------------------------------------
  3679. void handleExternalUIClosed()
  3680. {
  3681. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EXTERNAL,);
  3682. carla_debug("CarlaPluginLV2::handleExternalUIClosed()");
  3683. fNeedsUiClose = true;
  3684. }
  3685. void handlePluginUIClosed() override
  3686. {
  3687. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  3688. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  3689. carla_debug("CarlaPluginLV2::handlePluginUIClosed()");
  3690. fNeedsUiClose = true;
  3691. }
  3692. void handlePluginUIResized(const uint width, const uint height) override
  3693. {
  3694. CARLA_SAFE_ASSERT_RETURN(fUI.type == UI::TYPE_EMBED,);
  3695. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr,);
  3696. carla_debug("CarlaPluginLV2::handlePluginUIResized(%u, %u)", width, height);
  3697. if (fUI.handle != nullptr && fExt.uiresize != nullptr)
  3698. fExt.uiresize->ui_resize(fUI.handle, static_cast<int>(width), static_cast<int>(height));
  3699. }
  3700. // -------------------------------------------------------------------
  3701. uint32_t handleUIPortMap(const char* const symbol) const noexcept
  3702. {
  3703. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3704. carla_debug("CarlaPluginLV2::handleUIPortMap(\"%s\")", symbol);
  3705. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3706. {
  3707. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3708. return i;
  3709. }
  3710. return LV2UI_INVALID_PORT_INDEX;
  3711. }
  3712. int handleUIResize(const int width, const int height)
  3713. {
  3714. CARLA_SAFE_ASSERT_RETURN(fUI.window != nullptr, 1);
  3715. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3716. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3717. carla_debug("CarlaPluginLV2::handleUIResize(%i, %i)", width, height);
  3718. fUI.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3719. return 0;
  3720. }
  3721. void handleUIWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3722. {
  3723. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3724. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3725. carla_debug("CarlaPluginLV2::handleUIWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3726. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  3727. switch (format)
  3728. {
  3729. case CARLA_URI_MAP_ID_NULL: {
  3730. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3731. for (uint32_t i=0; i < pData->param.count; ++i)
  3732. {
  3733. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  3734. continue;
  3735. index = i;
  3736. break;
  3737. }
  3738. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  3739. const float value(*(const float*)buffer);
  3740. //if (carla_isNotEqual(fParamBuffers[index], value))
  3741. setParameterValue(index, value, false, true, true);
  3742. } break;
  3743. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3744. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT: {
  3745. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  3746. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  3747. // plugins sometimes fail on this, not good...
  3748. CARLA_SAFE_ASSERT_INT2(bufferSize == lv2_atom_total_size(atom), bufferSize, atom->size);
  3749. for (uint32_t i=0; i < fEventsIn.count; ++i)
  3750. {
  3751. if (fEventsIn.data[i].rindex != rindex)
  3752. continue;
  3753. index = i;
  3754. break;
  3755. }
  3756. // for bad plugins
  3757. if (index == LV2UI_INVALID_PORT_INDEX)
  3758. {
  3759. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  3760. index = fEventsIn.ctrlIndex;
  3761. }
  3762. fAtomBufferIn.put(atom, index);
  3763. } break;
  3764. default:
  3765. carla_stdout("CarlaPluginLV2::handleUIWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3766. break;
  3767. }
  3768. }
  3769. // -------------------------------------------------------------------
  3770. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3771. {
  3772. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3773. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3774. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3775. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3776. carla_debug("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3777. int32_t rindex = -1;
  3778. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3779. {
  3780. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3781. {
  3782. rindex = static_cast<int32_t>(i);
  3783. break;
  3784. }
  3785. }
  3786. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3787. float paramValue;
  3788. switch (type)
  3789. {
  3790. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3791. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3792. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3793. break;
  3794. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3795. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3796. paramValue = static_cast<float>((*(const double*)value));
  3797. break;
  3798. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3799. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3800. paramValue = (*(const float*)value);
  3801. break;
  3802. case CARLA_URI_MAP_ID_ATOM_INT:
  3803. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3804. paramValue = static_cast<float>((*(const int32_t*)value));
  3805. break;
  3806. case CARLA_URI_MAP_ID_ATOM_LONG:
  3807. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3808. paramValue = static_cast<float>((*(const int64_t*)value));
  3809. break;
  3810. default:
  3811. carla_stdout("CarlaPluginLV2::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3812. return;
  3813. }
  3814. for (uint32_t i=0; i < pData->param.count; ++i)
  3815. {
  3816. if (pData->param.data[i].rindex == rindex)
  3817. {
  3818. setParameterValue(i, paramValue, true, true, true);
  3819. break;
  3820. }
  3821. }
  3822. }
  3823. // -------------------------------------------------------------------
  3824. void* getNativeHandle() const noexcept override
  3825. {
  3826. return fHandle;
  3827. }
  3828. const void* getNativeDescriptor() const noexcept override
  3829. {
  3830. return fDescriptor;
  3831. }
  3832. uintptr_t getUiBridgeProcessId() const noexcept override
  3833. {
  3834. return fPipeServer.isPipeRunning() ? fPipeServer.getPID() : 0;
  3835. }
  3836. // -------------------------------------------------------------------
  3837. public:
  3838. bool init(const char* const name, const char* const uri, const uint options)
  3839. {
  3840. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3841. // ---------------------------------------------------------------
  3842. // first checks
  3843. if (pData->client != nullptr)
  3844. {
  3845. pData->engine->setLastError("Plugin client is already registered");
  3846. return false;
  3847. }
  3848. if (uri == nullptr || uri[0] == '\0')
  3849. {
  3850. pData->engine->setLastError("null uri");
  3851. return false;
  3852. }
  3853. // ---------------------------------------------------------------
  3854. // Init LV2 World if needed, sets LV2_PATH for lilv
  3855. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3856. if (pData->engine->getOptions().pathLV2 != nullptr && pData->engine->getOptions().pathLV2[0] != '\0')
  3857. lv2World.initIfNeeded(pData->engine->getOptions().pathLV2);
  3858. else if (const char* const LV2_PATH = std::getenv("LV2_PATH"))
  3859. lv2World.initIfNeeded(LV2_PATH);
  3860. else
  3861. lv2World.initIfNeeded(LILV_DEFAULT_LV2_PATH);
  3862. // ---------------------------------------------------------------
  3863. // get plugin from lv2_rdf (lilv)
  3864. fRdfDescriptor = lv2_rdf_new(uri, true);
  3865. if (fRdfDescriptor == nullptr)
  3866. {
  3867. pData->engine->setLastError("Failed to find the requested plugin");
  3868. return false;
  3869. }
  3870. // ---------------------------------------------------------------
  3871. // open DLL
  3872. if (! pData->libOpen(fRdfDescriptor->Binary))
  3873. {
  3874. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3875. return false;
  3876. }
  3877. // ---------------------------------------------------------------
  3878. // try to get DLL main entry via new mode
  3879. if (const LV2_Lib_Descriptor_Function libDescFn = pData->libSymbol<LV2_Lib_Descriptor_Function>("lv2_lib_descriptor"))
  3880. {
  3881. // -----------------------------------------------------------
  3882. // all ok, get lib descriptor
  3883. const LV2_Lib_Descriptor* const libDesc = libDescFn(fRdfDescriptor->Bundle, nullptr);
  3884. if (libDesc == nullptr)
  3885. {
  3886. pData->engine->setLastError("Could not find the LV2 Descriptor");
  3887. return false;
  3888. }
  3889. // -----------------------------------------------------------
  3890. // get descriptor that matches URI (new mode)
  3891. uint32_t i = 0;
  3892. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3893. {
  3894. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3895. break;
  3896. }
  3897. }
  3898. else
  3899. {
  3900. // -----------------------------------------------------------
  3901. // get DLL main entry (old mode)
  3902. const LV2_Descriptor_Function descFn = pData->libSymbol<LV2_Descriptor_Function>("lv2_descriptor");
  3903. if (descFn == nullptr)
  3904. {
  3905. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3906. return false;
  3907. }
  3908. // -----------------------------------------------------------
  3909. // get descriptor that matches URI (old mode)
  3910. uint32_t i = 0;
  3911. while ((fDescriptor = descFn(i++)))
  3912. {
  3913. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3914. break;
  3915. }
  3916. }
  3917. if (fDescriptor == nullptr)
  3918. {
  3919. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3920. return false;
  3921. }
  3922. // ---------------------------------------------------------------
  3923. // check supported port-types and features
  3924. bool canContinue = true;
  3925. // Check supported ports
  3926. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3927. {
  3928. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3929. if (! is_lv2_port_supported(portTypes))
  3930. {
  3931. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3932. {
  3933. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3934. canContinue = false;
  3935. break;
  3936. }
  3937. }
  3938. }
  3939. // Check supported features
  3940. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3941. {
  3942. const LV2_RDF_Feature& feature(fRdfDescriptor->Features[j]);
  3943. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3944. {
  3945. carla_stderr("Plugin DSP wants UI feature '%s', ignoring this", feature.URI);
  3946. }
  3947. else if (std::strcmp(feature.URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3948. {
  3949. fNeedsFixedBuffers = true;
  3950. }
  3951. else if (feature.Required && ! is_lv2_feature_supported(feature.URI))
  3952. {
  3953. CarlaString msg("Plugin wants a feature that is not supported:\n");
  3954. msg += feature.URI;
  3955. canContinue = false;
  3956. pData->engine->setLastError(msg);
  3957. break;
  3958. }
  3959. }
  3960. if (! canContinue)
  3961. {
  3962. // error already set
  3963. return false;
  3964. }
  3965. if (fNeedsFixedBuffers && ! pData->engine->usesConstantBufferSize())
  3966. {
  3967. pData->engine->setLastError("Cannot use this plugin under the current engine.\n"
  3968. "The plugin requires a fixed block size which is not possible right now.");
  3969. return false;
  3970. }
  3971. // ---------------------------------------------------------------
  3972. // get info
  3973. if (name != nullptr && name[0] != '\0')
  3974. pData->name = pData->engine->getUniquePluginName(name);
  3975. else
  3976. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3977. // ---------------------------------------------------------------
  3978. // register client
  3979. pData->client = pData->engine->addClient(this);
  3980. if (pData->client == nullptr || ! pData->client->isOk())
  3981. {
  3982. pData->engine->setLastError("Failed to register plugin client");
  3983. return false;
  3984. }
  3985. // ---------------------------------------------------------------
  3986. // initialize options
  3987. const int bufferSize = static_cast<int>(pData->engine->getBufferSize());
  3988. fLv2Options.minBufferSize = fNeedsFixedBuffers ? bufferSize : 1;
  3989. fLv2Options.maxBufferSize = bufferSize;
  3990. fLv2Options.nominalBufferSize = bufferSize;
  3991. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3992. fLv2Options.frontendWinId = static_cast<int64_t>(pData->engine->getOptions().frontendWinId);
  3993. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3994. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3995. {
  3996. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3997. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3998. {
  3999. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  4000. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  4001. }
  4002. }
  4003. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  4004. // ---------------------------------------------------------------
  4005. // initialize features (part 1)
  4006. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  4007. eventFt->callback_data = this;
  4008. eventFt->lv2_event_ref = carla_lv2_event_ref;
  4009. eventFt->lv2_event_unref = carla_lv2_event_unref;
  4010. LV2_Log_Log* const logFt = new LV2_Log_Log;
  4011. logFt->handle = this;
  4012. logFt->printf = carla_lv2_log_printf;
  4013. logFt->vprintf = carla_lv2_log_vprintf;
  4014. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  4015. stateMakePathFt->handle = this;
  4016. stateMakePathFt->path = carla_lv2_state_make_path;
  4017. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  4018. stateMapPathFt->handle = this;
  4019. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  4020. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  4021. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  4022. programsFt->handle = this;
  4023. programsFt->program_changed = carla_lv2_program_changed;
  4024. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  4025. rsPortFt->data = this;
  4026. rsPortFt->resize = carla_lv2_resize_port;
  4027. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  4028. lv2_rtmempool_init(rtMemPoolFt);
  4029. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  4030. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  4031. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  4032. uriMapFt->callback_data = this;
  4033. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  4034. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  4035. uridMapFt->handle = this;
  4036. uridMapFt->map = carla_lv2_urid_map;
  4037. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  4038. uridUnmapFt->handle = this;
  4039. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  4040. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  4041. workerFt->handle = this;
  4042. workerFt->schedule_work = carla_lv2_worker_schedule;
  4043. // ---------------------------------------------------------------
  4044. // initialize features (part 2)
  4045. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  4046. fFeatures[j] = new LV2_Feature;
  4047. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  4048. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  4049. fFeatures[kFeatureIdBufSizeFixed]->URI = fNeedsFixedBuffers
  4050. ? LV2_BUF_SIZE__fixedBlockLength
  4051. : LV2_BUF_SIZE__boundedBlockLength;
  4052. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  4053. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  4054. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  4055. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  4056. fFeatures[kFeatureIdEvent]->data = eventFt;
  4057. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  4058. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  4059. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  4060. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  4061. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  4062. fFeatures[kFeatureIdIsLive]->data = nullptr;
  4063. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  4064. fFeatures[kFeatureIdLogs]->data = logFt;
  4065. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  4066. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  4067. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  4068. fFeatures[kFeatureIdPrograms]->data = programsFt;
  4069. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  4070. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  4071. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  4072. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  4073. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  4074. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  4075. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  4076. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  4077. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  4078. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  4079. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  4080. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  4081. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  4082. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  4083. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  4084. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  4085. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  4086. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  4087. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  4088. fFeatures[kFeatureIdWorker]->data = workerFt;
  4089. // ---------------------------------------------------------------
  4090. // initialize plugin
  4091. try {
  4092. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  4093. } catch(...) {}
  4094. if (fHandle == nullptr)
  4095. {
  4096. pData->engine->setLastError("Plugin failed to initialize");
  4097. return false;
  4098. }
  4099. if (std::strcmp(uri, "http://hyperglitch.com/dev/VocProc") == 0)
  4100. fCanInit2 = false;
  4101. recheckExtensions();
  4102. // ---------------------------------------------------------------
  4103. // set default options
  4104. pData->options = 0x0;
  4105. if (fLatencyIndex >= 0 || getMidiOutCount() != 0 || fNeedsFixedBuffers)
  4106. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4107. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  4108. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  4109. if (fCanInit2)
  4110. {
  4111. if (pData->engine->getOptions().forceStereo)
  4112. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4113. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  4114. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  4115. }
  4116. if (getMidiInCount() != 0)
  4117. {
  4118. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  4119. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  4120. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  4121. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  4122. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  4123. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  4124. if (options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  4125. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  4126. }
  4127. if (fExt.programs != nullptr && (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0)
  4128. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  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