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.

5875 lines
220KB

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