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.

6213 lines
235KB

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