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.

3971 lines
142KB

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