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.

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