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.

5839 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. #ifndef BUILD_BRIDGE
  2585. // --------------------------------------------------------------------------------------------------------
  2586. // Latency, save values for next callback
  2587. if (pData->latency > 0)
  2588. {
  2589. if (pData->latency <= frames)
  2590. {
  2591. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2592. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i]+(frames-pData->latency), pData->latency);
  2593. }
  2594. else
  2595. {
  2596. for (uint32_t i=0, j, k; i < pData->audioIn.count; ++i)
  2597. {
  2598. for (k=0; k < pData->latency-frames; ++k)
  2599. pData->latencyBuffers[i][k] = pData->latencyBuffers[i][k+frames];
  2600. for (j=0; k < pData->latency; ++j, ++k)
  2601. pData->latencyBuffers[i][k] = inBuffer[i][j];
  2602. }
  2603. }
  2604. }
  2605. #endif
  2606. // --------------------------------------------------------------------------------------------------------
  2607. // MIDI Output
  2608. if (fEventsOut.ctrl != nullptr)
  2609. {
  2610. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2611. {
  2612. const LV2_Atom_Event* ev;
  2613. LV2_Atom_Buffer_Iterator iter;
  2614. uint8_t* data;
  2615. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2616. for (;;)
  2617. {
  2618. data = nullptr;
  2619. ev = lv2_atom_buffer_get(&iter, &data);
  2620. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2621. break;
  2622. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2623. {
  2624. if (fEventsOut.ctrl->port != nullptr)
  2625. {
  2626. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2627. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2628. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2629. }
  2630. }
  2631. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2632. {
  2633. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2634. fAtomQueueOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2635. }
  2636. lv2_atom_buffer_increment(&iter);
  2637. }
  2638. }
  2639. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2640. {
  2641. const LV2_Event* ev;
  2642. LV2_Event_Iterator iter;
  2643. uint8_t* data;
  2644. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2645. for (;;)
  2646. {
  2647. data = nullptr;
  2648. ev = lv2_event_get(&iter, &data);
  2649. if (ev == nullptr || data == nullptr)
  2650. break;
  2651. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2652. {
  2653. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2654. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2655. }
  2656. lv2_event_increment(&iter);
  2657. }
  2658. }
  2659. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2660. {
  2661. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2662. uint32_t eventSize;
  2663. double eventTime;
  2664. uchar* eventData;
  2665. for (;;)
  2666. {
  2667. eventSize = 0;
  2668. eventTime = 0.0;
  2669. eventData = nullptr;
  2670. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2671. if (eventData == nullptr || eventSize == 0)
  2672. break;
  2673. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2674. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2675. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2676. lv2midi_step(&state);
  2677. }
  2678. }
  2679. }
  2680. // --------------------------------------------------------------------------------------------------------
  2681. // Control Output
  2682. if (pData->event.portOut != nullptr)
  2683. {
  2684. uint8_t channel;
  2685. uint16_t param;
  2686. float value;
  2687. for (uint32_t k=0; k < pData->param.count; ++k)
  2688. {
  2689. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2690. continue;
  2691. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2692. if (pData->param.data[k].midiCC > 0)
  2693. {
  2694. channel = pData->param.data[k].midiChannel;
  2695. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2696. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2697. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2698. }
  2699. }
  2700. } // End of Control Output
  2701. // --------------------------------------------------------------------------------------------------------
  2702. // Final work
  2703. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2704. {
  2705. fExt.worker->end_run(fHandle);
  2706. if (fHandle2 != nullptr)
  2707. fExt.worker->end_run(fHandle2);
  2708. }
  2709. fFirstActive = false;
  2710. // --------------------------------------------------------------------------------------------------------
  2711. }
  2712. bool processSingle(float** const audioInBuf, float** const audioOutBuf, float** const cvInBuf, float** const cvOutBuf, const uint32_t frames, const uint32_t timeOffset)
  2713. {
  2714. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2715. if (pData->audioIn.count > 0)
  2716. {
  2717. CARLA_SAFE_ASSERT_RETURN(audioInBuf != nullptr, false);
  2718. }
  2719. if (pData->audioOut.count > 0)
  2720. {
  2721. CARLA_SAFE_ASSERT_RETURN(audioOutBuf != nullptr, false);
  2722. }
  2723. if (fCvIn.count > 0)
  2724. {
  2725. CARLA_SAFE_ASSERT_RETURN(cvInBuf != nullptr, false);
  2726. }
  2727. if (fCvOut.count > 0)
  2728. {
  2729. CARLA_SAFE_ASSERT_RETURN(cvOutBuf != nullptr, false);
  2730. }
  2731. // --------------------------------------------------------------------------------------------------------
  2732. // Try lock, silence otherwise
  2733. if (pData->engine->isOffline())
  2734. {
  2735. pData->singleMutex.lock();
  2736. }
  2737. else if (! pData->singleMutex.tryLock())
  2738. {
  2739. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2740. {
  2741. for (uint32_t k=0; k < frames; ++k)
  2742. audioOutBuf[i][k+timeOffset] = 0.0f;
  2743. }
  2744. return false;
  2745. }
  2746. // --------------------------------------------------------------------------------------------------------
  2747. // Set audio buffers
  2748. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2749. FLOAT_COPY(fAudioInBuffers[i], audioInBuf[i]+timeOffset, frames);
  2750. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2751. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  2752. // --------------------------------------------------------------------------------------------------------
  2753. // Set CV buffers
  2754. for (uint32_t i=0; i < fCvIn.count; ++i)
  2755. FLOAT_COPY(fCvInBuffers[i], cvInBuf[i]+timeOffset, frames);
  2756. for (uint32_t i=0; i < fCvOut.count; ++i)
  2757. FLOAT_CLEAR(fCvOutBuffers[i], frames);
  2758. // --------------------------------------------------------------------------------------------------------
  2759. // Run plugin
  2760. fDescriptor->run(fHandle, frames);
  2761. if (fHandle2 != nullptr)
  2762. fDescriptor->run(fHandle2, frames);
  2763. // --------------------------------------------------------------------------------------------------------
  2764. // Handle trigger parameters
  2765. for (uint32_t k=0; k < pData->param.count; ++k)
  2766. {
  2767. if (pData->param.data[k].type != PARAMETER_INPUT)
  2768. continue;
  2769. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2770. {
  2771. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2772. {
  2773. fParamBuffers[k] = pData->param.ranges[k].def;
  2774. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2775. }
  2776. }
  2777. }
  2778. pData->postRtEvents.trySplice();
  2779. #ifndef BUILD_BRIDGE
  2780. // --------------------------------------------------------------------------------------------------------
  2781. // Post-processing (dry/wet, volume and balance)
  2782. {
  2783. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2784. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2785. const bool isMono = (pData->audioIn.count == 1);
  2786. bool isPair;
  2787. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2788. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2789. {
  2790. // Dry/Wet
  2791. if (doDryWet)
  2792. {
  2793. for (uint32_t k=0; k < frames; ++k)
  2794. {
  2795. if (k < pData->latency)
  2796. bufValue = pData->latencyBuffers[isMono ? 0 : i][k];
  2797. else if (pData->latency < frames)
  2798. bufValue = fAudioInBuffers[isMono ? 0 : i][k-pData->latency];
  2799. else
  2800. bufValue = fAudioInBuffers[isMono ? 0 : i][k];
  2801. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2802. }
  2803. }
  2804. // Balance
  2805. if (doBalance)
  2806. {
  2807. isPair = (i % 2 == 0);
  2808. if (isPair)
  2809. {
  2810. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2811. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  2812. }
  2813. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2814. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2815. for (uint32_t k=0; k < frames; ++k)
  2816. {
  2817. if (isPair)
  2818. {
  2819. // left
  2820. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2821. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2822. }
  2823. else
  2824. {
  2825. // right
  2826. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2827. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2828. }
  2829. }
  2830. }
  2831. // Volume (and buffer copy)
  2832. {
  2833. for (uint32_t k=0; k < frames; ++k)
  2834. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2835. }
  2836. }
  2837. } // End of Post-processing
  2838. #else // BUILD_BRIDGE
  2839. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2840. {
  2841. for (uint32_t k=0; k < frames; ++k)
  2842. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2843. }
  2844. #endif
  2845. // --------------------------------------------------------------------------------------------------------
  2846. pData->singleMutex.unlock();
  2847. return true;
  2848. }
  2849. void bufferSizeChanged(const uint32_t newBufferSize) override
  2850. {
  2851. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2852. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2853. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2854. {
  2855. if (fAudioInBuffers[i] != nullptr)
  2856. delete[] fAudioInBuffers[i];
  2857. fAudioInBuffers[i] = new float[newBufferSize];
  2858. }
  2859. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2860. {
  2861. if (fAudioOutBuffers[i] != nullptr)
  2862. delete[] fAudioOutBuffers[i];
  2863. fAudioOutBuffers[i] = new float[newBufferSize];
  2864. }
  2865. if (fHandle2 == nullptr)
  2866. {
  2867. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2868. {
  2869. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2870. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2871. }
  2872. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2873. {
  2874. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2875. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2876. }
  2877. }
  2878. else
  2879. {
  2880. if (pData->audioIn.count > 0)
  2881. {
  2882. CARLA_ASSERT(pData->audioIn.count == 2);
  2883. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2884. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2885. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2886. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2887. }
  2888. if (pData->audioOut.count > 0)
  2889. {
  2890. CARLA_ASSERT(pData->audioOut.count == 2);
  2891. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2892. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2893. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2894. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2895. }
  2896. }
  2897. for (uint32_t i=0; i < fCvIn.count; ++i)
  2898. {
  2899. if (fCvInBuffers[i] != nullptr)
  2900. delete[] fCvInBuffers[i];
  2901. fCvInBuffers[i] = new float[newBufferSize];
  2902. fDescriptor->connect_port(fHandle, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2903. if (fHandle2 != nullptr)
  2904. fDescriptor->connect_port(fHandle2, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2905. }
  2906. for (uint32_t i=0; i < fCvOut.count; ++i)
  2907. {
  2908. if (fCvOutBuffers[i] != nullptr)
  2909. delete[] fCvOutBuffers[i];
  2910. fCvOutBuffers[i] = new float[newBufferSize];
  2911. fDescriptor->connect_port(fHandle, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2912. if (fHandle2 != nullptr)
  2913. fDescriptor->connect_port(fHandle2, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2914. }
  2915. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2916. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2917. {
  2918. fLv2Options.maxBufferSize = newBufferSizeInt;
  2919. if (fLv2Options.minBufferSize != 1)
  2920. fLv2Options.minBufferSize = newBufferSizeInt;
  2921. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2922. {
  2923. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2924. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2925. }
  2926. }
  2927. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2928. }
  2929. void sampleRateChanged(const double newSampleRate) override
  2930. {
  2931. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2932. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2933. if (fLv2Options.sampleRate != newSampleRate)
  2934. {
  2935. fLv2Options.sampleRate = newSampleRate;
  2936. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2937. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2938. }
  2939. for (uint32_t k=0; k < pData->param.count; ++k)
  2940. {
  2941. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2942. {
  2943. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2944. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2945. break;
  2946. }
  2947. }
  2948. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2949. }
  2950. void offlineModeChanged(const bool isOffline) override
  2951. {
  2952. for (uint32_t k=0; k < pData->param.count; ++k)
  2953. {
  2954. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  2955. {
  2956. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2957. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2958. break;
  2959. }
  2960. }
  2961. }
  2962. // -------------------------------------------------------------------
  2963. // Plugin buffers
  2964. void initBuffers() const noexcept override
  2965. {
  2966. fCvIn.initBuffers();
  2967. fCvOut.initBuffers();
  2968. fEventsIn.initBuffers();
  2969. fEventsOut.initBuffers();
  2970. CarlaPlugin::initBuffers();
  2971. }
  2972. void clearBuffers() noexcept override
  2973. {
  2974. carla_debug("Lv2Plugin::clearBuffers() - start");
  2975. if (fAudioInBuffers != nullptr)
  2976. {
  2977. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2978. {
  2979. if (fAudioInBuffers[i] != nullptr)
  2980. {
  2981. delete[] fAudioInBuffers[i];
  2982. fAudioInBuffers[i] = nullptr;
  2983. }
  2984. }
  2985. delete[] fAudioInBuffers;
  2986. fAudioInBuffers = nullptr;
  2987. }
  2988. if (fAudioOutBuffers != nullptr)
  2989. {
  2990. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2991. {
  2992. if (fAudioOutBuffers[i] != nullptr)
  2993. {
  2994. delete[] fAudioOutBuffers[i];
  2995. fAudioOutBuffers[i] = nullptr;
  2996. }
  2997. }
  2998. delete[] fAudioOutBuffers;
  2999. fAudioOutBuffers = nullptr;
  3000. }
  3001. if (fCvInBuffers != nullptr)
  3002. {
  3003. for (uint32_t i=0; i < fCvIn.count; ++i)
  3004. {
  3005. if (fCvInBuffers[i] != nullptr)
  3006. {
  3007. delete[] fCvInBuffers[i];
  3008. fCvInBuffers[i] = nullptr;
  3009. }
  3010. }
  3011. delete[] fCvInBuffers;
  3012. fCvInBuffers = nullptr;
  3013. }
  3014. if (fCvOutBuffers != nullptr)
  3015. {
  3016. for (uint32_t i=0; i < fCvOut.count; ++i)
  3017. {
  3018. if (fCvOutBuffers[i] != nullptr)
  3019. {
  3020. delete[] fCvOutBuffers[i];
  3021. fCvOutBuffers[i] = nullptr;
  3022. }
  3023. }
  3024. delete[] fCvOutBuffers;
  3025. fCvOutBuffers = nullptr;
  3026. }
  3027. if (fParamBuffers != nullptr)
  3028. {
  3029. delete[] fParamBuffers;
  3030. fParamBuffers = nullptr;
  3031. }
  3032. fCvIn.clear();
  3033. fCvOut.clear();
  3034. fEventsIn.clear();
  3035. fEventsOut.clear();
  3036. CarlaPlugin::clearBuffers();
  3037. carla_debug("Lv2Plugin::clearBuffers() - end");
  3038. }
  3039. // -------------------------------------------------------------------
  3040. // OSC stuff
  3041. bool updateOscDataExtra() override
  3042. {
  3043. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  3044. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i, nullptr));
  3045. osc_send_lv2_urid_map(pData->osc.data, CARLA_URI_MAP_ID_NULL, "Complete");
  3046. return true;
  3047. }
  3048. // -------------------------------------------------------------------
  3049. // Post-poned UI Stuff
  3050. void uiParameterChange(const uint32_t index, const float value) noexcept override
  3051. {
  3052. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  3053. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  3054. if (fUi.type == UI::TYPE_OSC)
  3055. {
  3056. if (pData->osc.data.target != nullptr)
  3057. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  3058. }
  3059. else
  3060. {
  3061. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  3062. {
  3063. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  3064. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  3065. }
  3066. }
  3067. }
  3068. void uiMidiProgramChange(const uint32_t index) noexcept override
  3069. {
  3070. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  3071. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  3072. if (fUi.type == UI::TYPE_OSC)
  3073. {
  3074. if (pData->osc.data.target != nullptr)
  3075. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3076. }
  3077. else
  3078. {
  3079. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  3080. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  3081. }
  3082. }
  3083. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  3084. {
  3085. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  3086. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3087. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3088. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  3089. if (fUi.type == UI::TYPE_OSC)
  3090. {
  3091. if (pData->osc.data.target != nullptr)
  3092. {
  3093. uint8_t midiData[4] = { 0, 0, 0, 0 };
  3094. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  3095. midiData[2] = note;
  3096. midiData[3] = velo;
  3097. osc_send_midi(pData->osc.data, midiData);
  3098. }
  3099. }
  3100. else
  3101. {
  3102. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3103. {
  3104. LV2_Atom_MidiEvent midiEv;
  3105. midiEv.event.time.frames = 0;
  3106. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3107. midiEv.event.body.size = 3;
  3108. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  3109. midiEv.data[1] = note;
  3110. midiEv.data[2] = velo;
  3111. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3112. }
  3113. }
  3114. }
  3115. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3116. {
  3117. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  3118. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3119. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3120. if (fUi.type == UI::TYPE_OSC)
  3121. {
  3122. if (pData->osc.data.target != nullptr)
  3123. {
  3124. uint8_t midiData[4] = { 0, 0, 0, 0 };
  3125. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  3126. midiData[2] = note;
  3127. osc_send_midi(pData->osc.data, midiData);
  3128. }
  3129. }
  3130. else
  3131. {
  3132. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3133. {
  3134. LV2_Atom_MidiEvent midiEv;
  3135. midiEv.event.time.frames = 0;
  3136. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3137. midiEv.event.body.size = 3;
  3138. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  3139. midiEv.data[1] = note;
  3140. midiEv.data[2] = 0;
  3141. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3142. }
  3143. }
  3144. }
  3145. // -------------------------------------------------------------------
  3146. bool isRealtimeSafe() const noexcept
  3147. {
  3148. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3149. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3150. {
  3151. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3152. return true;
  3153. }
  3154. return false;
  3155. }
  3156. bool needsFixedBuffer() const noexcept
  3157. {
  3158. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3159. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3160. {
  3161. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3162. return true;
  3163. }
  3164. return false;
  3165. }
  3166. // -------------------------------------------------------------------
  3167. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3168. {
  3169. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3170. #ifndef LV2_UIS_ONLY_INPROCESS
  3171. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  3172. if (std::strstr(rdfUi->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3173. return false;
  3174. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  3175. {
  3176. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3177. return false;
  3178. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3179. return false;
  3180. }
  3181. return true;
  3182. #else
  3183. return false;
  3184. #endif
  3185. }
  3186. bool isUiResizable() const noexcept
  3187. {
  3188. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  3189. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3190. {
  3191. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3192. return false;
  3193. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3194. return false;
  3195. }
  3196. return true;
  3197. }
  3198. const char* getUiBridgeBinary(const LV2_Property type) const
  3199. {
  3200. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3201. if (bridgeBinary.isEmpty())
  3202. return nullptr;
  3203. #ifdef CARLA_OS_LINUX
  3204. // test for local build
  3205. if (bridgeBinary.endsWith("/source/backend/"))
  3206. bridgeBinary += "../bridges/";
  3207. #endif
  3208. switch (type)
  3209. {
  3210. case LV2_UI_GTK2:
  3211. bridgeBinary += "carla-bridge-lv2-gtk2";
  3212. break;
  3213. case LV2_UI_GTK3:
  3214. bridgeBinary += "carla-bridge-lv2-gtk3";
  3215. break;
  3216. case LV2_UI_QT4:
  3217. bridgeBinary += "carla-bridge-lv2-qt4";
  3218. break;
  3219. case LV2_UI_QT5:
  3220. bridgeBinary += "carla-bridge-lv2-qt5";
  3221. break;
  3222. case LV2_UI_COCOA:
  3223. bridgeBinary += "carla-bridge-lv2-cocoa";
  3224. break;
  3225. case LV2_UI_WINDOWS:
  3226. bridgeBinary += "carla-bridge-lv2-windows";
  3227. break;
  3228. case LV2_UI_X11:
  3229. bridgeBinary += "carla-bridge-lv2-x11";
  3230. break;
  3231. case LV2_UI_EXTERNAL:
  3232. case LV2_UI_OLD_EXTERNAL:
  3233. bridgeBinary += "carla-bridge-lv2-external";
  3234. break;
  3235. default:
  3236. return nullptr;
  3237. }
  3238. #ifdef CARLA_OS_WIN
  3239. bridgeBinary += ".exe";
  3240. #endif
  3241. QFile file(bridgeBinary.buffer());
  3242. if (! file.exists())
  3243. return nullptr;
  3244. return bridgeBinary.dup();
  3245. }
  3246. // -------------------------------------------------------------------
  3247. void recheckExtensions()
  3248. {
  3249. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3250. carla_debug("Lv2Plugin::recheckExtensions()");
  3251. fExt.options = nullptr;
  3252. fExt.programs = nullptr;
  3253. fExt.state = nullptr;
  3254. fExt.worker = nullptr;
  3255. if (fRdfDescriptor->ExtensionCount == 0 || fDescriptor->extension_data == nullptr)
  3256. return;
  3257. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3258. {
  3259. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->Extensions[i] != nullptr);
  3260. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3261. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3262. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3263. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3264. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3265. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3266. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3267. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3268. else
  3269. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3270. }
  3271. if (fDescriptor->extension_data != nullptr)
  3272. {
  3273. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3274. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3275. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3276. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3277. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3278. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3279. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3280. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3281. // check if invalid
  3282. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3283. fExt.options = nullptr;
  3284. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3285. fExt.programs = nullptr;
  3286. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3287. fExt.state = nullptr;
  3288. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3289. fExt.worker = nullptr;
  3290. }
  3291. }
  3292. // -------------------------------------------------------------------
  3293. void updateUi()
  3294. {
  3295. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  3296. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  3297. carla_debug("Lv2Plugin::updateUi()");
  3298. // update midi program
  3299. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3300. {
  3301. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3302. fExt.uiprograms->select_program(fUi.handle, curData.bank, curData.program);
  3303. }
  3304. // update control ports
  3305. if (fUi.descriptor->port_event != nullptr)
  3306. {
  3307. float value;
  3308. for (uint32_t i=0; i < pData->param.count; ++i)
  3309. {
  3310. value = getParameterValue(i);
  3311. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3312. }
  3313. }
  3314. }
  3315. // -------------------------------------------------------------------
  3316. LV2_URID getCustomURID(const char* const uri)
  3317. {
  3318. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3319. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3320. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3321. {
  3322. const char* const thisUri(fCustomURIDs.getAt(i, nullptr));
  3323. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3324. return static_cast<LV2_URID>(i);
  3325. }
  3326. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3327. fCustomURIDs.append(carla_strdup(uri));
  3328. if (fUi.type == UI::TYPE_OSC && pData->osc.data.target != nullptr)
  3329. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3330. return urid;
  3331. }
  3332. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3333. {
  3334. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  3335. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  3336. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3337. return fCustomURIDs.getAt(urid, nullptr);
  3338. }
  3339. // -------------------------------------------------------------------
  3340. void handleProgramChanged(const int32_t index)
  3341. {
  3342. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3343. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3344. if (index == -1)
  3345. {
  3346. const ScopedSingleProcessLocker spl(this, true);
  3347. return reloadPrograms(false);
  3348. }
  3349. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3350. {
  3351. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3352. {
  3353. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3354. if (pData->midiprog.data[index].name != nullptr)
  3355. delete[] pData->midiprog.data[index].name;
  3356. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3357. if (index == pData->midiprog.current)
  3358. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3359. else
  3360. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3361. }
  3362. }
  3363. }
  3364. // -------------------------------------------------------------------
  3365. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3366. {
  3367. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3368. carla_debug("Lv2Plugin::handleResizePort(%i, " P_SIZE ")", index, size);
  3369. // TODO
  3370. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3371. (void)index;
  3372. }
  3373. // -------------------------------------------------------------------
  3374. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3375. {
  3376. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3377. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3378. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3379. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3380. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3381. 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);
  3382. const char* const skey(carla_lv2_urid_unmap(this, key));
  3383. const char* const stype(carla_lv2_urid_unmap(this, type));
  3384. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3385. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3386. // Check if we already have this key
  3387. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3388. {
  3389. CustomData& data(it.getValue());
  3390. if (std::strcmp(data.key, skey) == 0)
  3391. {
  3392. // found it
  3393. if (data.value != nullptr)
  3394. delete[] data.value;
  3395. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3396. data.value = carla_strdup((const char*)value);
  3397. else
  3398. data.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3399. return LV2_STATE_SUCCESS;
  3400. }
  3401. }
  3402. // Otherwise store it
  3403. CustomData newData;
  3404. newData.type = carla_strdup(stype);
  3405. newData.key = carla_strdup(skey);
  3406. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3407. newData.value = carla_strdup((const char*)value);
  3408. else
  3409. newData.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3410. pData->custom.append(newData);
  3411. return LV2_STATE_SUCCESS;
  3412. }
  3413. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3414. {
  3415. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3416. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3417. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3418. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3419. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3420. const char* const skey(carla_lv2_urid_unmap(this, key));
  3421. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3422. const char* stype = nullptr;
  3423. const char* stringData = nullptr;
  3424. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3425. {
  3426. const CustomData& data(it.getValue());
  3427. if (std::strcmp(data.key, skey) == 0)
  3428. {
  3429. stype = data.type;
  3430. stringData = data.value;
  3431. break;
  3432. }
  3433. }
  3434. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3435. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3436. *type = carla_lv2_urid_map(this, stype);
  3437. *flags = LV2_STATE_IS_POD;
  3438. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3439. {
  3440. *size = std::strlen(stringData);
  3441. return stringData;
  3442. }
  3443. else
  3444. {
  3445. static QByteArray chunk; // FIXME
  3446. chunk = QByteArray::fromBase64(stringData);
  3447. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3448. *size = static_cast<size_t>(chunk.size());
  3449. return chunk.constData();
  3450. }
  3451. }
  3452. // -------------------------------------------------------------------
  3453. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3454. {
  3455. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3456. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3457. carla_debug("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3458. if (pData->engine->isOffline())
  3459. {
  3460. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3461. return LV2_WORKER_SUCCESS;
  3462. }
  3463. LV2_Atom atom;
  3464. atom.size = size;
  3465. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3466. return fAtomQueueOut.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3467. }
  3468. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3469. {
  3470. carla_debug("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3471. LV2_Atom atom;
  3472. atom.size = size;
  3473. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3474. return fAtomQueueIn.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3475. }
  3476. // -------------------------------------------------------------------
  3477. void handleExternalUiClosed()
  3478. {
  3479. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EXTERNAL,);
  3480. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3481. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3482. fUi.descriptor->cleanup(fUi.handle);
  3483. fUi.handle = nullptr;
  3484. fUi.widget = nullptr;
  3485. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3486. }
  3487. void handlePluginUiClosed() override
  3488. {
  3489. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EMBED,);
  3490. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr,);
  3491. carla_debug("Lv2Plugin::handlePluginUiClosed()");
  3492. fUi.window->hide();
  3493. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3494. fUi.descriptor->cleanup(fUi.handle);
  3495. fUi.handle = nullptr;
  3496. fUi.widget = nullptr;
  3497. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3498. }
  3499. // -------------------------------------------------------------------
  3500. uint32_t handleUiPortMap(const char* const symbol) const noexcept
  3501. {
  3502. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3503. carla_debug("Lv2Plugin::handleUiPortMap(\"%s\")", symbol);
  3504. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3505. {
  3506. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3507. return i;
  3508. }
  3509. return LV2UI_INVALID_PORT_INDEX;
  3510. }
  3511. int handleUiResize(const int width, const int height)
  3512. {
  3513. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr, 1);
  3514. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3515. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3516. carla_debug("Lv2Plugin::handleUiResize(%i, %i)", width, height);
  3517. fUi.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3518. return 0;
  3519. }
  3520. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3521. {
  3522. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3523. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3524. carla_debug("Lv2Plugin::handleUiWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3525. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  3526. switch (format)
  3527. {
  3528. case CARLA_URI_MAP_ID_NULL: {
  3529. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3530. for (uint32_t i=0; i < pData->param.count; ++i)
  3531. {
  3532. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  3533. continue;
  3534. index = i;
  3535. break;
  3536. }
  3537. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  3538. const float value(*(const float*)buffer);
  3539. if (fParamBuffers[index] != value)
  3540. setParameterValue(index, value, false, true, true);
  3541. } break;
  3542. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3543. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT: {
  3544. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  3545. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  3546. // plugins sometimes fail on this, not good...
  3547. CARLA_SAFE_ASSERT_INT2(bufferSize == lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom)+atom->size)), bufferSize, atom->size);
  3548. for (uint32_t i=0; i < fEventsIn.count; ++i)
  3549. {
  3550. if (fEventsIn.data[i].rindex != rindex)
  3551. continue;
  3552. index = i;
  3553. break;
  3554. }
  3555. // for bad plugins
  3556. if (index == LV2UI_INVALID_PORT_INDEX)
  3557. {
  3558. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  3559. index = fEventsIn.ctrlIndex;
  3560. }
  3561. fAtomQueueIn.put(atom, index);
  3562. } break;
  3563. default:
  3564. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3565. break;
  3566. }
  3567. }
  3568. // -------------------------------------------------------------------
  3569. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3570. {
  3571. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3572. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3573. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3574. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3575. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3576. int32_t rindex = -1;
  3577. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3578. {
  3579. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3580. {
  3581. rindex = static_cast<int32_t>(i);
  3582. break;
  3583. }
  3584. }
  3585. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3586. float paramValue;
  3587. switch (type)
  3588. {
  3589. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3590. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3591. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3592. break;
  3593. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3594. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3595. paramValue = static_cast<float>((*(const double*)value));
  3596. break;
  3597. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3598. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3599. paramValue = (*(const float*)value);
  3600. break;
  3601. case CARLA_URI_MAP_ID_ATOM_INT:
  3602. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3603. paramValue = static_cast<float>((*(const int32_t*)value));
  3604. break;
  3605. case CARLA_URI_MAP_ID_ATOM_LONG:
  3606. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3607. paramValue = static_cast<float>((*(const int64_t*)value));
  3608. break;
  3609. default:
  3610. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3611. return;
  3612. }
  3613. for (uint32_t i=0; i < pData->param.count; ++i)
  3614. {
  3615. if (pData->param.data[i].rindex == rindex)
  3616. {
  3617. setParameterValue(i, paramValue, true, true, true);
  3618. break;
  3619. }
  3620. }
  3621. }
  3622. // -------------------------------------------------------------------
  3623. public:
  3624. bool init(const char* const bundle, const char* const name, const char* const uri)
  3625. {
  3626. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3627. // ---------------------------------------------------------------
  3628. // first checks
  3629. if (pData->client != nullptr)
  3630. {
  3631. pData->engine->setLastError("Plugin client is already registered");
  3632. return false;
  3633. }
  3634. if (bundle == nullptr || bundle[0] == '\0')
  3635. {
  3636. pData->engine->setLastError("null bundle");
  3637. return false;
  3638. }
  3639. if (uri == nullptr || uri[0] == '\0')
  3640. {
  3641. pData->engine->setLastError("null uri");
  3642. return false;
  3643. }
  3644. // ---------------------------------------------------------------
  3645. // get plugin from lv2_rdf (lilv)
  3646. fRdfDescriptor = lv2_rdf_new(uri, true);
  3647. if (fRdfDescriptor == nullptr)
  3648. {
  3649. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3650. return false;
  3651. }
  3652. // ---------------------------------------------------------------
  3653. // open DLL
  3654. if (! pData->libOpen(fRdfDescriptor->Binary))
  3655. {
  3656. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3657. return false;
  3658. }
  3659. // ---------------------------------------------------------------
  3660. // try to get DLL main entry via new mode
  3661. if (const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)pData->libSymbol("lv2_lib_descriptor"))
  3662. {
  3663. // -----------------------------------------------------------
  3664. // all ok, get lib descriptor
  3665. const LV2_Lib_Descriptor* const libDesc = libDescFn(bundle, nullptr);
  3666. if (libDesc == nullptr)
  3667. {
  3668. pData->engine->setLastError("Could not find the LV2 Descriptor");
  3669. return false;
  3670. }
  3671. // -----------------------------------------------------------
  3672. // get descriptor that matches URI (new mode)
  3673. uint32_t i = 0;
  3674. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3675. {
  3676. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3677. break;
  3678. }
  3679. }
  3680. else
  3681. {
  3682. // -----------------------------------------------------------
  3683. // get DLL main entry (old mode)
  3684. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3685. if (descFn == nullptr)
  3686. {
  3687. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3688. return false;
  3689. }
  3690. // -----------------------------------------------------------
  3691. // get descriptor that matches URI (old mode)
  3692. uint32_t i = 0;
  3693. while ((fDescriptor = descFn(i++)))
  3694. {
  3695. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3696. break;
  3697. }
  3698. }
  3699. if (fDescriptor == nullptr)
  3700. {
  3701. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3702. return false;
  3703. }
  3704. // ---------------------------------------------------------------
  3705. // check supported port-types and features
  3706. bool canContinue = true;
  3707. // Check supported ports
  3708. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3709. {
  3710. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3711. if (! is_lv2_port_supported(portTypes))
  3712. {
  3713. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3714. {
  3715. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3716. canContinue = false;
  3717. break;
  3718. }
  3719. }
  3720. }
  3721. // Check supported features
  3722. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3723. {
  3724. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  3725. {
  3726. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  3727. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  3728. {
  3729. canContinue = false;
  3730. pData->engine->setLastError(msg.toUtf8().constData());
  3731. break;
  3732. }
  3733. else
  3734. carla_stderr("%s", msg.toUtf8().constData());
  3735. }
  3736. }
  3737. if (! canContinue)
  3738. {
  3739. // error already set
  3740. return false;
  3741. }
  3742. // ---------------------------------------------------------------
  3743. // get info
  3744. if (name != nullptr && name[0] != '\0')
  3745. pData->name = pData->engine->getUniquePluginName(name);
  3746. else
  3747. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3748. pData->filename = carla_strdup(bundle);
  3749. // ---------------------------------------------------------------
  3750. // register client
  3751. pData->client = pData->engine->addClient(this);
  3752. if (pData->client == nullptr || ! pData->client->isOk())
  3753. {
  3754. pData->engine->setLastError("Failed to register plugin client");
  3755. return false;
  3756. }
  3757. // ---------------------------------------------------------------
  3758. // initialize options
  3759. fLv2Options.minBufferSize = 1;
  3760. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3761. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3762. fLv2Options.frontendWinId = static_cast<int64_t>(pData->engine->getOptions().frontendWinId);
  3763. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3764. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3765. {
  3766. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3767. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3768. {
  3769. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3770. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3771. }
  3772. }
  3773. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3774. // ---------------------------------------------------------------
  3775. // initialize features (part 1)
  3776. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3777. eventFt->callback_data = this;
  3778. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3779. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3780. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3781. logFt->handle = this;
  3782. logFt->printf = carla_lv2_log_printf;
  3783. logFt->vprintf = carla_lv2_log_vprintf;
  3784. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3785. stateMakePathFt->handle = this;
  3786. stateMakePathFt->path = carla_lv2_state_make_path;
  3787. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3788. stateMapPathFt->handle = this;
  3789. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3790. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3791. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3792. programsFt->handle = this;
  3793. programsFt->program_changed = carla_lv2_program_changed;
  3794. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3795. rsPortFt->data = this;
  3796. rsPortFt->resize = carla_lv2_resize_port;
  3797. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3798. lv2_rtmempool_init(rtMemPoolFt);
  3799. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3800. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3801. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3802. uriMapFt->callback_data = this;
  3803. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3804. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3805. uridMapFt->handle = this;
  3806. uridMapFt->map = carla_lv2_urid_map;
  3807. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3808. uridUnmapFt->handle = this;
  3809. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3810. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3811. workerFt->handle = this;
  3812. workerFt->schedule_work = carla_lv2_worker_schedule;
  3813. // ---------------------------------------------------------------
  3814. // initialize features (part 2)
  3815. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3816. fFeatures[j] = new LV2_Feature;
  3817. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3818. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3819. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3820. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3821. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3822. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3823. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3824. fFeatures[kFeatureIdEvent]->data = eventFt;
  3825. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3826. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3827. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3828. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3829. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3830. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3831. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3832. fFeatures[kFeatureIdLogs]->data = logFt;
  3833. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3834. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3835. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3836. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3837. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3838. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3839. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3840. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3841. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3842. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3843. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3844. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3845. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3846. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3847. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3848. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3849. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3850. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3851. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3852. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3853. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3854. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3855. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3856. fFeatures[kFeatureIdWorker]->data = workerFt;
  3857. // if a fixed buffer is not needed, skip its feature
  3858. if (! needsFixedBuffer())
  3859. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3860. // if power of 2 is not possible, skip its feature FIXME
  3861. //if (fLv2Options.maxBufferSize)
  3862. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3863. // ---------------------------------------------------------------
  3864. // initialize plugin
  3865. try {
  3866. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3867. } catch(...) {}
  3868. if (fHandle == nullptr)
  3869. {
  3870. pData->engine->setLastError("Plugin failed to initialize");
  3871. return false;
  3872. }
  3873. // ---------------------------------------------------------------
  3874. // load plugin settings
  3875. {
  3876. // set default options
  3877. pData->options = 0x0;
  3878. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3879. if (fLatencyIndex >= 0 || getMidiInCount() > 0 || needsFixedBuffer())
  3880. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3881. if (pData->engine->getOptions().forceStereo)
  3882. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3883. if (getMidiInCount() > 0)
  3884. {
  3885. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3886. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3887. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3888. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3889. }
  3890. #ifndef BUILD_BRIDGE
  3891. // set identifier string
  3892. CarlaString identifier("LV2/");
  3893. identifier += uri;
  3894. pData->identifier = identifier.dup();
  3895. // load settings
  3896. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3897. // ignore settings, we need this anyway
  3898. if (fLatencyIndex >= 0 || getMidiInCount() > 0 || needsFixedBuffer())
  3899. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3900. #endif
  3901. }
  3902. // ---------------------------------------------------------------
  3903. // gui stuff
  3904. if (fRdfDescriptor->UICount != 0)
  3905. initUi();
  3906. return true;
  3907. }
  3908. // -------------------------------------------------------------------
  3909. void initUi()
  3910. {
  3911. // ---------------------------------------------------------------
  3912. // find the most appropriate ui
  3913. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3914. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3915. #if defined(BUILD_BRIDGE)
  3916. const bool preferUiBridges(false);
  3917. #elif defined(LV2_UIS_ONLY_BRIDGES)
  3918. const bool preferUiBridges(true);
  3919. #else
  3920. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3921. #endif
  3922. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3923. {
  3924. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3925. const int ii(static_cast<int>(i));
  3926. switch (fRdfDescriptor->UIs[i].Type)
  3927. {
  3928. case LV2_UI_QT4:
  3929. if (isUiBridgeable(i))
  3930. eQt4 = ii;
  3931. break;
  3932. case LV2_UI_QT5:
  3933. if (isUiBridgeable(i))
  3934. eQt5 = ii;
  3935. break;
  3936. case LV2_UI_GTK2:
  3937. if (isUiBridgeable(i))
  3938. eGtk2 = ii;
  3939. break;
  3940. case LV2_UI_GTK3:
  3941. if (isUiBridgeable(i))
  3942. eGtk3 = ii;
  3943. break;
  3944. case LV2_UI_COCOA:
  3945. if (isUiBridgeable(i) && preferUiBridges)
  3946. eCocoa = ii;
  3947. iCocoa = ii;
  3948. break;
  3949. case LV2_UI_WINDOWS:
  3950. if (isUiBridgeable(i) && preferUiBridges)
  3951. eWindows = ii;
  3952. iWindows = ii;
  3953. break;
  3954. case LV2_UI_X11:
  3955. if (isUiBridgeable(i) && preferUiBridges)
  3956. eX11 = ii;
  3957. iX11 = ii;
  3958. break;
  3959. case LV2_UI_EXTERNAL:
  3960. case LV2_UI_OLD_EXTERNAL:
  3961. if (isUiBridgeable(i))
  3962. eExt = ii;
  3963. iExt = ii;
  3964. break;
  3965. default:
  3966. break;
  3967. }
  3968. }
  3969. if (eQt4 >= 0)
  3970. iFinal = eQt4;
  3971. else if (eQt5 >= 0)
  3972. iFinal = eQt5;
  3973. else if (eGtk2 >= 0)
  3974. iFinal = eGtk2;
  3975. else if (eGtk3 >= 0)
  3976. iFinal = eGtk3;
  3977. #ifdef CARLA_OS_MAC
  3978. else if (eCocoa >= 0)
  3979. iFinal = eCocoa;
  3980. #endif
  3981. #ifdef CARLA_OS_WIN
  3982. else if (eWindows >= 0)
  3983. iFinal = eWindows;
  3984. #endif
  3985. #ifdef HAVE_X11
  3986. else if (eX11 >= 0)
  3987. iFinal = eX11;
  3988. #endif
  3989. //else if (eExt >= 0) // TODO
  3990. // iFinal = eExt;
  3991. #ifndef LV2_UIS_ONLY_BRIDGES
  3992. # ifdef CARLA_OS_MAC
  3993. else if (iCocoa >= 0)
  3994. iFinal = iCocoa;
  3995. # endif
  3996. # ifdef CARLA_OS_WIN
  3997. else if (iWindows >= 0)
  3998. iFinal = iWindows;
  3999. # endif
  4000. # ifdef HAVE_X11
  4001. else if (iX11 >= 0)
  4002. iFinal = iX11;
  4003. # endif
  4004. #endif
  4005. else if (iExt >= 0)
  4006. iFinal = iExt;
  4007. if (iFinal < 0)
  4008. {
  4009. // no suitable UI found, see if there's one which supports ui:showInterface
  4010. bool hasShowInterface = false;
  4011. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  4012. {
  4013. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  4014. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  4015. {
  4016. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  4017. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  4018. continue;
  4019. iFinal = static_cast<int>(i);
  4020. hasShowInterface = true;
  4021. break;
  4022. }
  4023. }
  4024. if (! hasShowInterface)
  4025. {
  4026. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  4027. return;
  4028. }
  4029. }
  4030. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  4031. // ---------------------------------------------------------------
  4032. // check supported ui features
  4033. bool canContinue = true;
  4034. bool canDelete = true;
  4035. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  4036. {
  4037. if (! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  4038. {
  4039. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  4040. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type))
  4041. {
  4042. canContinue = false;
  4043. break;
  4044. }
  4045. }
  4046. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__makeResident) == 0)
  4047. canDelete = false;
  4048. }
  4049. if (! canContinue)
  4050. {
  4051. fUi.rdfDescriptor = nullptr;
  4052. return;
  4053. }
  4054. // ---------------------------------------------------------------
  4055. // initialize ui according to type
  4056. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  4057. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  4058. {
  4059. // -----------------------------------------------------------
  4060. // initialize osc-bridge
  4061. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  4062. {
  4063. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  4064. fUi.type = UI::TYPE_OSC;
  4065. pData->osc.thread.setOscData(bridgeBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  4066. delete[] bridgeBinary;
  4067. return;
  4068. }
  4069. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3)
  4070. {
  4071. carla_stderr2("Failed to find UI bridge binary, cannot use UI");
  4072. fUi.rdfDescriptor = nullptr;
  4073. return;
  4074. }
  4075. }
  4076. #ifdef LV2_UIS_ONLY_BRIDGES
  4077. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  4078. fUi.rdfDescriptor = nullptr;
  4079. return;
  4080. #endif
  4081. // ---------------------------------------------------------------
  4082. // open UI DLL
  4083. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary, canDelete))
  4084. {
  4085. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUi.rdfDescriptor->Binary));
  4086. fUi.rdfDescriptor = nullptr;
  4087. return;
  4088. }
  4089. // ---------------------------------------------------------------
  4090. // get UI DLL main entry
  4091. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  4092. if (uiDescFn == nullptr)
  4093. {
  4094. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  4095. pData->uiLibClose();
  4096. fUi.rdfDescriptor = nullptr;
  4097. return;
  4098. }
  4099. // ---------------------------------------------------------------
  4100. // get UI descriptor that matches UI URI
  4101. uint32_t i = 0;
  4102. while ((fUi.descriptor = uiDescFn(i++)))
  4103. {
  4104. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  4105. break;
  4106. }
  4107. if (fUi.descriptor == nullptr)
  4108. {
  4109. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  4110. pData->uiLibClose();
  4111. fUi.rdfDescriptor = nullptr;
  4112. return;
  4113. }
  4114. // ---------------------------------------------------------------
  4115. // check if ui is usable
  4116. switch (uiType)
  4117. {
  4118. case LV2_UI_QT4:
  4119. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  4120. fUi.type = UI::TYPE_EMBED;
  4121. break;
  4122. case LV2_UI_QT5:
  4123. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  4124. fUi.type = UI::TYPE_EMBED;
  4125. break;
  4126. case LV2_UI_GTK2:
  4127. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  4128. fUi.type = UI::TYPE_EMBED;
  4129. break;
  4130. case LV2_UI_GTK3:
  4131. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  4132. fUi.type = UI::TYPE_EMBED;
  4133. break;
  4134. #ifdef CARLA_OS_MAC
  4135. case LV2_UI_COCOA:
  4136. carla_stdout("Will use LV2 Cocoa UI");
  4137. fUi.type = UI::TYPE_EMBED;
  4138. break;
  4139. #endif
  4140. #ifdef CARLA_OS_WIN
  4141. case LV2_UI_WINDOWS:
  4142. carla_stdout("Will use LV2 Windows UI");
  4143. fUi.type = UI::TYPE_EMBED;
  4144. break;
  4145. #endif
  4146. case LV2_UI_X11:
  4147. #ifdef HAVE_X11
  4148. carla_stdout("Will use LV2 X11 UI");
  4149. #else
  4150. carla_stdout("Will use LV2 X11 UI, NOT!");
  4151. #endif
  4152. fUi.type = UI::TYPE_EMBED;
  4153. break;
  4154. case LV2_UI_EXTERNAL:
  4155. case LV2_UI_OLD_EXTERNAL:
  4156. carla_stdout("Will use LV2 External UI");
  4157. fUi.type = UI::TYPE_EXTERNAL;
  4158. break;
  4159. }
  4160. if (fUi.type == UI::TYPE_NULL)
  4161. {
  4162. pData->uiLibClose();
  4163. fUi.descriptor = nullptr;
  4164. fUi.rdfDescriptor = nullptr;
  4165. return;
  4166. }
  4167. // ---------------------------------------------------------------
  4168. // initialize ui data
  4169. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  4170. fLv2Options.windowTitle = carla_strdup(guiTitle.toUtf8().constData());
  4171. fLv2Options.opts[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  4172. fLv2Options.opts[Lv2PluginOptions::WindowTitle].value = fLv2Options.windowTitle;
  4173. // ---------------------------------------------------------------
  4174. // initialize ui features (part 1)
  4175. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  4176. uiDataFt->data_access = fDescriptor->extension_data;
  4177. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  4178. uiPortMapFt->handle = this;
  4179. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  4180. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  4181. uiResizeFt->handle = this;
  4182. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  4183. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  4184. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  4185. uiExternalHostFt->plugin_human_id = fLv2Options.windowTitle;
  4186. // ---------------------------------------------------------------
  4187. // initialize ui features (part 2)
  4188. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  4189. fFeatures[j] = new LV2_Feature;
  4190. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  4191. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  4192. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  4193. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  4194. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  4195. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  4196. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  4197. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  4198. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  4199. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  4200. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  4201. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  4202. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  4203. fFeatures[kFeatureIdUiParent]->data = nullptr;
  4204. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  4205. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  4206. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  4207. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  4208. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  4209. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  4210. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  4211. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  4212. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  4213. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  4214. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  4215. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  4216. // ---------------------------------------------------------------
  4217. // initialize ui extensions
  4218. if (fUi.descriptor->extension_data == nullptr)
  4219. return;
  4220. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  4221. fExt.uishow = (const LV2UI_Show_Interface*)fUi.descriptor->extension_data(LV2_UI__showInterface);
  4222. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  4223. // check if invalid
  4224. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  4225. fExt.uiidle = nullptr;
  4226. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  4227. fExt.uishow = nullptr;
  4228. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  4229. fExt.uiprograms = nullptr;
  4230. }
  4231. // -------------------------------------------------------------------
  4232. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  4233. {
  4234. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  4235. carla_stdout("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  4236. fAtomQueueIn.put(atom, portIndex);
  4237. }
  4238. void handleUridMap(const LV2_URID urid, const char* const uri)
  4239. {
  4240. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  4241. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  4242. carla_stdout("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count()-1, uri);
  4243. if (urid < fCustomURIDs.count())
  4244. {
  4245. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  4246. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr,);
  4247. if (std::strcmp(ourURI, uri) != 0)
  4248. {
  4249. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  4250. }
  4251. }
  4252. else
  4253. {
  4254. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  4255. fCustomURIDs.append(carla_strdup(uri));
  4256. }
  4257. }
  4258. // -------------------------------------------------------------------
  4259. private:
  4260. LV2_Handle fHandle;
  4261. LV2_Handle fHandle2;
  4262. LV2_Feature* fFeatures[kFeatureCountAll+1];
  4263. const LV2_Descriptor* fDescriptor;
  4264. const LV2_RDF_Descriptor* fRdfDescriptor;
  4265. float** fAudioInBuffers;
  4266. float** fAudioOutBuffers;
  4267. float** fCvInBuffers;
  4268. float** fCvOutBuffers;
  4269. float* fParamBuffers;
  4270. int32_t fLatencyIndex; // -1 if invalid
  4271. Lv2AtomQueue fAtomQueueIn;
  4272. Lv2AtomQueue fAtomQueueOut;
  4273. LV2_Atom_Forge fAtomForge;
  4274. PluginCVData fCvIn;
  4275. PluginCVData fCvOut;
  4276. Lv2PluginEventData fEventsIn;
  4277. Lv2PluginEventData fEventsOut;
  4278. Lv2PluginOptions fLv2Options;
  4279. LinkedList<const char*> fCustomURIDs;
  4280. bool fFirstActive; // first process() call after activate()
  4281. EngineTimeInfo fLastTimeInfo;
  4282. struct Extensions {
  4283. const LV2_Options_Interface* options;
  4284. const LV2_State_Interface* state;
  4285. const LV2_Worker_Interface* worker;
  4286. const LV2_Programs_Interface* programs;
  4287. const LV2UI_Idle_Interface* uiidle;
  4288. const LV2UI_Show_Interface* uishow;
  4289. const LV2_Programs_UI_Interface* uiprograms;
  4290. Extensions()
  4291. : options(nullptr),
  4292. state(nullptr),
  4293. worker(nullptr),
  4294. programs(nullptr),
  4295. uiidle(nullptr),
  4296. uishow(nullptr),
  4297. uiprograms(nullptr) {}
  4298. } fExt;
  4299. struct UI {
  4300. enum Type {
  4301. TYPE_NULL,
  4302. TYPE_EMBED,
  4303. TYPE_EXTERNAL,
  4304. TYPE_OSC
  4305. };
  4306. Type type;
  4307. LV2UI_Handle handle;
  4308. LV2UI_Widget widget;
  4309. const LV2UI_Descriptor* descriptor;
  4310. const LV2_RDF_UI* rdfDescriptor;
  4311. CarlaPluginUi* window;
  4312. UI()
  4313. : type(TYPE_NULL),
  4314. handle(nullptr),
  4315. widget(nullptr),
  4316. descriptor(nullptr),
  4317. rdfDescriptor(nullptr),
  4318. window(nullptr) {}
  4319. ~UI()
  4320. {
  4321. CARLA_ASSERT(handle == nullptr);
  4322. CARLA_ASSERT(widget == nullptr);
  4323. CARLA_ASSERT(descriptor == nullptr);
  4324. CARLA_ASSERT(rdfDescriptor == nullptr);
  4325. CARLA_ASSERT(window == nullptr);
  4326. }
  4327. } fUi;
  4328. // -------------------------------------------------------------------
  4329. // Event Feature
  4330. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4331. {
  4332. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4333. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4334. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4335. return 0;
  4336. }
  4337. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4338. {
  4339. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4340. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4341. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4342. return 0;
  4343. }
  4344. // -------------------------------------------------------------------
  4345. // Logs Feature
  4346. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4347. {
  4348. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4349. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4350. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4351. #ifndef DEBUG
  4352. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4353. return 0;
  4354. #endif
  4355. va_list args;
  4356. va_start(args, fmt);
  4357. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4358. va_end(args);
  4359. return ret;
  4360. }
  4361. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4362. {
  4363. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4364. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4365. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4366. #ifndef DEBUG
  4367. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4368. return 0;
  4369. #endif
  4370. int ret = 0;
  4371. switch (type)
  4372. {
  4373. case CARLA_URI_MAP_ID_LOG_ERROR:
  4374. std::fprintf(stderr, "\x1b[31m");
  4375. ret = std::vfprintf(stderr, fmt, ap);
  4376. std::fprintf(stderr, "\x1b[0m");
  4377. break;
  4378. case CARLA_URI_MAP_ID_LOG_NOTE:
  4379. ret = std::vfprintf(stdout, fmt, ap);
  4380. break;
  4381. case CARLA_URI_MAP_ID_LOG_TRACE:
  4382. #ifdef DEBUG
  4383. std::fprintf(stdout, "\x1b[30;1m");
  4384. ret = std::vfprintf(stdout, fmt, ap);
  4385. std::fprintf(stdout, "\x1b[0m");
  4386. #endif
  4387. break;
  4388. case CARLA_URI_MAP_ID_LOG_WARNING:
  4389. ret = std::vfprintf(stderr, fmt, ap);
  4390. break;
  4391. default:
  4392. break;
  4393. }
  4394. return ret;
  4395. }
  4396. // -------------------------------------------------------------------
  4397. // Programs Feature
  4398. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4399. {
  4400. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4401. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4402. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4403. }
  4404. // -------------------------------------------------------------------
  4405. // Resize Port Feature
  4406. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4407. {
  4408. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4409. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4410. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  4411. }
  4412. // -------------------------------------------------------------------
  4413. // State Feature
  4414. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4415. {
  4416. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4417. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4418. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4419. QDir dir;
  4420. dir.mkpath(path);
  4421. return strdup(path);
  4422. }
  4423. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4424. {
  4425. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4426. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4427. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4428. QDir dir(absolute_path);
  4429. return strdup(dir.canonicalPath().toUtf8().constData());
  4430. }
  4431. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4432. {
  4433. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4434. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4435. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4436. QDir dir(abstract_path);
  4437. return strdup(dir.absolutePath().toUtf8().constData());
  4438. }
  4439. 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)
  4440. {
  4441. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4442. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4443. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4444. }
  4445. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4446. {
  4447. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4448. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4449. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4450. }
  4451. // -------------------------------------------------------------------
  4452. // URI-Map Feature
  4453. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4454. {
  4455. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4456. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4457. // unused
  4458. (void)map;
  4459. }
  4460. // -------------------------------------------------------------------
  4461. // URID Feature
  4462. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4463. {
  4464. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4465. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4466. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4467. // Atom types
  4468. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4469. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4470. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4471. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4472. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4473. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4474. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4475. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4476. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  4477. return CARLA_URI_MAP_ID_ATOM_EVENT;
  4478. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4479. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4480. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4481. return CARLA_URI_MAP_ID_ATOM_INT;
  4482. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4483. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4484. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4485. return CARLA_URI_MAP_ID_ATOM_LONG;
  4486. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  4487. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  4488. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4489. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4490. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4491. return CARLA_URI_MAP_ID_ATOM_PATH;
  4492. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4493. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4494. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4495. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4496. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4497. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4498. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  4499. return CARLA_URI_MAP_ID_ATOM_SOUND;
  4500. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4501. return CARLA_URI_MAP_ID_ATOM_STRING;
  4502. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4503. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4504. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4505. return CARLA_URI_MAP_ID_ATOM_URI;
  4506. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4507. return CARLA_URI_MAP_ID_ATOM_URID;
  4508. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4509. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4510. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4511. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4512. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4513. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4514. // BufSize types
  4515. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4516. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4517. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4518. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4519. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4520. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4521. // Log types
  4522. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4523. return CARLA_URI_MAP_ID_LOG_ERROR;
  4524. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4525. return CARLA_URI_MAP_ID_LOG_NOTE;
  4526. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4527. return CARLA_URI_MAP_ID_LOG_TRACE;
  4528. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4529. return CARLA_URI_MAP_ID_LOG_WARNING;
  4530. // Time types
  4531. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4532. return CARLA_URI_MAP_ID_TIME_POSITION;
  4533. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4534. return CARLA_URI_MAP_ID_TIME_BAR;
  4535. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4536. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4537. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4538. return CARLA_URI_MAP_ID_TIME_BEAT;
  4539. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4540. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4541. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4542. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4543. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4544. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4545. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4546. return CARLA_URI_MAP_ID_TIME_FRAME;
  4547. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4548. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4549. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4550. return CARLA_URI_MAP_ID_TIME_SPEED;
  4551. // Others
  4552. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4553. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4554. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4555. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4556. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  4557. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  4558. // Custom
  4559. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  4560. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  4561. if (std::strcmp(uri, URI_CARLA_FRONTEND_WIN_ID) == 0)
  4562. return CARLA_URI_MAP_ID_CARLA_FRONTEND_WIN_ID;
  4563. // Custom types
  4564. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4565. }
  4566. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4567. {
  4568. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4569. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4570. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4571. // Atom types
  4572. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4573. return LV2_ATOM__Blank;
  4574. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4575. return LV2_ATOM__Bool;
  4576. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4577. return LV2_ATOM__Chunk;
  4578. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4579. return LV2_ATOM__Double;
  4580. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  4581. return LV2_ATOM__Event;
  4582. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4583. return LV2_ATOM__Float;
  4584. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4585. return LV2_ATOM__Int;
  4586. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4587. return LV2_ATOM__Literal;
  4588. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4589. return LV2_ATOM__Long;
  4590. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  4591. return LV2_ATOM__Number;
  4592. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4593. return LV2_ATOM__Object;
  4594. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4595. return LV2_ATOM__Path;
  4596. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4597. return LV2_ATOM__Property;
  4598. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4599. return LV2_ATOM__Resource;
  4600. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4601. return LV2_ATOM__Sequence;
  4602. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  4603. return LV2_ATOM__Sound;
  4604. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4605. return LV2_ATOM__String;
  4606. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4607. return LV2_ATOM__Tuple;
  4608. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4609. return LV2_ATOM__URI;
  4610. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4611. return LV2_ATOM__URID;
  4612. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4613. return LV2_ATOM__Vector;
  4614. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4615. return LV2_ATOM__atomTransfer;
  4616. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4617. return LV2_ATOM__eventTransfer;
  4618. // BufSize types
  4619. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4620. return LV2_BUF_SIZE__maxBlockLength;
  4621. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4622. return LV2_BUF_SIZE__minBlockLength;
  4623. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4624. return LV2_BUF_SIZE__sequenceSize;
  4625. // Log types
  4626. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4627. return LV2_LOG__Error;
  4628. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4629. return LV2_LOG__Note;
  4630. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4631. return LV2_LOG__Trace;
  4632. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4633. return LV2_LOG__Warning;
  4634. // Time types
  4635. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4636. return LV2_TIME__Position;
  4637. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4638. return LV2_TIME__bar;
  4639. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4640. return LV2_TIME__barBeat;
  4641. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4642. return LV2_TIME__beat;
  4643. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4644. return LV2_TIME__beatUnit;
  4645. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4646. return LV2_TIME__beatsPerBar;
  4647. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4648. return LV2_TIME__beatsPerMinute;
  4649. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4650. return LV2_TIME__frame;
  4651. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4652. return LV2_TIME__framesPerSecond;
  4653. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4654. return LV2_TIME__speed;
  4655. // Others
  4656. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4657. return LV2_MIDI__MidiEvent;
  4658. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4659. return LV2_PARAMETERS__sampleRate;
  4660. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  4661. return LV2_UI__windowTitle;
  4662. // Custom
  4663. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  4664. return URI_CARLA_ATOM_WORKER;
  4665. if (urid == CARLA_URI_MAP_ID_CARLA_FRONTEND_WIN_ID)
  4666. return URI_CARLA_FRONTEND_WIN_ID;
  4667. // Custom types
  4668. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4669. }
  4670. // -------------------------------------------------------------------
  4671. // Worker Feature
  4672. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4673. {
  4674. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4675. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4676. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4677. }
  4678. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4679. {
  4680. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4681. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4682. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4683. }
  4684. // -------------------------------------------------------------------
  4685. // External UI Feature
  4686. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4687. {
  4688. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4689. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4690. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4691. }
  4692. // -------------------------------------------------------------------
  4693. // UI Port-Map Feature
  4694. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4695. {
  4696. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4697. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4698. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4699. }
  4700. // -------------------------------------------------------------------
  4701. // UI Resize Feature
  4702. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4703. {
  4704. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4705. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4706. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4707. }
  4708. // -------------------------------------------------------------------
  4709. // UI Extension
  4710. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4711. {
  4712. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4713. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4714. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4715. }
  4716. // -------------------------------------------------------------------
  4717. // Lilv State
  4718. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4719. {
  4720. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4721. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4722. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4723. }
  4724. // -------------------------------------------------------------------
  4725. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4726. };
  4727. // -------------------------------------------------------------------------------------------------------------------
  4728. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4729. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4730. {
  4731. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4732. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4733. const int32_t portIndex = argv[0]->i;
  4734. const char* const atomBuf = (const char*)&argv[1]->s;
  4735. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4736. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  4737. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4738. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  4739. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4740. return 0;
  4741. }
  4742. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4743. {
  4744. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4745. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4746. const int32_t urid = argv[0]->i;
  4747. const char* const uri = (const char*)&argv[1]->s;
  4748. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4749. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4750. return 0;
  4751. }
  4752. #undef lv2PluginPtr
  4753. CARLA_BACKEND_END_NAMESPACE
  4754. #endif // WANT_LV2
  4755. // -------------------------------------------------------------------------------------------------------------------
  4756. CARLA_BACKEND_START_NAMESPACE
  4757. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4758. {
  4759. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  4760. #ifdef WANT_LV2
  4761. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4762. if (! plugin->init(init.filename, init.name, init.label))
  4763. {
  4764. delete plugin;
  4765. return nullptr;
  4766. }
  4767. plugin->reload();
  4768. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4769. {
  4770. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4771. delete plugin;
  4772. return nullptr;
  4773. }
  4774. return plugin;
  4775. #else
  4776. init.engine->setLastError("LV2 support not available");
  4777. return nullptr;
  4778. #endif
  4779. }
  4780. CARLA_BACKEND_END_NAMESPACE
  4781. // -------------------------------------------------------------------------------------------------------------------