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.

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