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.

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