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.

3011 lines
110KB

  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2014 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef WANT_LV2
  20. #include "CarlaLv2Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "Lv2AtomQueue.hpp"
  23. #include "../engine/CarlaEngineOsc.hpp"
  24. extern "C" {
  25. #include "rtmempool/rtmempool-lv2.h"
  26. }
  27. #include <QtCore/QDir>
  28. #include <QtCore/QUrl>
  29. // -----------------------------------------------------
  30. CARLA_BACKEND_START_NAMESPACE
  31. #if 0
  32. }
  33. #endif
  34. // Maximum default buffer size
  35. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  36. // Extra Plugin Hints
  37. const unsigned int PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  38. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  39. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  40. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  41. // Extra Parameter Hints
  42. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  43. const unsigned int PARAMETER_IS_TRIGGER = 0x2000;
  44. // LV2 Event Data/Types
  45. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  46. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  47. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  48. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  49. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  50. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  51. // LV2 URI Map Ids
  52. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  53. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  56. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  57. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 5;
  58. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 6;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 7;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 8;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 9;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 10;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 11;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 12;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 13;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 14;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 15;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 16;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 17;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 18;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 19;
  72. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 20;
  73. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 21;
  74. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 22;
  75. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 23;
  76. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 24;
  77. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 25;
  78. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 26;
  79. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 27; // base type
  80. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 28; // values
  81. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 29;
  82. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 30;
  83. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 31;
  84. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 32;
  85. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 33;
  86. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 34;
  87. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 35;
  88. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 36;
  89. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 37;
  90. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 38;
  91. const uint32_t CARLA_URI_MAP_ID_COUNT = 39;
  92. // LV2 Feature Ids
  93. const uint32_t kFeatureIdBufSizeBounded = 0;
  94. const uint32_t kFeatureIdBufSizeFixed = 1;
  95. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  96. const uint32_t kFeatureIdEvent = 3;
  97. const uint32_t kFeatureIdHardRtCapable = 4;
  98. const uint32_t kFeatureIdInPlaceBroken = 5;
  99. const uint32_t kFeatureIdIsLive = 6;
  100. const uint32_t kFeatureIdLogs = 7;
  101. const uint32_t kFeatureIdOptions = 8;
  102. const uint32_t kFeatureIdPrograms = 9;
  103. const uint32_t kFeatureIdRtMemPool = 10;
  104. const uint32_t kFeatureIdStateMakePath = 11;
  105. const uint32_t kFeatureIdStateMapPath = 12;
  106. const uint32_t kFeatureIdStrictBounds = 13;
  107. const uint32_t kFeatureIdUriMap = 14;
  108. const uint32_t kFeatureIdUridMap = 15;
  109. const uint32_t kFeatureIdUridUnmap = 16;
  110. const uint32_t kFeatureIdWorker = 17;
  111. const uint32_t kFeatureCountPlugin = 18;
  112. const uint32_t kFeatureIdUiDataAccess = 18;
  113. const uint32_t kFeatureIdUiInstanceAccess = 19;
  114. const uint32_t kFeatureIdUiIdle = 20;
  115. const uint32_t kFeatureIdUiFixedSize = 21;
  116. const uint32_t kFeatureIdUiMakeResident = 22;
  117. const uint32_t kFeatureIdUiNoUserResize = 23;
  118. const uint32_t kFeatureIdUiParent = 24;
  119. const uint32_t kFeatureIdUiPortMap = 25;
  120. const uint32_t kFeatureIdUiPortSubscribe = 26;
  121. const uint32_t kFeatureIdUiResize = 27;
  122. const uint32_t kFeatureIdUiTouch = 28;
  123. const uint32_t kFeatureIdExternalUi = 29;
  124. const uint32_t kFeatureIdExternalUiOld = 30;
  125. const uint32_t kFeatureCountAll = 31;
  126. // -----------------------------------------------------
  127. struct Lv2PluginOptions {
  128. enum OptIndex {
  129. MaxBlockLenth = 0,
  130. MinBlockLenth,
  131. SequenceSize,
  132. SampleRate,
  133. Null
  134. };
  135. int maxBufferSize;
  136. int minBufferSize;
  137. int sequenceSize;
  138. double sampleRate;
  139. LV2_Options_Option opts[5];
  140. Lv2PluginOptions()
  141. : maxBufferSize(0),
  142. minBufferSize(0),
  143. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  144. sampleRate(0.0)
  145. {
  146. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  147. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  148. optMaxBlockLenth.subject = 0;
  149. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  150. optMaxBlockLenth.size = sizeof(int);
  151. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  152. optMaxBlockLenth.value = &maxBufferSize;
  153. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  154. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  155. optMinBlockLenth.subject = 0;
  156. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  157. optMinBlockLenth.size = sizeof(int);
  158. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  159. optMinBlockLenth.value = &minBufferSize;
  160. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  161. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  162. optSequenceSize.subject = 0;
  163. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  164. optSequenceSize.size = sizeof(int);
  165. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  166. optSequenceSize.value = &sequenceSize;
  167. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  168. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  169. optSampleRate.subject = 0;
  170. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  171. optSampleRate.size = sizeof(double);
  172. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  173. optSampleRate.value = &sampleRate;
  174. LV2_Options_Option& optNull(opts[Null]);
  175. optNull.context = LV2_OPTIONS_INSTANCE;
  176. optNull.subject = 0;
  177. optNull.key = CARLA_URI_MAP_ID_NULL;
  178. optNull.size = 0;
  179. optNull.type = CARLA_URI_MAP_ID_NULL;
  180. optNull.value = nullptr;
  181. }
  182. };
  183. // -----------------------------------------------------
  184. class Lv2Plugin : public CarlaPlugin
  185. {
  186. public:
  187. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  188. : CarlaPlugin(engine, id),
  189. fHandle(nullptr),
  190. fHandle2(nullptr),
  191. fDescriptor(nullptr),
  192. fRdfDescriptor(nullptr),
  193. fAudioInBuffers(nullptr),
  194. fAudioOutBuffers(nullptr),
  195. fParamBuffers(nullptr),
  196. fFirstActive(true)
  197. {
  198. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  199. carla_fill<LV2_Feature*>(fFeatures, kFeatureCountAll+1, nullptr);
  200. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  201. fCustomURIDs.append(nullptr);
  202. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  203. }
  204. ~Lv2Plugin() override
  205. {
  206. carla_debug("Lv2Plugin::~Lv2Plugin()");
  207. pData->singleMutex.lock();
  208. pData->masterMutex.lock();
  209. if (pData->client != nullptr && pData->client->isActive())
  210. pData->client->deactivate();
  211. if (pData->active)
  212. {
  213. deactivate();
  214. pData->active = false;
  215. }
  216. if (fDescriptor != nullptr)
  217. {
  218. if (fDescriptor->cleanup != nullptr)
  219. {
  220. if (fHandle != nullptr)
  221. fDescriptor->cleanup(fHandle);
  222. if (fHandle2 != nullptr)
  223. fDescriptor->cleanup(fHandle2);
  224. }
  225. fHandle = nullptr;
  226. fHandle2 = nullptr;
  227. fDescriptor = nullptr;
  228. }
  229. if (fRdfDescriptor != nullptr)
  230. {
  231. delete fRdfDescriptor;
  232. fRdfDescriptor = nullptr;
  233. }
  234. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  235. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  236. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  237. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  238. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  239. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  240. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  241. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  242. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  243. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  244. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  245. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  246. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  247. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  248. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  249. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  250. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  251. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  252. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  253. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  254. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  255. {
  256. if (fFeatures[i] != nullptr)
  257. {
  258. delete fFeatures[i];
  259. fFeatures[i] = nullptr;
  260. }
  261. }
  262. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  263. {
  264. const char*& uri(it.getValue());
  265. if (uri != nullptr)
  266. {
  267. delete[] uri;
  268. uri = nullptr;
  269. }
  270. }
  271. fCustomURIDs.clear();
  272. clearBuffers();
  273. }
  274. // -------------------------------------------------------------------
  275. // Information (base)
  276. PluginType getType() const noexcept override
  277. {
  278. return PLUGIN_LV2;
  279. }
  280. PluginCategory getCategory() const noexcept override
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  283. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  284. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  285. if (LV2_IS_DELAY(cat1, cat2))
  286. return PLUGIN_CATEGORY_DELAY;
  287. if (LV2_IS_DISTORTION(cat1, cat2))
  288. return PLUGIN_CATEGORY_OTHER;
  289. if (LV2_IS_DYNAMICS(cat1, cat2))
  290. return PLUGIN_CATEGORY_DYNAMICS;
  291. if (LV2_IS_EQ(cat1, cat2))
  292. return PLUGIN_CATEGORY_EQ;
  293. if (LV2_IS_FILTER(cat1, cat2))
  294. return PLUGIN_CATEGORY_FILTER;
  295. if (LV2_IS_GENERATOR(cat1, cat2))
  296. return PLUGIN_CATEGORY_SYNTH;
  297. if (LV2_IS_MODULATOR(cat1, cat2))
  298. return PLUGIN_CATEGORY_MODULATOR;
  299. if (LV2_IS_REVERB(cat1, cat2))
  300. return PLUGIN_CATEGORY_DELAY;
  301. if (LV2_IS_SIMULATOR(cat1, cat2))
  302. return PLUGIN_CATEGORY_OTHER;
  303. if (LV2_IS_SPATIAL(cat1, cat2))
  304. return PLUGIN_CATEGORY_OTHER;
  305. if (LV2_IS_SPECTRAL(cat1, cat2))
  306. return PLUGIN_CATEGORY_UTILITY;
  307. if (LV2_IS_UTILITY(cat1, cat2))
  308. return PLUGIN_CATEGORY_UTILITY;
  309. return CarlaPlugin::getCategory();
  310. }
  311. long getUniqueId() const noexcept override
  312. {
  313. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  314. return static_cast<long>(fRdfDescriptor->UniqueID);
  315. }
  316. // -------------------------------------------------------------------
  317. // Information (count)
  318. uint32_t getMidiInCount() const noexcept override
  319. {
  320. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  321. uint32_t count = 0;
  322. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  323. {
  324. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  325. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  326. count += 1;
  327. }
  328. return count;
  329. }
  330. uint32_t getMidiOutCount() const noexcept override
  331. {
  332. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  333. uint32_t count = 0;
  334. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  335. {
  336. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  337. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  338. count += 1;
  339. }
  340. return count;
  341. }
  342. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  343. {
  344. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  345. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  346. const int32_t rindex(pData->param.data[parameterId].rindex);
  347. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  348. {
  349. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  350. return port->ScalePointCount;
  351. }
  352. return 0;
  353. }
  354. // -------------------------------------------------------------------
  355. // Information (current data)
  356. // nothing
  357. // -------------------------------------------------------------------
  358. // Information (per-plugin data)
  359. unsigned int getOptionsAvailable() const noexcept override
  360. {
  361. const bool hasMidiIn(getMidiInCount() > 0);
  362. unsigned int options = 0x0;
  363. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  364. if (! (hasMidiIn || needsFixedBuffer()))
  365. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  366. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  367. {
  368. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  369. options |= PLUGIN_OPTION_FORCE_STEREO;
  370. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  371. options |= PLUGIN_OPTION_FORCE_STEREO;
  372. }
  373. if (hasMidiIn)
  374. {
  375. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  376. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  377. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  378. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  379. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  380. }
  381. return options;
  382. }
  383. float getParameterValue(const uint32_t parameterId) const noexcept override
  384. {
  385. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  386. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  387. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  388. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  389. return fParamBuffers[parameterId];
  390. }
  391. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  392. {
  393. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  394. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  395. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  396. const int32_t rindex(pData->param.data[parameterId].rindex);
  397. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  398. {
  399. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  400. if (scalePointId < port->ScalePointCount)
  401. {
  402. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  403. return portScalePoint->Value;
  404. }
  405. }
  406. return 0.0f;
  407. }
  408. void getLabel(char* const strBuf) const noexcept override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  411. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  412. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  413. }
  414. void getMaker(char* const strBuf) const noexcept override
  415. {
  416. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  417. if (fRdfDescriptor->Author != nullptr)
  418. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  419. else
  420. CarlaPlugin::getMaker(strBuf);
  421. }
  422. void getCopyright(char* const strBuf) const noexcept override
  423. {
  424. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  425. if (fRdfDescriptor->License != nullptr)
  426. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  427. else
  428. CarlaPlugin::getCopyright(strBuf);
  429. }
  430. void getRealName(char* const strBuf) const noexcept override
  431. {
  432. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  433. if (fRdfDescriptor->Name != nullptr)
  434. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  435. else
  436. CarlaPlugin::getRealName(strBuf);
  437. }
  438. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  439. {
  440. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  441. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  442. const int32_t rindex(pData->param.data[parameterId].rindex);
  443. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  444. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  445. else
  446. CarlaPlugin::getParameterName(parameterId, strBuf);
  447. }
  448. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  449. {
  450. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  451. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  452. const int32_t rindex(pData->param.data[parameterId].rindex);
  453. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  454. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  455. else
  456. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  457. }
  458. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  459. {
  460. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  461. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  462. const int32_t rindex(pData->param.data[parameterId].rindex);
  463. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  464. {
  465. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  466. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  467. {
  468. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  469. return;
  470. }
  471. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  472. {
  473. switch (port->Unit.Unit)
  474. {
  475. case LV2_PORT_UNIT_BAR:
  476. std::strncpy(strBuf, "bars", STR_MAX);
  477. return;
  478. case LV2_PORT_UNIT_BEAT:
  479. std::strncpy(strBuf, "beats", STR_MAX);
  480. return;
  481. case LV2_PORT_UNIT_BPM:
  482. std::strncpy(strBuf, "BPM", STR_MAX);
  483. return;
  484. case LV2_PORT_UNIT_CENT:
  485. std::strncpy(strBuf, "ct", STR_MAX);
  486. return;
  487. case LV2_PORT_UNIT_CM:
  488. std::strncpy(strBuf, "cm", STR_MAX);
  489. return;
  490. case LV2_PORT_UNIT_COEF:
  491. std::strncpy(strBuf, "(coef)", STR_MAX);
  492. return;
  493. case LV2_PORT_UNIT_DB:
  494. std::strncpy(strBuf, "dB", STR_MAX);
  495. return;
  496. case LV2_PORT_UNIT_DEGREE:
  497. std::strncpy(strBuf, "deg", STR_MAX);
  498. return;
  499. case LV2_PORT_UNIT_FRAME:
  500. std::strncpy(strBuf, "frames", STR_MAX);
  501. return;
  502. case LV2_PORT_UNIT_HZ:
  503. std::strncpy(strBuf, "Hz", STR_MAX);
  504. return;
  505. case LV2_PORT_UNIT_INCH:
  506. std::strncpy(strBuf, "in", STR_MAX);
  507. return;
  508. case LV2_PORT_UNIT_KHZ:
  509. std::strncpy(strBuf, "kHz", STR_MAX);
  510. return;
  511. case LV2_PORT_UNIT_KM:
  512. std::strncpy(strBuf, "km", STR_MAX);
  513. return;
  514. case LV2_PORT_UNIT_M:
  515. std::strncpy(strBuf, "m", STR_MAX);
  516. return;
  517. case LV2_PORT_UNIT_MHZ:
  518. std::strncpy(strBuf, "MHz", STR_MAX);
  519. return;
  520. case LV2_PORT_UNIT_MIDINOTE:
  521. std::strncpy(strBuf, "note", STR_MAX);
  522. return;
  523. case LV2_PORT_UNIT_MILE:
  524. std::strncpy(strBuf, "mi", STR_MAX);
  525. return;
  526. case LV2_PORT_UNIT_MIN:
  527. std::strncpy(strBuf, "min", STR_MAX);
  528. return;
  529. case LV2_PORT_UNIT_MM:
  530. std::strncpy(strBuf, "mm", STR_MAX);
  531. return;
  532. case LV2_PORT_UNIT_MS:
  533. std::strncpy(strBuf, "ms", STR_MAX);
  534. return;
  535. case LV2_PORT_UNIT_OCT:
  536. std::strncpy(strBuf, "oct", STR_MAX);
  537. return;
  538. case LV2_PORT_UNIT_PC:
  539. std::strncpy(strBuf, "%", STR_MAX);
  540. return;
  541. case LV2_PORT_UNIT_S:
  542. std::strncpy(strBuf, "s", STR_MAX);
  543. return;
  544. case LV2_PORT_UNIT_SEMITONE:
  545. std::strncpy(strBuf, "semi", STR_MAX);
  546. return;
  547. }
  548. }
  549. }
  550. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  551. }
  552. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  553. {
  554. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  555. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  556. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  557. const int32_t rindex(pData->param.data[parameterId].rindex);
  558. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  559. {
  560. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  561. if (scalePointId < port->ScalePointCount)
  562. {
  563. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  564. if (portScalePoint->Label != nullptr)
  565. {
  566. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  567. return;
  568. }
  569. }
  570. }
  571. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  572. }
  573. // -------------------------------------------------------------------
  574. // Set data (state)
  575. void prepareForSave() override
  576. {
  577. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  578. if (fExt.state != nullptr && fExt.state->save != nullptr)
  579. {
  580. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  581. if (fHandle2 != nullptr)
  582. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  583. }
  584. }
  585. // -------------------------------------------------------------------
  586. // Set data (internal stuff)
  587. void setName(const char* const newName) override
  588. {
  589. CarlaPlugin::setName(newName);
  590. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  591. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  592. {
  593. LV2_External_UI_Host* const uiHost((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data);
  594. if (uiHost->plugin_human_id != nullptr)
  595. delete[] uiHost->plugin_human_id;
  596. uiHost->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  597. }
  598. }
  599. // -------------------------------------------------------------------
  600. // Set data (plugin-specific stuff)
  601. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  602. {
  603. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  604. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  605. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  606. fParamBuffers[parameterId] = fixedValue;
  607. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  608. }
  609. // -------------------------------------------------------------------
  610. // Set ui stuff
  611. // nothing
  612. // -------------------------------------------------------------------
  613. // Plugin state
  614. void reload() override
  615. {
  616. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  617. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  618. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  619. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  620. carla_debug("Lv2Plugin::reload() - start");
  621. const EngineProcessMode processMode(pData->engine->getProccessMode());
  622. // Safely disable plugin for reload
  623. const ScopedDisabler sd(this);
  624. if (pData->active)
  625. deactivate();
  626. clearBuffers();
  627. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  628. const uint32_t portCount(fRdfDescriptor->PortCount);
  629. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  630. aIns = aOuts = cvIns = cvOuts = params = 0;
  631. LinkedList<unsigned int> evIns, evOuts;
  632. bool forcedStereoIn, forcedStereoOut;
  633. forcedStereoIn = forcedStereoOut = false;
  634. bool needsCtrlIn, needsCtrlOut;
  635. needsCtrlIn = needsCtrlOut = false;
  636. for (uint32_t i=0; i < portCount; ++i)
  637. {
  638. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  639. if (LV2_IS_PORT_AUDIO(portTypes))
  640. {
  641. if (LV2_IS_PORT_INPUT(portTypes))
  642. aIns += 1;
  643. else if (LV2_IS_PORT_OUTPUT(portTypes))
  644. aOuts += 1;
  645. }
  646. else if (LV2_IS_PORT_CV(portTypes))
  647. {
  648. if (LV2_IS_PORT_INPUT(portTypes))
  649. cvIns += 1;
  650. else if (LV2_IS_PORT_OUTPUT(portTypes))
  651. cvOuts += 1;
  652. }
  653. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  654. {
  655. if (LV2_IS_PORT_INPUT(portTypes))
  656. evIns.append(CARLA_EVENT_DATA_ATOM);
  657. else if (LV2_IS_PORT_OUTPUT(portTypes))
  658. evOuts.append(CARLA_EVENT_DATA_ATOM);
  659. }
  660. else if (LV2_IS_PORT_EVENT(portTypes))
  661. {
  662. if (LV2_IS_PORT_INPUT(portTypes))
  663. evIns.append(CARLA_EVENT_DATA_EVENT);
  664. else if (LV2_IS_PORT_OUTPUT(portTypes))
  665. evOuts.append(CARLA_EVENT_DATA_EVENT);
  666. }
  667. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  668. {
  669. if (LV2_IS_PORT_INPUT(portTypes))
  670. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  671. else if (LV2_IS_PORT_OUTPUT(portTypes))
  672. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  673. }
  674. else if (LV2_IS_PORT_CONTROL(portTypes))
  675. params += 1;
  676. }
  677. recheckExtensions();
  678. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  679. {
  680. if (fHandle2 == nullptr)
  681. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  682. if (fHandle2 != nullptr)
  683. {
  684. if (aIns == 1)
  685. {
  686. aIns = 2;
  687. forcedStereoIn = true;
  688. }
  689. if (aOuts == 1)
  690. {
  691. aOuts = 2;
  692. forcedStereoOut = true;
  693. }
  694. }
  695. }
  696. if (aIns > 0)
  697. {
  698. pData->audioIn.createNew(aIns);
  699. fAudioInBuffers = new float*[aIns];
  700. for (uint32_t i=0; i < aIns; ++i)
  701. fAudioInBuffers[i] = nullptr;
  702. }
  703. if (aOuts > 0)
  704. {
  705. pData->audioOut.createNew(aOuts);
  706. fAudioOutBuffers = new float*[aOuts];
  707. needsCtrlIn = true;
  708. for (uint32_t i=0; i < aOuts; ++i)
  709. fAudioOutBuffers[i] = nullptr;
  710. }
  711. if (params > 0)
  712. {
  713. pData->param.createNew(params, true);
  714. fParamBuffers = new float[params];
  715. FLOAT_CLEAR(fParamBuffers, params);
  716. }
  717. const uint portNameSize(pData->engine->getMaxPortNameSize());
  718. CarlaString portName;
  719. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  720. {
  721. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  722. portName.clear();
  723. 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))
  724. {
  725. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  726. {
  727. portName = pData->name;
  728. portName += ":";
  729. }
  730. portName += fRdfDescriptor->Ports[i].Name;
  731. portName.truncate(portNameSize);
  732. }
  733. if (LV2_IS_PORT_AUDIO(portTypes))
  734. {
  735. if (LV2_IS_PORT_INPUT(portTypes))
  736. {
  737. uint32_t j = iAudioIn++;
  738. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  739. pData->audioIn.ports[j].rindex = i;
  740. if (forcedStereoIn)
  741. {
  742. portName += "_2";
  743. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  744. pData->audioIn.ports[1].rindex = i;
  745. }
  746. }
  747. else if (LV2_IS_PORT_OUTPUT(portTypes))
  748. {
  749. uint32_t j = iAudioOut++;
  750. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  751. pData->audioOut.ports[j].rindex = i;
  752. if (forcedStereoOut)
  753. {
  754. portName += "_2";
  755. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  756. pData->audioOut.ports[1].rindex = i;
  757. }
  758. }
  759. else
  760. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  761. }
  762. else if (LV2_IS_PORT_CONTROL(portTypes))
  763. {
  764. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  765. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  766. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  767. uint32_t j = iCtrl++;
  768. pData->param.data[j].hints = 0x0;
  769. pData->param.data[j].index = static_cast<int32_t>(j);
  770. pData->param.data[j].rindex = static_cast<int32_t>(i);
  771. pData->param.data[j].midiCC = -1;
  772. pData->param.data[j].midiChannel = 0;
  773. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  774. float min, max, def, step, stepSmall, stepLarge;
  775. // min value
  776. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  777. min = portPoints.Minimum;
  778. else
  779. min = 0.0f;
  780. // max value
  781. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  782. max = portPoints.Maximum;
  783. else
  784. max = 1.0f;
  785. if (min > max)
  786. max = min;
  787. // stupid hack for ir.lv2 (broken plugin)
  788. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  789. {
  790. min = 0.0f;
  791. max = (float)0xffffff;
  792. }
  793. if (max - min == 0.0f)
  794. {
  795. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  796. max = min + 0.1f;
  797. }
  798. // default value
  799. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  800. {
  801. def = portPoints.Default;
  802. }
  803. else
  804. {
  805. // no default value
  806. if (min < 0.0f && max > 0.0f)
  807. def = 0.0f;
  808. else
  809. def = min;
  810. }
  811. if (def < min)
  812. def = min;
  813. else if (def > max)
  814. def = max;
  815. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  816. {
  817. min *= sampleRate;
  818. max *= sampleRate;
  819. def *= sampleRate;
  820. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  821. }
  822. if (LV2_IS_PORT_TOGGLED(portProps))
  823. {
  824. step = max - min;
  825. stepSmall = step;
  826. stepLarge = step;
  827. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  828. }
  829. else if (LV2_IS_PORT_INTEGER(portProps))
  830. {
  831. step = 1.0f;
  832. stepSmall = 1.0f;
  833. stepLarge = 10.0f;
  834. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  835. }
  836. else
  837. {
  838. float range = max - min;
  839. step = range/100.0f;
  840. stepSmall = range/1000.0f;
  841. stepLarge = range/10.0f;
  842. }
  843. if (LV2_IS_PORT_INPUT(portTypes))
  844. {
  845. pData->param.data[j].type = PARAMETER_INPUT;
  846. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  847. {
  848. carla_stderr("Plugin has latency input port, this should not happen!");
  849. }
  850. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  851. {
  852. def = sampleRate;
  853. step = 1.0f;
  854. stepSmall = 1.0f;
  855. stepLarge = 1.0f;
  856. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  857. }
  858. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  859. {
  860. pData->param.special[j] = PARAMETER_SPECIAL_LV2_FREEWHEEL;
  861. }
  862. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  863. {
  864. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  865. }
  866. else
  867. {
  868. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  869. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  870. needsCtrlIn = true;
  871. }
  872. // MIDI CC value
  873. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  874. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  875. {
  876. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  877. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  878. }
  879. }
  880. else if (LV2_IS_PORT_OUTPUT(portTypes))
  881. {
  882. pData->param.data[j].type = PARAMETER_OUTPUT;
  883. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  884. {
  885. min = 0.0f;
  886. max = sampleRate;
  887. def = 0.0f;
  888. step = 1.0f;
  889. stepSmall = 1.0f;
  890. stepLarge = 1.0f;
  891. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  892. }
  893. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  894. {
  895. def = sampleRate;
  896. step = 1.0f;
  897. stepSmall = 1.0f;
  898. stepLarge = 1.0f;
  899. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  900. }
  901. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  902. {
  903. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  904. }
  905. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  906. {
  907. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  908. }
  909. else
  910. {
  911. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  912. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  913. needsCtrlOut = true;
  914. }
  915. }
  916. else
  917. {
  918. pData->param.data[j].type = PARAMETER_UNKNOWN;
  919. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  920. }
  921. // extra parameter hints
  922. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  923. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  924. if (LV2_IS_PORT_TRIGGER(portProps))
  925. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  926. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  927. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  928. if (LV2_IS_PORT_ENUMERATION(portProps))
  929. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  930. // check if parameter is not enabled or automable
  931. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  932. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  933. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  934. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  935. pData->param.ranges[j].min = min;
  936. pData->param.ranges[j].max = max;
  937. pData->param.ranges[j].def = def;
  938. pData->param.ranges[j].step = step;
  939. pData->param.ranges[j].stepSmall = stepSmall;
  940. pData->param.ranges[j].stepLarge = stepLarge;
  941. // Start parameters in their default values (except freewheel, which is off by default)
  942. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  943. fParamBuffers[j] = min;
  944. else
  945. fParamBuffers[j] = def;
  946. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  947. if (fHandle2 != nullptr)
  948. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  949. }
  950. else
  951. {
  952. // Port Type not supported, but it's optional anyway
  953. fDescriptor->connect_port(fHandle, i, nullptr);
  954. if (fHandle2 != nullptr)
  955. fDescriptor->connect_port(fHandle2, i, nullptr);
  956. }
  957. }
  958. if (needsCtrlIn)
  959. {
  960. portName.clear();
  961. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  962. {
  963. portName = pData->name;
  964. portName += ":";
  965. }
  966. portName += "events-in";
  967. portName.truncate(portNameSize);
  968. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  969. }
  970. if (needsCtrlOut)
  971. {
  972. portName.clear();
  973. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  974. {
  975. portName = pData->name;
  976. portName += ":";
  977. }
  978. portName += "events-out";
  979. portName.truncate(portNameSize);
  980. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  981. }
  982. if (forcedStereoIn || forcedStereoOut)
  983. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  984. else
  985. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  986. // plugin hints
  987. pData->hints = 0x0;
  988. if (isRealtimeSafe())
  989. pData->hints |= PLUGIN_IS_RTSAFE;
  990. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  991. pData->hints |= PLUGIN_IS_SYNTH;
  992. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  993. pData->hints |= PLUGIN_CAN_DRYWET;
  994. if (aOuts > 0)
  995. pData->hints |= PLUGIN_CAN_VOLUME;
  996. if (aOuts >= 2 && aOuts % 2 == 0)
  997. pData->hints |= PLUGIN_CAN_BALANCE;
  998. // extra plugin hints
  999. pData->extraHints = 0x0;
  1000. if (fExt.state != nullptr || fExt.worker != nullptr)
  1001. {
  1002. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1003. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1004. }
  1005. else
  1006. {
  1007. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1008. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1009. }
  1010. bufferSizeChanged(pData->engine->getBufferSize());
  1011. reloadPrograms(true);
  1012. // check latency
  1013. // TODO
  1014. evIns.clear();
  1015. evOuts.clear();
  1016. if (pData->active)
  1017. activate();
  1018. carla_debug("Lv2Plugin::reload() - end");
  1019. }
  1020. // -------------------------------------------------------------------
  1021. // Plugin processing
  1022. void activate() noexcept override
  1023. {
  1024. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1025. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1026. if (fDescriptor->activate != nullptr)
  1027. {
  1028. try {
  1029. fDescriptor->activate(fHandle);
  1030. } catch(...) {}
  1031. if (fHandle2 != nullptr)
  1032. {
  1033. try {
  1034. fDescriptor->activate(fHandle2);
  1035. } catch(...) {}
  1036. }
  1037. }
  1038. fFirstActive = true;
  1039. }
  1040. void deactivate() noexcept override
  1041. {
  1042. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1043. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1044. if (fDescriptor->deactivate != nullptr)
  1045. {
  1046. try {
  1047. fDescriptor->deactivate(fHandle);
  1048. } catch(...) {}
  1049. if (fHandle2 != nullptr)
  1050. {
  1051. try {
  1052. fDescriptor->deactivate(fHandle2);
  1053. } catch(...) {}
  1054. }
  1055. }
  1056. }
  1057. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1058. {
  1059. // --------------------------------------------------------------------------------------------------------
  1060. // Check if active
  1061. if (! pData->active)
  1062. {
  1063. // disable any output sound
  1064. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1065. FLOAT_CLEAR(outBuffer[i], frames);
  1066. return;
  1067. }
  1068. // --------------------------------------------------------------------------------------------------------
  1069. // Check if needs reset
  1070. if (pData->needsReset)
  1071. {
  1072. if (pData->latency > 0)
  1073. {
  1074. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1075. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  1076. }
  1077. pData->needsReset = false;
  1078. }
  1079. // --------------------------------------------------------------------------------------------------------
  1080. // TimeInfo
  1081. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1082. if (fFirstActive || fLastTimeInfo != timeInfo)
  1083. {
  1084. bool doPostRt;
  1085. int32_t rindex;
  1086. // update input ports
  1087. for (uint32_t k=0; k < pData->param.count; ++k)
  1088. {
  1089. if (pData->param.data[k].type != PARAMETER_INPUT)
  1090. continue;
  1091. if (pData->param.special[k] != PARAMETER_SPECIAL_LV2_TIME)
  1092. continue;
  1093. doPostRt = false;
  1094. rindex = pData->param.data[k].rindex;
  1095. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  1096. switch (fRdfDescriptor->Ports[rindex].Designation)
  1097. {
  1098. // Non-BBT
  1099. case LV2_PORT_DESIGNATION_TIME_SPEED:
  1100. if (fLastTimeInfo.playing != timeInfo.playing)
  1101. {
  1102. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  1103. doPostRt = true;
  1104. }
  1105. break;
  1106. case LV2_PORT_DESIGNATION_TIME_FRAME:
  1107. if (fLastTimeInfo.frame != timeInfo.frame)
  1108. {
  1109. fParamBuffers[k] = float(timeInfo.frame);
  1110. doPostRt = true;
  1111. }
  1112. break;
  1113. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  1114. break;
  1115. // BBT
  1116. case LV2_PORT_DESIGNATION_TIME_BAR:
  1117. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  1118. {
  1119. fParamBuffers[k] = float(timeInfo.bbt.bar - 1);
  1120. doPostRt = true;
  1121. }
  1122. break;
  1123. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  1124. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  1125. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  1126. {
  1127. fParamBuffers[k] = float(double(timeInfo.bbt.beat) - 1.0 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  1128. doPostRt = true;
  1129. }
  1130. break;
  1131. case LV2_PORT_DESIGNATION_TIME_BEAT:
  1132. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  1133. {
  1134. fParamBuffers[k] = float(timeInfo.bbt.beat - 1);
  1135. doPostRt = true;
  1136. }
  1137. break;
  1138. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  1139. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  1140. {
  1141. fParamBuffers[k] = timeInfo.bbt.beatType;
  1142. doPostRt = true;
  1143. }
  1144. break;
  1145. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  1146. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  1147. {
  1148. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  1149. doPostRt = true;
  1150. }
  1151. break;
  1152. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  1153. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  1154. {
  1155. fParamBuffers[k] = float(timeInfo.bbt.beatsPerMinute);
  1156. doPostRt = true;
  1157. }
  1158. break;
  1159. }
  1160. if (doPostRt)
  1161. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  1162. }
  1163. pData->postRtEvents.trySplice();
  1164. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  1165. CARLA_PROCESS_CONTINUE_CHECK;
  1166. }
  1167. // --------------------------------------------------------------------------------------------------------
  1168. // Plugin processing (no events)
  1169. {
  1170. processSingle(inBuffer, outBuffer, frames, 0);
  1171. } // End of Plugin processing (no events)
  1172. CARLA_PROCESS_CONTINUE_CHECK;
  1173. // --------------------------------------------------------------------------------------------------------
  1174. // Control Output
  1175. if (pData->event.portOut != nullptr)
  1176. {
  1177. uint8_t channel;
  1178. uint16_t param;
  1179. float value;
  1180. for (uint32_t k=0; k < pData->param.count; ++k)
  1181. {
  1182. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1183. continue;
  1184. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1185. if (pData->param.data[k].midiCC > 0)
  1186. {
  1187. channel = pData->param.data[k].midiChannel;
  1188. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1189. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1190. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1191. }
  1192. }
  1193. } // End of Control Output
  1194. CARLA_PROCESS_CONTINUE_CHECK;
  1195. // --------------------------------------------------------------------------------------------------------
  1196. // Final work
  1197. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  1198. {
  1199. fExt.worker->end_run(fHandle);
  1200. if (fHandle2 != nullptr)
  1201. fExt.worker->end_run(fHandle2);
  1202. }
  1203. fFirstActive = false;
  1204. // --------------------------------------------------------------------------------------------------------
  1205. }
  1206. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1207. {
  1208. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1209. if (pData->audioIn.count > 0)
  1210. {
  1211. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  1212. }
  1213. if (pData->audioOut.count > 0)
  1214. {
  1215. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  1216. }
  1217. // --------------------------------------------------------------------------------------------------------
  1218. // Try lock, silence otherwise
  1219. if (pData->engine->isOffline())
  1220. {
  1221. pData->singleMutex.lock();
  1222. }
  1223. else if (! pData->singleMutex.tryLock())
  1224. {
  1225. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1226. {
  1227. for (uint32_t k=0; k < frames; ++k)
  1228. outBuffer[i][k+timeOffset] = 0.0f;
  1229. }
  1230. return false;
  1231. }
  1232. // --------------------------------------------------------------------------------------------------------
  1233. // Reset audio buffers
  1234. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1235. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1236. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1237. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1238. // --------------------------------------------------------------------------------------------------------
  1239. // Run plugin
  1240. fDescriptor->run(fHandle, frames);
  1241. if (fHandle2 != nullptr)
  1242. fDescriptor->run(fHandle2, frames);
  1243. // --------------------------------------------------------------------------------------------------------
  1244. // Handle trigger parameters
  1245. for (uint32_t k=0; k < pData->param.count; ++k)
  1246. {
  1247. if (pData->param.data[k].type != PARAMETER_INPUT)
  1248. continue;
  1249. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  1250. {
  1251. if (fParamBuffers[k] != pData->param.ranges[k].def)
  1252. {
  1253. fParamBuffers[k] = pData->param.ranges[k].def;
  1254. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  1255. }
  1256. }
  1257. }
  1258. pData->postRtEvents.trySplice();
  1259. #ifndef BUILD_BRIDGE
  1260. // --------------------------------------------------------------------------------------------------------
  1261. // Post-processing (dry/wet, volume and balance)
  1262. {
  1263. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1264. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1265. bool isPair;
  1266. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1267. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1268. {
  1269. // Dry/Wet
  1270. if (doDryWet)
  1271. {
  1272. for (uint32_t k=0; k < frames; ++k)
  1273. {
  1274. // TODO
  1275. //if (k < pData->latency && pData->latency < frames)
  1276. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  1277. //else
  1278. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1279. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1280. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1281. }
  1282. }
  1283. // Balance
  1284. if (doBalance)
  1285. {
  1286. isPair = (i % 2 == 0);
  1287. if (isPair)
  1288. {
  1289. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1290. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1291. }
  1292. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1293. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1294. for (uint32_t k=0; k < frames; ++k)
  1295. {
  1296. if (isPair)
  1297. {
  1298. // left
  1299. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1300. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1301. }
  1302. else
  1303. {
  1304. // right
  1305. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1306. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1307. }
  1308. }
  1309. }
  1310. // Volume (and buffer copy)
  1311. {
  1312. for (uint32_t k=0; k < frames; ++k)
  1313. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1314. }
  1315. }
  1316. } // End of Post-processing
  1317. #else // BUILD_BRIDGE
  1318. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1319. {
  1320. for (uint32_t k=0; k < frames; ++k)
  1321. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1322. }
  1323. #endif
  1324. // --------------------------------------------------------------------------------------------------------
  1325. pData->singleMutex.unlock();
  1326. return true;
  1327. }
  1328. void bufferSizeChanged(const uint32_t newBufferSize) override
  1329. {
  1330. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1331. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  1332. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1333. {
  1334. if (fAudioInBuffers[i] != nullptr)
  1335. delete[] fAudioInBuffers[i];
  1336. fAudioInBuffers[i] = new float[newBufferSize];
  1337. }
  1338. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1339. {
  1340. if (fAudioOutBuffers[i] != nullptr)
  1341. delete[] fAudioOutBuffers[i];
  1342. fAudioOutBuffers[i] = new float[newBufferSize];
  1343. }
  1344. if (fHandle2 == nullptr)
  1345. {
  1346. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1347. {
  1348. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1349. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1350. }
  1351. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1352. {
  1353. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1354. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1355. }
  1356. }
  1357. else
  1358. {
  1359. if (pData->audioIn.count > 0)
  1360. {
  1361. CARLA_ASSERT(pData->audioIn.count == 2);
  1362. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1363. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1364. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1365. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1366. }
  1367. if (pData->audioOut.count > 0)
  1368. {
  1369. CARLA_ASSERT(pData->audioOut.count == 2);
  1370. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1371. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1372. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1373. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1374. }
  1375. }
  1376. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  1377. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  1378. {
  1379. fLv2Options.maxBufferSize = newBufferSizeInt;
  1380. if (fLv2Options.minBufferSize != 1)
  1381. fLv2Options.minBufferSize = newBufferSizeInt;
  1382. if (fExt.options != nullptr && fExt.options->set != nullptr)
  1383. {
  1384. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  1385. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  1386. }
  1387. }
  1388. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  1389. }
  1390. void sampleRateChanged(const double newSampleRate) override
  1391. {
  1392. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1393. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  1394. if (fLv2Options.sampleRate != newSampleRate)
  1395. {
  1396. fLv2Options.sampleRate = newSampleRate;
  1397. if (fExt.options != nullptr && fExt.options->set != nullptr)
  1398. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  1399. }
  1400. for (uint32_t k=0; k < pData->param.count; ++k)
  1401. {
  1402. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  1403. {
  1404. fParamBuffers[k] = static_cast<float>(newSampleRate);
  1405. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  1406. break;
  1407. }
  1408. }
  1409. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  1410. }
  1411. void offlineModeChanged(const bool isOffline) override
  1412. {
  1413. for (uint32_t k=0; k < pData->param.count; ++k)
  1414. {
  1415. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1416. {
  1417. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  1418. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  1419. break;
  1420. }
  1421. }
  1422. }
  1423. // -------------------------------------------------------------------
  1424. // Plugin buffers
  1425. void initBuffers() override
  1426. {
  1427. //fEventsIn.initBuffers(pData->engine);
  1428. //fEventsOut.initBuffers(pData->engine);
  1429. CarlaPlugin::initBuffers();
  1430. }
  1431. void clearBuffers() override
  1432. {
  1433. carla_debug("Lv2Plugin::clearBuffers() - start");
  1434. if (fAudioInBuffers != nullptr)
  1435. {
  1436. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1437. {
  1438. if (fAudioInBuffers[i] != nullptr)
  1439. {
  1440. delete[] fAudioInBuffers[i];
  1441. fAudioInBuffers[i] = nullptr;
  1442. }
  1443. }
  1444. delete[] fAudioInBuffers;
  1445. fAudioInBuffers = nullptr;
  1446. }
  1447. if (fAudioOutBuffers != nullptr)
  1448. {
  1449. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1450. {
  1451. if (fAudioOutBuffers[i] != nullptr)
  1452. {
  1453. delete[] fAudioOutBuffers[i];
  1454. fAudioOutBuffers[i] = nullptr;
  1455. }
  1456. }
  1457. delete[] fAudioOutBuffers;
  1458. fAudioOutBuffers = nullptr;
  1459. }
  1460. if (fParamBuffers != nullptr)
  1461. {
  1462. delete[] fParamBuffers;
  1463. fParamBuffers = nullptr;
  1464. }
  1465. //fEventsIn.clear();
  1466. //fEventsOut.clear();
  1467. CarlaPlugin::clearBuffers();
  1468. carla_debug("Lv2Plugin::clearBuffers() - end");
  1469. }
  1470. // -------------------------------------------------------------------
  1471. bool isRealtimeSafe() const noexcept
  1472. {
  1473. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  1474. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  1475. {
  1476. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  1477. return true;
  1478. }
  1479. return false;
  1480. }
  1481. bool needsFixedBuffer() const noexcept
  1482. {
  1483. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  1484. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  1485. {
  1486. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  1487. return true;
  1488. }
  1489. return false;
  1490. }
  1491. // -------------------------------------------------------------------
  1492. bool isUiBridgeable(const uint32_t uiId) const noexcept
  1493. {
  1494. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  1495. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  1496. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  1497. {
  1498. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  1499. return false;
  1500. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  1501. return false;
  1502. }
  1503. return true;
  1504. }
  1505. bool isUiResizable() const noexcept
  1506. {
  1507. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  1508. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  1509. {
  1510. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  1511. return false;
  1512. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  1513. return false;
  1514. }
  1515. return true;
  1516. }
  1517. // -------------------------------------------------------------------
  1518. void recheckExtensions()
  1519. {
  1520. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1521. fExt.options = nullptr;
  1522. fExt.programs = nullptr;
  1523. fExt.state = nullptr;
  1524. fExt.worker = nullptr;
  1525. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  1526. {
  1527. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  1528. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  1529. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  1530. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  1531. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  1532. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  1533. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  1534. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  1535. else
  1536. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  1537. }
  1538. if (fDescriptor->extension_data != nullptr)
  1539. {
  1540. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  1541. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  1542. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1543. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  1544. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  1545. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  1546. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  1547. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  1548. // check if invalid
  1549. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  1550. fExt.options = nullptr;
  1551. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  1552. fExt.programs = nullptr;
  1553. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  1554. fExt.state = nullptr;
  1555. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  1556. fExt.worker = nullptr;
  1557. }
  1558. }
  1559. // -------------------------------------------------------------------
  1560. void updateUi()
  1561. {
  1562. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  1563. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  1564. fExt.uiidle = nullptr;
  1565. fExt.uiprograms = nullptr;
  1566. if (fUi.descriptor->extension_data != nullptr)
  1567. {
  1568. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  1569. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  1570. // check if invalid
  1571. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  1572. fExt.uiidle = nullptr;
  1573. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  1574. fExt.uiprograms = nullptr;
  1575. }
  1576. // update midi program
  1577. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  1578. {
  1579. const MidiProgramData& curData(pData->midiprog.getCurrent());
  1580. fExt.uiprograms->select_program(fUi.handle, curData.bank, curData.program);
  1581. }
  1582. // update control ports
  1583. if (fUi.descriptor->port_event != nullptr)
  1584. {
  1585. float value;
  1586. for (uint32_t i=0; i < pData->param.count; ++i)
  1587. {
  1588. value = getParameterValue(i);
  1589. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  1590. }
  1591. }
  1592. }
  1593. // -------------------------------------------------------------------
  1594. LV2_URID getCustomURID(const char* const uri)
  1595. {
  1596. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  1597. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  1598. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  1599. {
  1600. const char*& thisUri(fCustomURIDs.getAt(i));
  1601. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  1602. return static_cast<LV2_URID>(i);
  1603. }
  1604. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  1605. fCustomURIDs.append(carla_strdup(uri));
  1606. #if 0
  1607. if (fUi.type == PLUGIN_UI_OSC && kData->osc.data.target != nullptr)
  1608. osc_send_lv2_urid_map(&kData->osc.data, urid, uri);
  1609. #endif
  1610. return urid;
  1611. }
  1612. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  1613. {
  1614. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  1615. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  1616. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  1617. return fCustomURIDs.getAt(urid);
  1618. }
  1619. // -------------------------------------------------------------------
  1620. void handleProgramChanged(const int32_t index)
  1621. {
  1622. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  1623. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  1624. if (index == -1)
  1625. {
  1626. const ScopedSingleProcessLocker spl(this, true);
  1627. return reloadPrograms(false);
  1628. }
  1629. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  1630. {
  1631. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  1632. {
  1633. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  1634. if (pData->midiprog.data[index].name != nullptr)
  1635. delete[] pData->midiprog.data[index].name;
  1636. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  1637. if (index == pData->midiprog.current)
  1638. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  1639. else
  1640. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  1641. }
  1642. }
  1643. }
  1644. // -------------------------------------------------------------------
  1645. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  1646. {
  1647. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  1648. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  1649. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  1650. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  1651. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  1652. carla_debug("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i)", key, value, size, type, flags);
  1653. const char* const skey(carla_lv2_urid_unmap(this, key));
  1654. const char* const stype(carla_lv2_urid_unmap(this, type));
  1655. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  1656. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  1657. // Check if we already have this key
  1658. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1659. {
  1660. CustomData& data(it.getValue());
  1661. if (std::strcmp(data.key, skey) == 0)
  1662. {
  1663. // found it
  1664. if (data.value != nullptr)
  1665. delete[] data.value;
  1666. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  1667. data.value = carla_strdup((const char*)value);
  1668. else
  1669. data.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  1670. return LV2_STATE_SUCCESS;
  1671. }
  1672. }
  1673. // Otherwise store it
  1674. CustomData newData;
  1675. newData.type = carla_strdup(stype);
  1676. newData.key = carla_strdup(skey);
  1677. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  1678. newData.value = carla_strdup((const char*)value);
  1679. else
  1680. newData.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  1681. pData->custom.append(newData);
  1682. return LV2_STATE_SUCCESS;
  1683. }
  1684. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  1685. {
  1686. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  1687. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  1688. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  1689. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  1690. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  1691. const char* const skey(carla_lv2_urid_unmap(this, key));
  1692. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  1693. const char* stype = nullptr;
  1694. const char* stringData = nullptr;
  1695. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  1696. {
  1697. const CustomData& data(it.getValue());
  1698. if (std::strcmp(data.key, skey) == 0)
  1699. {
  1700. stype = data.type;
  1701. stringData = data.value;
  1702. break;
  1703. }
  1704. }
  1705. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  1706. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  1707. *type = carla_lv2_urid_map(this, stype);
  1708. *flags = LV2_STATE_IS_POD;
  1709. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  1710. {
  1711. *size = std::strlen(stringData);
  1712. return stringData;
  1713. }
  1714. else
  1715. {
  1716. static QByteArray chunk; // FIXME
  1717. chunk = QByteArray::fromBase64(stringData);
  1718. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  1719. *size = static_cast<size_t>(chunk.size());
  1720. return chunk.constData();
  1721. }
  1722. }
  1723. // -------------------------------------------------------------------
  1724. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  1725. {
  1726. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  1727. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  1728. //if (pData->engine->isOffline())
  1729. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  1730. //else
  1731. // postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  1732. return LV2_WORKER_SUCCESS;
  1733. }
  1734. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  1735. {
  1736. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  1737. #if 0
  1738. LV2_Atom_Worker workerAtom;
  1739. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  1740. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  1741. workerAtom.body.size = size;
  1742. workerAtom.body.data = data;
  1743. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  1744. #endif
  1745. return LV2_WORKER_SUCCESS;
  1746. }
  1747. // -------------------------------------------------------------------
  1748. public:
  1749. bool init(const char* const bundle, const char* const name, const char* const uri)
  1750. {
  1751. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1752. // ---------------------------------------------------------------
  1753. // first checks
  1754. if (pData->client != nullptr)
  1755. {
  1756. pData->engine->setLastError("Plugin client is already registered");
  1757. return false;
  1758. }
  1759. if (bundle == nullptr || bundle[0] == '\0')
  1760. {
  1761. pData->engine->setLastError("null bundle");
  1762. return false;
  1763. }
  1764. if (uri == nullptr || uri[0] == '\0')
  1765. {
  1766. pData->engine->setLastError("null uri");
  1767. return false;
  1768. }
  1769. // ---------------------------------------------------------------
  1770. // get plugin from lv2_rdf (lilv)
  1771. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  1772. // Convert bundle filename to URI
  1773. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  1774. if (! qBundle.endsWith(OS_SEP_STR))
  1775. qBundle += OS_SEP_STR;
  1776. // Load bundle
  1777. Lilv::Node lilvBundle(lv2World.new_uri(qBundle.toUtf8().constData()));
  1778. lv2World.load_bundle(lilvBundle);
  1779. fRdfDescriptor = lv2_rdf_new(uri, true);
  1780. if (fRdfDescriptor == nullptr)
  1781. {
  1782. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  1783. return false;
  1784. }
  1785. // ---------------------------------------------------------------
  1786. // open DLL
  1787. if (! pData->libOpen(fRdfDescriptor->Binary))
  1788. {
  1789. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  1790. return false;
  1791. }
  1792. // ---------------------------------------------------------------
  1793. // get DLL main entry
  1794. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  1795. if (descFn == nullptr)
  1796. {
  1797. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  1798. return false;
  1799. }
  1800. // -----------------------------------------------------------
  1801. // get descriptor that matches URI
  1802. uint32_t i = 0;
  1803. while ((fDescriptor = descFn(i++)))
  1804. {
  1805. if (std::strcmp(fDescriptor->URI, uri) == 0)
  1806. break;
  1807. }
  1808. if (fDescriptor == nullptr)
  1809. {
  1810. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  1811. return false;
  1812. }
  1813. // ---------------------------------------------------------------
  1814. // check supported port-types and features
  1815. bool canContinue = true;
  1816. // Check supported ports
  1817. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  1818. {
  1819. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  1820. if (! is_lv2_port_supported(portTypes))
  1821. {
  1822. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  1823. {
  1824. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  1825. canContinue = false;
  1826. break;
  1827. }
  1828. }
  1829. }
  1830. // Check supported features
  1831. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  1832. {
  1833. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  1834. {
  1835. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  1836. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  1837. {
  1838. canContinue = false;
  1839. pData->engine->setLastError(msg.toUtf8().constData());
  1840. break;
  1841. }
  1842. else
  1843. carla_stderr("%s", msg.toUtf8().constData());
  1844. }
  1845. }
  1846. if (! canContinue)
  1847. {
  1848. // error already set
  1849. return false;
  1850. }
  1851. // ---------------------------------------------------------------
  1852. // get info
  1853. if (name != nullptr && name[0] != '\0')
  1854. pData->name = pData->engine->getUniquePluginName(name);
  1855. else
  1856. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  1857. pData->filename = carla_strdup(bundle);
  1858. // ---------------------------------------------------------------
  1859. // register client
  1860. pData->client = pData->engine->addClient(this);
  1861. if (pData->client == nullptr || ! pData->client->isOk())
  1862. {
  1863. pData->engine->setLastError("Failed to register plugin client");
  1864. return false;
  1865. }
  1866. // ---------------------------------------------------------------
  1867. // initialize options
  1868. fLv2Options.minBufferSize = 1;
  1869. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  1870. fLv2Options.sampleRate = pData->engine->getSampleRate();
  1871. // ---------------------------------------------------------------
  1872. // initialize features (part 1)
  1873. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  1874. eventFt->callback_data = this;
  1875. eventFt->lv2_event_ref = carla_lv2_event_ref;
  1876. eventFt->lv2_event_unref = carla_lv2_event_unref;
  1877. LV2_Log_Log* const logFt = new LV2_Log_Log;
  1878. logFt->handle = this;
  1879. logFt->printf = carla_lv2_log_printf;
  1880. logFt->vprintf = carla_lv2_log_vprintf;
  1881. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  1882. stateMakePathFt->handle = this;
  1883. stateMakePathFt->path = carla_lv2_state_make_path;
  1884. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  1885. stateMapPathFt->handle = this;
  1886. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  1887. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  1888. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  1889. programsFt->handle = this;
  1890. programsFt->program_changed = carla_lv2_program_changed;
  1891. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  1892. lv2_rtmempool_init(rtMemPoolFt);
  1893. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  1894. uriMapFt->callback_data = this;
  1895. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  1896. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  1897. uridMapFt->handle = this;
  1898. uridMapFt->map = carla_lv2_urid_map;
  1899. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  1900. uridUnmapFt->handle = this;
  1901. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  1902. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  1903. workerFt->handle = this;
  1904. workerFt->schedule_work = carla_lv2_worker_schedule;
  1905. // ---------------------------------------------------------------
  1906. // initialize features (part 2)
  1907. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  1908. fFeatures[j] = new LV2_Feature;
  1909. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  1910. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  1911. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  1912. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  1913. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  1914. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  1915. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  1916. fFeatures[kFeatureIdEvent]->data = eventFt;
  1917. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  1918. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  1919. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  1920. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  1921. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  1922. fFeatures[kFeatureIdIsLive]->data = nullptr;
  1923. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  1924. fFeatures[kFeatureIdLogs]->data = logFt;
  1925. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  1926. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  1927. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  1928. fFeatures[kFeatureIdPrograms]->data = programsFt;
  1929. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  1930. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  1931. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  1932. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  1933. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  1934. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  1935. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  1936. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  1937. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  1938. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  1939. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  1940. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  1941. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  1942. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  1943. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  1944. fFeatures[kFeatureIdWorker]->data = workerFt;
  1945. // if a fixed buffer is not needed, skip its feature
  1946. if (! needsFixedBuffer())
  1947. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  1948. // if power of 2 is not possible, skip its feature FIXME
  1949. //if (fLv2Options.maxBufferSize)
  1950. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  1951. // ---------------------------------------------------------------
  1952. // initialize plugin
  1953. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  1954. if (fHandle == nullptr)
  1955. {
  1956. pData->engine->setLastError("Plugin failed to initialize");
  1957. return false;
  1958. }
  1959. // ---------------------------------------------------------------
  1960. // load plugin settings
  1961. {
  1962. // set default options
  1963. pData->options = 0x0;
  1964. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1965. if (getMidiInCount() > 0 || needsFixedBuffer())
  1966. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1967. if (pData->engine->getOptions().forceStereo)
  1968. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1969. if (getMidiInCount() > 0)
  1970. {
  1971. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1972. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1973. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1974. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1975. }
  1976. // set identifier string
  1977. CarlaString identifier("LV2/");
  1978. identifier += uri;
  1979. // load settings
  1980. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1981. // ignore settings, we need this anyway
  1982. if (getMidiInCount() > 0 || needsFixedBuffer())
  1983. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1984. }
  1985. // ---------------------------------------------------------------
  1986. // gui stuff
  1987. if (fRdfDescriptor->UICount == 0)
  1988. return true;
  1989. // ---------------------------------------------------------------
  1990. return true;
  1991. }
  1992. // -------------------------------------------------------------------
  1993. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  1994. {
  1995. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  1996. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  1997. //fAtomQueueIn.put(portIndex, atom);
  1998. return; (void)portIndex;
  1999. }
  2000. void handleUridMap(const LV2_URID urid, const char* const uri)
  2001. {
  2002. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  2003. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  2004. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  2005. carla_debug("Lv2Plugin::handleUridMap(%i, \"%s\")", urid, uri);
  2006. fCustomURIDs.append(carla_strdup(uri));
  2007. }
  2008. // -------------------------------------------------------------------
  2009. private:
  2010. LV2_Handle fHandle;
  2011. LV2_Handle fHandle2;
  2012. LV2_Feature* fFeatures[kFeatureCountAll+1];
  2013. const LV2_Descriptor* fDescriptor;
  2014. const LV2_RDF_Descriptor* fRdfDescriptor;
  2015. float** fAudioInBuffers;
  2016. float** fAudioOutBuffers;
  2017. float* fParamBuffers;
  2018. Lv2PluginOptions fLv2Options;
  2019. LinkedList<const char*> fCustomURIDs;
  2020. bool fFirstActive; // first process() call after activate()
  2021. EngineTimeInfo fLastTimeInfo;
  2022. struct Extensions {
  2023. const LV2_Options_Interface* options;
  2024. const LV2_State_Interface* state;
  2025. const LV2_Worker_Interface* worker;
  2026. const LV2_Programs_Interface* programs;
  2027. const LV2UI_Idle_Interface* uiidle;
  2028. const LV2_Programs_UI_Interface* uiprograms;
  2029. Extensions()
  2030. : options(nullptr),
  2031. state(nullptr),
  2032. worker(nullptr),
  2033. programs(nullptr),
  2034. uiidle(nullptr),
  2035. uiprograms(nullptr) {}
  2036. } fExt;
  2037. struct UI {
  2038. enum Type {
  2039. TYPE_NULL,
  2040. TYPE_EMBED,
  2041. TYPE_EXTERNAL,
  2042. TYPE_OSC
  2043. };
  2044. Type type;
  2045. LV2UI_Handle handle;
  2046. LV2UI_Widget widget;
  2047. const LV2UI_Descriptor* descriptor;
  2048. const LV2_RDF_UI* rdfDescriptor;
  2049. UI()
  2050. : type(TYPE_NULL),
  2051. handle(nullptr),
  2052. widget(nullptr),
  2053. descriptor(nullptr),
  2054. rdfDescriptor(nullptr) {}
  2055. ~UI()
  2056. {
  2057. CARLA_ASSERT(handle == nullptr);
  2058. CARLA_ASSERT(widget == nullptr);
  2059. CARLA_ASSERT(descriptor == nullptr);
  2060. CARLA_ASSERT(rdfDescriptor == nullptr);
  2061. }
  2062. } fUi;
  2063. // -------------------------------------------------------------------
  2064. // Event Feature
  2065. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  2066. {
  2067. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  2068. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  2069. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  2070. return 0;
  2071. }
  2072. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  2073. {
  2074. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  2075. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  2076. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  2077. return 0;
  2078. }
  2079. // -------------------------------------------------------------------
  2080. // Logs Feature
  2081. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  2082. {
  2083. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  2084. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  2085. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  2086. #ifndef DEBUG
  2087. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2088. return 0;
  2089. #endif
  2090. va_list args;
  2091. va_start(args, fmt);
  2092. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  2093. va_end(args);
  2094. return ret;
  2095. }
  2096. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  2097. {
  2098. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  2099. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  2100. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  2101. #ifndef DEBUG
  2102. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2103. return 0;
  2104. #endif
  2105. int ret = 0;
  2106. switch (type)
  2107. {
  2108. case CARLA_URI_MAP_ID_LOG_ERROR:
  2109. std::fprintf(stderr, "\x1b[31m");
  2110. ret = std::vfprintf(stderr, fmt, ap);
  2111. std::fprintf(stderr, "\x1b[0m");
  2112. break;
  2113. case CARLA_URI_MAP_ID_LOG_NOTE:
  2114. ret = std::vfprintf(stdout, fmt, ap);
  2115. break;
  2116. case CARLA_URI_MAP_ID_LOG_TRACE:
  2117. #ifdef DEBUG
  2118. std::fprintf(stdout, "\x1b[30;1m");
  2119. ret = std::vfprintf(stdout, fmt, ap);
  2120. std::fprintf(stdout, "\x1b[0m");
  2121. #endif
  2122. break;
  2123. case CARLA_URI_MAP_ID_LOG_WARNING:
  2124. ret = std::vfprintf(stderr, fmt, ap);
  2125. break;
  2126. default:
  2127. break;
  2128. }
  2129. return ret;
  2130. }
  2131. // -------------------------------------------------------------------
  2132. // Programs Feature
  2133. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  2134. {
  2135. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  2136. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  2137. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  2138. }
  2139. // -------------------------------------------------------------------
  2140. // State Feature
  2141. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  2142. {
  2143. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  2144. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  2145. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  2146. QDir dir;
  2147. dir.mkpath(path);
  2148. return strdup(path);
  2149. }
  2150. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  2151. {
  2152. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  2153. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  2154. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  2155. QDir dir(absolute_path);
  2156. return strdup(dir.canonicalPath().toUtf8().constData());
  2157. }
  2158. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  2159. {
  2160. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  2161. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  2162. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  2163. QDir dir(abstract_path);
  2164. return strdup(dir.absolutePath().toUtf8().constData());
  2165. }
  2166. 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)
  2167. {
  2168. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  2169. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  2170. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  2171. }
  2172. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  2173. {
  2174. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  2175. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  2176. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  2177. }
  2178. // -------------------------------------------------------------------
  2179. // URI-Map Feature
  2180. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  2181. {
  2182. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  2183. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  2184. // unused
  2185. (void)map;
  2186. }
  2187. // -------------------------------------------------------------------
  2188. // URID Feature
  2189. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  2190. {
  2191. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  2192. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  2193. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  2194. // Atom types
  2195. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  2196. return CARLA_URI_MAP_ID_ATOM_BLANK;
  2197. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  2198. return CARLA_URI_MAP_ID_ATOM_BOOL;
  2199. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  2200. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  2201. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  2202. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  2203. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  2204. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  2205. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  2206. return CARLA_URI_MAP_ID_ATOM_INT;
  2207. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  2208. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  2209. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  2210. return CARLA_URI_MAP_ID_ATOM_LONG;
  2211. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  2212. return CARLA_URI_MAP_ID_ATOM_PATH;
  2213. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  2214. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  2215. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  2216. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  2217. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  2218. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  2219. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  2220. return CARLA_URI_MAP_ID_ATOM_STRING;
  2221. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  2222. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  2223. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  2224. return CARLA_URI_MAP_ID_ATOM_URI;
  2225. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  2226. return CARLA_URI_MAP_ID_ATOM_URID;
  2227. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  2228. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  2229. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  2230. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  2231. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  2232. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  2233. // BufSize types
  2234. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  2235. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  2236. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  2237. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  2238. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  2239. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  2240. // Log types
  2241. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  2242. return CARLA_URI_MAP_ID_LOG_ERROR;
  2243. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  2244. return CARLA_URI_MAP_ID_LOG_NOTE;
  2245. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  2246. return CARLA_URI_MAP_ID_LOG_TRACE;
  2247. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  2248. return CARLA_URI_MAP_ID_LOG_WARNING;
  2249. // Time types
  2250. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  2251. return CARLA_URI_MAP_ID_TIME_POSITION;
  2252. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  2253. return CARLA_URI_MAP_ID_TIME_BAR;
  2254. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  2255. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  2256. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  2257. return CARLA_URI_MAP_ID_TIME_BEAT;
  2258. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  2259. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  2260. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  2261. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  2262. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  2263. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  2264. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  2265. return CARLA_URI_MAP_ID_TIME_FRAME;
  2266. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  2267. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  2268. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  2269. return CARLA_URI_MAP_ID_TIME_SPEED;
  2270. // Others
  2271. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  2272. return CARLA_URI_MAP_ID_MIDI_EVENT;
  2273. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  2274. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  2275. // Custom types
  2276. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  2277. }
  2278. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  2279. {
  2280. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  2281. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  2282. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  2283. // Atom types
  2284. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  2285. return LV2_ATOM__Blank;
  2286. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  2287. return LV2_ATOM__Bool;
  2288. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2289. return LV2_ATOM__Chunk;
  2290. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  2291. return LV2_ATOM__Double;
  2292. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  2293. return LV2_ATOM__Float;
  2294. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  2295. return LV2_ATOM__Int;
  2296. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  2297. return LV2_ATOM__Literal;
  2298. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  2299. return LV2_ATOM__Long;
  2300. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  2301. return LV2_ATOM__Path;
  2302. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  2303. return LV2_ATOM__Property;
  2304. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  2305. return LV2_ATOM__Resource;
  2306. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  2307. return LV2_ATOM__Sequence;
  2308. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  2309. return LV2_ATOM__String;
  2310. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  2311. return LV2_ATOM__Tuple;
  2312. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  2313. return LV2_ATOM__URI;
  2314. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  2315. return LV2_ATOM__URID;
  2316. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  2317. return LV2_ATOM__Vector;
  2318. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2319. return LV2_ATOM__atomTransfer;
  2320. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2321. return LV2_ATOM__eventTransfer;
  2322. // BufSize types
  2323. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  2324. return LV2_BUF_SIZE__maxBlockLength;
  2325. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  2326. return LV2_BUF_SIZE__minBlockLength;
  2327. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  2328. return LV2_BUF_SIZE__sequenceSize;
  2329. // Log types
  2330. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  2331. return LV2_LOG__Error;
  2332. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  2333. return LV2_LOG__Note;
  2334. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  2335. return LV2_LOG__Trace;
  2336. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  2337. return LV2_LOG__Warning;
  2338. // Time types
  2339. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  2340. return LV2_TIME__Position;
  2341. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  2342. return LV2_TIME__bar;
  2343. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  2344. return LV2_TIME__barBeat;
  2345. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  2346. return LV2_TIME__beat;
  2347. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  2348. return LV2_TIME__beatUnit;
  2349. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  2350. return LV2_TIME__beatsPerBar;
  2351. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  2352. return LV2_TIME__beatsPerMinute;
  2353. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  2354. return LV2_TIME__frame;
  2355. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  2356. return LV2_TIME__framesPerSecond;
  2357. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  2358. return LV2_TIME__speed;
  2359. // Others
  2360. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  2361. return LV2_MIDI__MidiEvent;
  2362. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  2363. return LV2_PARAMETERS__sampleRate;
  2364. // Custom types
  2365. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  2366. }
  2367. // -------------------------------------------------------------------
  2368. // Worker Feature
  2369. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  2370. {
  2371. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  2372. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  2373. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  2374. }
  2375. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  2376. {
  2377. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  2378. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  2379. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  2380. }
  2381. // -------------------------------------------------------------------
  2382. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  2383. };
  2384. // -------------------------------------------------------------------------------------------------------------------
  2385. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  2386. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  2387. {
  2388. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  2389. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  2390. const int32_t portIndex = argv[0]->i;
  2391. const char* const atomBuf = (const char*)&argv[1]->s;
  2392. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  2393. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  2394. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  2395. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  2396. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  2397. return 0;
  2398. }
  2399. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  2400. {
  2401. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  2402. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  2403. const int32_t urid = argv[0]->i;
  2404. const char* const uri = (const char*)&argv[1]->s;
  2405. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  2406. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  2407. return 0;
  2408. }
  2409. #undef lv2PluginPtr
  2410. CARLA_BACKEND_END_NAMESPACE
  2411. #endif // WANT_LV2
  2412. // -------------------------------------------------------------------------------------------------------------------
  2413. CARLA_BACKEND_START_NAMESPACE
  2414. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  2415. {
  2416. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  2417. #ifdef WANT_LV2
  2418. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  2419. if (! plugin->init(init.filename, init.name, init.label))
  2420. {
  2421. delete plugin;
  2422. return nullptr;
  2423. }
  2424. plugin->reload();
  2425. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  2426. {
  2427. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  2428. delete plugin;
  2429. return nullptr;
  2430. }
  2431. return plugin;
  2432. #else
  2433. init.engine->setLastError("LV2 support not available");
  2434. return nullptr;
  2435. #endif
  2436. }
  2437. CARLA_BACKEND_END_NAMESPACE
  2438. // -------------------------------------------------------------------------------------------------------------------