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.

5453 lines
204KB

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