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.

5068 lines
181KB

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