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.

5838 lines
219KB

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