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.

3348 lines
117KB

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