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.

6106 lines
229KB

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