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.

4588 lines
162KB

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