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.

4633 lines
164KB

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