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.

5462 lines
205KB

  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef WANT_LV2
  20. // need this first for juce headers
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaLv2Utils.hpp"
  23. #include "CarlaPluginUi.hpp"
  24. #include "Lv2AtomQueue.hpp"
  25. #include "../engine/CarlaEngineOsc.hpp"
  26. extern "C" {
  27. #include "rtmempool/rtmempool-lv2.h"
  28. }
  29. #include <QtCore/QDir>
  30. #include <QtCore/QUrl>
  31. // -----------------------------------------------------
  32. CARLA_BACKEND_START_NAMESPACE
  33. #if 0
  34. }
  35. #endif
  36. // Maximum default buffer size
  37. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  38. // Extra Plugin Hints
  39. const unsigned int PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  40. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  41. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  42. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  43. // Extra Parameter Hints
  44. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  45. const unsigned int PARAMETER_IS_TRIGGER = 0x2000;
  46. // LV2 Event Data/Types
  47. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  48. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  49. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  50. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  51. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  52. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  53. // LV2 URI Map Ids
  54. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  56. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  57. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  58. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 5;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 6;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 7;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 8;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 9;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 10;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 11;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 12;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 13;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 14;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 15;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 16;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 17;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 18;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_WORKER = 19; // custom
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 20;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 21;
  76. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 22;
  77. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 23;
  78. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 24;
  79. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 25;
  80. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 26;
  81. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 27;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 28;
  83. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 29; // base type
  84. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 30; // values
  85. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 31;
  86. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 32;
  87. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 33;
  88. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 34;
  89. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 35;
  90. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 36;
  91. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 37;
  92. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 38;
  93. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 39;
  94. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 40;
  95. const uint32_t CARLA_URI_MAP_ID_COUNT = 41;
  96. // LV2 Feature Ids
  97. const uint32_t kFeatureIdBufSizeBounded = 0;
  98. const uint32_t kFeatureIdBufSizeFixed = 1;
  99. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  100. const uint32_t kFeatureIdEvent = 3;
  101. const uint32_t kFeatureIdHardRtCapable = 4;
  102. const uint32_t kFeatureIdInPlaceBroken = 5;
  103. const uint32_t kFeatureIdIsLive = 6;
  104. const uint32_t kFeatureIdLogs = 7;
  105. const uint32_t kFeatureIdOptions = 8;
  106. const uint32_t kFeatureIdPrograms = 9;
  107. const uint32_t kFeatureIdResizePort = 10;
  108. const uint32_t kFeatureIdRtMemPool = 11;
  109. const uint32_t kFeatureIdRtMemPoolOld = 12;
  110. const uint32_t kFeatureIdStateMakePath = 13;
  111. const uint32_t kFeatureIdStateMapPath = 14;
  112. const uint32_t kFeatureIdStrictBounds = 15;
  113. const uint32_t kFeatureIdUriMap = 16;
  114. const uint32_t kFeatureIdUridMap = 17;
  115. const uint32_t kFeatureIdUridUnmap = 18;
  116. const uint32_t kFeatureIdWorker = 19;
  117. const uint32_t kFeatureCountPlugin = 20;
  118. const uint32_t kFeatureIdUiDataAccess = 20;
  119. const uint32_t kFeatureIdUiInstanceAccess = 21;
  120. const uint32_t kFeatureIdUiIdleInterface = 22;
  121. const uint32_t kFeatureIdUiFixedSize = 23;
  122. const uint32_t kFeatureIdUiMakeResident = 24;
  123. const uint32_t kFeatureIdUiNoUserResize = 25;
  124. const uint32_t kFeatureIdUiParent = 26;
  125. const uint32_t kFeatureIdUiPortMap = 27;
  126. const uint32_t kFeatureIdUiPortSubscribe = 28;
  127. const uint32_t kFeatureIdUiResize = 29;
  128. const uint32_t kFeatureIdUiTouch = 30;
  129. const uint32_t kFeatureIdExternalUi = 31;
  130. const uint32_t kFeatureIdExternalUiOld = 32;
  131. const uint32_t kFeatureCountAll = 33;
  132. // -----------------------------------------------------
  133. struct Lv2EventData {
  134. uint32_t type;
  135. uint32_t rindex;
  136. CarlaEngineEventPort* port;
  137. union {
  138. LV2_Atom_Buffer* atom;
  139. LV2_Event_Buffer* event;
  140. LV2_MIDI midi;
  141. };
  142. Lv2EventData()
  143. : type(0x0),
  144. rindex(0),
  145. port(nullptr) {}
  146. ~Lv2EventData()
  147. {
  148. if (port != nullptr)
  149. {
  150. delete port;
  151. port = nullptr;
  152. }
  153. const uint32_t rtype(type);
  154. type = 0x0;
  155. if (rtype & CARLA_EVENT_DATA_ATOM)
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  158. std::free(atom);
  159. atom = nullptr;
  160. }
  161. else if (rtype & CARLA_EVENT_DATA_EVENT)
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  164. std::free(event);
  165. event = nullptr;
  166. }
  167. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  170. delete[] midi.data;
  171. midi.data = nullptr;
  172. }
  173. }
  174. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  175. };
  176. struct Lv2PluginEventData {
  177. uint32_t count;
  178. Lv2EventData* data;
  179. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  180. uint32_t ctrlIndex;
  181. Lv2PluginEventData()
  182. : count(0),
  183. data(nullptr),
  184. ctrl(nullptr),
  185. ctrlIndex(0) {}
  186. ~Lv2PluginEventData()
  187. {
  188. CARLA_ASSERT_INT(count == 0, count);
  189. CARLA_ASSERT(data == nullptr);
  190. CARLA_ASSERT(ctrl == nullptr);
  191. CARLA_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  192. }
  193. void createNew(const uint32_t newCount)
  194. {
  195. CARLA_SAFE_ASSERT_INT(count == 0, count);
  196. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  197. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  198. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  199. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  200. data = new Lv2EventData[newCount];
  201. count = newCount;
  202. }
  203. void clear()
  204. {
  205. if (data != nullptr)
  206. {
  207. for (uint32_t i=0; i < count; ++i)
  208. {
  209. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  210. data[i].port = nullptr;
  211. }
  212. delete[] data;
  213. data = nullptr;
  214. }
  215. count = 0;
  216. ctrl = nullptr;
  217. ctrlIndex = 0;
  218. }
  219. void initBuffers() const noexcept
  220. {
  221. for (uint32_t i=0; i < count; ++i)
  222. {
  223. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  224. data[i].port->initBuffer();
  225. }
  226. }
  227. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginEventData)
  228. };
  229. // -----------------------------------------------------
  230. struct Lv2PluginOptions {
  231. enum OptIndex {
  232. MaxBlockLenth = 0,
  233. MinBlockLenth,
  234. SequenceSize,
  235. SampleRate,
  236. Null
  237. };
  238. int maxBufferSize;
  239. int minBufferSize;
  240. int sequenceSize;
  241. double sampleRate;
  242. LV2_Options_Option opts[5];
  243. Lv2PluginOptions()
  244. : maxBufferSize(0),
  245. minBufferSize(0),
  246. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  247. sampleRate(0.0)
  248. {
  249. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  250. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  251. optMaxBlockLenth.subject = 0;
  252. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  253. optMaxBlockLenth.size = sizeof(int);
  254. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  255. optMaxBlockLenth.value = &maxBufferSize;
  256. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  257. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  258. optMinBlockLenth.subject = 0;
  259. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  260. optMinBlockLenth.size = sizeof(int);
  261. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  262. optMinBlockLenth.value = &minBufferSize;
  263. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  264. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  265. optSequenceSize.subject = 0;
  266. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  267. optSequenceSize.size = sizeof(int);
  268. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  269. optSequenceSize.value = &sequenceSize;
  270. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  271. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  272. optSampleRate.subject = 0;
  273. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  274. optSampleRate.size = sizeof(double);
  275. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  276. optSampleRate.value = &sampleRate;
  277. LV2_Options_Option& optNull(opts[Null]);
  278. optNull.context = LV2_OPTIONS_INSTANCE;
  279. optNull.subject = 0;
  280. optNull.key = CARLA_URI_MAP_ID_NULL;
  281. optNull.size = 0;
  282. optNull.type = CARLA_URI_MAP_ID_NULL;
  283. optNull.value = nullptr;
  284. }
  285. };
  286. // -----------------------------------------------------
  287. class Lv2Plugin : public CarlaPlugin,
  288. CarlaPluginUi::CloseCallback
  289. {
  290. public:
  291. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  292. : CarlaPlugin(engine, id),
  293. fHandle(nullptr),
  294. fHandle2(nullptr),
  295. fDescriptor(nullptr),
  296. fRdfDescriptor(nullptr),
  297. fAudioInBuffers(nullptr),
  298. fAudioOutBuffers(nullptr),
  299. fCvInBuffers(nullptr),
  300. fCvOutBuffers(nullptr),
  301. fParamBuffers(nullptr),
  302. fFirstActive(true)
  303. {
  304. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  305. carla_fill<LV2_Feature*>(fFeatures, kFeatureCountAll+1, nullptr);
  306. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  307. fCustomURIDs.append(nullptr);
  308. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  309. # pragma GCC diagnostic push
  310. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  311. #endif
  312. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  313. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  314. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  315. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  316. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  317. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  318. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  319. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  320. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  321. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  322. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  323. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  324. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  325. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  326. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  327. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  328. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  329. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  330. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  331. # pragma GCC diagnostic pop
  332. #endif
  333. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  334. }
  335. ~Lv2Plugin() override
  336. {
  337. carla_debug("Lv2Plugin::~Lv2Plugin()");
  338. // close UI
  339. if (fUi.type != UI::TYPE_NULL)
  340. {
  341. showCustomUI(false);
  342. if (fUi.type == UI::TYPE_OSC)
  343. {
  344. pData->osc.thread.stop(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  345. }
  346. else
  347. {
  348. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  349. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  350. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  351. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  352. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  353. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  354. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  355. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  356. fUi.descriptor = nullptr;
  357. pData->uiLibClose();
  358. }
  359. if (fUi.title != nullptr)
  360. {
  361. delete[] fUi.title;
  362. fUi.title = nullptr;
  363. }
  364. if (fUi.window != nullptr)
  365. {
  366. delete fUi.window;
  367. fUi.window = nullptr;
  368. }
  369. fUi.rdfDescriptor = nullptr;
  370. }
  371. pData->singleMutex.lock();
  372. pData->masterMutex.lock();
  373. if (pData->client != nullptr && pData->client->isActive())
  374. pData->client->deactivate();
  375. if (pData->active)
  376. {
  377. deactivate();
  378. pData->active = false;
  379. }
  380. if (fDescriptor != nullptr)
  381. {
  382. if (fDescriptor->cleanup != nullptr)
  383. {
  384. if (fHandle != nullptr)
  385. fDescriptor->cleanup(fHandle);
  386. if (fHandle2 != nullptr)
  387. fDescriptor->cleanup(fHandle2);
  388. }
  389. fHandle = nullptr;
  390. fHandle2 = nullptr;
  391. fDescriptor = nullptr;
  392. }
  393. if (fRdfDescriptor != nullptr)
  394. {
  395. delete fRdfDescriptor;
  396. fRdfDescriptor = nullptr;
  397. }
  398. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  399. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  400. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  401. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  402. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  403. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  404. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  405. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  406. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  407. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  408. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  409. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  410. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  411. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  412. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  413. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  414. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  415. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  416. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  417. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  418. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  419. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  420. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  421. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  422. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  423. {
  424. if (fFeatures[i] != nullptr)
  425. {
  426. delete fFeatures[i];
  427. fFeatures[i] = nullptr;
  428. }
  429. }
  430. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  431. {
  432. const char*& uri(it.getValue());
  433. if (uri != nullptr)
  434. {
  435. delete[] uri;
  436. uri = nullptr;
  437. }
  438. }
  439. fCustomURIDs.clear();
  440. clearBuffers();
  441. }
  442. // -------------------------------------------------------------------
  443. // Information (base)
  444. PluginType getType() const noexcept override
  445. {
  446. return PLUGIN_LV2;
  447. }
  448. PluginCategory getCategory() const noexcept override
  449. {
  450. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  451. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  452. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  453. if (LV2_IS_DELAY(cat1, cat2))
  454. return PLUGIN_CATEGORY_DELAY;
  455. if (LV2_IS_DISTORTION(cat1, cat2))
  456. return PLUGIN_CATEGORY_OTHER;
  457. if (LV2_IS_DYNAMICS(cat1, cat2))
  458. return PLUGIN_CATEGORY_DYNAMICS;
  459. if (LV2_IS_EQ(cat1, cat2))
  460. return PLUGIN_CATEGORY_EQ;
  461. if (LV2_IS_FILTER(cat1, cat2))
  462. return PLUGIN_CATEGORY_FILTER;
  463. if (LV2_IS_GENERATOR(cat1, cat2))
  464. return PLUGIN_CATEGORY_SYNTH;
  465. if (LV2_IS_MODULATOR(cat1, cat2))
  466. return PLUGIN_CATEGORY_MODULATOR;
  467. if (LV2_IS_REVERB(cat1, cat2))
  468. return PLUGIN_CATEGORY_DELAY;
  469. if (LV2_IS_SIMULATOR(cat1, cat2))
  470. return PLUGIN_CATEGORY_OTHER;
  471. if (LV2_IS_SPATIAL(cat1, cat2))
  472. return PLUGIN_CATEGORY_OTHER;
  473. if (LV2_IS_SPECTRAL(cat1, cat2))
  474. return PLUGIN_CATEGORY_UTILITY;
  475. if (LV2_IS_UTILITY(cat1, cat2))
  476. return PLUGIN_CATEGORY_UTILITY;
  477. return CarlaPlugin::getCategory();
  478. }
  479. long getUniqueId() const noexcept override
  480. {
  481. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  482. return static_cast<long>(fRdfDescriptor->UniqueID);
  483. }
  484. // -------------------------------------------------------------------
  485. // Information (count)
  486. uint32_t getMidiInCount() const noexcept override
  487. {
  488. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  489. uint32_t count = 0;
  490. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  491. {
  492. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  493. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  494. count += 1;
  495. }
  496. return count;
  497. }
  498. uint32_t getMidiOutCount() const noexcept override
  499. {
  500. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  501. uint32_t count = 0;
  502. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  503. {
  504. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  505. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  506. count += 1;
  507. }
  508. return count;
  509. }
  510. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  511. {
  512. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  513. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  514. const int32_t rindex(pData->param.data[parameterId].rindex);
  515. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  516. {
  517. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  518. return port->ScalePointCount;
  519. }
  520. return 0;
  521. }
  522. // -------------------------------------------------------------------
  523. // Information (current data)
  524. // nothing
  525. // -------------------------------------------------------------------
  526. // Information (per-plugin data)
  527. unsigned int getOptionsAvailable() const noexcept override
  528. {
  529. const bool hasMidiIn(getMidiInCount() > 0);
  530. unsigned int options = 0x0;
  531. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  532. if (! (hasMidiIn || needsFixedBuffer()))
  533. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  534. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  535. {
  536. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  537. options |= PLUGIN_OPTION_FORCE_STEREO;
  538. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  539. options |= PLUGIN_OPTION_FORCE_STEREO;
  540. }
  541. if (hasMidiIn)
  542. {
  543. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  544. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  545. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  546. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  547. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  548. }
  549. return options;
  550. }
  551. float getParameterValue(const uint32_t parameterId) const noexcept override
  552. {
  553. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  554. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  555. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  556. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  557. return fParamBuffers[parameterId];
  558. }
  559. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  560. {
  561. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  562. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  563. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  564. const int32_t rindex(pData->param.data[parameterId].rindex);
  565. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  566. {
  567. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  568. if (scalePointId < port->ScalePointCount)
  569. {
  570. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  571. return portScalePoint->Value;
  572. }
  573. }
  574. return 0.0f;
  575. }
  576. void getLabel(char* const strBuf) const noexcept override
  577. {
  578. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  579. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  580. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  581. }
  582. void getMaker(char* const strBuf) const noexcept override
  583. {
  584. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  585. if (fRdfDescriptor->Author != nullptr)
  586. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  587. else
  588. CarlaPlugin::getMaker(strBuf);
  589. }
  590. void getCopyright(char* const strBuf) const noexcept override
  591. {
  592. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  593. if (fRdfDescriptor->License != nullptr)
  594. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  595. else
  596. CarlaPlugin::getCopyright(strBuf);
  597. }
  598. void getRealName(char* const strBuf) const noexcept override
  599. {
  600. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  601. if (fRdfDescriptor->Name != nullptr)
  602. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  603. else
  604. CarlaPlugin::getRealName(strBuf);
  605. }
  606. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  607. {
  608. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  609. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  610. const int32_t rindex(pData->param.data[parameterId].rindex);
  611. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  612. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  613. else
  614. CarlaPlugin::getParameterName(parameterId, strBuf);
  615. }
  616. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  617. {
  618. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  619. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  620. const int32_t rindex(pData->param.data[parameterId].rindex);
  621. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  622. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  623. else
  624. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  625. }
  626. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  627. {
  628. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  629. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  630. const int32_t rindex(pData->param.data[parameterId].rindex);
  631. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  632. {
  633. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  634. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  635. {
  636. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  637. return;
  638. }
  639. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  640. {
  641. switch (port->Unit.Unit)
  642. {
  643. case LV2_PORT_UNIT_BAR:
  644. std::strncpy(strBuf, "bars", STR_MAX);
  645. return;
  646. case LV2_PORT_UNIT_BEAT:
  647. std::strncpy(strBuf, "beats", STR_MAX);
  648. return;
  649. case LV2_PORT_UNIT_BPM:
  650. std::strncpy(strBuf, "BPM", STR_MAX);
  651. return;
  652. case LV2_PORT_UNIT_CENT:
  653. std::strncpy(strBuf, "ct", STR_MAX);
  654. return;
  655. case LV2_PORT_UNIT_CM:
  656. std::strncpy(strBuf, "cm", STR_MAX);
  657. return;
  658. case LV2_PORT_UNIT_COEF:
  659. std::strncpy(strBuf, "(coef)", STR_MAX);
  660. return;
  661. case LV2_PORT_UNIT_DB:
  662. std::strncpy(strBuf, "dB", STR_MAX);
  663. return;
  664. case LV2_PORT_UNIT_DEGREE:
  665. std::strncpy(strBuf, "deg", STR_MAX);
  666. return;
  667. case LV2_PORT_UNIT_FRAME:
  668. std::strncpy(strBuf, "frames", STR_MAX);
  669. return;
  670. case LV2_PORT_UNIT_HZ:
  671. std::strncpy(strBuf, "Hz", STR_MAX);
  672. return;
  673. case LV2_PORT_UNIT_INCH:
  674. std::strncpy(strBuf, "in", STR_MAX);
  675. return;
  676. case LV2_PORT_UNIT_KHZ:
  677. std::strncpy(strBuf, "kHz", STR_MAX);
  678. return;
  679. case LV2_PORT_UNIT_KM:
  680. std::strncpy(strBuf, "km", STR_MAX);
  681. return;
  682. case LV2_PORT_UNIT_M:
  683. std::strncpy(strBuf, "m", STR_MAX);
  684. return;
  685. case LV2_PORT_UNIT_MHZ:
  686. std::strncpy(strBuf, "MHz", STR_MAX);
  687. return;
  688. case LV2_PORT_UNIT_MIDINOTE:
  689. std::strncpy(strBuf, "note", STR_MAX);
  690. return;
  691. case LV2_PORT_UNIT_MILE:
  692. std::strncpy(strBuf, "mi", STR_MAX);
  693. return;
  694. case LV2_PORT_UNIT_MIN:
  695. std::strncpy(strBuf, "min", STR_MAX);
  696. return;
  697. case LV2_PORT_UNIT_MM:
  698. std::strncpy(strBuf, "mm", STR_MAX);
  699. return;
  700. case LV2_PORT_UNIT_MS:
  701. std::strncpy(strBuf, "ms", STR_MAX);
  702. return;
  703. case LV2_PORT_UNIT_OCT:
  704. std::strncpy(strBuf, "oct", STR_MAX);
  705. return;
  706. case LV2_PORT_UNIT_PC:
  707. std::strncpy(strBuf, "%", STR_MAX);
  708. return;
  709. case LV2_PORT_UNIT_S:
  710. std::strncpy(strBuf, "s", STR_MAX);
  711. return;
  712. case LV2_PORT_UNIT_SEMITONE:
  713. std::strncpy(strBuf, "semi", STR_MAX);
  714. return;
  715. }
  716. }
  717. }
  718. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  719. }
  720. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  721. {
  722. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  723. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  724. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  725. const int32_t rindex(pData->param.data[parameterId].rindex);
  726. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  727. {
  728. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  729. if (scalePointId < port->ScalePointCount)
  730. {
  731. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  732. if (portScalePoint->Label != nullptr)
  733. {
  734. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  735. return;
  736. }
  737. }
  738. }
  739. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  740. }
  741. // -------------------------------------------------------------------
  742. // Set data (state)
  743. void prepareForSave() override
  744. {
  745. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  746. if (fExt.state != nullptr && fExt.state->save != nullptr)
  747. {
  748. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  749. if (fHandle2 != nullptr)
  750. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  751. }
  752. }
  753. // -------------------------------------------------------------------
  754. // Set data (internal stuff)
  755. void setName(const char* const newName) override
  756. {
  757. CarlaPlugin::setName(newName);
  758. if (fUi.title == nullptr)
  759. return;
  760. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  761. delete[] fUi.title;
  762. fUi.title = carla_strdup(guiTitle.toUtf8().constData());
  763. if (fUi.window != nullptr)
  764. fUi.window->setTitle(fUi.title);
  765. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  766. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fUi.title;
  767. }
  768. // -------------------------------------------------------------------
  769. // Set data (plugin-specific stuff)
  770. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  771. {
  772. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  773. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  774. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  775. fParamBuffers[parameterId] = fixedValue;
  776. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  777. }
  778. #if 0
  779. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  780. {
  781. CARLA_ASSERT(fDescriptor != nullptr);
  782. CARLA_ASSERT(fHandle != nullptr);
  783. CARLA_ASSERT(type != nullptr);
  784. CARLA_ASSERT(key != nullptr);
  785. CARLA_ASSERT(value != nullptr);
  786. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  787. if (type == nullptr)
  788. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  789. if (key == nullptr)
  790. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  791. if (value == nullptr)
  792. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  793. CarlaPlugin::setCustomData(type, key, value, sendGui);
  794. // FIXME - we should only call this once, after all data is stored
  795. if (fExt.state != nullptr)
  796. {
  797. LV2_State_Status status;
  798. {
  799. const ScopedSingleProcessLocker spl(this, true);
  800. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  801. if (fHandle2 != nullptr)
  802. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  803. }
  804. switch (status)
  805. {
  806. case LV2_STATE_SUCCESS:
  807. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  808. break;
  809. case LV2_STATE_ERR_UNKNOWN:
  810. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  811. break;
  812. case LV2_STATE_ERR_BAD_TYPE:
  813. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  814. break;
  815. case LV2_STATE_ERR_BAD_FLAGS:
  816. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  817. break;
  818. case LV2_STATE_ERR_NO_FEATURE:
  819. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  820. break;
  821. case LV2_STATE_ERR_NO_PROPERTY:
  822. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  823. break;
  824. }
  825. }
  826. if (sendGui)
  827. {
  828. //CustomData cdata;
  829. //cdata.type = type;
  830. //cdata.key = key;
  831. //cdata.value = value;
  832. //uiTransferCustomData(&cdata);
  833. }
  834. }
  835. void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  836. {
  837. CARLA_ASSERT(fRdfDescriptor != nullptr);
  838. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount));
  839. CARLA_ASSERT(sendGui || sendOsc || sendCallback); // never call this from RT
  840. if (index < -1)
  841. index = -1;
  842. else if (index > static_cast<int32_t>(fRdfDescriptor->PresetCount))
  843. return;
  844. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  845. {
  846. if (const LilvState* state = gLv2World.getState(fRdfDescriptor->Presets[index].URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  847. {
  848. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  849. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  850. if (fHandle2 != nullptr)
  851. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  852. }
  853. }
  854. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  855. }
  856. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  857. {
  858. CARLA_ASSERT(fDescriptor != nullptr);
  859. CARLA_ASSERT(fHandle != nullptr);
  860. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
  861. if (index < -1)
  862. index = -1;
  863. else if (index > static_cast<int32_t>(pData->midiprog.count))
  864. return;
  865. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  866. {
  867. const uint32_t bank(pData->midiprog.data[index].bank);
  868. const uint32_t program(pData->midiprog.data[index].program);
  869. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  870. fExt.programs->select_program(fHandle, bank, program);
  871. if (fHandle2 != nullptr)
  872. fExt.programs->select_program(fHandle2, bank, program);
  873. }
  874. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  875. }
  876. #endif
  877. // -------------------------------------------------------------------
  878. // Set ui stuff
  879. void showCustomUI(const bool yesNo) override
  880. {
  881. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  882. if (fUi.type == UI::TYPE_OSC)
  883. {
  884. if (yesNo)
  885. {
  886. pData->osc.data.free();
  887. pData->osc.thread.start();
  888. }
  889. else
  890. {
  891. if (pData->osc.data.target != nullptr)
  892. {
  893. osc_send_hide(pData->osc.data);
  894. osc_send_quit(pData->osc.data);
  895. pData->osc.data.free();
  896. }
  897. pData->osc.thread.stop(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  898. }
  899. return;
  900. }
  901. // take some precautions
  902. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  903. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr,);
  904. if (yesNo)
  905. {
  906. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->instantiate != nullptr,);
  907. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->cleanup != nullptr,);
  908. }
  909. else
  910. {
  911. if (fUi.handle == nullptr)
  912. return;
  913. }
  914. if (yesNo)
  915. {
  916. if (fUi.handle == nullptr)
  917. {
  918. const char* msg = nullptr;
  919. switch (fUi.rdfDescriptor->Type)
  920. {
  921. case LV2_UI_GTK2:
  922. case LV2_UI_GTK3:
  923. case LV2_UI_QT4:
  924. case LV2_UI_QT5:
  925. case LV2_UI_EXTERNAL:
  926. case LV2_UI_OLD_EXTERNAL:
  927. msg = "Invalid UI type";
  928. break;
  929. case LV2_UI_COCOA:
  930. #ifdef CARLA_OS_MAC
  931. fUi.window = CarlaPluginUi::newCocoa(this);
  932. #else
  933. msg = "UI is for MacOS only";
  934. #endif
  935. break;
  936. case LV2_UI_WINDOWS:
  937. #ifdef CARLA_OS_WIN
  938. fUi.window = CarlaPluginUi::newWindows(this);
  939. #else
  940. msg = "UI is for Windows only";
  941. #endif
  942. break;
  943. case LV2_UI_X11:
  944. #ifdef HAVE_X11
  945. fUi.window = CarlaPluginUi::newX11(this);
  946. #else
  947. msg = "UI is only for systems with X11";
  948. #endif
  949. break;
  950. default:
  951. msg = "Unknown UI type";
  952. break;
  953. }
  954. if (fUi.window == nullptr)
  955. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  956. fUi.window->setTitle(fUi.title);
  957. #if 0
  958. if (const char* const win = getenv("CARLA_TRANSIENT_WINDOW"))
  959. {
  960. const long winl = std::atol(win);
  961. carla_stderr2("got transient, %s vs %li", win, winl);
  962. fUi.window->setTransient(winl);
  963. }
  964. #endif
  965. fFeatures[kFeatureIdUiParent]->data = fUi.window->getPtr();
  966. fUi.widget = nullptr;
  967. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  968. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  969. }
  970. CARLA_SAFE_ASSERT(fUi.handle != nullptr);
  971. CARLA_SAFE_ASSERT(fUi.type != UI::TYPE_EXTERNAL || fUi.widget != nullptr);
  972. if (fUi.handle == nullptr || (fUi.type == UI::TYPE_EXTERNAL && fUi.widget == nullptr))
  973. {
  974. fUi.widget = nullptr;
  975. if (fUi.handle != nullptr)
  976. {
  977. fUi.descriptor->cleanup(fUi.handle);
  978. fUi.handle = nullptr;
  979. }
  980. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  981. }
  982. updateUi();
  983. if (fUi.type == UI::TYPE_EMBED)
  984. {
  985. fUi.window->show();
  986. }
  987. else
  988. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  989. }
  990. else
  991. {
  992. if (fUi.type == UI::TYPE_EMBED)
  993. {
  994. CARLA_SAFE_ASSERT(fUi.window != nullptr);
  995. if (fUi.window != nullptr)
  996. fUi.window->hide();
  997. }
  998. else
  999. {
  1000. CARLA_SAFE_ASSERT(fUi.widget != nullptr);
  1001. if (fUi.widget != nullptr)
  1002. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  1003. }
  1004. fUi.descriptor->cleanup(fUi.handle);
  1005. fUi.handle = nullptr;
  1006. fUi.widget = nullptr;
  1007. }
  1008. }
  1009. void idle() override
  1010. {
  1011. if (! fAtomQueueOut.isEmpty())
  1012. {
  1013. char dumpBuf[fAtomQueueOut.getSize()];
  1014. Lv2AtomQueue tmpQueue;
  1015. tmpQueue.copyAndDumpDataFromQueue(fAtomQueueOut, dumpBuf);
  1016. CARLA_SAFE_ASSERT(! tmpQueue.isEmpty());
  1017. uint32_t portIndex;
  1018. const LV2_Atom* atom;
  1019. const bool hasPortEvent(fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr);
  1020. for (; tmpQueue.get(&atom, &portIndex);)
  1021. {
  1022. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  1023. {
  1024. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1025. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1026. }
  1027. else if (fUi.type == UI::TYPE_OSC)
  1028. {
  1029. if (pData->osc.data.target != nullptr)
  1030. {
  1031. QByteArray chunk((const char*)atom, static_cast<int>(lv2_atom_total_size(atom)));
  1032. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.toBase64().constData());
  1033. }
  1034. }
  1035. else
  1036. {
  1037. if (hasPortEvent)
  1038. fUi.descriptor->port_event(fUi.handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1039. }
  1040. }
  1041. }
  1042. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  1043. {
  1044. if (fUi.type == UI::TYPE_EMBED && fUi.window != nullptr)
  1045. fUi.window->idle();
  1046. else if (fUi.type == UI::TYPE_EXTERNAL && fUi.widget != nullptr)
  1047. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  1048. if (fExt.uiidle != nullptr && fExt.uiidle->idle(fUi.handle) != 0)
  1049. {
  1050. showCustomUI(false);
  1051. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1052. }
  1053. }
  1054. CarlaPlugin::idle();
  1055. }
  1056. // -------------------------------------------------------------------
  1057. // Plugin state
  1058. void reload() override
  1059. {
  1060. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1061. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1062. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1063. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1064. carla_debug("Lv2Plugin::reload() - start");
  1065. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1066. // Safely disable plugin for reload
  1067. const ScopedDisabler sd(this);
  1068. if (pData->active)
  1069. deactivate();
  1070. clearBuffers();
  1071. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1072. const uint32_t portCount(fRdfDescriptor->PortCount);
  1073. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1074. aIns = aOuts = cvIns = cvOuts = params = 0;
  1075. LinkedList<unsigned int> evIns, evOuts;
  1076. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1077. bool forcedStereoIn, forcedStereoOut;
  1078. forcedStereoIn = forcedStereoOut = false;
  1079. bool needsCtrlIn, needsCtrlOut;
  1080. needsCtrlIn = needsCtrlOut = false;
  1081. for (uint32_t i=0; i < portCount; ++i)
  1082. {
  1083. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1084. if (LV2_IS_PORT_AUDIO(portTypes))
  1085. {
  1086. if (LV2_IS_PORT_INPUT(portTypes))
  1087. aIns += 1;
  1088. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1089. aOuts += 1;
  1090. }
  1091. else if (LV2_IS_PORT_CV(portTypes))
  1092. {
  1093. if (LV2_IS_PORT_INPUT(portTypes))
  1094. cvIns += 1;
  1095. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1096. cvOuts += 1;
  1097. }
  1098. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1099. {
  1100. if (LV2_IS_PORT_INPUT(portTypes))
  1101. evIns.append(CARLA_EVENT_DATA_ATOM);
  1102. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1103. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1104. }
  1105. else if (LV2_IS_PORT_EVENT(portTypes))
  1106. {
  1107. if (LV2_IS_PORT_INPUT(portTypes))
  1108. evIns.append(CARLA_EVENT_DATA_EVENT);
  1109. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1110. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1111. }
  1112. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1113. {
  1114. if (LV2_IS_PORT_INPUT(portTypes))
  1115. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1116. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1117. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1118. }
  1119. else if (LV2_IS_PORT_CONTROL(portTypes))
  1120. params += 1;
  1121. }
  1122. recheckExtensions();
  1123. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1124. {
  1125. if (fHandle2 == nullptr)
  1126. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1127. if (fHandle2 != nullptr)
  1128. {
  1129. if (aIns == 1)
  1130. {
  1131. aIns = 2;
  1132. forcedStereoIn = true;
  1133. }
  1134. if (aOuts == 1)
  1135. {
  1136. aOuts = 2;
  1137. forcedStereoOut = true;
  1138. }
  1139. }
  1140. }
  1141. if (aIns > 0)
  1142. {
  1143. pData->audioIn.createNew(aIns);
  1144. fAudioInBuffers = new float*[aIns];
  1145. for (uint32_t i=0; i < aIns; ++i)
  1146. fAudioInBuffers[i] = nullptr;
  1147. }
  1148. if (aOuts > 0)
  1149. {
  1150. pData->audioOut.createNew(aOuts);
  1151. fAudioOutBuffers = new float*[aOuts];
  1152. needsCtrlIn = true;
  1153. for (uint32_t i=0; i < aOuts; ++i)
  1154. fAudioOutBuffers[i] = nullptr;
  1155. }
  1156. if (cvIns > 0)
  1157. {
  1158. fCvIn.createNew(cvIns);
  1159. fCvInBuffers = new float*[cvIns];
  1160. for (uint32_t i=0; i < cvIns; ++i)
  1161. fCvInBuffers[i] = nullptr;
  1162. }
  1163. if (cvOuts > 0)
  1164. {
  1165. fCvOut.createNew(cvOuts);
  1166. fCvOutBuffers = new float*[cvOuts];
  1167. for (uint32_t i=0; i < cvOuts; ++i)
  1168. fCvOutBuffers[i] = nullptr;
  1169. }
  1170. if (params > 0)
  1171. {
  1172. pData->param.createNew(params, true);
  1173. fParamBuffers = new float[params];
  1174. FLOAT_CLEAR(fParamBuffers, params);
  1175. }
  1176. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1177. {
  1178. fEventsIn.createNew(count);
  1179. for (uint32_t i=0; i < count; ++i)
  1180. {
  1181. const uint32_t& type(evIns.getAt(i));
  1182. if (type == CARLA_EVENT_DATA_ATOM)
  1183. {
  1184. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1185. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1186. }
  1187. else if (type == CARLA_EVENT_DATA_EVENT)
  1188. {
  1189. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1190. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1191. }
  1192. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1193. {
  1194. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1195. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1196. fEventsIn.data[i].midi.data = new unsigned char[eventBufferSize];
  1197. }
  1198. }
  1199. }
  1200. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1201. {
  1202. fEventsOut.createNew(count);
  1203. for (uint32_t i=0; i < count; ++i)
  1204. {
  1205. const uint32_t& type(evOuts.getAt(i));
  1206. if (type == CARLA_EVENT_DATA_ATOM)
  1207. {
  1208. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1209. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1210. }
  1211. else if (type == CARLA_EVENT_DATA_EVENT)
  1212. {
  1213. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1214. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1215. }
  1216. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1217. {
  1218. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1219. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1220. fEventsOut.data[i].midi.data = new unsigned char[eventBufferSize];
  1221. }
  1222. }
  1223. }
  1224. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1225. CarlaString portName;
  1226. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1227. {
  1228. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1229. portName.clear();
  1230. 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))
  1231. {
  1232. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1233. {
  1234. portName = pData->name;
  1235. portName += ":";
  1236. }
  1237. portName += fRdfDescriptor->Ports[i].Name;
  1238. portName.truncate(portNameSize);
  1239. }
  1240. if (LV2_IS_PORT_AUDIO(portTypes))
  1241. {
  1242. if (LV2_IS_PORT_INPUT(portTypes))
  1243. {
  1244. uint32_t j = iAudioIn++;
  1245. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1246. pData->audioIn.ports[j].rindex = i;
  1247. if (forcedStereoIn)
  1248. {
  1249. portName += "_2";
  1250. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1251. pData->audioIn.ports[1].rindex = i;
  1252. }
  1253. }
  1254. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1255. {
  1256. uint32_t j = iAudioOut++;
  1257. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1258. pData->audioOut.ports[j].rindex = i;
  1259. if (forcedStereoOut)
  1260. {
  1261. portName += "_2";
  1262. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1263. pData->audioOut.ports[1].rindex = i;
  1264. }
  1265. }
  1266. else
  1267. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1268. }
  1269. else if (LV2_IS_PORT_CV(portTypes))
  1270. {
  1271. if (LV2_IS_PORT_INPUT(portTypes))
  1272. {
  1273. uint32_t j = iCvIn++;
  1274. fCvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1275. fCvIn.ports[j].rindex = i;
  1276. }
  1277. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1278. {
  1279. uint32_t j = iCvOut++;
  1280. fCvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1281. fCvOut.ports[j].rindex = i;
  1282. }
  1283. else
  1284. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1285. }
  1286. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1287. {
  1288. if (LV2_IS_PORT_INPUT(portTypes))
  1289. {
  1290. uint32_t j = iEvIn++;
  1291. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1292. if (fHandle2 != nullptr)
  1293. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1294. fEventsIn.data[j].rindex = i;
  1295. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1296. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1297. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1298. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1299. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1300. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1301. if (evIns.count() == 1)
  1302. {
  1303. fEventsIn.ctrl = &fEventsIn.data[j];
  1304. fEventsIn.ctrlIndex = j;
  1305. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1306. needsCtrlIn = true;
  1307. }
  1308. else
  1309. {
  1310. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1311. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1312. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1313. {
  1314. fEventsIn.ctrl = &fEventsIn.data[j];
  1315. fEventsIn.ctrlIndex = j;
  1316. }
  1317. }
  1318. }
  1319. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1320. {
  1321. uint32_t j = iEvOut++;
  1322. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1323. if (fHandle2 != nullptr)
  1324. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1325. fEventsOut.data[j].rindex = i;
  1326. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1327. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1328. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1329. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1330. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1331. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1332. if (evOuts.count() == 1)
  1333. {
  1334. fEventsOut.ctrl = &fEventsOut.data[j];
  1335. fEventsOut.ctrlIndex = j;
  1336. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1337. needsCtrlOut = true;
  1338. }
  1339. else
  1340. {
  1341. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1342. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1343. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1344. {
  1345. fEventsOut.ctrl = &fEventsOut.data[j];
  1346. fEventsOut.ctrlIndex = j;
  1347. }
  1348. }
  1349. }
  1350. else
  1351. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1352. }
  1353. else if (LV2_IS_PORT_EVENT(portTypes))
  1354. {
  1355. if (LV2_IS_PORT_INPUT(portTypes))
  1356. {
  1357. uint32_t j = iEvIn++;
  1358. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1359. if (fHandle2 != nullptr)
  1360. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1361. fEventsIn.data[j].rindex = i;
  1362. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1363. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1364. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1365. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1366. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1367. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1368. if (evIns.count() == 1)
  1369. {
  1370. fEventsIn.ctrl = &fEventsIn.data[j];
  1371. fEventsIn.ctrlIndex = j;
  1372. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1373. needsCtrlIn = true;
  1374. }
  1375. else
  1376. {
  1377. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1378. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1379. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1380. {
  1381. fEventsIn.ctrl = &fEventsIn.data[j];
  1382. fEventsIn.ctrlIndex = j;
  1383. }
  1384. }
  1385. }
  1386. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1387. {
  1388. uint32_t j = iEvOut++;
  1389. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1390. if (fHandle2 != nullptr)
  1391. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1392. fEventsOut.data[j].rindex = i;
  1393. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1394. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1395. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1396. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1397. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1398. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1399. if (evOuts.count() == 1)
  1400. {
  1401. fEventsOut.ctrl = &fEventsOut.data[j];
  1402. fEventsOut.ctrlIndex = j;
  1403. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1404. needsCtrlOut = true;
  1405. }
  1406. else
  1407. {
  1408. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1409. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1410. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1411. {
  1412. fEventsOut.ctrl = &fEventsOut.data[j];
  1413. fEventsOut.ctrlIndex = j;
  1414. }
  1415. }
  1416. }
  1417. else
  1418. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1419. }
  1420. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1421. {
  1422. if (LV2_IS_PORT_INPUT(portTypes))
  1423. {
  1424. uint32_t j = iEvIn++;
  1425. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1426. if (fHandle2 != nullptr)
  1427. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1428. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1429. fEventsIn.data[j].rindex = i;
  1430. if (evIns.count() == 1)
  1431. {
  1432. needsCtrlIn = true;
  1433. fEventsIn.ctrl = &fEventsIn.data[j];
  1434. fEventsIn.ctrlIndex = j;
  1435. }
  1436. else
  1437. {
  1438. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1439. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1440. {
  1441. fEventsIn.ctrl = &fEventsIn.data[j];
  1442. fEventsIn.ctrlIndex = j;
  1443. }
  1444. }
  1445. }
  1446. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1447. {
  1448. uint32_t j = iEvOut++;
  1449. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1450. if (fHandle2 != nullptr)
  1451. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1452. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1453. fEventsOut.data[j].rindex = i;
  1454. if (evOuts.count() == 1)
  1455. {
  1456. needsCtrlOut = true;
  1457. fEventsOut.ctrl = &fEventsOut.data[j];
  1458. fEventsOut.ctrlIndex = j;
  1459. }
  1460. else
  1461. {
  1462. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1463. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1464. {
  1465. fEventsOut.ctrl = &fEventsOut.data[j];
  1466. fEventsOut.ctrlIndex = j;
  1467. }
  1468. }
  1469. }
  1470. else
  1471. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1472. }
  1473. else if (LV2_IS_PORT_CONTROL(portTypes))
  1474. {
  1475. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1476. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1477. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1478. uint32_t j = iCtrl++;
  1479. pData->param.data[j].hints = 0x0;
  1480. pData->param.data[j].index = static_cast<int32_t>(j);
  1481. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1482. pData->param.data[j].midiCC = -1;
  1483. pData->param.data[j].midiChannel = 0;
  1484. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  1485. float min, max, def, step, stepSmall, stepLarge;
  1486. // min value
  1487. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1488. min = portPoints.Minimum;
  1489. else
  1490. min = 0.0f;
  1491. // max value
  1492. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1493. max = portPoints.Maximum;
  1494. else
  1495. max = 1.0f;
  1496. if (min > max)
  1497. max = min;
  1498. // stupid hack for ir.lv2 (broken plugin)
  1499. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1500. {
  1501. min = 0.0f;
  1502. max = (float)0xffffff;
  1503. }
  1504. if (max - min == 0.0f)
  1505. {
  1506. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1507. max = min + 0.1f;
  1508. }
  1509. // default value
  1510. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1511. {
  1512. def = portPoints.Default;
  1513. }
  1514. else
  1515. {
  1516. // no default value
  1517. if (min < 0.0f && max > 0.0f)
  1518. def = 0.0f;
  1519. else
  1520. def = min;
  1521. }
  1522. if (def < min)
  1523. def = min;
  1524. else if (def > max)
  1525. def = max;
  1526. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1527. {
  1528. min *= sampleRate;
  1529. max *= sampleRate;
  1530. def *= sampleRate;
  1531. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1532. }
  1533. if (LV2_IS_PORT_TOGGLED(portProps))
  1534. {
  1535. step = max - min;
  1536. stepSmall = step;
  1537. stepLarge = step;
  1538. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1539. }
  1540. else if (LV2_IS_PORT_INTEGER(portProps))
  1541. {
  1542. step = 1.0f;
  1543. stepSmall = 1.0f;
  1544. stepLarge = 10.0f;
  1545. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1546. }
  1547. else
  1548. {
  1549. float range = max - min;
  1550. step = range/100.0f;
  1551. stepSmall = range/1000.0f;
  1552. stepLarge = range/10.0f;
  1553. }
  1554. if (LV2_IS_PORT_INPUT(portTypes))
  1555. {
  1556. pData->param.data[j].type = PARAMETER_INPUT;
  1557. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1558. {
  1559. carla_stderr("Plugin has latency input port, this should not happen!");
  1560. }
  1561. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1562. {
  1563. def = sampleRate;
  1564. step = 1.0f;
  1565. stepSmall = 1.0f;
  1566. stepLarge = 1.0f;
  1567. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1568. }
  1569. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1570. {
  1571. pData->param.special[j] = PARAMETER_SPECIAL_LV2_FREEWHEEL;
  1572. }
  1573. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1574. {
  1575. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1576. }
  1577. else
  1578. {
  1579. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1580. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1581. needsCtrlIn = true;
  1582. }
  1583. // MIDI CC value
  1584. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1585. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1586. {
  1587. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1588. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  1589. }
  1590. }
  1591. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1592. {
  1593. pData->param.data[j].type = PARAMETER_OUTPUT;
  1594. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1595. {
  1596. min = 0.0f;
  1597. max = sampleRate;
  1598. def = 0.0f;
  1599. step = 1.0f;
  1600. stepSmall = 1.0f;
  1601. stepLarge = 1.0f;
  1602. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1603. }
  1604. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1605. {
  1606. def = sampleRate;
  1607. step = 1.0f;
  1608. stepSmall = 1.0f;
  1609. stepLarge = 1.0f;
  1610. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1611. }
  1612. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1613. {
  1614. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1615. }
  1616. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1617. {
  1618. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1619. }
  1620. else
  1621. {
  1622. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1623. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1624. needsCtrlOut = true;
  1625. }
  1626. }
  1627. else
  1628. {
  1629. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1630. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1631. }
  1632. // extra parameter hints
  1633. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1634. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1635. if (LV2_IS_PORT_TRIGGER(portProps))
  1636. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1637. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1638. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1639. if (LV2_IS_PORT_ENUMERATION(portProps))
  1640. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1641. // check if parameter is not enabled or automable
  1642. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1643. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1644. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1645. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1646. pData->param.ranges[j].min = min;
  1647. pData->param.ranges[j].max = max;
  1648. pData->param.ranges[j].def = def;
  1649. pData->param.ranges[j].step = step;
  1650. pData->param.ranges[j].stepSmall = stepSmall;
  1651. pData->param.ranges[j].stepLarge = stepLarge;
  1652. // Start parameters in their default values (except freewheel, which is off by default)
  1653. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1654. fParamBuffers[j] = min;
  1655. else
  1656. fParamBuffers[j] = def;
  1657. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1658. if (fHandle2 != nullptr)
  1659. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1660. }
  1661. else
  1662. {
  1663. // Port Type not supported, but it's optional anyway
  1664. fDescriptor->connect_port(fHandle, i, nullptr);
  1665. if (fHandle2 != nullptr)
  1666. fDescriptor->connect_port(fHandle2, i, nullptr);
  1667. }
  1668. }
  1669. if (needsCtrlIn)
  1670. {
  1671. portName.clear();
  1672. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1673. {
  1674. portName = pData->name;
  1675. portName += ":";
  1676. }
  1677. portName += "events-in";
  1678. portName.truncate(portNameSize);
  1679. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1680. }
  1681. if (needsCtrlOut)
  1682. {
  1683. portName.clear();
  1684. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1685. {
  1686. portName = pData->name;
  1687. portName += ":";
  1688. }
  1689. portName += "events-out";
  1690. portName.truncate(portNameSize);
  1691. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1692. }
  1693. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1694. fAtomQueueIn.createBuffer(eventBufferSize);
  1695. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1696. fAtomQueueOut.createBuffer(eventBufferSize);
  1697. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1698. fEventsIn.ctrl->port = pData->event.portIn;
  1699. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1700. fEventsOut.ctrl->port = pData->event.portOut;
  1701. if (forcedStereoIn || forcedStereoOut)
  1702. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1703. else
  1704. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1705. // plugin hints
  1706. pData->hints = 0x0;
  1707. if (isRealtimeSafe())
  1708. pData->hints |= PLUGIN_IS_RTSAFE;
  1709. if (fUi.type != UI::TYPE_NULL)
  1710. {
  1711. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1712. if (fUi.type == UI::TYPE_EMBED)
  1713. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1714. }
  1715. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1716. pData->hints |= PLUGIN_IS_SYNTH;
  1717. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1718. pData->hints |= PLUGIN_CAN_DRYWET;
  1719. if (aOuts > 0)
  1720. pData->hints |= PLUGIN_CAN_VOLUME;
  1721. if (aOuts >= 2 && aOuts % 2 == 0)
  1722. pData->hints |= PLUGIN_CAN_BALANCE;
  1723. // extra plugin hints
  1724. pData->extraHints = 0x0;
  1725. if (fExt.state != nullptr || fExt.worker != nullptr)
  1726. {
  1727. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1728. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1729. }
  1730. else
  1731. {
  1732. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1733. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1734. }
  1735. bufferSizeChanged(pData->engine->getBufferSize());
  1736. reloadPrograms(true);
  1737. // check latency
  1738. // TODO
  1739. evIns.clear();
  1740. evOuts.clear();
  1741. if (pData->active)
  1742. activate();
  1743. carla_debug("Lv2Plugin::reload() - end");
  1744. }
  1745. #if 0
  1746. void reloadPrograms(const bool init) override
  1747. {
  1748. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  1749. uint32_t i, oldCount = pData->midiprog.count;
  1750. const int32_t current = pData->midiprog.current;
  1751. // special LV2 programs handling
  1752. if (init)
  1753. {
  1754. pData->prog.clear();
  1755. const uint32_t count(fRdfDescriptor->PresetCount);
  1756. if (count > 0)
  1757. {
  1758. pData->prog.createNew(count);
  1759. for (i=0; i < count; ++i)
  1760. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1761. }
  1762. }
  1763. // Delete old programs
  1764. pData->midiprog.clear();
  1765. // Query new programs
  1766. uint32_t count = 0;
  1767. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1768. {
  1769. while (fExt.programs->get_program(fHandle, count))
  1770. ++count;
  1771. }
  1772. if (count > 0)
  1773. {
  1774. pData->midiprog.createNew(count);
  1775. // Update data
  1776. for (i=0; i < count; ++i)
  1777. {
  1778. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1779. CARLA_ASSERT(pdesc != nullptr);
  1780. CARLA_ASSERT(pdesc->name != nullptr);
  1781. pData->midiprog.data[i].bank = pdesc->bank;
  1782. pData->midiprog.data[i].program = pdesc->program;
  1783. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1784. }
  1785. }
  1786. #ifndef BUILD_BRIDGE
  1787. // Update OSC Names
  1788. if (pData->engine->isOscControlRegistered())
  1789. {
  1790. pData->engine->osc_send_control_set_midi_program_count(fId, count);
  1791. for (i=0; i < count; ++i)
  1792. 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);
  1793. }
  1794. #endif
  1795. if (init)
  1796. {
  1797. if (count > 0)
  1798. {
  1799. setMidiProgram(0, false, false, false);
  1800. }
  1801. else
  1802. {
  1803. // load default state
  1804. if (const LilvState* state = gLv2World.getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1805. {
  1806. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1807. if (fHandle2 != nullptr)
  1808. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1809. }
  1810. }
  1811. }
  1812. else
  1813. {
  1814. // Check if current program is invalid
  1815. bool programChanged = false;
  1816. if (count == oldCount+1)
  1817. {
  1818. // one midi program added, probably created by user
  1819. pData->midiprog.current = oldCount;
  1820. programChanged = true;
  1821. }
  1822. else if (current < 0 && count > 0)
  1823. {
  1824. // programs exist now, but not before
  1825. pData->midiprog.current = 0;
  1826. programChanged = true;
  1827. }
  1828. else if (current >= 0 && count == 0)
  1829. {
  1830. // programs existed before, but not anymore
  1831. pData->midiprog.current = -1;
  1832. programChanged = true;
  1833. }
  1834. else if (current >= static_cast<int32_t>(count))
  1835. {
  1836. // current midi program > count
  1837. pData->midiprog.current = 0;
  1838. programChanged = true;
  1839. }
  1840. else
  1841. {
  1842. // no change
  1843. pData->midiprog.current = current;
  1844. }
  1845. if (programChanged)
  1846. setMidiProgram(pData->midiprog.current, true, true, true);
  1847. pData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  1848. }
  1849. }
  1850. #endif
  1851. // -------------------------------------------------------------------
  1852. // Plugin processing
  1853. void activate() noexcept override
  1854. {
  1855. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1856. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1857. if (fDescriptor->activate != nullptr)
  1858. {
  1859. try {
  1860. fDescriptor->activate(fHandle);
  1861. } catch(...) {}
  1862. if (fHandle2 != nullptr)
  1863. {
  1864. try {
  1865. fDescriptor->activate(fHandle2);
  1866. } catch(...) {}
  1867. }
  1868. }
  1869. fFirstActive = true;
  1870. }
  1871. void deactivate() noexcept override
  1872. {
  1873. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1874. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1875. if (fDescriptor->deactivate != nullptr)
  1876. {
  1877. try {
  1878. fDescriptor->deactivate(fHandle);
  1879. } catch(...) {}
  1880. if (fHandle2 != nullptr)
  1881. {
  1882. try {
  1883. fDescriptor->deactivate(fHandle2);
  1884. } catch(...) {}
  1885. }
  1886. }
  1887. }
  1888. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1889. {
  1890. // --------------------------------------------------------------------------------------------------------
  1891. // Check if active
  1892. if (! pData->active)
  1893. {
  1894. // disable any output sound
  1895. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1896. FLOAT_CLEAR(outBuffer[i], frames);
  1897. return;
  1898. }
  1899. // --------------------------------------------------------------------------------------------------------
  1900. // Event itenerators from different APIs (input)
  1901. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1902. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1903. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1904. for (uint32_t i=0; i < fEventsIn.count; ++i)
  1905. {
  1906. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1907. {
  1908. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1909. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1910. }
  1911. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1912. {
  1913. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1914. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1915. }
  1916. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1917. {
  1918. fEventsIn.data[i].midi.event_count = 0;
  1919. fEventsIn.data[i].midi.size = 0;
  1920. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  1921. evInMidiStates[i].frame_count = frames;
  1922. evInMidiStates[i].position = 0;
  1923. }
  1924. }
  1925. for (uint32_t i=0; i < fEventsOut.count; ++i)
  1926. {
  1927. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1928. {
  1929. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1930. }
  1931. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1932. {
  1933. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1934. }
  1935. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1936. {
  1937. // not needed
  1938. }
  1939. }
  1940. CARLA_PROCESS_CONTINUE_CHECK;
  1941. // --------------------------------------------------------------------------------------------------------
  1942. // Check if needs reset
  1943. if (pData->needsReset)
  1944. {
  1945. uint8_t midiData[3] = { 0 };
  1946. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1947. {
  1948. const uint32_t j = fEventsIn.ctrlIndex;
  1949. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1950. {
  1951. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  1952. {
  1953. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1954. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1955. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1956. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1957. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1958. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1959. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1960. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1961. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1962. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1963. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1964. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1965. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1966. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1967. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1968. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1969. }
  1970. }
  1971. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1972. {
  1973. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1974. {
  1975. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  1976. midiData[1] = k;
  1977. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1978. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1979. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1980. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1981. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1982. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  1983. }
  1984. }
  1985. }
  1986. if (pData->latency > 0)
  1987. {
  1988. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1989. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  1990. }
  1991. pData->needsReset = false;
  1992. }
  1993. // --------------------------------------------------------------------------------------------------------
  1994. // TimeInfo
  1995. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1996. if (fFirstActive || fLastTimeInfo != timeInfo)
  1997. {
  1998. bool doPostRt;
  1999. int32_t rindex;
  2000. // update input ports
  2001. for (uint32_t k=0; k < pData->param.count; ++k)
  2002. {
  2003. if (pData->param.data[k].type != PARAMETER_INPUT)
  2004. continue;
  2005. if (pData->param.special[k] != PARAMETER_SPECIAL_LV2_TIME)
  2006. continue;
  2007. doPostRt = false;
  2008. rindex = pData->param.data[k].rindex;
  2009. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2010. switch (fRdfDescriptor->Ports[rindex].Designation)
  2011. {
  2012. // Non-BBT
  2013. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2014. if (fLastTimeInfo.playing != timeInfo.playing)
  2015. {
  2016. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2017. doPostRt = true;
  2018. }
  2019. break;
  2020. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2021. if (fLastTimeInfo.frame != timeInfo.frame)
  2022. {
  2023. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2024. doPostRt = true;
  2025. }
  2026. break;
  2027. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2028. break;
  2029. // BBT
  2030. case LV2_PORT_DESIGNATION_TIME_BAR:
  2031. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2032. {
  2033. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2034. doPostRt = true;
  2035. }
  2036. break;
  2037. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2038. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2039. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2040. {
  2041. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2042. doPostRt = true;
  2043. }
  2044. break;
  2045. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2046. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2047. {
  2048. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2049. doPostRt = true;
  2050. }
  2051. break;
  2052. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2053. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2054. {
  2055. fParamBuffers[k] = timeInfo.bbt.beatType;
  2056. doPostRt = true;
  2057. }
  2058. break;
  2059. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2060. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2061. {
  2062. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2063. doPostRt = true;
  2064. }
  2065. break;
  2066. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2067. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2068. {
  2069. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2070. doPostRt = true;
  2071. }
  2072. break;
  2073. }
  2074. if (doPostRt)
  2075. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2076. }
  2077. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2078. {
  2079. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2080. continue;
  2081. uint8_t timeInfoBuf[256];
  2082. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2083. LV2_Atom_Forge_Frame forgeFrame;
  2084. lv2_atom_forge_object(&fAtomForge, &forgeFrame, 1, CARLA_URI_MAP_ID_TIME_POSITION);
  2085. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2086. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2087. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2088. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2089. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2090. {
  2091. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2092. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2093. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2094. 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)));
  2095. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2096. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2097. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2098. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2099. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2100. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2101. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2102. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2103. }
  2104. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2105. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2106. CARLA_ASSERT(atom->size < 256);
  2107. }
  2108. pData->postRtEvents.trySplice();
  2109. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2110. CARLA_PROCESS_CONTINUE_CHECK;
  2111. }
  2112. // --------------------------------------------------------------------------------------------------------
  2113. // CV ports
  2114. float* cvInBuf[fCvIn.count /*> 0 ? fCvIn.count : 1*/];
  2115. float* cvOutBuf[fCvOut.count /*> 0 ? fCvOut.count : 1*/];
  2116. for (uint32_t i=0; i < fCvIn.count; ++i)
  2117. {
  2118. CARLA_SAFE_ASSERT_CONTINUE(fCvIn.ports[i].port != nullptr);
  2119. cvInBuf[i] = fCvIn.ports[i].port->getBuffer();
  2120. }
  2121. for (uint32_t i=0; i < fCvOut.count; ++i)
  2122. {
  2123. CARLA_SAFE_ASSERT_CONTINUE(fCvOut.ports[i].port != nullptr);
  2124. cvOutBuf[i] = fCvOut.ports[i].port->getBuffer();
  2125. }
  2126. // --------------------------------------------------------------------------------------------------------
  2127. // Event Input and Processing
  2128. if (fEventsIn.ctrl != nullptr)
  2129. {
  2130. // ----------------------------------------------------------------------------------------------------
  2131. // Message Input
  2132. if (fAtomQueueIn.tryLock())
  2133. {
  2134. if (! fAtomQueueIn.isEmpty())
  2135. {
  2136. const LV2_Atom* atom;
  2137. uint32_t portIndex;
  2138. const uint32_t j = fEventsIn.ctrlIndex;
  2139. for (; fAtomQueueIn.get(&atom, &portIndex);)
  2140. {
  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. void handlePluginUiClosed() override
  3323. {
  3324. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EMBED,);
  3325. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr,);
  3326. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3327. fUi.window->hide();
  3328. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3329. fUi.descriptor->cleanup(fUi.handle);
  3330. fUi.handle = nullptr;
  3331. fUi.widget = nullptr;
  3332. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3333. }
  3334. // -------------------------------------------------------------------
  3335. uint32_t handleUiPortMap(const char* const symbol) const noexcept
  3336. {
  3337. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3338. carla_debug("Lv2Plugin::handleUiPortMap(\"%s\")", symbol);
  3339. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3340. {
  3341. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3342. return i;
  3343. }
  3344. return LV2UI_INVALID_PORT_INDEX;
  3345. }
  3346. int handleUiResize(const int width, const int height)
  3347. {
  3348. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr, 1);
  3349. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3350. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3351. carla_debug("Lv2Plugin::handleUiResize(%i, %i)", width, height);
  3352. fUi.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3353. return 0;
  3354. }
  3355. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3356. {
  3357. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3358. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3359. carla_debug("Lv2Plugin::handleUiWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3360. switch (format)
  3361. {
  3362. case CARLA_URI_MAP_ID_NULL:
  3363. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3364. for (uint32_t i=0; i < pData->param.count; ++i)
  3365. {
  3366. if (pData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3367. {
  3368. const float value(*(const float*)buffer);
  3369. if (fParamBuffers[i] != value)
  3370. setParameterValue(i, value, false, true, true);
  3371. break;
  3372. }
  3373. }
  3374. break;
  3375. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3376. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT:
  3377. CARLA_SAFE_ASSERT_RETURN(((const LV2_Atom*)buffer)->size == bufferSize,);
  3378. fAtomQueueIn.put((const LV2_Atom*)buffer, rindex);
  3379. break;
  3380. default:
  3381. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3382. break;
  3383. }
  3384. }
  3385. // -------------------------------------------------------------------
  3386. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3387. {
  3388. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3389. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3390. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3391. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3392. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3393. int32_t rindex = -1;
  3394. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3395. {
  3396. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3397. {
  3398. rindex = static_cast<int32_t>(i);
  3399. break;
  3400. }
  3401. }
  3402. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3403. switch (type)
  3404. {
  3405. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3406. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3407. for (uint32_t i=0; i < pData->param.count; ++i)
  3408. {
  3409. if (pData->param.data[i].rindex == rindex)
  3410. {
  3411. const float valuef(*(const float*)value);
  3412. setParameterValue(i, valuef, true, true, true);
  3413. break;
  3414. }
  3415. }
  3416. break;
  3417. default:
  3418. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3419. break;
  3420. }
  3421. }
  3422. // -------------------------------------------------------------------
  3423. public:
  3424. bool init(const char* const bundle, const char* const name, const char* const uri)
  3425. {
  3426. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3427. // ---------------------------------------------------------------
  3428. // first checks
  3429. if (pData->client != nullptr)
  3430. {
  3431. pData->engine->setLastError("Plugin client is already registered");
  3432. return false;
  3433. }
  3434. if (bundle == nullptr || bundle[0] == '\0')
  3435. {
  3436. pData->engine->setLastError("null bundle");
  3437. return false;
  3438. }
  3439. if (uri == nullptr || uri[0] == '\0')
  3440. {
  3441. pData->engine->setLastError("null uri");
  3442. return false;
  3443. }
  3444. // ---------------------------------------------------------------
  3445. // get plugin from lv2_rdf (lilv)
  3446. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3447. // Convert bundle filename to URI
  3448. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  3449. if (! qBundle.endsWith(OS_SEP_STR))
  3450. qBundle += OS_SEP_STR;
  3451. // Load bundle
  3452. Lilv::Node lilvBundle(lv2World.new_uri(qBundle.toUtf8().constData()));
  3453. lv2World.load_bundle(lilvBundle);
  3454. fRdfDescriptor = lv2_rdf_new(uri, true);
  3455. if (fRdfDescriptor == nullptr)
  3456. {
  3457. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3458. return false;
  3459. }
  3460. // ---------------------------------------------------------------
  3461. // open DLL
  3462. if (! pData->libOpen(fRdfDescriptor->Binary))
  3463. {
  3464. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3465. return false;
  3466. }
  3467. // ---------------------------------------------------------------
  3468. // get DLL main entry
  3469. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3470. if (descFn == nullptr)
  3471. {
  3472. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3473. return false;
  3474. }
  3475. // -----------------------------------------------------------
  3476. // get descriptor that matches URI
  3477. uint32_t i = 0;
  3478. while ((fDescriptor = descFn(i++)))
  3479. {
  3480. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3481. break;
  3482. }
  3483. if (fDescriptor == nullptr)
  3484. {
  3485. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3486. return false;
  3487. }
  3488. // ---------------------------------------------------------------
  3489. // check supported port-types and features
  3490. bool canContinue = true;
  3491. // Check supported ports
  3492. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3493. {
  3494. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3495. if (! is_lv2_port_supported(portTypes))
  3496. {
  3497. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3498. {
  3499. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3500. canContinue = false;
  3501. break;
  3502. }
  3503. }
  3504. }
  3505. // Check supported features
  3506. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3507. {
  3508. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  3509. {
  3510. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  3511. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  3512. {
  3513. canContinue = false;
  3514. pData->engine->setLastError(msg.toUtf8().constData());
  3515. break;
  3516. }
  3517. else
  3518. carla_stderr("%s", msg.toUtf8().constData());
  3519. }
  3520. }
  3521. if (! canContinue)
  3522. {
  3523. // error already set
  3524. return false;
  3525. }
  3526. // ---------------------------------------------------------------
  3527. // get info
  3528. if (name != nullptr && name[0] != '\0')
  3529. pData->name = pData->engine->getUniquePluginName(name);
  3530. else
  3531. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3532. pData->filename = carla_strdup(bundle);
  3533. // ---------------------------------------------------------------
  3534. // register client
  3535. pData->client = pData->engine->addClient(this);
  3536. if (pData->client == nullptr || ! pData->client->isOk())
  3537. {
  3538. pData->engine->setLastError("Failed to register plugin client");
  3539. return false;
  3540. }
  3541. // ---------------------------------------------------------------
  3542. // initialize options
  3543. fLv2Options.minBufferSize = 1;
  3544. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3545. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3546. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3547. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3548. {
  3549. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3550. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3551. {
  3552. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3553. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3554. }
  3555. }
  3556. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3557. // ---------------------------------------------------------------
  3558. // initialize features (part 1)
  3559. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3560. eventFt->callback_data = this;
  3561. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3562. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3563. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3564. logFt->handle = this;
  3565. logFt->printf = carla_lv2_log_printf;
  3566. logFt->vprintf = carla_lv2_log_vprintf;
  3567. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3568. stateMakePathFt->handle = this;
  3569. stateMakePathFt->path = carla_lv2_state_make_path;
  3570. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3571. stateMapPathFt->handle = this;
  3572. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3573. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3574. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3575. programsFt->handle = this;
  3576. programsFt->program_changed = carla_lv2_program_changed;
  3577. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3578. rsPortFt->data = this;
  3579. rsPortFt->resize = carla_lv2_resize_port;
  3580. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3581. lv2_rtmempool_init(rtMemPoolFt);
  3582. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3583. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3584. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3585. uriMapFt->callback_data = this;
  3586. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3587. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3588. uridMapFt->handle = this;
  3589. uridMapFt->map = carla_lv2_urid_map;
  3590. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3591. uridUnmapFt->handle = this;
  3592. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3593. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3594. workerFt->handle = this;
  3595. workerFt->schedule_work = carla_lv2_worker_schedule;
  3596. // ---------------------------------------------------------------
  3597. // initialize features (part 2)
  3598. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3599. fFeatures[j] = new LV2_Feature;
  3600. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3601. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3602. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3603. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3604. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3605. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3606. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3607. fFeatures[kFeatureIdEvent]->data = eventFt;
  3608. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3609. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3610. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3611. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3612. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3613. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3614. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3615. fFeatures[kFeatureIdLogs]->data = logFt;
  3616. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3617. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3618. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3619. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3620. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3621. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3622. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3623. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3624. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3625. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3626. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3627. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3628. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3629. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3630. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3631. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3632. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3633. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3634. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3635. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3636. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3637. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3638. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3639. fFeatures[kFeatureIdWorker]->data = workerFt;
  3640. // if a fixed buffer is not needed, skip its feature
  3641. if (! needsFixedBuffer())
  3642. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3643. // if power of 2 is not possible, skip its feature FIXME
  3644. //if (fLv2Options.maxBufferSize)
  3645. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3646. // ---------------------------------------------------------------
  3647. // initialize plugin
  3648. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3649. if (fHandle == nullptr)
  3650. {
  3651. pData->engine->setLastError("Plugin failed to initialize");
  3652. return false;
  3653. }
  3654. // ---------------------------------------------------------------
  3655. // load plugin settings
  3656. {
  3657. // set default options
  3658. pData->options = 0x0;
  3659. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3660. if (getMidiInCount() > 0 || needsFixedBuffer())
  3661. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3662. if (pData->engine->getOptions().forceStereo)
  3663. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3664. if (getMidiInCount() > 0)
  3665. {
  3666. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3667. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3668. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3669. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3670. }
  3671. // set identifier string
  3672. CarlaString identifier("LV2/");
  3673. identifier += uri;
  3674. pData->identifier = identifier.dup();
  3675. // load settings
  3676. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3677. // ignore settings, we need this anyway
  3678. if (getMidiInCount() > 0 || needsFixedBuffer())
  3679. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3680. }
  3681. // ---------------------------------------------------------------
  3682. // gui stuff
  3683. if (fRdfDescriptor->UICount != 0)
  3684. initUi();
  3685. return true;
  3686. }
  3687. // -------------------------------------------------------------------
  3688. void initUi()
  3689. {
  3690. // ---------------------------------------------------------------
  3691. // find more appropriate ui
  3692. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3693. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3694. #ifdef BUILD_BRIDGE
  3695. const bool preferUiBridges(false);
  3696. #else
  3697. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3698. #endif
  3699. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3700. {
  3701. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3702. const int ii(static_cast<int>(i));
  3703. switch (fRdfDescriptor->UIs[i].Type)
  3704. {
  3705. case LV2_UI_QT4:
  3706. if (isUiBridgeable(i))
  3707. eQt4 = ii;
  3708. break;
  3709. case LV2_UI_QT5:
  3710. if (isUiBridgeable(i))
  3711. eQt5 = ii;
  3712. break;
  3713. case LV2_UI_GTK2:
  3714. if (isUiBridgeable(i))
  3715. eGtk2 = ii;
  3716. break;
  3717. case LV2_UI_GTK3:
  3718. if (isUiBridgeable(i))
  3719. eGtk3 = ii;
  3720. break;
  3721. #if defined(CARLA_OS_HAIKU)
  3722. #elif defined(CARLA_OS_MAC)
  3723. case LV2_UI_COCOA:
  3724. if (isUiBridgeable(i) && preferUiBridges)
  3725. eCocoa = ii;
  3726. iCocoa = ii;
  3727. break;
  3728. #elif defined(CARLA_OS_WIN)
  3729. case LV2_UI_WINDOWS:
  3730. if (isUiBridgeable(i) && preferUiBridges)
  3731. eWindows = ii;
  3732. iWindows = ii;
  3733. break;
  3734. #else
  3735. case LV2_UI_X11:
  3736. if (isUiBridgeable(i) && preferUiBridges)
  3737. eX11 = ii;
  3738. iX11 = ii;
  3739. break;
  3740. #endif
  3741. case LV2_UI_EXTERNAL:
  3742. case LV2_UI_OLD_EXTERNAL:
  3743. if (isUiBridgeable(i))
  3744. eExt = ii;
  3745. iExt = ii;
  3746. break;
  3747. default:
  3748. break;
  3749. }
  3750. }
  3751. if (eQt4 >= 0)
  3752. iFinal = eQt4;
  3753. else if (eQt5 >= 0)
  3754. iFinal = eQt5;
  3755. else if (eGtk2 >= 0)
  3756. iFinal = eGtk2;
  3757. else if (eGtk3 >= 0)
  3758. iFinal = eGtk3;
  3759. else if (eCocoa >= 0)
  3760. iFinal = eCocoa;
  3761. else if (eWindows >= 0)
  3762. iFinal = eWindows;
  3763. else if (eX11 >= 0)
  3764. iFinal = eX11;
  3765. //else if (eExt >= 0) // TODO
  3766. // iFinal = eExt;
  3767. else if (iCocoa >= 0)
  3768. iFinal = iCocoa;
  3769. else if (iWindows >= 0)
  3770. iFinal = iWindows;
  3771. else if (iX11 >= 0)
  3772. iFinal = iX11;
  3773. else if (iExt >= 0)
  3774. iFinal = iExt;
  3775. if (iFinal < 0)
  3776. {
  3777. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3778. return;
  3779. }
  3780. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3781. // ---------------------------------------------------------------
  3782. // check supported ui features
  3783. bool canContinue = true;
  3784. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3785. {
  3786. if (! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3787. {
  3788. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3789. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type))
  3790. {
  3791. canContinue = false;
  3792. break;
  3793. }
  3794. }
  3795. }
  3796. if (! canContinue)
  3797. {
  3798. fUi.rdfDescriptor = nullptr;
  3799. return;
  3800. }
  3801. // ---------------------------------------------------------------
  3802. // initialize ui according to type
  3803. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3804. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  3805. {
  3806. // -----------------------------------------------------------
  3807. // initialize osc-bridge
  3808. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  3809. {
  3810. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  3811. fUi.type = UI::TYPE_OSC;
  3812. pData->osc.thread.setOscData(bridgeBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3813. delete[] bridgeBinary;
  3814. return;
  3815. }
  3816. }
  3817. // ---------------------------------------------------------------
  3818. // open UI DLL
  3819. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3820. {
  3821. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUi.rdfDescriptor->Binary));
  3822. fUi.rdfDescriptor = nullptr;
  3823. return;
  3824. }
  3825. // ---------------------------------------------------------------
  3826. // get UI DLL main entry
  3827. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  3828. if (uiDescFn == nullptr)
  3829. {
  3830. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3831. pData->uiLibClose();
  3832. fUi.rdfDescriptor = nullptr;
  3833. return;
  3834. }
  3835. // ---------------------------------------------------------------
  3836. // get UI descriptor that matches UI URI
  3837. uint32_t i = 0;
  3838. while ((fUi.descriptor = uiDescFn(i++)))
  3839. {
  3840. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3841. break;
  3842. }
  3843. if (fUi.descriptor == nullptr)
  3844. {
  3845. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3846. pData->uiLibClose();
  3847. fUi.rdfDescriptor = nullptr;
  3848. return;
  3849. }
  3850. // ---------------------------------------------------------------
  3851. // check if ui is usable
  3852. switch (uiType)
  3853. {
  3854. case LV2_UI_QT4:
  3855. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  3856. break;
  3857. case LV2_UI_QT5:
  3858. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  3859. break;
  3860. case LV2_UI_GTK2:
  3861. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  3862. break;
  3863. case LV2_UI_GTK3:
  3864. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  3865. break;
  3866. #if defined(CARLA_OS_HAIKU)
  3867. #elif defined(CARLA_OS_MAC)
  3868. case LV2_UI_COCOA:
  3869. carla_stdout("Will use LV2 Cocoa UI");
  3870. fUi.type = UI::TYPE_EMBED;
  3871. break;
  3872. #elif defined(CARLA_OS_WIN)
  3873. case LV2_UI_WINDOWS:
  3874. carla_stdout("Will use LV2 Windows UI");
  3875. fUi.type = UI::TYPE_EMBED;
  3876. break;
  3877. #else
  3878. case LV2_UI_X11:
  3879. carla_stdout("Will use LV2 X11 UI");
  3880. fUi.type = UI::TYPE_EMBED;
  3881. break;
  3882. #endif
  3883. case LV2_UI_EXTERNAL:
  3884. case LV2_UI_OLD_EXTERNAL:
  3885. carla_stdout("Will use LV2 External UI");
  3886. fUi.type = UI::TYPE_EXTERNAL;
  3887. break;
  3888. }
  3889. if (fUi.type == UI::TYPE_NULL)
  3890. {
  3891. pData->uiLibClose();
  3892. fUi.descriptor = nullptr;
  3893. fUi.rdfDescriptor = nullptr;
  3894. return;
  3895. }
  3896. // ---------------------------------------------------------------
  3897. // initialize ui data
  3898. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  3899. fUi.title = carla_strdup(guiTitle.toUtf8().constData());
  3900. // ---------------------------------------------------------------
  3901. // initialize ui features (part 1)
  3902. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3903. uiDataFt->data_access = fDescriptor->extension_data;
  3904. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3905. uiPortMapFt->handle = this;
  3906. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3907. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3908. uiResizeFt->handle = this;
  3909. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3910. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3911. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3912. uiExternalHostFt->plugin_human_id = fUi.title;
  3913. // ---------------------------------------------------------------
  3914. // initialize ui features (part 2)
  3915. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  3916. fFeatures[j] = new LV2_Feature;
  3917. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3918. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3919. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3920. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3921. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  3922. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  3923. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  3924. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  3925. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  3926. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  3927. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  3928. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  3929. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3930. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3931. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3932. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3933. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  3934. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  3935. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3936. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3937. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  3938. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  3939. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3940. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3941. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3942. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3943. }
  3944. // -------------------------------------------------------------------
  3945. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  3946. {
  3947. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  3948. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  3949. fAtomQueueIn.put(atom, portIndex);
  3950. }
  3951. void handleUridMap(const LV2_URID urid, const char* const uri)
  3952. {
  3953. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  3954. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  3955. carla_debug("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count(), uri);
  3956. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  3957. fCustomURIDs.append(carla_strdup(uri));
  3958. }
  3959. // -------------------------------------------------------------------
  3960. private:
  3961. LV2_Handle fHandle;
  3962. LV2_Handle fHandle2;
  3963. LV2_Feature* fFeatures[kFeatureCountAll+1];
  3964. const LV2_Descriptor* fDescriptor;
  3965. const LV2_RDF_Descriptor* fRdfDescriptor;
  3966. float** fAudioInBuffers;
  3967. float** fAudioOutBuffers;
  3968. float** fCvInBuffers;
  3969. float** fCvOutBuffers;
  3970. float* fParamBuffers;
  3971. Lv2AtomQueue fAtomQueueIn;
  3972. Lv2AtomQueue fAtomQueueOut;
  3973. LV2_Atom_Forge fAtomForge;
  3974. PluginCVData fCvIn;
  3975. PluginCVData fCvOut;
  3976. Lv2PluginEventData fEventsIn;
  3977. Lv2PluginEventData fEventsOut;
  3978. Lv2PluginOptions fLv2Options;
  3979. LinkedList<const char*> fCustomURIDs;
  3980. bool fFirstActive; // first process() call after activate()
  3981. EngineTimeInfo fLastTimeInfo;
  3982. struct Extensions {
  3983. const LV2_Options_Interface* options;
  3984. const LV2_State_Interface* state;
  3985. const LV2_Worker_Interface* worker;
  3986. const LV2_Programs_Interface* programs;
  3987. const LV2UI_Idle_Interface* uiidle;
  3988. const LV2_Programs_UI_Interface* uiprograms;
  3989. Extensions()
  3990. : options(nullptr),
  3991. state(nullptr),
  3992. worker(nullptr),
  3993. programs(nullptr),
  3994. uiidle(nullptr),
  3995. uiprograms(nullptr) {}
  3996. } fExt;
  3997. struct UI {
  3998. enum Type {
  3999. TYPE_NULL,
  4000. TYPE_EMBED,
  4001. TYPE_EXTERNAL,
  4002. TYPE_OSC
  4003. };
  4004. Type type;
  4005. LV2UI_Handle handle;
  4006. LV2UI_Widget widget;
  4007. const LV2UI_Descriptor* descriptor;
  4008. const LV2_RDF_UI* rdfDescriptor;
  4009. const char* title;
  4010. CarlaPluginUi* window;
  4011. UI()
  4012. : type(TYPE_NULL),
  4013. handle(nullptr),
  4014. widget(nullptr),
  4015. descriptor(nullptr),
  4016. rdfDescriptor(nullptr),
  4017. title(nullptr),
  4018. window(nullptr) {}
  4019. ~UI()
  4020. {
  4021. CARLA_ASSERT(handle == nullptr);
  4022. CARLA_ASSERT(widget == nullptr);
  4023. CARLA_ASSERT(descriptor == nullptr);
  4024. CARLA_ASSERT(rdfDescriptor == nullptr);
  4025. CARLA_ASSERT(title == nullptr);
  4026. CARLA_ASSERT(window == nullptr);
  4027. }
  4028. } fUi;
  4029. // -------------------------------------------------------------------
  4030. // Event Feature
  4031. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4032. {
  4033. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4034. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4035. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4036. return 0;
  4037. }
  4038. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4039. {
  4040. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4041. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4042. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4043. return 0;
  4044. }
  4045. // -------------------------------------------------------------------
  4046. // Logs Feature
  4047. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4048. {
  4049. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4050. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4051. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4052. #ifndef DEBUG
  4053. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4054. return 0;
  4055. #endif
  4056. va_list args;
  4057. va_start(args, fmt);
  4058. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4059. va_end(args);
  4060. return ret;
  4061. }
  4062. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4063. {
  4064. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4065. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4066. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4067. #ifndef DEBUG
  4068. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4069. return 0;
  4070. #endif
  4071. int ret = 0;
  4072. switch (type)
  4073. {
  4074. case CARLA_URI_MAP_ID_LOG_ERROR:
  4075. std::fprintf(stderr, "\x1b[31m");
  4076. ret = std::vfprintf(stderr, fmt, ap);
  4077. std::fprintf(stderr, "\x1b[0m");
  4078. break;
  4079. case CARLA_URI_MAP_ID_LOG_NOTE:
  4080. ret = std::vfprintf(stdout, fmt, ap);
  4081. break;
  4082. case CARLA_URI_MAP_ID_LOG_TRACE:
  4083. #ifdef DEBUG
  4084. std::fprintf(stdout, "\x1b[30;1m");
  4085. ret = std::vfprintf(stdout, fmt, ap);
  4086. std::fprintf(stdout, "\x1b[0m");
  4087. #endif
  4088. break;
  4089. case CARLA_URI_MAP_ID_LOG_WARNING:
  4090. ret = std::vfprintf(stderr, fmt, ap);
  4091. break;
  4092. default:
  4093. break;
  4094. }
  4095. return ret;
  4096. }
  4097. // -------------------------------------------------------------------
  4098. // Programs Feature
  4099. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4100. {
  4101. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4102. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4103. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4104. }
  4105. // -------------------------------------------------------------------
  4106. // Resize Port Feature
  4107. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4108. {
  4109. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4110. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4111. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  4112. }
  4113. // -------------------------------------------------------------------
  4114. // State Feature
  4115. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4116. {
  4117. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4118. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4119. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4120. QDir dir;
  4121. dir.mkpath(path);
  4122. return strdup(path);
  4123. }
  4124. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4125. {
  4126. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4127. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4128. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4129. QDir dir(absolute_path);
  4130. return strdup(dir.canonicalPath().toUtf8().constData());
  4131. }
  4132. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4133. {
  4134. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4135. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4136. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4137. QDir dir(abstract_path);
  4138. return strdup(dir.absolutePath().toUtf8().constData());
  4139. }
  4140. 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)
  4141. {
  4142. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4143. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4144. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4145. }
  4146. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4147. {
  4148. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4149. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4150. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4151. }
  4152. // -------------------------------------------------------------------
  4153. // URI-Map Feature
  4154. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4155. {
  4156. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4157. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4158. // unused
  4159. (void)map;
  4160. }
  4161. // -------------------------------------------------------------------
  4162. // URID Feature
  4163. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4164. {
  4165. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4166. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4167. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4168. // Atom types
  4169. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4170. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4171. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4172. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4173. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4174. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4175. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4176. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4177. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4178. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4179. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4180. return CARLA_URI_MAP_ID_ATOM_INT;
  4181. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4182. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4183. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4184. return CARLA_URI_MAP_ID_ATOM_LONG;
  4185. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4186. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4187. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4188. return CARLA_URI_MAP_ID_ATOM_PATH;
  4189. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4190. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4191. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4192. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4193. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4194. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4195. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4196. return CARLA_URI_MAP_ID_ATOM_STRING;
  4197. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4198. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4199. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4200. return CARLA_URI_MAP_ID_ATOM_URI;
  4201. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4202. return CARLA_URI_MAP_ID_ATOM_URID;
  4203. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4204. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4205. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4206. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4207. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4208. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4209. // BufSize types
  4210. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4211. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4212. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4213. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4214. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4215. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4216. // Log types
  4217. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4218. return CARLA_URI_MAP_ID_LOG_ERROR;
  4219. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4220. return CARLA_URI_MAP_ID_LOG_NOTE;
  4221. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4222. return CARLA_URI_MAP_ID_LOG_TRACE;
  4223. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4224. return CARLA_URI_MAP_ID_LOG_WARNING;
  4225. // Time types
  4226. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4227. return CARLA_URI_MAP_ID_TIME_POSITION;
  4228. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4229. return CARLA_URI_MAP_ID_TIME_BAR;
  4230. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4231. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4232. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4233. return CARLA_URI_MAP_ID_TIME_BEAT;
  4234. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4235. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4236. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4237. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4238. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4239. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4240. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4241. return CARLA_URI_MAP_ID_TIME_FRAME;
  4242. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4243. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4244. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4245. return CARLA_URI_MAP_ID_TIME_SPEED;
  4246. // Others
  4247. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4248. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4249. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4250. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4251. // Custom types
  4252. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4253. }
  4254. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4255. {
  4256. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4257. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4258. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4259. // Atom types
  4260. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4261. return LV2_ATOM__Blank;
  4262. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4263. return LV2_ATOM__Bool;
  4264. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4265. return LV2_ATOM__Chunk;
  4266. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4267. return LV2_ATOM__Double;
  4268. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4269. return LV2_ATOM__Float;
  4270. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4271. return LV2_ATOM__Int;
  4272. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4273. return LV2_ATOM__Literal;
  4274. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4275. return LV2_ATOM__Long;
  4276. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4277. return LV2_ATOM__Object;
  4278. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4279. return LV2_ATOM__Path;
  4280. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4281. return LV2_ATOM__Property;
  4282. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4283. return LV2_ATOM__Resource;
  4284. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4285. return LV2_ATOM__Sequence;
  4286. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4287. return LV2_ATOM__String;
  4288. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4289. return LV2_ATOM__Tuple;
  4290. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4291. return LV2_ATOM__URI;
  4292. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4293. return LV2_ATOM__URID;
  4294. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4295. return LV2_ATOM__Vector;
  4296. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  4297. return nullptr; // custom
  4298. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4299. return LV2_ATOM__atomTransfer;
  4300. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4301. return LV2_ATOM__eventTransfer;
  4302. // BufSize types
  4303. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4304. return LV2_BUF_SIZE__maxBlockLength;
  4305. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4306. return LV2_BUF_SIZE__minBlockLength;
  4307. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4308. return LV2_BUF_SIZE__sequenceSize;
  4309. // Log types
  4310. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4311. return LV2_LOG__Error;
  4312. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4313. return LV2_LOG__Note;
  4314. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4315. return LV2_LOG__Trace;
  4316. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4317. return LV2_LOG__Warning;
  4318. // Time types
  4319. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4320. return LV2_TIME__Position;
  4321. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4322. return LV2_TIME__bar;
  4323. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4324. return LV2_TIME__barBeat;
  4325. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4326. return LV2_TIME__beat;
  4327. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4328. return LV2_TIME__beatUnit;
  4329. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4330. return LV2_TIME__beatsPerBar;
  4331. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4332. return LV2_TIME__beatsPerMinute;
  4333. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4334. return LV2_TIME__frame;
  4335. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4336. return LV2_TIME__framesPerSecond;
  4337. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4338. return LV2_TIME__speed;
  4339. // Others
  4340. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4341. return LV2_MIDI__MidiEvent;
  4342. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4343. return LV2_PARAMETERS__sampleRate;
  4344. // Custom types
  4345. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4346. }
  4347. // -------------------------------------------------------------------
  4348. // Worker Feature
  4349. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4350. {
  4351. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4352. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4353. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4354. }
  4355. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4356. {
  4357. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4358. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4359. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4360. }
  4361. // -------------------------------------------------------------------
  4362. // External UI Feature
  4363. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4364. {
  4365. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4366. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4367. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4368. }
  4369. // -------------------------------------------------------------------
  4370. // UI Port-Map Feature
  4371. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4372. {
  4373. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4374. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4375. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4376. }
  4377. // -------------------------------------------------------------------
  4378. // UI Resize Feature
  4379. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4380. {
  4381. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4382. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4383. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4384. }
  4385. // -------------------------------------------------------------------
  4386. // UI Extension
  4387. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4388. {
  4389. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4390. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4391. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4392. }
  4393. // -------------------------------------------------------------------
  4394. // Lilv State
  4395. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4396. {
  4397. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4398. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4399. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4400. }
  4401. // -------------------------------------------------------------------
  4402. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4403. };
  4404. // -------------------------------------------------------------------------------------------------------------------
  4405. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4406. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4407. {
  4408. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4409. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4410. const int32_t portIndex = argv[0]->i;
  4411. const char* const atomBuf = (const char*)&argv[1]->s;
  4412. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4413. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  4414. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4415. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  4416. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4417. return 0;
  4418. }
  4419. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4420. {
  4421. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4422. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4423. const int32_t urid = argv[0]->i;
  4424. const char* const uri = (const char*)&argv[1]->s;
  4425. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4426. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4427. return 0;
  4428. }
  4429. #undef lv2PluginPtr
  4430. CARLA_BACKEND_END_NAMESPACE
  4431. #endif // WANT_LV2
  4432. // -------------------------------------------------------------------------------------------------------------------
  4433. CARLA_BACKEND_START_NAMESPACE
  4434. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4435. {
  4436. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4437. #ifdef WANT_LV2
  4438. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4439. if (! plugin->init(init.filename, init.name, init.label))
  4440. {
  4441. delete plugin;
  4442. return nullptr;
  4443. }
  4444. plugin->reload();
  4445. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4446. {
  4447. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4448. delete plugin;
  4449. return nullptr;
  4450. }
  4451. return plugin;
  4452. #else
  4453. init.engine->setLastError("LV2 support not available");
  4454. return nullptr;
  4455. #endif
  4456. }
  4457. CARLA_BACKEND_END_NAMESPACE
  4458. // -------------------------------------------------------------------------------------------------------------------