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.

2439 lines
87KB

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