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.

6231 lines
232KB

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