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.

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