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.

5166 lines
185KB

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