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.

4561 lines
161KB

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