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.

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