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.

5305 lines
200KB

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