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.

5463 lines
205KB

  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. // need this first for juce headers
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaLv2Utils.hpp"
  23. #include "CarlaPluginUi.hpp"
  24. #include "Lv2AtomQueue.hpp"
  25. #include "../engine/CarlaEngineOsc.hpp"
  26. extern "C" {
  27. #include "rtmempool/rtmempool-lv2.h"
  28. }
  29. #include <QtCore/QDir>
  30. #include <QtCore/QFile>
  31. #include <QtCore/QUrl>
  32. // -----------------------------------------------------
  33. CARLA_BACKEND_START_NAMESPACE
  34. #if 0
  35. }
  36. #endif
  37. // Maximum default buffer size
  38. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  39. // Extra Plugin Hints
  40. const unsigned int PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  41. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  42. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  43. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  44. // Extra Parameter Hints
  45. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  46. const unsigned int PARAMETER_IS_TRIGGER = 0x2000;
  47. // LV2 Event Data/Types
  48. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  49. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  50. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  51. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  52. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  53. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  54. // LV2 URI Map Ids
  55. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  56. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  57. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  58. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 5;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 6;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 7;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 8;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 9;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 10;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 11;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 12;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 13;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 14;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 15;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 16;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 17;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 18;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_WORKER = 19; // custom
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 20;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 21;
  77. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 22;
  78. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 23;
  79. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 24;
  80. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 25;
  81. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 26;
  82. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 27;
  83. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 28;
  84. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 29; // base type
  85. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 30; // values
  86. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 31;
  87. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 32;
  88. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 33;
  89. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 34;
  90. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 35;
  91. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 36;
  92. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 37;
  93. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 38;
  94. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 39;
  95. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 40;
  96. const uint32_t CARLA_URI_MAP_ID_COUNT = 41;
  97. // LV2 Feature Ids
  98. const uint32_t kFeatureIdBufSizeBounded = 0;
  99. const uint32_t kFeatureIdBufSizeFixed = 1;
  100. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  101. const uint32_t kFeatureIdEvent = 3;
  102. const uint32_t kFeatureIdHardRtCapable = 4;
  103. const uint32_t kFeatureIdInPlaceBroken = 5;
  104. const uint32_t kFeatureIdIsLive = 6;
  105. const uint32_t kFeatureIdLogs = 7;
  106. const uint32_t kFeatureIdOptions = 8;
  107. const uint32_t kFeatureIdPrograms = 9;
  108. const uint32_t kFeatureIdResizePort = 10;
  109. const uint32_t kFeatureIdRtMemPool = 11;
  110. const uint32_t kFeatureIdRtMemPoolOld = 12;
  111. const uint32_t kFeatureIdStateMakePath = 13;
  112. const uint32_t kFeatureIdStateMapPath = 14;
  113. const uint32_t kFeatureIdStrictBounds = 15;
  114. const uint32_t kFeatureIdUriMap = 16;
  115. const uint32_t kFeatureIdUridMap = 17;
  116. const uint32_t kFeatureIdUridUnmap = 18;
  117. const uint32_t kFeatureIdWorker = 19;
  118. const uint32_t kFeatureCountPlugin = 20;
  119. const uint32_t kFeatureIdUiDataAccess = 20;
  120. const uint32_t kFeatureIdUiInstanceAccess = 21;
  121. const uint32_t kFeatureIdUiIdleInterface = 22;
  122. const uint32_t kFeatureIdUiFixedSize = 23;
  123. const uint32_t kFeatureIdUiMakeResident = 24;
  124. const uint32_t kFeatureIdUiNoUserResize = 25;
  125. const uint32_t kFeatureIdUiParent = 26;
  126. const uint32_t kFeatureIdUiPortMap = 27;
  127. const uint32_t kFeatureIdUiPortSubscribe = 28;
  128. const uint32_t kFeatureIdUiResize = 29;
  129. const uint32_t kFeatureIdUiTouch = 30;
  130. const uint32_t kFeatureIdExternalUi = 31;
  131. const uint32_t kFeatureIdExternalUiOld = 32;
  132. const uint32_t kFeatureCountAll = 33;
  133. // -----------------------------------------------------
  134. struct Lv2EventData {
  135. uint32_t type;
  136. uint32_t rindex;
  137. CarlaEngineEventPort* port;
  138. union {
  139. LV2_Atom_Buffer* atom;
  140. LV2_Event_Buffer* event;
  141. LV2_MIDI midi;
  142. };
  143. Lv2EventData()
  144. : type(0x0),
  145. rindex(0),
  146. port(nullptr) {}
  147. ~Lv2EventData()
  148. {
  149. if (port != nullptr)
  150. {
  151. delete port;
  152. port = nullptr;
  153. }
  154. const uint32_t rtype(type);
  155. type = 0x0;
  156. if (rtype & CARLA_EVENT_DATA_ATOM)
  157. {
  158. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  159. std::free(atom);
  160. atom = nullptr;
  161. }
  162. else if (rtype & CARLA_EVENT_DATA_EVENT)
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  165. std::free(event);
  166. event = nullptr;
  167. }
  168. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  171. delete[] midi.data;
  172. midi.data = nullptr;
  173. }
  174. }
  175. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  176. };
  177. struct Lv2PluginEventData {
  178. uint32_t count;
  179. Lv2EventData* data;
  180. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  181. uint32_t ctrlIndex;
  182. Lv2PluginEventData()
  183. : count(0),
  184. data(nullptr),
  185. ctrl(nullptr),
  186. ctrlIndex(0) {}
  187. ~Lv2PluginEventData()
  188. {
  189. CARLA_ASSERT_INT(count == 0, count);
  190. CARLA_ASSERT(data == nullptr);
  191. CARLA_ASSERT(ctrl == nullptr);
  192. CARLA_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  193. }
  194. void createNew(const uint32_t newCount)
  195. {
  196. CARLA_SAFE_ASSERT_INT(count == 0, count);
  197. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  198. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  199. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  200. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  201. data = new Lv2EventData[newCount];
  202. count = newCount;
  203. }
  204. void clear()
  205. {
  206. if (data != nullptr)
  207. {
  208. for (uint32_t i=0; i < count; ++i)
  209. {
  210. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  211. data[i].port = nullptr;
  212. }
  213. delete[] data;
  214. data = nullptr;
  215. }
  216. count = 0;
  217. ctrl = nullptr;
  218. ctrlIndex = 0;
  219. }
  220. void initBuffers() const noexcept
  221. {
  222. for (uint32_t i=0; i < count; ++i)
  223. {
  224. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  225. data[i].port->initBuffer();
  226. }
  227. }
  228. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginEventData)
  229. };
  230. // -----------------------------------------------------
  231. struct Lv2PluginOptions {
  232. enum OptIndex {
  233. MaxBlockLenth = 0,
  234. MinBlockLenth,
  235. SequenceSize,
  236. SampleRate,
  237. Null
  238. };
  239. int maxBufferSize;
  240. int minBufferSize;
  241. int sequenceSize;
  242. double sampleRate;
  243. LV2_Options_Option opts[5];
  244. Lv2PluginOptions()
  245. : maxBufferSize(0),
  246. minBufferSize(0),
  247. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  248. sampleRate(0.0)
  249. {
  250. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  251. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  252. optMaxBlockLenth.subject = 0;
  253. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  254. optMaxBlockLenth.size = sizeof(int);
  255. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  256. optMaxBlockLenth.value = &maxBufferSize;
  257. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  258. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  259. optMinBlockLenth.subject = 0;
  260. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  261. optMinBlockLenth.size = sizeof(int);
  262. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  263. optMinBlockLenth.value = &minBufferSize;
  264. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  265. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  266. optSequenceSize.subject = 0;
  267. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  268. optSequenceSize.size = sizeof(int);
  269. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  270. optSequenceSize.value = &sequenceSize;
  271. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  272. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  273. optSampleRate.subject = 0;
  274. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  275. optSampleRate.size = sizeof(double);
  276. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  277. optSampleRate.value = &sampleRate;
  278. LV2_Options_Option& optNull(opts[Null]);
  279. optNull.context = LV2_OPTIONS_INSTANCE;
  280. optNull.subject = 0;
  281. optNull.key = CARLA_URI_MAP_ID_NULL;
  282. optNull.size = 0;
  283. optNull.type = CARLA_URI_MAP_ID_NULL;
  284. optNull.value = nullptr;
  285. }
  286. };
  287. // -----------------------------------------------------
  288. class Lv2Plugin : public CarlaPlugin,
  289. CarlaPluginUi::CloseCallback
  290. {
  291. public:
  292. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  293. : CarlaPlugin(engine, id),
  294. fHandle(nullptr),
  295. fHandle2(nullptr),
  296. fDescriptor(nullptr),
  297. fRdfDescriptor(nullptr),
  298. fAudioInBuffers(nullptr),
  299. fAudioOutBuffers(nullptr),
  300. fCvInBuffers(nullptr),
  301. fCvOutBuffers(nullptr),
  302. fParamBuffers(nullptr),
  303. fFirstActive(true)
  304. {
  305. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  306. carla_fill<LV2_Feature*>(fFeatures, kFeatureCountAll+1, nullptr);
  307. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  308. fCustomURIDs.append(nullptr);
  309. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  310. # pragma GCC diagnostic push
  311. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  312. #endif
  313. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  314. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  315. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  316. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  317. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  318. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  319. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  320. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  321. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  322. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  323. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  324. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  325. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  326. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  327. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  328. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  329. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  330. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  331. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
  332. # pragma GCC diagnostic pop
  333. #endif
  334. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  335. }
  336. ~Lv2Plugin() override
  337. {
  338. carla_debug("Lv2Plugin::~Lv2Plugin()");
  339. // close UI
  340. if (fUi.type != UI::TYPE_NULL)
  341. {
  342. showCustomUI(false);
  343. if (fUi.type == UI::TYPE_OSC)
  344. {
  345. pData->osc.thread.stop(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  346. }
  347. else
  348. {
  349. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  350. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  351. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  352. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  353. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  354. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  355. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  356. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  357. fUi.descriptor = nullptr;
  358. pData->uiLibClose();
  359. }
  360. if (fUi.title != nullptr)
  361. {
  362. delete[] fUi.title;
  363. fUi.title = nullptr;
  364. }
  365. if (fUi.window != nullptr)
  366. {
  367. delete fUi.window;
  368. fUi.window = nullptr;
  369. }
  370. fUi.rdfDescriptor = nullptr;
  371. }
  372. pData->singleMutex.lock();
  373. pData->masterMutex.lock();
  374. if (pData->client != nullptr && pData->client->isActive())
  375. pData->client->deactivate();
  376. if (pData->active)
  377. {
  378. deactivate();
  379. pData->active = false;
  380. }
  381. if (fDescriptor != nullptr)
  382. {
  383. if (fDescriptor->cleanup != nullptr)
  384. {
  385. if (fHandle != nullptr)
  386. fDescriptor->cleanup(fHandle);
  387. if (fHandle2 != nullptr)
  388. fDescriptor->cleanup(fHandle2);
  389. }
  390. fHandle = nullptr;
  391. fHandle2 = nullptr;
  392. fDescriptor = nullptr;
  393. }
  394. if (fRdfDescriptor != nullptr)
  395. {
  396. delete fRdfDescriptor;
  397. fRdfDescriptor = nullptr;
  398. }
  399. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  400. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  401. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  402. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  403. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  404. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  405. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  406. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  407. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  408. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  409. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  410. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  411. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  412. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  413. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  414. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  415. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  416. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  417. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  418. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  419. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  420. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  421. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  422. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  423. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  424. {
  425. if (fFeatures[i] != nullptr)
  426. {
  427. delete fFeatures[i];
  428. fFeatures[i] = nullptr;
  429. }
  430. }
  431. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  432. {
  433. const char*& uri(it.getValue());
  434. if (uri != nullptr)
  435. {
  436. delete[] uri;
  437. uri = nullptr;
  438. }
  439. }
  440. fCustomURIDs.clear();
  441. clearBuffers();
  442. }
  443. // -------------------------------------------------------------------
  444. // Information (base)
  445. PluginType getType() const noexcept override
  446. {
  447. return PLUGIN_LV2;
  448. }
  449. PluginCategory getCategory() const noexcept override
  450. {
  451. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  452. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  453. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  454. if (LV2_IS_DELAY(cat1, cat2))
  455. return PLUGIN_CATEGORY_DELAY;
  456. if (LV2_IS_DISTORTION(cat1, cat2))
  457. return PLUGIN_CATEGORY_OTHER;
  458. if (LV2_IS_DYNAMICS(cat1, cat2))
  459. return PLUGIN_CATEGORY_DYNAMICS;
  460. if (LV2_IS_EQ(cat1, cat2))
  461. return PLUGIN_CATEGORY_EQ;
  462. if (LV2_IS_FILTER(cat1, cat2))
  463. return PLUGIN_CATEGORY_FILTER;
  464. if (LV2_IS_GENERATOR(cat1, cat2))
  465. return PLUGIN_CATEGORY_SYNTH;
  466. if (LV2_IS_MODULATOR(cat1, cat2))
  467. return PLUGIN_CATEGORY_MODULATOR;
  468. if (LV2_IS_REVERB(cat1, cat2))
  469. return PLUGIN_CATEGORY_DELAY;
  470. if (LV2_IS_SIMULATOR(cat1, cat2))
  471. return PLUGIN_CATEGORY_OTHER;
  472. if (LV2_IS_SPATIAL(cat1, cat2))
  473. return PLUGIN_CATEGORY_OTHER;
  474. if (LV2_IS_SPECTRAL(cat1, cat2))
  475. return PLUGIN_CATEGORY_UTILITY;
  476. if (LV2_IS_UTILITY(cat1, cat2))
  477. return PLUGIN_CATEGORY_UTILITY;
  478. return CarlaPlugin::getCategory();
  479. }
  480. long getUniqueId() const noexcept override
  481. {
  482. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  483. return static_cast<long>(fRdfDescriptor->UniqueID);
  484. }
  485. // -------------------------------------------------------------------
  486. // Information (count)
  487. uint32_t getMidiInCount() const noexcept override
  488. {
  489. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  490. uint32_t count = 0;
  491. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  492. {
  493. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  494. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  495. count += 1;
  496. }
  497. return count;
  498. }
  499. uint32_t getMidiOutCount() const noexcept override
  500. {
  501. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  502. uint32_t count = 0;
  503. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  504. {
  505. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  506. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  507. count += 1;
  508. }
  509. return count;
  510. }
  511. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  512. {
  513. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  514. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  515. const int32_t rindex(pData->param.data[parameterId].rindex);
  516. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  517. {
  518. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  519. return port->ScalePointCount;
  520. }
  521. return 0;
  522. }
  523. // -------------------------------------------------------------------
  524. // Information (current data)
  525. // nothing
  526. // -------------------------------------------------------------------
  527. // Information (per-plugin data)
  528. unsigned int getOptionsAvailable() const noexcept override
  529. {
  530. const bool hasMidiIn(getMidiInCount() > 0);
  531. unsigned int options = 0x0;
  532. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  533. if (! (hasMidiIn || needsFixedBuffer()))
  534. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  535. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  536. {
  537. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  538. options |= PLUGIN_OPTION_FORCE_STEREO;
  539. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  540. options |= PLUGIN_OPTION_FORCE_STEREO;
  541. }
  542. if (hasMidiIn)
  543. {
  544. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  545. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  546. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  547. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  548. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  549. }
  550. return options;
  551. }
  552. float getParameterValue(const uint32_t parameterId) const noexcept override
  553. {
  554. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  555. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  556. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  557. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  558. return fParamBuffers[parameterId];
  559. }
  560. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  561. {
  562. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  563. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  564. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  565. const int32_t rindex(pData->param.data[parameterId].rindex);
  566. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  567. {
  568. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  569. if (scalePointId < port->ScalePointCount)
  570. {
  571. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  572. return portScalePoint->Value;
  573. }
  574. }
  575. return 0.0f;
  576. }
  577. void getLabel(char* const strBuf) const noexcept override
  578. {
  579. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  580. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  581. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  582. }
  583. void getMaker(char* const strBuf) const noexcept override
  584. {
  585. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  586. if (fRdfDescriptor->Author != nullptr)
  587. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  588. else
  589. CarlaPlugin::getMaker(strBuf);
  590. }
  591. void getCopyright(char* const strBuf) const noexcept override
  592. {
  593. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  594. if (fRdfDescriptor->License != nullptr)
  595. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  596. else
  597. CarlaPlugin::getCopyright(strBuf);
  598. }
  599. void getRealName(char* const strBuf) const noexcept override
  600. {
  601. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  602. if (fRdfDescriptor->Name != nullptr)
  603. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  604. else
  605. CarlaPlugin::getRealName(strBuf);
  606. }
  607. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  608. {
  609. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  610. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  611. const int32_t rindex(pData->param.data[parameterId].rindex);
  612. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  613. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  614. else
  615. CarlaPlugin::getParameterName(parameterId, strBuf);
  616. }
  617. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  618. {
  619. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  620. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  621. const int32_t rindex(pData->param.data[parameterId].rindex);
  622. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  623. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  624. else
  625. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  626. }
  627. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  628. {
  629. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  630. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  631. const int32_t rindex(pData->param.data[parameterId].rindex);
  632. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  633. {
  634. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  635. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  636. {
  637. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  638. return;
  639. }
  640. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  641. {
  642. switch (port->Unit.Unit)
  643. {
  644. case LV2_PORT_UNIT_BAR:
  645. std::strncpy(strBuf, "bars", STR_MAX);
  646. return;
  647. case LV2_PORT_UNIT_BEAT:
  648. std::strncpy(strBuf, "beats", STR_MAX);
  649. return;
  650. case LV2_PORT_UNIT_BPM:
  651. std::strncpy(strBuf, "BPM", STR_MAX);
  652. return;
  653. case LV2_PORT_UNIT_CENT:
  654. std::strncpy(strBuf, "ct", STR_MAX);
  655. return;
  656. case LV2_PORT_UNIT_CM:
  657. std::strncpy(strBuf, "cm", STR_MAX);
  658. return;
  659. case LV2_PORT_UNIT_COEF:
  660. std::strncpy(strBuf, "(coef)", STR_MAX);
  661. return;
  662. case LV2_PORT_UNIT_DB:
  663. std::strncpy(strBuf, "dB", STR_MAX);
  664. return;
  665. case LV2_PORT_UNIT_DEGREE:
  666. std::strncpy(strBuf, "deg", STR_MAX);
  667. return;
  668. case LV2_PORT_UNIT_FRAME:
  669. std::strncpy(strBuf, "frames", STR_MAX);
  670. return;
  671. case LV2_PORT_UNIT_HZ:
  672. std::strncpy(strBuf, "Hz", STR_MAX);
  673. return;
  674. case LV2_PORT_UNIT_INCH:
  675. std::strncpy(strBuf, "in", STR_MAX);
  676. return;
  677. case LV2_PORT_UNIT_KHZ:
  678. std::strncpy(strBuf, "kHz", STR_MAX);
  679. return;
  680. case LV2_PORT_UNIT_KM:
  681. std::strncpy(strBuf, "km", STR_MAX);
  682. return;
  683. case LV2_PORT_UNIT_M:
  684. std::strncpy(strBuf, "m", STR_MAX);
  685. return;
  686. case LV2_PORT_UNIT_MHZ:
  687. std::strncpy(strBuf, "MHz", STR_MAX);
  688. return;
  689. case LV2_PORT_UNIT_MIDINOTE:
  690. std::strncpy(strBuf, "note", STR_MAX);
  691. return;
  692. case LV2_PORT_UNIT_MILE:
  693. std::strncpy(strBuf, "mi", STR_MAX);
  694. return;
  695. case LV2_PORT_UNIT_MIN:
  696. std::strncpy(strBuf, "min", STR_MAX);
  697. return;
  698. case LV2_PORT_UNIT_MM:
  699. std::strncpy(strBuf, "mm", STR_MAX);
  700. return;
  701. case LV2_PORT_UNIT_MS:
  702. std::strncpy(strBuf, "ms", STR_MAX);
  703. return;
  704. case LV2_PORT_UNIT_OCT:
  705. std::strncpy(strBuf, "oct", STR_MAX);
  706. return;
  707. case LV2_PORT_UNIT_PC:
  708. std::strncpy(strBuf, "%", STR_MAX);
  709. return;
  710. case LV2_PORT_UNIT_S:
  711. std::strncpy(strBuf, "s", STR_MAX);
  712. return;
  713. case LV2_PORT_UNIT_SEMITONE:
  714. std::strncpy(strBuf, "semi", STR_MAX);
  715. return;
  716. }
  717. }
  718. }
  719. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  720. }
  721. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  722. {
  723. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  724. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  725. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  726. const int32_t rindex(pData->param.data[parameterId].rindex);
  727. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  728. {
  729. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  730. if (scalePointId < port->ScalePointCount)
  731. {
  732. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  733. if (portScalePoint->Label != nullptr)
  734. {
  735. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  736. return;
  737. }
  738. }
  739. }
  740. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  741. }
  742. // -------------------------------------------------------------------
  743. // Set data (state)
  744. void prepareForSave() override
  745. {
  746. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  747. if (fExt.state != nullptr && fExt.state->save != nullptr)
  748. {
  749. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  750. if (fHandle2 != nullptr)
  751. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  752. }
  753. }
  754. // -------------------------------------------------------------------
  755. // Set data (internal stuff)
  756. void setName(const char* const newName) override
  757. {
  758. CarlaPlugin::setName(newName);
  759. if (fUi.title == nullptr)
  760. return;
  761. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  762. delete[] fUi.title;
  763. fUi.title = carla_strdup(guiTitle.toUtf8().constData());
  764. if (fUi.window != nullptr)
  765. fUi.window->setTitle(fUi.title);
  766. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  767. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fUi.title;
  768. }
  769. // -------------------------------------------------------------------
  770. // Set data (plugin-specific stuff)
  771. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  772. {
  773. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  774. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  775. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  776. fParamBuffers[parameterId] = fixedValue;
  777. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  778. }
  779. #if 0
  780. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  781. {
  782. CARLA_ASSERT(fDescriptor != nullptr);
  783. CARLA_ASSERT(fHandle != nullptr);
  784. CARLA_ASSERT(type != nullptr);
  785. CARLA_ASSERT(key != nullptr);
  786. CARLA_ASSERT(value != nullptr);
  787. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  788. if (type == nullptr)
  789. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  790. if (key == nullptr)
  791. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  792. if (value == nullptr)
  793. return carla_stderr2("Lv2Plugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  794. CarlaPlugin::setCustomData(type, key, value, sendGui);
  795. // FIXME - we should only call this once, after all data is stored
  796. if (fExt.state != nullptr)
  797. {
  798. LV2_State_Status status;
  799. {
  800. const ScopedSingleProcessLocker spl(this, true);
  801. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  802. if (fHandle2 != nullptr)
  803. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  804. }
  805. switch (status)
  806. {
  807. case LV2_STATE_SUCCESS:
  808. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  809. break;
  810. case LV2_STATE_ERR_UNKNOWN:
  811. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  812. break;
  813. case LV2_STATE_ERR_BAD_TYPE:
  814. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  815. break;
  816. case LV2_STATE_ERR_BAD_FLAGS:
  817. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  818. break;
  819. case LV2_STATE_ERR_NO_FEATURE:
  820. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  821. break;
  822. case LV2_STATE_ERR_NO_PROPERTY:
  823. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  824. break;
  825. }
  826. }
  827. if (sendGui)
  828. {
  829. //CustomData cdata;
  830. //cdata.type = type;
  831. //cdata.key = key;
  832. //cdata.value = value;
  833. //uiTransferCustomData(&cdata);
  834. }
  835. }
  836. void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  837. {
  838. CARLA_ASSERT(fRdfDescriptor != nullptr);
  839. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount));
  840. CARLA_ASSERT(sendGui || sendOsc || sendCallback); // never call this from RT
  841. if (index < -1)
  842. index = -1;
  843. else if (index > static_cast<int32_t>(fRdfDescriptor->PresetCount))
  844. return;
  845. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  846. {
  847. if (const LilvState* state = gLv2World.getState(fRdfDescriptor->Presets[index].URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  848. {
  849. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  850. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  851. if (fHandle2 != nullptr)
  852. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  853. }
  854. }
  855. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  856. }
  857. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  858. {
  859. CARLA_ASSERT(fDescriptor != nullptr);
  860. CARLA_ASSERT(fHandle != nullptr);
  861. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count));
  862. if (index < -1)
  863. index = -1;
  864. else if (index > static_cast<int32_t>(pData->midiprog.count))
  865. return;
  866. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  867. {
  868. const uint32_t bank(pData->midiprog.data[index].bank);
  869. const uint32_t program(pData->midiprog.data[index].program);
  870. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  871. fExt.programs->select_program(fHandle, bank, program);
  872. if (fHandle2 != nullptr)
  873. fExt.programs->select_program(fHandle2, bank, program);
  874. }
  875. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  876. }
  877. #endif
  878. // -------------------------------------------------------------------
  879. // Set ui stuff
  880. void showCustomUI(const bool yesNo) override
  881. {
  882. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  883. if (! yesNo)
  884. pData->transientTryCounter = 0;
  885. if (fUi.type == UI::TYPE_OSC)
  886. {
  887. if (yesNo)
  888. {
  889. pData->osc.data.free();
  890. pData->osc.thread.start();
  891. }
  892. else
  893. {
  894. if (pData->osc.data.target != nullptr)
  895. {
  896. osc_send_hide(pData->osc.data);
  897. osc_send_quit(pData->osc.data);
  898. pData->osc.data.free();
  899. }
  900. pData->osc.thread.stop(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  901. }
  902. return;
  903. }
  904. // take some precautions
  905. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  906. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr,);
  907. if (yesNo)
  908. {
  909. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->instantiate != nullptr,);
  910. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->cleanup != nullptr,);
  911. }
  912. else
  913. {
  914. if (fUi.handle == nullptr)
  915. return;
  916. }
  917. if (yesNo)
  918. {
  919. if (fUi.handle == nullptr)
  920. {
  921. if (fUi.type == UI::TYPE_EMBED)
  922. {
  923. const char* msg = nullptr;
  924. switch (fUi.rdfDescriptor->Type)
  925. {
  926. case LV2_UI_GTK2:
  927. case LV2_UI_GTK3:
  928. case LV2_UI_QT4:
  929. case LV2_UI_QT5:
  930. case LV2_UI_EXTERNAL:
  931. case LV2_UI_OLD_EXTERNAL:
  932. msg = "Invalid UI type";
  933. break;
  934. case LV2_UI_COCOA:
  935. #ifdef CARLA_OS_MAC
  936. fUi.window = CarlaPluginUi::newCocoa(this);
  937. #else
  938. msg = "UI is for MacOS only";
  939. #endif
  940. break;
  941. case LV2_UI_WINDOWS:
  942. #ifdef CARLA_OS_WIN
  943. fUi.window = CarlaPluginUi::newWindows(this);
  944. #else
  945. msg = "UI is for Windows only";
  946. #endif
  947. break;
  948. case LV2_UI_X11:
  949. #ifdef HAVE_X11
  950. fUi.window = CarlaPluginUi::newX11(this);
  951. #else
  952. msg = "UI is only for systems with X11";
  953. #endif
  954. break;
  955. default:
  956. msg = "Unknown UI type";
  957. break;
  958. }
  959. if (fUi.window == nullptr)
  960. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  961. fUi.window->setTitle(fUi.title);
  962. fFeatures[kFeatureIdUiParent]->data = fUi.window->getPtr();
  963. }
  964. fUi.widget = nullptr;
  965. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  966. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  967. }
  968. CARLA_SAFE_ASSERT(fUi.handle != nullptr);
  969. CARLA_SAFE_ASSERT(fUi.type != UI::TYPE_EXTERNAL || fUi.widget != nullptr);
  970. if (fUi.handle == nullptr || (fUi.type == UI::TYPE_EXTERNAL && fUi.widget == nullptr))
  971. {
  972. fUi.widget = nullptr;
  973. if (fUi.handle != nullptr)
  974. {
  975. fUi.descriptor->cleanup(fUi.handle);
  976. fUi.handle = nullptr;
  977. }
  978. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  979. }
  980. updateUi();
  981. if (fUi.type == UI::TYPE_EMBED)
  982. {
  983. fUi.window->show();
  984. }
  985. else
  986. {
  987. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  988. pData->tryTransient();
  989. }
  990. }
  991. else
  992. {
  993. if (fUi.type == UI::TYPE_EMBED)
  994. {
  995. CARLA_SAFE_ASSERT(fUi.window != nullptr);
  996. if (fUi.window != nullptr)
  997. fUi.window->hide();
  998. }
  999. else
  1000. {
  1001. CARLA_SAFE_ASSERT(fUi.widget != nullptr);
  1002. if (fUi.widget != nullptr)
  1003. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  1004. }
  1005. fUi.descriptor->cleanup(fUi.handle);
  1006. fUi.handle = nullptr;
  1007. fUi.widget = nullptr;
  1008. }
  1009. }
  1010. void idle() override
  1011. {
  1012. if (! fAtomQueueOut.isEmpty())
  1013. {
  1014. char dumpBuf[fAtomQueueOut.getSize()];
  1015. Lv2AtomQueue tmpQueue;
  1016. tmpQueue.copyAndDumpDataFromQueue(fAtomQueueOut, dumpBuf);
  1017. CARLA_SAFE_ASSERT(! tmpQueue.isEmpty());
  1018. uint32_t portIndex;
  1019. const LV2_Atom* atom;
  1020. const bool hasPortEvent(fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr);
  1021. for (; tmpQueue.get(&atom, &portIndex);)
  1022. {
  1023. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  1024. {
  1025. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1026. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1027. }
  1028. else if (fUi.type == UI::TYPE_OSC)
  1029. {
  1030. if (pData->osc.data.target != nullptr)
  1031. {
  1032. QByteArray chunk((const char*)atom, static_cast<int>(lv2_atom_total_size(atom)));
  1033. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.toBase64().constData());
  1034. }
  1035. }
  1036. else
  1037. {
  1038. if (hasPortEvent)
  1039. fUi.descriptor->port_event(fUi.handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1040. }
  1041. }
  1042. }
  1043. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  1044. {
  1045. if (fUi.type == UI::TYPE_EMBED && fUi.window != nullptr)
  1046. fUi.window->idle();
  1047. else if (fUi.type == UI::TYPE_EXTERNAL && fUi.widget != nullptr)
  1048. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  1049. if (fExt.uiidle != nullptr && fExt.uiidle->idle(fUi.handle) != 0)
  1050. {
  1051. showCustomUI(false);
  1052. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1053. }
  1054. }
  1055. CarlaPlugin::idle();
  1056. }
  1057. // -------------------------------------------------------------------
  1058. // Plugin state
  1059. void reload() override
  1060. {
  1061. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1062. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1063. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1064. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1065. carla_debug("Lv2Plugin::reload() - start");
  1066. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1067. // Safely disable plugin for reload
  1068. const ScopedDisabler sd(this);
  1069. if (pData->active)
  1070. deactivate();
  1071. clearBuffers();
  1072. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1073. const uint32_t portCount(fRdfDescriptor->PortCount);
  1074. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1075. aIns = aOuts = cvIns = cvOuts = params = 0;
  1076. LinkedList<unsigned int> evIns, evOuts;
  1077. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1078. bool forcedStereoIn, forcedStereoOut;
  1079. forcedStereoIn = forcedStereoOut = false;
  1080. bool needsCtrlIn, needsCtrlOut;
  1081. needsCtrlIn = needsCtrlOut = false;
  1082. for (uint32_t i=0; i < portCount; ++i)
  1083. {
  1084. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1085. if (LV2_IS_PORT_AUDIO(portTypes))
  1086. {
  1087. if (LV2_IS_PORT_INPUT(portTypes))
  1088. aIns += 1;
  1089. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1090. aOuts += 1;
  1091. }
  1092. else if (LV2_IS_PORT_CV(portTypes))
  1093. {
  1094. if (LV2_IS_PORT_INPUT(portTypes))
  1095. cvIns += 1;
  1096. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1097. cvOuts += 1;
  1098. }
  1099. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1100. {
  1101. if (LV2_IS_PORT_INPUT(portTypes))
  1102. evIns.append(CARLA_EVENT_DATA_ATOM);
  1103. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1104. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1105. }
  1106. else if (LV2_IS_PORT_EVENT(portTypes))
  1107. {
  1108. if (LV2_IS_PORT_INPUT(portTypes))
  1109. evIns.append(CARLA_EVENT_DATA_EVENT);
  1110. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1111. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1112. }
  1113. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1114. {
  1115. if (LV2_IS_PORT_INPUT(portTypes))
  1116. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1117. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1118. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1119. }
  1120. else if (LV2_IS_PORT_CONTROL(portTypes))
  1121. params += 1;
  1122. }
  1123. recheckExtensions();
  1124. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1125. {
  1126. if (fHandle2 == nullptr)
  1127. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1128. if (fHandle2 != nullptr)
  1129. {
  1130. if (aIns == 1)
  1131. {
  1132. aIns = 2;
  1133. forcedStereoIn = true;
  1134. }
  1135. if (aOuts == 1)
  1136. {
  1137. aOuts = 2;
  1138. forcedStereoOut = true;
  1139. }
  1140. }
  1141. }
  1142. if (aIns > 0)
  1143. {
  1144. pData->audioIn.createNew(aIns);
  1145. fAudioInBuffers = new float*[aIns];
  1146. for (uint32_t i=0; i < aIns; ++i)
  1147. fAudioInBuffers[i] = nullptr;
  1148. }
  1149. if (aOuts > 0)
  1150. {
  1151. pData->audioOut.createNew(aOuts);
  1152. fAudioOutBuffers = new float*[aOuts];
  1153. needsCtrlIn = true;
  1154. for (uint32_t i=0; i < aOuts; ++i)
  1155. fAudioOutBuffers[i] = nullptr;
  1156. }
  1157. if (cvIns > 0)
  1158. {
  1159. fCvIn.createNew(cvIns);
  1160. fCvInBuffers = new float*[cvIns];
  1161. for (uint32_t i=0; i < cvIns; ++i)
  1162. fCvInBuffers[i] = nullptr;
  1163. }
  1164. if (cvOuts > 0)
  1165. {
  1166. fCvOut.createNew(cvOuts);
  1167. fCvOutBuffers = new float*[cvOuts];
  1168. for (uint32_t i=0; i < cvOuts; ++i)
  1169. fCvOutBuffers[i] = nullptr;
  1170. }
  1171. if (params > 0)
  1172. {
  1173. pData->param.createNew(params, true);
  1174. fParamBuffers = new float[params];
  1175. FLOAT_CLEAR(fParamBuffers, params);
  1176. }
  1177. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1178. {
  1179. fEventsIn.createNew(count);
  1180. for (uint32_t i=0; i < count; ++i)
  1181. {
  1182. const uint32_t& type(evIns.getAt(i));
  1183. if (type == CARLA_EVENT_DATA_ATOM)
  1184. {
  1185. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1186. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1187. }
  1188. else if (type == CARLA_EVENT_DATA_EVENT)
  1189. {
  1190. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1191. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1192. }
  1193. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1194. {
  1195. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1196. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1197. fEventsIn.data[i].midi.data = new unsigned char[eventBufferSize];
  1198. }
  1199. }
  1200. }
  1201. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1202. {
  1203. fEventsOut.createNew(count);
  1204. for (uint32_t i=0; i < count; ++i)
  1205. {
  1206. const uint32_t& type(evOuts.getAt(i));
  1207. if (type == CARLA_EVENT_DATA_ATOM)
  1208. {
  1209. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1210. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1211. }
  1212. else if (type == CARLA_EVENT_DATA_EVENT)
  1213. {
  1214. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1215. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1216. }
  1217. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1218. {
  1219. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1220. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1221. fEventsOut.data[i].midi.data = new unsigned char[eventBufferSize];
  1222. }
  1223. }
  1224. }
  1225. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1226. CarlaString portName;
  1227. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1228. {
  1229. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1230. portName.clear();
  1231. if (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_CV(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  1232. {
  1233. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1234. {
  1235. portName = pData->name;
  1236. portName += ":";
  1237. }
  1238. portName += fRdfDescriptor->Ports[i].Name;
  1239. portName.truncate(portNameSize);
  1240. }
  1241. if (LV2_IS_PORT_AUDIO(portTypes))
  1242. {
  1243. if (LV2_IS_PORT_INPUT(portTypes))
  1244. {
  1245. uint32_t j = iAudioIn++;
  1246. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1247. pData->audioIn.ports[j].rindex = i;
  1248. if (forcedStereoIn)
  1249. {
  1250. portName += "_2";
  1251. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1252. pData->audioIn.ports[1].rindex = i;
  1253. }
  1254. }
  1255. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1256. {
  1257. uint32_t j = iAudioOut++;
  1258. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1259. pData->audioOut.ports[j].rindex = i;
  1260. if (forcedStereoOut)
  1261. {
  1262. portName += "_2";
  1263. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1264. pData->audioOut.ports[1].rindex = i;
  1265. }
  1266. }
  1267. else
  1268. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1269. }
  1270. else if (LV2_IS_PORT_CV(portTypes))
  1271. {
  1272. if (LV2_IS_PORT_INPUT(portTypes))
  1273. {
  1274. uint32_t j = iCvIn++;
  1275. fCvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1276. fCvIn.ports[j].rindex = i;
  1277. }
  1278. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1279. {
  1280. uint32_t j = iCvOut++;
  1281. fCvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1282. fCvOut.ports[j].rindex = i;
  1283. }
  1284. else
  1285. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1286. }
  1287. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1288. {
  1289. if (LV2_IS_PORT_INPUT(portTypes))
  1290. {
  1291. uint32_t j = iEvIn++;
  1292. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1293. if (fHandle2 != nullptr)
  1294. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1295. fEventsIn.data[j].rindex = i;
  1296. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1297. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1298. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1299. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1300. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1301. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1302. if (evIns.count() == 1)
  1303. {
  1304. fEventsIn.ctrl = &fEventsIn.data[j];
  1305. fEventsIn.ctrlIndex = j;
  1306. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1307. needsCtrlIn = true;
  1308. }
  1309. else
  1310. {
  1311. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1312. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1313. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1314. {
  1315. fEventsIn.ctrl = &fEventsIn.data[j];
  1316. fEventsIn.ctrlIndex = j;
  1317. }
  1318. }
  1319. }
  1320. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1321. {
  1322. uint32_t j = iEvOut++;
  1323. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1324. if (fHandle2 != nullptr)
  1325. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1326. fEventsOut.data[j].rindex = i;
  1327. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1328. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1329. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1330. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1331. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1332. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1333. if (evOuts.count() == 1)
  1334. {
  1335. fEventsOut.ctrl = &fEventsOut.data[j];
  1336. fEventsOut.ctrlIndex = j;
  1337. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1338. needsCtrlOut = true;
  1339. }
  1340. else
  1341. {
  1342. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1343. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1344. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1345. {
  1346. fEventsOut.ctrl = &fEventsOut.data[j];
  1347. fEventsOut.ctrlIndex = j;
  1348. }
  1349. }
  1350. }
  1351. else
  1352. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1353. }
  1354. else if (LV2_IS_PORT_EVENT(portTypes))
  1355. {
  1356. if (LV2_IS_PORT_INPUT(portTypes))
  1357. {
  1358. uint32_t j = iEvIn++;
  1359. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1360. if (fHandle2 != nullptr)
  1361. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1362. fEventsIn.data[j].rindex = i;
  1363. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1364. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1365. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1366. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1367. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1368. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1369. if (evIns.count() == 1)
  1370. {
  1371. fEventsIn.ctrl = &fEventsIn.data[j];
  1372. fEventsIn.ctrlIndex = j;
  1373. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1374. needsCtrlIn = true;
  1375. }
  1376. else
  1377. {
  1378. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1379. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1380. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1381. {
  1382. fEventsIn.ctrl = &fEventsIn.data[j];
  1383. fEventsIn.ctrlIndex = j;
  1384. }
  1385. }
  1386. }
  1387. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1388. {
  1389. uint32_t j = iEvOut++;
  1390. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1391. if (fHandle2 != nullptr)
  1392. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1393. fEventsOut.data[j].rindex = i;
  1394. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1395. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1396. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1397. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1398. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1399. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1400. if (evOuts.count() == 1)
  1401. {
  1402. fEventsOut.ctrl = &fEventsOut.data[j];
  1403. fEventsOut.ctrlIndex = j;
  1404. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1405. needsCtrlOut = true;
  1406. }
  1407. else
  1408. {
  1409. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1410. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1411. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1412. {
  1413. fEventsOut.ctrl = &fEventsOut.data[j];
  1414. fEventsOut.ctrlIndex = j;
  1415. }
  1416. }
  1417. }
  1418. else
  1419. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1420. }
  1421. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1422. {
  1423. if (LV2_IS_PORT_INPUT(portTypes))
  1424. {
  1425. uint32_t j = iEvIn++;
  1426. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1427. if (fHandle2 != nullptr)
  1428. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1429. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1430. fEventsIn.data[j].rindex = i;
  1431. if (evIns.count() == 1)
  1432. {
  1433. needsCtrlIn = true;
  1434. fEventsIn.ctrl = &fEventsIn.data[j];
  1435. fEventsIn.ctrlIndex = j;
  1436. }
  1437. else
  1438. {
  1439. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1440. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1441. {
  1442. fEventsIn.ctrl = &fEventsIn.data[j];
  1443. fEventsIn.ctrlIndex = j;
  1444. }
  1445. }
  1446. }
  1447. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1448. {
  1449. uint32_t j = iEvOut++;
  1450. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1451. if (fHandle2 != nullptr)
  1452. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1453. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1454. fEventsOut.data[j].rindex = i;
  1455. if (evOuts.count() == 1)
  1456. {
  1457. needsCtrlOut = true;
  1458. fEventsOut.ctrl = &fEventsOut.data[j];
  1459. fEventsOut.ctrlIndex = j;
  1460. }
  1461. else
  1462. {
  1463. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1464. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1465. {
  1466. fEventsOut.ctrl = &fEventsOut.data[j];
  1467. fEventsOut.ctrlIndex = j;
  1468. }
  1469. }
  1470. }
  1471. else
  1472. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1473. }
  1474. else if (LV2_IS_PORT_CONTROL(portTypes))
  1475. {
  1476. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1477. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1478. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1479. uint32_t j = iCtrl++;
  1480. pData->param.data[j].hints = 0x0;
  1481. pData->param.data[j].index = static_cast<int32_t>(j);
  1482. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1483. pData->param.data[j].midiCC = -1;
  1484. pData->param.data[j].midiChannel = 0;
  1485. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  1486. float min, max, def, step, stepSmall, stepLarge;
  1487. // min value
  1488. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1489. min = portPoints.Minimum;
  1490. else
  1491. min = 0.0f;
  1492. // max value
  1493. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1494. max = portPoints.Maximum;
  1495. else
  1496. max = 1.0f;
  1497. if (min > max)
  1498. max = min;
  1499. // stupid hack for ir.lv2 (broken plugin)
  1500. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1501. {
  1502. min = 0.0f;
  1503. max = (float)0xffffff;
  1504. }
  1505. if (max - min == 0.0f)
  1506. {
  1507. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1508. max = min + 0.1f;
  1509. }
  1510. // default value
  1511. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1512. {
  1513. def = portPoints.Default;
  1514. }
  1515. else
  1516. {
  1517. // no default value
  1518. if (min < 0.0f && max > 0.0f)
  1519. def = 0.0f;
  1520. else
  1521. def = min;
  1522. }
  1523. if (def < min)
  1524. def = min;
  1525. else if (def > max)
  1526. def = max;
  1527. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1528. {
  1529. min *= sampleRate;
  1530. max *= sampleRate;
  1531. def *= sampleRate;
  1532. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1533. }
  1534. if (LV2_IS_PORT_TOGGLED(portProps))
  1535. {
  1536. step = max - min;
  1537. stepSmall = step;
  1538. stepLarge = step;
  1539. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1540. }
  1541. else if (LV2_IS_PORT_INTEGER(portProps))
  1542. {
  1543. step = 1.0f;
  1544. stepSmall = 1.0f;
  1545. stepLarge = 10.0f;
  1546. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1547. }
  1548. else
  1549. {
  1550. float range = max - min;
  1551. step = range/100.0f;
  1552. stepSmall = range/1000.0f;
  1553. stepLarge = range/10.0f;
  1554. }
  1555. if (LV2_IS_PORT_INPUT(portTypes))
  1556. {
  1557. pData->param.data[j].type = PARAMETER_INPUT;
  1558. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1559. {
  1560. carla_stderr("Plugin has latency input port, this should not happen!");
  1561. }
  1562. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1563. {
  1564. def = sampleRate;
  1565. step = 1.0f;
  1566. stepSmall = 1.0f;
  1567. stepLarge = 1.0f;
  1568. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1569. }
  1570. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1571. {
  1572. pData->param.special[j] = PARAMETER_SPECIAL_LV2_FREEWHEEL;
  1573. }
  1574. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1575. {
  1576. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1577. }
  1578. else
  1579. {
  1580. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1581. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1582. needsCtrlIn = true;
  1583. }
  1584. // MIDI CC value
  1585. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1586. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1587. {
  1588. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1589. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  1590. }
  1591. }
  1592. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1593. {
  1594. pData->param.data[j].type = PARAMETER_OUTPUT;
  1595. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1596. {
  1597. min = 0.0f;
  1598. max = sampleRate;
  1599. def = 0.0f;
  1600. step = 1.0f;
  1601. stepSmall = 1.0f;
  1602. stepLarge = 1.0f;
  1603. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1604. }
  1605. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1606. {
  1607. def = sampleRate;
  1608. step = 1.0f;
  1609. stepSmall = 1.0f;
  1610. stepLarge = 1.0f;
  1611. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1612. }
  1613. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1614. {
  1615. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1616. }
  1617. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1618. {
  1619. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1620. }
  1621. else
  1622. {
  1623. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1624. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1625. needsCtrlOut = true;
  1626. }
  1627. }
  1628. else
  1629. {
  1630. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1631. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1632. }
  1633. // extra parameter hints
  1634. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1635. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1636. if (LV2_IS_PORT_TRIGGER(portProps))
  1637. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1638. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1639. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1640. if (LV2_IS_PORT_ENUMERATION(portProps))
  1641. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1642. // check if parameter is not enabled or automable
  1643. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1644. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1645. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1646. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1647. pData->param.ranges[j].min = min;
  1648. pData->param.ranges[j].max = max;
  1649. pData->param.ranges[j].def = def;
  1650. pData->param.ranges[j].step = step;
  1651. pData->param.ranges[j].stepSmall = stepSmall;
  1652. pData->param.ranges[j].stepLarge = stepLarge;
  1653. // Start parameters in their default values (except freewheel, which is off by default)
  1654. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1655. fParamBuffers[j] = min;
  1656. else
  1657. fParamBuffers[j] = def;
  1658. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1659. if (fHandle2 != nullptr)
  1660. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1661. }
  1662. else
  1663. {
  1664. // Port Type not supported, but it's optional anyway
  1665. fDescriptor->connect_port(fHandle, i, nullptr);
  1666. if (fHandle2 != nullptr)
  1667. fDescriptor->connect_port(fHandle2, i, nullptr);
  1668. }
  1669. }
  1670. if (needsCtrlIn)
  1671. {
  1672. portName.clear();
  1673. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1674. {
  1675. portName = pData->name;
  1676. portName += ":";
  1677. }
  1678. portName += "events-in";
  1679. portName.truncate(portNameSize);
  1680. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1681. }
  1682. if (needsCtrlOut)
  1683. {
  1684. portName.clear();
  1685. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1686. {
  1687. portName = pData->name;
  1688. portName += ":";
  1689. }
  1690. portName += "events-out";
  1691. portName.truncate(portNameSize);
  1692. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1693. }
  1694. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1695. fAtomQueueIn.createBuffer(eventBufferSize);
  1696. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1697. fAtomQueueOut.createBuffer(eventBufferSize);
  1698. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1699. fEventsIn.ctrl->port = pData->event.portIn;
  1700. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1701. fEventsOut.ctrl->port = pData->event.portOut;
  1702. if (forcedStereoIn || forcedStereoOut)
  1703. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1704. else
  1705. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1706. // plugin hints
  1707. pData->hints = 0x0;
  1708. if (isRealtimeSafe())
  1709. pData->hints |= PLUGIN_IS_RTSAFE;
  1710. if (fUi.type != UI::TYPE_NULL)
  1711. {
  1712. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1713. if (fUi.type == UI::TYPE_EMBED)
  1714. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1715. }
  1716. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1717. pData->hints |= PLUGIN_IS_SYNTH;
  1718. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1719. pData->hints |= PLUGIN_CAN_DRYWET;
  1720. if (aOuts > 0)
  1721. pData->hints |= PLUGIN_CAN_VOLUME;
  1722. if (aOuts >= 2 && aOuts % 2 == 0)
  1723. pData->hints |= PLUGIN_CAN_BALANCE;
  1724. // extra plugin hints
  1725. pData->extraHints = 0x0;
  1726. if (fExt.state != nullptr || fExt.worker != nullptr)
  1727. {
  1728. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1729. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1730. }
  1731. else
  1732. {
  1733. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1734. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1735. }
  1736. bufferSizeChanged(pData->engine->getBufferSize());
  1737. reloadPrograms(true);
  1738. // check latency
  1739. // TODO
  1740. evIns.clear();
  1741. evOuts.clear();
  1742. if (pData->active)
  1743. activate();
  1744. carla_debug("Lv2Plugin::reload() - end");
  1745. }
  1746. #if 0
  1747. void reloadPrograms(const bool init) override
  1748. {
  1749. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  1750. uint32_t i, oldCount = pData->midiprog.count;
  1751. const int32_t current = pData->midiprog.current;
  1752. // special LV2 programs handling
  1753. if (init)
  1754. {
  1755. pData->prog.clear();
  1756. const uint32_t count(fRdfDescriptor->PresetCount);
  1757. if (count > 0)
  1758. {
  1759. pData->prog.createNew(count);
  1760. for (i=0; i < count; ++i)
  1761. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1762. }
  1763. }
  1764. // Delete old programs
  1765. pData->midiprog.clear();
  1766. // Query new programs
  1767. uint32_t count = 0;
  1768. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1769. {
  1770. while (fExt.programs->get_program(fHandle, count))
  1771. ++count;
  1772. }
  1773. if (count > 0)
  1774. {
  1775. pData->midiprog.createNew(count);
  1776. // Update data
  1777. for (i=0; i < count; ++i)
  1778. {
  1779. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1780. CARLA_ASSERT(pdesc != nullptr);
  1781. CARLA_ASSERT(pdesc->name != nullptr);
  1782. pData->midiprog.data[i].bank = pdesc->bank;
  1783. pData->midiprog.data[i].program = pdesc->program;
  1784. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1785. }
  1786. }
  1787. #ifndef BUILD_BRIDGE
  1788. // Update OSC Names
  1789. if (pData->engine->isOscControlRegistered())
  1790. {
  1791. pData->engine->osc_send_control_set_midi_program_count(fId, count);
  1792. for (i=0; i < count; ++i)
  1793. pData->engine->osc_send_control_set_midi_program_data(fId, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  1794. }
  1795. #endif
  1796. if (init)
  1797. {
  1798. if (count > 0)
  1799. {
  1800. setMidiProgram(0, false, false, false);
  1801. }
  1802. else
  1803. {
  1804. // load default state
  1805. if (const LilvState* state = gLv2World.getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1806. {
  1807. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1808. if (fHandle2 != nullptr)
  1809. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1810. }
  1811. }
  1812. }
  1813. else
  1814. {
  1815. // Check if current program is invalid
  1816. bool programChanged = false;
  1817. if (count == oldCount+1)
  1818. {
  1819. // one midi program added, probably created by user
  1820. pData->midiprog.current = oldCount;
  1821. programChanged = true;
  1822. }
  1823. else if (current < 0 && count > 0)
  1824. {
  1825. // programs exist now, but not before
  1826. pData->midiprog.current = 0;
  1827. programChanged = true;
  1828. }
  1829. else if (current >= 0 && count == 0)
  1830. {
  1831. // programs existed before, but not anymore
  1832. pData->midiprog.current = -1;
  1833. programChanged = true;
  1834. }
  1835. else if (current >= static_cast<int32_t>(count))
  1836. {
  1837. // current midi program > count
  1838. pData->midiprog.current = 0;
  1839. programChanged = true;
  1840. }
  1841. else
  1842. {
  1843. // no change
  1844. pData->midiprog.current = current;
  1845. }
  1846. if (programChanged)
  1847. setMidiProgram(pData->midiprog.current, true, true, true);
  1848. pData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  1849. }
  1850. }
  1851. #endif
  1852. // -------------------------------------------------------------------
  1853. // Plugin processing
  1854. void activate() noexcept override
  1855. {
  1856. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1857. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1858. if (fDescriptor->activate != nullptr)
  1859. {
  1860. try {
  1861. fDescriptor->activate(fHandle);
  1862. } catch(...) {}
  1863. if (fHandle2 != nullptr)
  1864. {
  1865. try {
  1866. fDescriptor->activate(fHandle2);
  1867. } catch(...) {}
  1868. }
  1869. }
  1870. fFirstActive = true;
  1871. }
  1872. void deactivate() noexcept override
  1873. {
  1874. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1875. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1876. if (fDescriptor->deactivate != nullptr)
  1877. {
  1878. try {
  1879. fDescriptor->deactivate(fHandle);
  1880. } catch(...) {}
  1881. if (fHandle2 != nullptr)
  1882. {
  1883. try {
  1884. fDescriptor->deactivate(fHandle2);
  1885. } catch(...) {}
  1886. }
  1887. }
  1888. }
  1889. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1890. {
  1891. // --------------------------------------------------------------------------------------------------------
  1892. // Check if active
  1893. if (! pData->active)
  1894. {
  1895. // disable any output sound
  1896. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1897. FLOAT_CLEAR(outBuffer[i], frames);
  1898. return;
  1899. }
  1900. // --------------------------------------------------------------------------------------------------------
  1901. // Event itenerators from different APIs (input)
  1902. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1903. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1904. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1905. for (uint32_t i=0; i < fEventsIn.count; ++i)
  1906. {
  1907. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1908. {
  1909. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1910. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1911. }
  1912. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1913. {
  1914. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1915. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1916. }
  1917. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1918. {
  1919. fEventsIn.data[i].midi.event_count = 0;
  1920. fEventsIn.data[i].midi.size = 0;
  1921. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  1922. evInMidiStates[i].frame_count = frames;
  1923. evInMidiStates[i].position = 0;
  1924. }
  1925. }
  1926. for (uint32_t i=0; i < fEventsOut.count; ++i)
  1927. {
  1928. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1929. {
  1930. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1931. }
  1932. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1933. {
  1934. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1935. }
  1936. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1937. {
  1938. // not needed
  1939. }
  1940. }
  1941. CARLA_PROCESS_CONTINUE_CHECK;
  1942. // --------------------------------------------------------------------------------------------------------
  1943. // Check if needs reset
  1944. if (pData->needsReset)
  1945. {
  1946. uint8_t midiData[3] = { 0 };
  1947. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1948. {
  1949. const uint32_t j = fEventsIn.ctrlIndex;
  1950. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1951. {
  1952. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  1953. {
  1954. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1955. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1956. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1957. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1958. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1959. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1960. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1961. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1962. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1963. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1964. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1965. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1966. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1967. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1968. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1969. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1970. }
  1971. }
  1972. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1973. {
  1974. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1975. {
  1976. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  1977. midiData[1] = k;
  1978. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1979. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1980. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1981. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1982. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1983. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  1984. }
  1985. }
  1986. }
  1987. if (pData->latency > 0)
  1988. {
  1989. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1990. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  1991. }
  1992. pData->needsReset = false;
  1993. }
  1994. // --------------------------------------------------------------------------------------------------------
  1995. // TimeInfo
  1996. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1997. if (fFirstActive || fLastTimeInfo != timeInfo)
  1998. {
  1999. bool doPostRt;
  2000. int32_t rindex;
  2001. // update input ports
  2002. for (uint32_t k=0; k < pData->param.count; ++k)
  2003. {
  2004. if (pData->param.data[k].type != PARAMETER_INPUT)
  2005. continue;
  2006. if (pData->param.special[k] != PARAMETER_SPECIAL_LV2_TIME)
  2007. continue;
  2008. doPostRt = false;
  2009. rindex = pData->param.data[k].rindex;
  2010. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2011. switch (fRdfDescriptor->Ports[rindex].Designation)
  2012. {
  2013. // Non-BBT
  2014. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2015. if (fLastTimeInfo.playing != timeInfo.playing)
  2016. {
  2017. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2018. doPostRt = true;
  2019. }
  2020. break;
  2021. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2022. if (fLastTimeInfo.frame != timeInfo.frame)
  2023. {
  2024. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2025. doPostRt = true;
  2026. }
  2027. break;
  2028. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2029. break;
  2030. // BBT
  2031. case LV2_PORT_DESIGNATION_TIME_BAR:
  2032. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2033. {
  2034. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2035. doPostRt = true;
  2036. }
  2037. break;
  2038. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2039. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2040. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2041. {
  2042. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2043. doPostRt = true;
  2044. }
  2045. break;
  2046. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2047. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2048. {
  2049. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2050. doPostRt = true;
  2051. }
  2052. break;
  2053. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2054. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2055. {
  2056. fParamBuffers[k] = timeInfo.bbt.beatType;
  2057. doPostRt = true;
  2058. }
  2059. break;
  2060. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2061. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2062. {
  2063. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2064. doPostRt = true;
  2065. }
  2066. break;
  2067. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2068. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2069. {
  2070. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2071. doPostRt = true;
  2072. }
  2073. break;
  2074. }
  2075. if (doPostRt)
  2076. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2077. }
  2078. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2079. {
  2080. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2081. continue;
  2082. uint8_t timeInfoBuf[256];
  2083. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2084. LV2_Atom_Forge_Frame forgeFrame;
  2085. lv2_atom_forge_object(&fAtomForge, &forgeFrame, 1, CARLA_URI_MAP_ID_TIME_POSITION);
  2086. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2087. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2088. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2089. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2090. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2091. {
  2092. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2093. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2094. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2095. lv2_atom_forge_float(&fAtomForge, static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat)));
  2096. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2097. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2098. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2099. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2100. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2101. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2102. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2103. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2104. }
  2105. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2106. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2107. CARLA_ASSERT(atom->size < 256);
  2108. }
  2109. pData->postRtEvents.trySplice();
  2110. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2111. CARLA_PROCESS_CONTINUE_CHECK;
  2112. }
  2113. // --------------------------------------------------------------------------------------------------------
  2114. // CV ports
  2115. float* cvInBuf[fCvIn.count /*> 0 ? fCvIn.count : 1*/];
  2116. float* cvOutBuf[fCvOut.count /*> 0 ? fCvOut.count : 1*/];
  2117. for (uint32_t i=0; i < fCvIn.count; ++i)
  2118. {
  2119. CARLA_SAFE_ASSERT_CONTINUE(fCvIn.ports[i].port != nullptr);
  2120. cvInBuf[i] = fCvIn.ports[i].port->getBuffer();
  2121. }
  2122. for (uint32_t i=0; i < fCvOut.count; ++i)
  2123. {
  2124. CARLA_SAFE_ASSERT_CONTINUE(fCvOut.ports[i].port != nullptr);
  2125. cvOutBuf[i] = fCvOut.ports[i].port->getBuffer();
  2126. }
  2127. // --------------------------------------------------------------------------------------------------------
  2128. // Event Input and Processing
  2129. if (fEventsIn.ctrl != nullptr)
  2130. {
  2131. // ----------------------------------------------------------------------------------------------------
  2132. // Message Input
  2133. if (fAtomQueueIn.tryLock())
  2134. {
  2135. if (! fAtomQueueIn.isEmpty())
  2136. {
  2137. const LV2_Atom* atom;
  2138. uint32_t portIndex;
  2139. const uint32_t j = fEventsIn.ctrlIndex;
  2140. for (; fAtomQueueIn.get(&atom, &portIndex);)
  2141. {
  2142. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2143. {
  2144. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2145. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2146. }
  2147. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2148. {
  2149. carla_stdout("Event input buffer full, at least 1 message lost");
  2150. continue;
  2151. }
  2152. }
  2153. }
  2154. fAtomQueueIn.unlock();
  2155. }
  2156. // ----------------------------------------------------------------------------------------------------
  2157. // MIDI Input (External)
  2158. if (pData->extNotes.mutex.tryLock())
  2159. {
  2160. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2161. {
  2162. // does not handle MIDI
  2163. pData->extNotes.data.clear();
  2164. }
  2165. else
  2166. {
  2167. const uint32_t j = fEventsIn.ctrlIndex;
  2168. for (; ! pData->extNotes.data.isEmpty();)
  2169. {
  2170. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  2171. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2172. uint8_t midiEvent[3];
  2173. midiEvent[0] = static_cast<uint8_t>(note.channel + (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF);
  2174. midiEvent[1] = note.note;
  2175. midiEvent[2] = note.velo;
  2176. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2177. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2178. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2179. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2180. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2181. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2182. }
  2183. }
  2184. pData->extNotes.mutex.unlock();
  2185. } // End of MIDI Input (External)
  2186. // ----------------------------------------------------------------------------------------------------
  2187. // Event Input (System)
  2188. bool allNotesOffSent = false;
  2189. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2190. uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2191. uint32_t startTime = 0;
  2192. uint32_t timeOffset = 0;
  2193. uint32_t nextBankId;
  2194. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2195. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2196. else
  2197. nextBankId = 0;
  2198. for (uint32_t i=0; i < numEvents; ++i)
  2199. {
  2200. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2201. if (event.time >= frames)
  2202. continue;
  2203. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2204. if (isSampleAccurate && event.time > timeOffset)
  2205. {
  2206. if (processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, event.time - timeOffset, timeOffset))
  2207. {
  2208. startTime = 0;
  2209. timeOffset = event.time;
  2210. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2211. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2212. else
  2213. nextBankId = 0;
  2214. // reset iters
  2215. const uint32_t j = fEventsIn.ctrlIndex;
  2216. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2217. {
  2218. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2219. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2220. }
  2221. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2222. {
  2223. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2224. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2225. }
  2226. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2227. {
  2228. fEventsIn.data[j].midi.event_count = 0;
  2229. fEventsIn.data[j].midi.size = 0;
  2230. evInMidiStates[j].position = event.time;
  2231. }
  2232. }
  2233. else
  2234. startTime += timeOffset;
  2235. }
  2236. switch (event.type)
  2237. {
  2238. case kEngineEventTypeNull:
  2239. break;
  2240. case kEngineEventTypeControl: {
  2241. const EngineControlEvent& ctrlEvent(event.ctrl);
  2242. switch (ctrlEvent.type)
  2243. {
  2244. case kEngineControlEventTypeNull:
  2245. break;
  2246. case kEngineControlEventTypeParameter: {
  2247. #ifndef BUILD_BRIDGE
  2248. // Control backend stuff
  2249. if (event.channel == pData->ctrlChannel)
  2250. {
  2251. float value;
  2252. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2253. {
  2254. value = ctrlEvent.value;
  2255. setDryWet(value, false, false);
  2256. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2257. break;
  2258. }
  2259. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2260. {
  2261. value = ctrlEvent.value*127.0f/100.0f;
  2262. setVolume(value, false, false);
  2263. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2264. break;
  2265. }
  2266. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2267. {
  2268. float left, right;
  2269. value = ctrlEvent.value/0.5f - 1.0f;
  2270. if (value < 0.0f)
  2271. {
  2272. left = -1.0f;
  2273. right = (value*2.0f)+1.0f;
  2274. }
  2275. else if (value > 0.0f)
  2276. {
  2277. left = (value*2.0f)-1.0f;
  2278. right = 1.0f;
  2279. }
  2280. else
  2281. {
  2282. left = -1.0f;
  2283. right = 1.0f;
  2284. }
  2285. setBalanceLeft(left, false, false);
  2286. setBalanceRight(right, false, false);
  2287. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2288. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2289. break;
  2290. }
  2291. }
  2292. #endif
  2293. // Control plugin parameters
  2294. uint32_t k;
  2295. for (k=0; k < pData->param.count; ++k)
  2296. {
  2297. if (pData->param.data[k].midiChannel != event.channel)
  2298. continue;
  2299. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2300. continue;
  2301. if (pData->param.data[k].type != PARAMETER_INPUT)
  2302. continue;
  2303. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2304. continue;
  2305. float value;
  2306. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2307. {
  2308. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2309. }
  2310. else
  2311. {
  2312. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2313. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2314. value = std::rint(value);
  2315. }
  2316. setParameterValue(k, value, false, false, false);
  2317. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2318. break;
  2319. }
  2320. // check if event is already handled
  2321. if (k != pData->param.count)
  2322. break;
  2323. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2324. {
  2325. uint8_t midiData[3];
  2326. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2327. midiData[1] = static_cast<uint8_t>(ctrlEvent.param);
  2328. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2329. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2330. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2331. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2332. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2333. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2334. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0.0, 3, midiData);
  2335. }
  2336. break;
  2337. } // case kEngineControlEventTypeParameter
  2338. case kEngineControlEventTypeMidiBank:
  2339. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2340. nextBankId = ctrlEvent.param;
  2341. break;
  2342. case kEngineControlEventTypeMidiProgram:
  2343. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2344. {
  2345. const uint32_t nextProgramId = ctrlEvent.param;
  2346. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2347. {
  2348. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2349. {
  2350. const int32_t index(static_cast<int32_t>(k));
  2351. setMidiProgram(index, false, false, false);
  2352. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2353. break;
  2354. }
  2355. }
  2356. }
  2357. break;
  2358. case kEngineControlEventTypeAllSoundOff:
  2359. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2360. {
  2361. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2362. uint8_t midiData[3];
  2363. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2364. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2365. midiData[2] = 0;
  2366. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2367. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2368. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2369. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2370. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2371. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2372. }
  2373. break;
  2374. case kEngineControlEventTypeAllNotesOff:
  2375. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2376. {
  2377. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2378. {
  2379. allNotesOffSent = true;
  2380. sendMidiAllNotesOffToCallback();
  2381. }
  2382. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2383. uint8_t midiData[3];
  2384. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2385. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2386. midiData[2] = 0;
  2387. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2388. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2389. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2390. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2391. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2392. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2393. }
  2394. break;
  2395. } // switch (ctrlEvent.type)
  2396. break;
  2397. } // case kEngineEventTypeControl
  2398. case kEngineEventTypeMidi:
  2399. {
  2400. const EngineMidiEvent& midiEvent(event.midi);
  2401. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  2402. uint8_t channel = event.channel;
  2403. uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2404. // Fix bad note-off (per LV2 spec)
  2405. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  2406. status = MIDI_STATUS_NOTE_OFF;
  2407. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2408. continue;
  2409. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2410. continue;
  2411. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2412. continue;
  2413. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2414. continue;
  2415. const uint32_t j = fEventsIn.ctrlIndex;
  2416. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2417. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2418. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2419. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2420. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2421. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiEvent.data);
  2422. if (status == MIDI_STATUS_NOTE_ON)
  2423. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2424. else if (status == MIDI_STATUS_NOTE_OFF)
  2425. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2426. break;
  2427. } // case kEngineEventTypeMidi
  2428. } // switch (event.type)
  2429. }
  2430. pData->postRtEvents.trySplice();
  2431. if (frames > timeOffset)
  2432. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames - timeOffset, timeOffset);
  2433. } // End of Event Input and Processing
  2434. // --------------------------------------------------------------------------------------------------------
  2435. // Plugin processing (no events)
  2436. else
  2437. {
  2438. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames, 0);
  2439. } // End of Plugin processing (no events)
  2440. CARLA_PROCESS_CONTINUE_CHECK;
  2441. // --------------------------------------------------------------------------------------------------------
  2442. // MIDI Output
  2443. if (fEventsOut.ctrl != nullptr)
  2444. {
  2445. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2446. {
  2447. const LV2_Atom_Event* ev;
  2448. LV2_Atom_Buffer_Iterator iter;
  2449. uint8_t* data;
  2450. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2451. for (;;)
  2452. {
  2453. data = nullptr;
  2454. ev = lv2_atom_buffer_get(&iter, &data);
  2455. if (ev == nullptr || data == nullptr)
  2456. break;
  2457. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2458. {
  2459. if (fEventsOut.ctrl->port != nullptr)
  2460. {
  2461. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2462. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2463. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2464. }
  2465. }
  2466. else if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2467. {
  2468. fAtomQueueOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2469. }
  2470. lv2_atom_buffer_increment(&iter);
  2471. }
  2472. }
  2473. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2474. {
  2475. const LV2_Event* ev;
  2476. LV2_Event_Iterator iter;
  2477. uint8_t* data;
  2478. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2479. for (;;)
  2480. {
  2481. data = nullptr;
  2482. ev = lv2_event_get(&iter, &data);
  2483. if (ev == nullptr || data == nullptr)
  2484. break;
  2485. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2486. {
  2487. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2488. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2489. }
  2490. lv2_event_increment(&iter);
  2491. }
  2492. }
  2493. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2494. {
  2495. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2496. uint32_t eventSize;
  2497. double eventTime;
  2498. unsigned char* eventData;
  2499. for (;;)
  2500. {
  2501. eventSize = 0;
  2502. eventTime = 0.0;
  2503. eventData = nullptr;
  2504. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2505. if (eventData == nullptr || eventSize == 0)
  2506. break;
  2507. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2508. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2509. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2510. lv2midi_step(&state);
  2511. }
  2512. }
  2513. }
  2514. // --------------------------------------------------------------------------------------------------------
  2515. // Control Output
  2516. if (pData->event.portOut != nullptr)
  2517. {
  2518. uint8_t channel;
  2519. uint16_t param;
  2520. float value;
  2521. for (uint32_t k=0; k < pData->param.count; ++k)
  2522. {
  2523. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2524. continue;
  2525. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2526. if (pData->param.data[k].midiCC > 0)
  2527. {
  2528. channel = pData->param.data[k].midiChannel;
  2529. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2530. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2531. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2532. }
  2533. }
  2534. } // End of Control Output
  2535. CARLA_PROCESS_CONTINUE_CHECK;
  2536. // --------------------------------------------------------------------------------------------------------
  2537. // Final work
  2538. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2539. {
  2540. fExt.worker->end_run(fHandle);
  2541. if (fHandle2 != nullptr)
  2542. fExt.worker->end_run(fHandle2);
  2543. }
  2544. fFirstActive = false;
  2545. // --------------------------------------------------------------------------------------------------------
  2546. }
  2547. bool processSingle(float** const audioInBuf, float** const audioOutBuf, float** const cvInBuf, float** const cvOutBuf, const uint32_t frames, const uint32_t timeOffset)
  2548. {
  2549. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2550. if (pData->audioIn.count > 0)
  2551. {
  2552. CARLA_SAFE_ASSERT_RETURN(audioInBuf != nullptr, false);
  2553. }
  2554. if (pData->audioOut.count > 0)
  2555. {
  2556. CARLA_SAFE_ASSERT_RETURN(audioOutBuf != nullptr, false);
  2557. }
  2558. if (fCvIn.count > 0)
  2559. {
  2560. CARLA_SAFE_ASSERT_RETURN(cvInBuf != nullptr, false);
  2561. }
  2562. if (fCvOut.count > 0)
  2563. {
  2564. CARLA_SAFE_ASSERT_RETURN(cvOutBuf != nullptr, false);
  2565. }
  2566. // --------------------------------------------------------------------------------------------------------
  2567. // Try lock, silence otherwise
  2568. if (pData->engine->isOffline())
  2569. {
  2570. pData->singleMutex.lock();
  2571. }
  2572. else if (! pData->singleMutex.tryLock())
  2573. {
  2574. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2575. {
  2576. for (uint32_t k=0; k < frames; ++k)
  2577. audioOutBuf[i][k+timeOffset] = 0.0f;
  2578. }
  2579. return false;
  2580. }
  2581. // --------------------------------------------------------------------------------------------------------
  2582. // Set audio buffers
  2583. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2584. FLOAT_COPY(fAudioInBuffers[i], audioInBuf[i]+timeOffset, frames);
  2585. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2586. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  2587. // --------------------------------------------------------------------------------------------------------
  2588. // Set CV buffers
  2589. for (uint32_t i=0; i < fCvIn.count; ++i)
  2590. FLOAT_COPY(fCvInBuffers[i], cvInBuf[i]+timeOffset, frames);
  2591. for (uint32_t i=0; i < fCvOut.count; ++i)
  2592. FLOAT_CLEAR(fCvOutBuffers[i], frames);
  2593. // --------------------------------------------------------------------------------------------------------
  2594. // Run plugin
  2595. fDescriptor->run(fHandle, frames);
  2596. if (fHandle2 != nullptr)
  2597. fDescriptor->run(fHandle2, frames);
  2598. // --------------------------------------------------------------------------------------------------------
  2599. // Handle trigger parameters
  2600. for (uint32_t k=0; k < pData->param.count; ++k)
  2601. {
  2602. if (pData->param.data[k].type != PARAMETER_INPUT)
  2603. continue;
  2604. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2605. {
  2606. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2607. {
  2608. fParamBuffers[k] = pData->param.ranges[k].def;
  2609. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2610. }
  2611. }
  2612. }
  2613. pData->postRtEvents.trySplice();
  2614. #ifndef BUILD_BRIDGE
  2615. // --------------------------------------------------------------------------------------------------------
  2616. // Post-processing (dry/wet, volume and balance)
  2617. {
  2618. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2619. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2620. bool isPair;
  2621. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2622. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2623. {
  2624. // Dry/Wet
  2625. if (doDryWet)
  2626. {
  2627. for (uint32_t k=0; k < frames; ++k)
  2628. {
  2629. // TODO
  2630. //if (k < pData->latency && pData->latency < frames)
  2631. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  2632. //else
  2633. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2634. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  2635. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2636. }
  2637. }
  2638. // Balance
  2639. if (doBalance)
  2640. {
  2641. isPair = (i % 2 == 0);
  2642. if (isPair)
  2643. {
  2644. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2645. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  2646. }
  2647. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2648. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2649. for (uint32_t k=0; k < frames; ++k)
  2650. {
  2651. if (isPair)
  2652. {
  2653. // left
  2654. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2655. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2656. }
  2657. else
  2658. {
  2659. // right
  2660. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2661. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2662. }
  2663. }
  2664. }
  2665. // Volume (and buffer copy)
  2666. {
  2667. for (uint32_t k=0; k < frames; ++k)
  2668. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2669. }
  2670. }
  2671. } // End of Post-processing
  2672. #else // BUILD_BRIDGE
  2673. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2674. {
  2675. for (uint32_t k=0; k < frames; ++k)
  2676. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2677. }
  2678. #endif
  2679. // --------------------------------------------------------------------------------------------------------
  2680. pData->singleMutex.unlock();
  2681. return true;
  2682. }
  2683. void bufferSizeChanged(const uint32_t newBufferSize) override
  2684. {
  2685. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2686. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2687. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2688. {
  2689. if (fAudioInBuffers[i] != nullptr)
  2690. delete[] fAudioInBuffers[i];
  2691. fAudioInBuffers[i] = new float[newBufferSize];
  2692. }
  2693. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2694. {
  2695. if (fAudioOutBuffers[i] != nullptr)
  2696. delete[] fAudioOutBuffers[i];
  2697. fAudioOutBuffers[i] = new float[newBufferSize];
  2698. }
  2699. if (fHandle2 == nullptr)
  2700. {
  2701. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2702. {
  2703. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2704. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2705. }
  2706. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2707. {
  2708. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2709. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2710. }
  2711. }
  2712. else
  2713. {
  2714. if (pData->audioIn.count > 0)
  2715. {
  2716. CARLA_ASSERT(pData->audioIn.count == 2);
  2717. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2718. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2719. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2720. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2721. }
  2722. if (pData->audioOut.count > 0)
  2723. {
  2724. CARLA_ASSERT(pData->audioOut.count == 2);
  2725. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2726. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2727. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2728. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2729. }
  2730. }
  2731. for (uint32_t i=0; i < fCvIn.count; ++i)
  2732. {
  2733. if (fCvInBuffers[i] != nullptr)
  2734. delete[] fCvInBuffers[i];
  2735. fCvInBuffers[i] = new float[newBufferSize];
  2736. fDescriptor->connect_port(fHandle, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2737. if (fHandle2 != nullptr)
  2738. fDescriptor->connect_port(fHandle2, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2739. }
  2740. for (uint32_t i=0; i < fCvOut.count; ++i)
  2741. {
  2742. if (fCvOutBuffers[i] != nullptr)
  2743. delete[] fCvOutBuffers[i];
  2744. fCvOutBuffers[i] = new float[newBufferSize];
  2745. fDescriptor->connect_port(fHandle, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2746. if (fHandle2 != nullptr)
  2747. fDescriptor->connect_port(fHandle2, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2748. }
  2749. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2750. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2751. {
  2752. fLv2Options.maxBufferSize = newBufferSizeInt;
  2753. if (fLv2Options.minBufferSize != 1)
  2754. fLv2Options.minBufferSize = newBufferSizeInt;
  2755. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2756. {
  2757. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2758. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2759. }
  2760. }
  2761. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2762. }
  2763. void sampleRateChanged(const double newSampleRate) override
  2764. {
  2765. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2766. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2767. if (fLv2Options.sampleRate != newSampleRate)
  2768. {
  2769. fLv2Options.sampleRate = newSampleRate;
  2770. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2771. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2772. }
  2773. for (uint32_t k=0; k < pData->param.count; ++k)
  2774. {
  2775. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2776. {
  2777. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2778. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2779. break;
  2780. }
  2781. }
  2782. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2783. }
  2784. void offlineModeChanged(const bool isOffline) override
  2785. {
  2786. for (uint32_t k=0; k < pData->param.count; ++k)
  2787. {
  2788. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  2789. {
  2790. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2791. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2792. break;
  2793. }
  2794. }
  2795. }
  2796. // -------------------------------------------------------------------
  2797. // Plugin buffers
  2798. void initBuffers() override
  2799. {
  2800. fCvIn.initBuffers();
  2801. fCvOut.initBuffers();
  2802. fEventsIn.initBuffers();
  2803. fEventsOut.initBuffers();
  2804. CarlaPlugin::initBuffers();
  2805. }
  2806. void clearBuffers() override
  2807. {
  2808. carla_debug("Lv2Plugin::clearBuffers() - start");
  2809. if (fAudioInBuffers != nullptr)
  2810. {
  2811. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2812. {
  2813. if (fAudioInBuffers[i] != nullptr)
  2814. {
  2815. delete[] fAudioInBuffers[i];
  2816. fAudioInBuffers[i] = nullptr;
  2817. }
  2818. }
  2819. delete[] fAudioInBuffers;
  2820. fAudioInBuffers = nullptr;
  2821. }
  2822. if (fAudioOutBuffers != nullptr)
  2823. {
  2824. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2825. {
  2826. if (fAudioOutBuffers[i] != nullptr)
  2827. {
  2828. delete[] fAudioOutBuffers[i];
  2829. fAudioOutBuffers[i] = nullptr;
  2830. }
  2831. }
  2832. delete[] fAudioOutBuffers;
  2833. fAudioOutBuffers = nullptr;
  2834. }
  2835. if (fCvInBuffers != nullptr)
  2836. {
  2837. for (uint32_t i=0; i < fCvIn.count; ++i)
  2838. {
  2839. if (fCvInBuffers[i] != nullptr)
  2840. {
  2841. delete[] fCvInBuffers[i];
  2842. fCvInBuffers[i] = nullptr;
  2843. }
  2844. }
  2845. delete[] fCvInBuffers;
  2846. fCvInBuffers = nullptr;
  2847. }
  2848. if (fCvOutBuffers != nullptr)
  2849. {
  2850. for (uint32_t i=0; i < fCvOut.count; ++i)
  2851. {
  2852. if (fCvOutBuffers[i] != nullptr)
  2853. {
  2854. delete[] fCvOutBuffers[i];
  2855. fCvOutBuffers[i] = nullptr;
  2856. }
  2857. }
  2858. delete[] fCvOutBuffers;
  2859. fCvOutBuffers = nullptr;
  2860. }
  2861. if (fParamBuffers != nullptr)
  2862. {
  2863. delete[] fParamBuffers;
  2864. fParamBuffers = nullptr;
  2865. }
  2866. fCvIn.clear();
  2867. fCvOut.clear();
  2868. fEventsIn.clear();
  2869. fEventsOut.clear();
  2870. CarlaPlugin::clearBuffers();
  2871. carla_debug("Lv2Plugin::clearBuffers() - end");
  2872. }
  2873. // -------------------------------------------------------------------
  2874. // OSC stuff
  2875. void updateOscData(const lo_address& source, const char* const url) override
  2876. {
  2877. CarlaPlugin::updateOscData(source, url);
  2878. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  2879. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i));
  2880. }
  2881. // -------------------------------------------------------------------
  2882. // Post-poned UI Stuff
  2883. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2884. {
  2885. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2886. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2887. if (fUi.type == UI::TYPE_OSC)
  2888. {
  2889. if (pData->osc.data.target != nullptr)
  2890. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  2891. }
  2892. else
  2893. {
  2894. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2895. {
  2896. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  2897. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  2898. }
  2899. }
  2900. }
  2901. void uiMidiProgramChange(const uint32_t index) noexcept override
  2902. {
  2903. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2904. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2905. if (fUi.type == UI::TYPE_OSC)
  2906. {
  2907. if (pData->osc.data.target != nullptr)
  2908. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2909. }
  2910. else
  2911. {
  2912. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2913. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2914. }
  2915. }
  2916. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2917. {
  2918. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2919. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2920. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2921. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2922. if (fUi.type == UI::TYPE_OSC)
  2923. {
  2924. if (pData->osc.data.target != nullptr)
  2925. {
  2926. uint8_t midiData[4] = { 0 };
  2927. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2928. midiData[2] = note;
  2929. midiData[3] = velo;
  2930. osc_send_midi(pData->osc.data, midiData);
  2931. }
  2932. }
  2933. else
  2934. {
  2935. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2936. {
  2937. LV2_Atom_MidiEvent midiEv;
  2938. midiEv.event.time.frames = 0;
  2939. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2940. midiEv.event.body.size = 3;
  2941. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2942. midiEv.data[1] = note;
  2943. midiEv.data[2] = velo;
  2944. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2945. }
  2946. }
  2947. }
  2948. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2949. {
  2950. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2951. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2952. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2953. if (fUi.type == UI::TYPE_OSC)
  2954. {
  2955. if (pData->osc.data.target != nullptr)
  2956. {
  2957. uint8_t midiData[4] = { 0 };
  2958. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2959. midiData[2] = note;
  2960. osc_send_midi(pData->osc.data, midiData);
  2961. }
  2962. }
  2963. else
  2964. {
  2965. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2966. {
  2967. LV2_Atom_MidiEvent midiEv;
  2968. midiEv.event.time.frames = 0;
  2969. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2970. midiEv.event.body.size = 3;
  2971. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2972. midiEv.data[1] = note;
  2973. midiEv.data[2] = 0;
  2974. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2975. }
  2976. }
  2977. }
  2978. // -------------------------------------------------------------------
  2979. bool isRealtimeSafe() const noexcept
  2980. {
  2981. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  2982. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2983. {
  2984. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  2985. return true;
  2986. }
  2987. return false;
  2988. }
  2989. bool needsFixedBuffer() const noexcept
  2990. {
  2991. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  2992. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2993. {
  2994. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  2995. return true;
  2996. }
  2997. return false;
  2998. }
  2999. // -------------------------------------------------------------------
  3000. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3001. {
  3002. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3003. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  3004. if (std::strstr(rdfUi->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3005. return false;
  3006. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  3007. {
  3008. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3009. return false;
  3010. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3011. return false;
  3012. }
  3013. return true;
  3014. }
  3015. bool isUiResizable() const noexcept
  3016. {
  3017. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  3018. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3019. {
  3020. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3021. return false;
  3022. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3023. return false;
  3024. }
  3025. return true;
  3026. }
  3027. const char* getUiBridgeBinary(const LV2_Property type) const
  3028. {
  3029. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3030. if (bridgeBinary.isEmpty())
  3031. return nullptr;
  3032. // test for local build
  3033. if (bridgeBinary.endsWith("/source/backend/"))
  3034. bridgeBinary += "../bridges/";
  3035. switch (type)
  3036. {
  3037. case LV2_UI_GTK2:
  3038. bridgeBinary += "carla-bridge-lv2-gtk2";
  3039. break;
  3040. case LV2_UI_GTK3:
  3041. bridgeBinary += "carla-bridge-lv2-gtk3";
  3042. break;
  3043. case LV2_UI_QT4:
  3044. bridgeBinary += "carla-bridge-lv2-qt4";
  3045. break;
  3046. case LV2_UI_QT5:
  3047. bridgeBinary += "carla-bridge-lv2-qt5";
  3048. break;
  3049. case LV2_UI_COCOA:
  3050. bridgeBinary += "carla-bridge-lv2-cocoa";
  3051. break;
  3052. case LV2_UI_WINDOWS:
  3053. bridgeBinary += "carla-bridge-lv2-windows";
  3054. break;
  3055. case LV2_UI_X11:
  3056. bridgeBinary += "carla-bridge-lv2-x11";
  3057. break;
  3058. case LV2_UI_EXTERNAL:
  3059. case LV2_UI_OLD_EXTERNAL:
  3060. bridgeBinary += "carla-bridge-lv2-external";
  3061. break;
  3062. default:
  3063. return nullptr;
  3064. }
  3065. #ifdef CARLA_OS_WIN
  3066. bridgeBinary += ".exe";
  3067. #endif
  3068. QFile file(bridgeBinary.getBuffer());
  3069. if (! file.exists())
  3070. return nullptr;
  3071. return bridgeBinary.dup();
  3072. }
  3073. // -------------------------------------------------------------------
  3074. void recheckExtensions()
  3075. {
  3076. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3077. carla_debug("Lv2Plugin::recheckExtensions()");
  3078. fExt.options = nullptr;
  3079. fExt.programs = nullptr;
  3080. fExt.state = nullptr;
  3081. fExt.worker = nullptr;
  3082. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3083. {
  3084. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3085. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3086. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3087. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3088. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3089. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3090. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3091. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3092. else
  3093. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3094. }
  3095. if (fDescriptor->extension_data != nullptr)
  3096. {
  3097. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3098. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3099. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3100. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3101. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3102. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3103. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3104. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3105. // check if invalid
  3106. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3107. fExt.options = nullptr;
  3108. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3109. fExt.programs = nullptr;
  3110. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3111. fExt.state = nullptr;
  3112. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3113. fExt.worker = nullptr;
  3114. }
  3115. }
  3116. // -------------------------------------------------------------------
  3117. void updateUi()
  3118. {
  3119. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  3120. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  3121. carla_debug("Lv2Plugin::updateUi()");
  3122. fExt.uiidle = nullptr;
  3123. fExt.uiprograms = nullptr;
  3124. if (fUi.descriptor->extension_data != nullptr)
  3125. {
  3126. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3127. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3128. // check if invalid
  3129. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3130. fExt.uiidle = nullptr;
  3131. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3132. fExt.uiprograms = nullptr;
  3133. }
  3134. // update midi program
  3135. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3136. {
  3137. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3138. fExt.uiprograms->select_program(fUi.handle, curData.bank, curData.program);
  3139. }
  3140. // update control ports
  3141. if (fUi.descriptor->port_event != nullptr)
  3142. {
  3143. float value;
  3144. for (uint32_t i=0; i < pData->param.count; ++i)
  3145. {
  3146. value = getParameterValue(i);
  3147. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3148. }
  3149. }
  3150. }
  3151. // -------------------------------------------------------------------
  3152. LV2_URID getCustomURID(const char* const uri)
  3153. {
  3154. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3155. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3156. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3157. {
  3158. const char*& thisUri(fCustomURIDs.getAt(i));
  3159. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3160. return static_cast<LV2_URID>(i);
  3161. }
  3162. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3163. fCustomURIDs.append(carla_strdup(uri));
  3164. if (fUi.type == UI::TYPE_OSC && pData->osc.data.target != nullptr)
  3165. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3166. return urid;
  3167. }
  3168. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3169. {
  3170. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  3171. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  3172. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3173. return fCustomURIDs.getAt(urid);
  3174. }
  3175. // -------------------------------------------------------------------
  3176. void handleProgramChanged(const int32_t index)
  3177. {
  3178. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3179. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3180. if (index == -1)
  3181. {
  3182. const ScopedSingleProcessLocker spl(this, true);
  3183. return reloadPrograms(false);
  3184. }
  3185. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3186. {
  3187. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3188. {
  3189. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3190. if (pData->midiprog.data[index].name != nullptr)
  3191. delete[] pData->midiprog.data[index].name;
  3192. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3193. if (index == pData->midiprog.current)
  3194. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3195. else
  3196. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3197. }
  3198. }
  3199. }
  3200. // -------------------------------------------------------------------
  3201. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3202. {
  3203. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3204. carla_debug("Lv2Plugin::handleResizePort(%i, " P_SIZE ")", index, size);
  3205. // TODO
  3206. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3207. (void)index;
  3208. }
  3209. // -------------------------------------------------------------------
  3210. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3211. {
  3212. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3213. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3214. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3215. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3216. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3217. carla_debug("Lv2Plugin::handleStateStore(%i:\"%s\", %p, " P_SIZE ", %i:\"%s\", %i)", key, carla_lv2_urid_unmap(this, key), value, size, type, carla_lv2_urid_unmap(this, type), flags);
  3218. const char* const skey(carla_lv2_urid_unmap(this, key));
  3219. const char* const stype(carla_lv2_urid_unmap(this, type));
  3220. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3221. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3222. // Check if we already have this key
  3223. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3224. {
  3225. CustomData& data(it.getValue());
  3226. if (std::strcmp(data.key, skey) == 0)
  3227. {
  3228. // found it
  3229. if (data.value != nullptr)
  3230. delete[] data.value;
  3231. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3232. data.value = carla_strdup((const char*)value);
  3233. else
  3234. data.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3235. return LV2_STATE_SUCCESS;
  3236. }
  3237. }
  3238. // Otherwise store it
  3239. CustomData newData;
  3240. newData.type = carla_strdup(stype);
  3241. newData.key = carla_strdup(skey);
  3242. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3243. newData.value = carla_strdup((const char*)value);
  3244. else
  3245. newData.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3246. pData->custom.append(newData);
  3247. return LV2_STATE_SUCCESS;
  3248. }
  3249. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3250. {
  3251. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3252. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3253. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3254. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3255. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3256. const char* const skey(carla_lv2_urid_unmap(this, key));
  3257. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3258. const char* stype = nullptr;
  3259. const char* stringData = nullptr;
  3260. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3261. {
  3262. const CustomData& data(it.getValue());
  3263. if (std::strcmp(data.key, skey) == 0)
  3264. {
  3265. stype = data.type;
  3266. stringData = data.value;
  3267. break;
  3268. }
  3269. }
  3270. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3271. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3272. *type = carla_lv2_urid_map(this, stype);
  3273. *flags = LV2_STATE_IS_POD;
  3274. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3275. {
  3276. *size = std::strlen(stringData);
  3277. return stringData;
  3278. }
  3279. else
  3280. {
  3281. static QByteArray chunk; // FIXME
  3282. chunk = QByteArray::fromBase64(stringData);
  3283. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3284. *size = static_cast<size_t>(chunk.size());
  3285. return chunk.constData();
  3286. }
  3287. }
  3288. // -------------------------------------------------------------------
  3289. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3290. {
  3291. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3292. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3293. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3294. if (pData->engine->isOffline())
  3295. {
  3296. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3297. return LV2_WORKER_SUCCESS;
  3298. }
  3299. LV2_Atom atom;
  3300. atom.size = size;
  3301. atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3302. return fAtomQueueOut.putChunk(&atom, data, fEventsIn.ctrl->rindex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3303. }
  3304. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3305. {
  3306. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3307. LV2_Atom atom;
  3308. atom.size = size;
  3309. atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3310. return fAtomQueueIn.putChunk(&atom, data, fEventsIn.ctrl->rindex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3311. }
  3312. // -------------------------------------------------------------------
  3313. void handleExternalUiClosed()
  3314. {
  3315. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EXTERNAL,);
  3316. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3317. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3318. fUi.descriptor->cleanup(fUi.handle);
  3319. fUi.handle = nullptr;
  3320. fUi.widget = nullptr;
  3321. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3322. }
  3323. void handlePluginUiClosed() override
  3324. {
  3325. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EMBED,);
  3326. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr,);
  3327. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3328. fUi.window->hide();
  3329. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3330. fUi.descriptor->cleanup(fUi.handle);
  3331. fUi.handle = nullptr;
  3332. fUi.widget = nullptr;
  3333. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3334. }
  3335. // -------------------------------------------------------------------
  3336. uint32_t handleUiPortMap(const char* const symbol) const noexcept
  3337. {
  3338. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3339. carla_debug("Lv2Plugin::handleUiPortMap(\"%s\")", symbol);
  3340. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3341. {
  3342. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3343. return i;
  3344. }
  3345. return LV2UI_INVALID_PORT_INDEX;
  3346. }
  3347. int handleUiResize(const int width, const int height)
  3348. {
  3349. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr, 1);
  3350. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3351. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3352. carla_debug("Lv2Plugin::handleUiResize(%i, %i)", width, height);
  3353. fUi.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3354. return 0;
  3355. }
  3356. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3357. {
  3358. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3359. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3360. carla_debug("Lv2Plugin::handleUiWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3361. switch (format)
  3362. {
  3363. case CARLA_URI_MAP_ID_NULL:
  3364. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3365. for (uint32_t i=0; i < pData->param.count; ++i)
  3366. {
  3367. if (pData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3368. {
  3369. const float value(*(const float*)buffer);
  3370. if (fParamBuffers[i] != value)
  3371. setParameterValue(i, value, false, true, true);
  3372. break;
  3373. }
  3374. }
  3375. break;
  3376. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3377. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT:
  3378. CARLA_SAFE_ASSERT_RETURN(((const LV2_Atom*)buffer)->size == bufferSize,);
  3379. fAtomQueueIn.put((const LV2_Atom*)buffer, rindex);
  3380. break;
  3381. default:
  3382. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3383. break;
  3384. }
  3385. }
  3386. // -------------------------------------------------------------------
  3387. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3388. {
  3389. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3390. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3391. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3392. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3393. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3394. int32_t rindex = -1;
  3395. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3396. {
  3397. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3398. {
  3399. rindex = static_cast<int32_t>(i);
  3400. break;
  3401. }
  3402. }
  3403. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3404. switch (type)
  3405. {
  3406. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3407. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3408. for (uint32_t i=0; i < pData->param.count; ++i)
  3409. {
  3410. if (pData->param.data[i].rindex == rindex)
  3411. {
  3412. const float valuef(*(const float*)value);
  3413. setParameterValue(i, valuef, true, true, true);
  3414. break;
  3415. }
  3416. }
  3417. break;
  3418. default:
  3419. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3420. break;
  3421. }
  3422. }
  3423. // -------------------------------------------------------------------
  3424. public:
  3425. bool init(const char* const bundle, const char* const name, const char* const uri)
  3426. {
  3427. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3428. // ---------------------------------------------------------------
  3429. // first checks
  3430. if (pData->client != nullptr)
  3431. {
  3432. pData->engine->setLastError("Plugin client is already registered");
  3433. return false;
  3434. }
  3435. if (bundle == nullptr || bundle[0] == '\0')
  3436. {
  3437. pData->engine->setLastError("null bundle");
  3438. return false;
  3439. }
  3440. if (uri == nullptr || uri[0] == '\0')
  3441. {
  3442. pData->engine->setLastError("null uri");
  3443. return false;
  3444. }
  3445. // ---------------------------------------------------------------
  3446. // get plugin from lv2_rdf (lilv)
  3447. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3448. // Convert bundle filename to URI
  3449. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  3450. if (! qBundle.endsWith(OS_SEP_STR))
  3451. qBundle += OS_SEP_STR;
  3452. // Load bundle
  3453. Lilv::Node lilvBundle(lv2World.new_uri(qBundle.toUtf8().constData()));
  3454. lv2World.load_bundle(lilvBundle);
  3455. fRdfDescriptor = lv2_rdf_new(uri, true);
  3456. if (fRdfDescriptor == nullptr)
  3457. {
  3458. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3459. return false;
  3460. }
  3461. // ---------------------------------------------------------------
  3462. // open DLL
  3463. if (! pData->libOpen(fRdfDescriptor->Binary))
  3464. {
  3465. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3466. return false;
  3467. }
  3468. // ---------------------------------------------------------------
  3469. // get DLL main entry
  3470. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3471. if (descFn == nullptr)
  3472. {
  3473. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3474. return false;
  3475. }
  3476. // -----------------------------------------------------------
  3477. // get descriptor that matches URI
  3478. uint32_t i = 0;
  3479. while ((fDescriptor = descFn(i++)))
  3480. {
  3481. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3482. break;
  3483. }
  3484. if (fDescriptor == nullptr)
  3485. {
  3486. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3487. return false;
  3488. }
  3489. // ---------------------------------------------------------------
  3490. // check supported port-types and features
  3491. bool canContinue = true;
  3492. // Check supported ports
  3493. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3494. {
  3495. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3496. if (! is_lv2_port_supported(portTypes))
  3497. {
  3498. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3499. {
  3500. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3501. canContinue = false;
  3502. break;
  3503. }
  3504. }
  3505. }
  3506. // Check supported features
  3507. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3508. {
  3509. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  3510. {
  3511. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  3512. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  3513. {
  3514. canContinue = false;
  3515. pData->engine->setLastError(msg.toUtf8().constData());
  3516. break;
  3517. }
  3518. else
  3519. carla_stderr("%s", msg.toUtf8().constData());
  3520. }
  3521. }
  3522. if (! canContinue)
  3523. {
  3524. // error already set
  3525. return false;
  3526. }
  3527. // ---------------------------------------------------------------
  3528. // get info
  3529. if (name != nullptr && name[0] != '\0')
  3530. pData->name = pData->engine->getUniquePluginName(name);
  3531. else
  3532. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3533. pData->filename = carla_strdup(bundle);
  3534. // ---------------------------------------------------------------
  3535. // register client
  3536. pData->client = pData->engine->addClient(this);
  3537. if (pData->client == nullptr || ! pData->client->isOk())
  3538. {
  3539. pData->engine->setLastError("Failed to register plugin client");
  3540. return false;
  3541. }
  3542. // ---------------------------------------------------------------
  3543. // initialize options
  3544. fLv2Options.minBufferSize = 1;
  3545. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3546. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3547. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3548. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3549. {
  3550. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3551. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3552. {
  3553. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3554. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3555. }
  3556. }
  3557. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3558. // ---------------------------------------------------------------
  3559. // initialize features (part 1)
  3560. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3561. eventFt->callback_data = this;
  3562. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3563. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3564. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3565. logFt->handle = this;
  3566. logFt->printf = carla_lv2_log_printf;
  3567. logFt->vprintf = carla_lv2_log_vprintf;
  3568. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3569. stateMakePathFt->handle = this;
  3570. stateMakePathFt->path = carla_lv2_state_make_path;
  3571. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3572. stateMapPathFt->handle = this;
  3573. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3574. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3575. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3576. programsFt->handle = this;
  3577. programsFt->program_changed = carla_lv2_program_changed;
  3578. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3579. rsPortFt->data = this;
  3580. rsPortFt->resize = carla_lv2_resize_port;
  3581. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3582. lv2_rtmempool_init(rtMemPoolFt);
  3583. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3584. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3585. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3586. uriMapFt->callback_data = this;
  3587. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3588. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3589. uridMapFt->handle = this;
  3590. uridMapFt->map = carla_lv2_urid_map;
  3591. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3592. uridUnmapFt->handle = this;
  3593. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3594. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3595. workerFt->handle = this;
  3596. workerFt->schedule_work = carla_lv2_worker_schedule;
  3597. // ---------------------------------------------------------------
  3598. // initialize features (part 2)
  3599. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3600. fFeatures[j] = new LV2_Feature;
  3601. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3602. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3603. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3604. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3605. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3606. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3607. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3608. fFeatures[kFeatureIdEvent]->data = eventFt;
  3609. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3610. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3611. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3612. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3613. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3614. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3615. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3616. fFeatures[kFeatureIdLogs]->data = logFt;
  3617. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3618. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3619. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3620. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3621. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3622. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3623. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3624. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3625. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3626. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3627. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3628. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3629. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3630. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3631. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3632. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3633. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3634. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3635. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3636. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3637. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3638. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3639. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3640. fFeatures[kFeatureIdWorker]->data = workerFt;
  3641. // if a fixed buffer is not needed, skip its feature
  3642. if (! needsFixedBuffer())
  3643. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3644. // if power of 2 is not possible, skip its feature FIXME
  3645. //if (fLv2Options.maxBufferSize)
  3646. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3647. // ---------------------------------------------------------------
  3648. // initialize plugin
  3649. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3650. if (fHandle == nullptr)
  3651. {
  3652. pData->engine->setLastError("Plugin failed to initialize");
  3653. return false;
  3654. }
  3655. // ---------------------------------------------------------------
  3656. // load plugin settings
  3657. {
  3658. // set default options
  3659. pData->options = 0x0;
  3660. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3661. if (getMidiInCount() > 0 || needsFixedBuffer())
  3662. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3663. if (pData->engine->getOptions().forceStereo)
  3664. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3665. if (getMidiInCount() > 0)
  3666. {
  3667. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3668. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3669. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3670. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3671. }
  3672. // set identifier string
  3673. CarlaString identifier("LV2/");
  3674. identifier += uri;
  3675. pData->identifier = identifier.dup();
  3676. // load settings
  3677. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3678. // ignore settings, we need this anyway
  3679. if (getMidiInCount() > 0 || needsFixedBuffer())
  3680. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3681. }
  3682. // ---------------------------------------------------------------
  3683. // gui stuff
  3684. if (fRdfDescriptor->UICount != 0)
  3685. initUi();
  3686. return true;
  3687. }
  3688. // -------------------------------------------------------------------
  3689. void initUi()
  3690. {
  3691. // ---------------------------------------------------------------
  3692. // find more appropriate ui
  3693. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3694. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3695. #ifdef BUILD_BRIDGE
  3696. const bool preferUiBridges(false);
  3697. #else
  3698. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3699. #endif
  3700. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3701. {
  3702. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3703. const int ii(static_cast<int>(i));
  3704. switch (fRdfDescriptor->UIs[i].Type)
  3705. {
  3706. case LV2_UI_QT4:
  3707. if (isUiBridgeable(i))
  3708. eQt4 = ii;
  3709. break;
  3710. case LV2_UI_QT5:
  3711. if (isUiBridgeable(i))
  3712. eQt5 = ii;
  3713. break;
  3714. case LV2_UI_GTK2:
  3715. if (isUiBridgeable(i))
  3716. eGtk2 = ii;
  3717. break;
  3718. case LV2_UI_GTK3:
  3719. if (isUiBridgeable(i))
  3720. eGtk3 = ii;
  3721. break;
  3722. #if defined(CARLA_OS_HAIKU)
  3723. #elif defined(CARLA_OS_MAC)
  3724. case LV2_UI_COCOA:
  3725. if (isUiBridgeable(i) && preferUiBridges)
  3726. eCocoa = ii;
  3727. iCocoa = ii;
  3728. break;
  3729. #elif defined(CARLA_OS_WIN)
  3730. case LV2_UI_WINDOWS:
  3731. if (isUiBridgeable(i) && preferUiBridges)
  3732. eWindows = ii;
  3733. iWindows = ii;
  3734. break;
  3735. #else
  3736. case LV2_UI_X11:
  3737. if (isUiBridgeable(i) && preferUiBridges)
  3738. eX11 = ii;
  3739. iX11 = ii;
  3740. break;
  3741. #endif
  3742. case LV2_UI_EXTERNAL:
  3743. case LV2_UI_OLD_EXTERNAL:
  3744. if (isUiBridgeable(i))
  3745. eExt = ii;
  3746. iExt = ii;
  3747. break;
  3748. default:
  3749. break;
  3750. }
  3751. }
  3752. if (eQt4 >= 0)
  3753. iFinal = eQt4;
  3754. else if (eQt5 >= 0)
  3755. iFinal = eQt5;
  3756. else if (eGtk2 >= 0)
  3757. iFinal = eGtk2;
  3758. else if (eGtk3 >= 0)
  3759. iFinal = eGtk3;
  3760. else if (eCocoa >= 0)
  3761. iFinal = eCocoa;
  3762. else if (eWindows >= 0)
  3763. iFinal = eWindows;
  3764. else if (eX11 >= 0)
  3765. iFinal = eX11;
  3766. //else if (eExt >= 0) // TODO
  3767. // iFinal = eExt;
  3768. else if (iCocoa >= 0)
  3769. iFinal = iCocoa;
  3770. else if (iWindows >= 0)
  3771. iFinal = iWindows;
  3772. else if (iX11 >= 0)
  3773. iFinal = iX11;
  3774. else if (iExt >= 0)
  3775. iFinal = iExt;
  3776. if (iFinal < 0)
  3777. {
  3778. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3779. return;
  3780. }
  3781. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3782. // ---------------------------------------------------------------
  3783. // check supported ui features
  3784. bool canContinue = true;
  3785. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3786. {
  3787. if (! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3788. {
  3789. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3790. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type))
  3791. {
  3792. canContinue = false;
  3793. break;
  3794. }
  3795. }
  3796. }
  3797. if (! canContinue)
  3798. {
  3799. fUi.rdfDescriptor = nullptr;
  3800. return;
  3801. }
  3802. // ---------------------------------------------------------------
  3803. // initialize ui according to type
  3804. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3805. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  3806. {
  3807. // -----------------------------------------------------------
  3808. // initialize osc-bridge
  3809. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  3810. {
  3811. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  3812. fUi.type = UI::TYPE_OSC;
  3813. pData->osc.thread.setOscData(bridgeBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3814. delete[] bridgeBinary;
  3815. return;
  3816. }
  3817. }
  3818. // ---------------------------------------------------------------
  3819. // open UI DLL
  3820. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3821. {
  3822. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUi.rdfDescriptor->Binary));
  3823. fUi.rdfDescriptor = nullptr;
  3824. return;
  3825. }
  3826. // ---------------------------------------------------------------
  3827. // get UI DLL main entry
  3828. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  3829. if (uiDescFn == nullptr)
  3830. {
  3831. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3832. pData->uiLibClose();
  3833. fUi.rdfDescriptor = nullptr;
  3834. return;
  3835. }
  3836. // ---------------------------------------------------------------
  3837. // get UI descriptor that matches UI URI
  3838. uint32_t i = 0;
  3839. while ((fUi.descriptor = uiDescFn(i++)))
  3840. {
  3841. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3842. break;
  3843. }
  3844. if (fUi.descriptor == nullptr)
  3845. {
  3846. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3847. pData->uiLibClose();
  3848. fUi.rdfDescriptor = nullptr;
  3849. return;
  3850. }
  3851. // ---------------------------------------------------------------
  3852. // check if ui is usable
  3853. switch (uiType)
  3854. {
  3855. case LV2_UI_QT4:
  3856. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  3857. break;
  3858. case LV2_UI_QT5:
  3859. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  3860. break;
  3861. case LV2_UI_GTK2:
  3862. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  3863. break;
  3864. case LV2_UI_GTK3:
  3865. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  3866. break;
  3867. #if defined(CARLA_OS_HAIKU)
  3868. #elif defined(CARLA_OS_MAC)
  3869. case LV2_UI_COCOA:
  3870. carla_stdout("Will use LV2 Cocoa UI");
  3871. fUi.type = UI::TYPE_EMBED;
  3872. break;
  3873. #elif defined(CARLA_OS_WIN)
  3874. case LV2_UI_WINDOWS:
  3875. carla_stdout("Will use LV2 Windows UI");
  3876. fUi.type = UI::TYPE_EMBED;
  3877. break;
  3878. #else
  3879. case LV2_UI_X11:
  3880. carla_stdout("Will use LV2 X11 UI");
  3881. fUi.type = UI::TYPE_EMBED;
  3882. break;
  3883. #endif
  3884. case LV2_UI_EXTERNAL:
  3885. case LV2_UI_OLD_EXTERNAL:
  3886. carla_stdout("Will use LV2 External UI");
  3887. fUi.type = UI::TYPE_EXTERNAL;
  3888. break;
  3889. }
  3890. if (fUi.type == UI::TYPE_NULL)
  3891. {
  3892. pData->uiLibClose();
  3893. fUi.descriptor = nullptr;
  3894. fUi.rdfDescriptor = nullptr;
  3895. return;
  3896. }
  3897. // ---------------------------------------------------------------
  3898. // initialize ui data
  3899. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  3900. fUi.title = carla_strdup(guiTitle.toUtf8().constData());
  3901. // ---------------------------------------------------------------
  3902. // initialize ui features (part 1)
  3903. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3904. uiDataFt->data_access = fDescriptor->extension_data;
  3905. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3906. uiPortMapFt->handle = this;
  3907. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3908. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3909. uiResizeFt->handle = this;
  3910. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3911. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3912. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3913. uiExternalHostFt->plugin_human_id = fUi.title;
  3914. // ---------------------------------------------------------------
  3915. // initialize ui features (part 2)
  3916. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  3917. fFeatures[j] = new LV2_Feature;
  3918. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3919. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3920. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3921. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3922. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  3923. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  3924. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  3925. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  3926. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  3927. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  3928. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  3929. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  3930. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3931. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3932. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3933. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3934. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  3935. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  3936. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3937. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3938. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  3939. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  3940. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3941. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3942. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3943. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3944. }
  3945. // -------------------------------------------------------------------
  3946. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  3947. {
  3948. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  3949. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  3950. fAtomQueueIn.put(atom, portIndex);
  3951. }
  3952. void handleUridMap(const LV2_URID urid, const char* const uri)
  3953. {
  3954. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  3955. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  3956. carla_debug("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count(), uri);
  3957. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  3958. fCustomURIDs.append(carla_strdup(uri));
  3959. }
  3960. // -------------------------------------------------------------------
  3961. private:
  3962. LV2_Handle fHandle;
  3963. LV2_Handle fHandle2;
  3964. LV2_Feature* fFeatures[kFeatureCountAll+1];
  3965. const LV2_Descriptor* fDescriptor;
  3966. const LV2_RDF_Descriptor* fRdfDescriptor;
  3967. float** fAudioInBuffers;
  3968. float** fAudioOutBuffers;
  3969. float** fCvInBuffers;
  3970. float** fCvOutBuffers;
  3971. float* fParamBuffers;
  3972. Lv2AtomQueue fAtomQueueIn;
  3973. Lv2AtomQueue fAtomQueueOut;
  3974. LV2_Atom_Forge fAtomForge;
  3975. PluginCVData fCvIn;
  3976. PluginCVData fCvOut;
  3977. Lv2PluginEventData fEventsIn;
  3978. Lv2PluginEventData fEventsOut;
  3979. Lv2PluginOptions fLv2Options;
  3980. LinkedList<const char*> fCustomURIDs;
  3981. bool fFirstActive; // first process() call after activate()
  3982. EngineTimeInfo fLastTimeInfo;
  3983. struct Extensions {
  3984. const LV2_Options_Interface* options;
  3985. const LV2_State_Interface* state;
  3986. const LV2_Worker_Interface* worker;
  3987. const LV2_Programs_Interface* programs;
  3988. const LV2UI_Idle_Interface* uiidle;
  3989. const LV2_Programs_UI_Interface* uiprograms;
  3990. Extensions()
  3991. : options(nullptr),
  3992. state(nullptr),
  3993. worker(nullptr),
  3994. programs(nullptr),
  3995. uiidle(nullptr),
  3996. uiprograms(nullptr) {}
  3997. } fExt;
  3998. struct UI {
  3999. enum Type {
  4000. TYPE_NULL,
  4001. TYPE_EMBED,
  4002. TYPE_EXTERNAL,
  4003. TYPE_OSC
  4004. };
  4005. Type type;
  4006. LV2UI_Handle handle;
  4007. LV2UI_Widget widget;
  4008. const LV2UI_Descriptor* descriptor;
  4009. const LV2_RDF_UI* rdfDescriptor;
  4010. const char* title;
  4011. CarlaPluginUi* window;
  4012. UI()
  4013. : type(TYPE_NULL),
  4014. handle(nullptr),
  4015. widget(nullptr),
  4016. descriptor(nullptr),
  4017. rdfDescriptor(nullptr),
  4018. title(nullptr),
  4019. window(nullptr) {}
  4020. ~UI()
  4021. {
  4022. CARLA_ASSERT(handle == nullptr);
  4023. CARLA_ASSERT(widget == nullptr);
  4024. CARLA_ASSERT(descriptor == nullptr);
  4025. CARLA_ASSERT(rdfDescriptor == nullptr);
  4026. CARLA_ASSERT(title == nullptr);
  4027. CARLA_ASSERT(window == nullptr);
  4028. }
  4029. } fUi;
  4030. // -------------------------------------------------------------------
  4031. // Event Feature
  4032. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4033. {
  4034. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4035. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4036. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4037. return 0;
  4038. }
  4039. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4040. {
  4041. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4042. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4043. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4044. return 0;
  4045. }
  4046. // -------------------------------------------------------------------
  4047. // Logs Feature
  4048. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4049. {
  4050. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4051. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4052. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4053. #ifndef DEBUG
  4054. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4055. return 0;
  4056. #endif
  4057. va_list args;
  4058. va_start(args, fmt);
  4059. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4060. va_end(args);
  4061. return ret;
  4062. }
  4063. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4064. {
  4065. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4066. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4067. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4068. #ifndef DEBUG
  4069. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4070. return 0;
  4071. #endif
  4072. int ret = 0;
  4073. switch (type)
  4074. {
  4075. case CARLA_URI_MAP_ID_LOG_ERROR:
  4076. std::fprintf(stderr, "\x1b[31m");
  4077. ret = std::vfprintf(stderr, fmt, ap);
  4078. std::fprintf(stderr, "\x1b[0m");
  4079. break;
  4080. case CARLA_URI_MAP_ID_LOG_NOTE:
  4081. ret = std::vfprintf(stdout, fmt, ap);
  4082. break;
  4083. case CARLA_URI_MAP_ID_LOG_TRACE:
  4084. #ifdef DEBUG
  4085. std::fprintf(stdout, "\x1b[30;1m");
  4086. ret = std::vfprintf(stdout, fmt, ap);
  4087. std::fprintf(stdout, "\x1b[0m");
  4088. #endif
  4089. break;
  4090. case CARLA_URI_MAP_ID_LOG_WARNING:
  4091. ret = std::vfprintf(stderr, fmt, ap);
  4092. break;
  4093. default:
  4094. break;
  4095. }
  4096. return ret;
  4097. }
  4098. // -------------------------------------------------------------------
  4099. // Programs Feature
  4100. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4101. {
  4102. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4103. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4104. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4105. }
  4106. // -------------------------------------------------------------------
  4107. // Resize Port Feature
  4108. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4109. {
  4110. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4111. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4112. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  4113. }
  4114. // -------------------------------------------------------------------
  4115. // State Feature
  4116. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4117. {
  4118. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4119. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4120. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4121. QDir dir;
  4122. dir.mkpath(path);
  4123. return strdup(path);
  4124. }
  4125. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4126. {
  4127. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4128. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4129. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4130. QDir dir(absolute_path);
  4131. return strdup(dir.canonicalPath().toUtf8().constData());
  4132. }
  4133. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4134. {
  4135. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4136. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4137. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4138. QDir dir(abstract_path);
  4139. return strdup(dir.absolutePath().toUtf8().constData());
  4140. }
  4141. 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)
  4142. {
  4143. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4144. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4145. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4146. }
  4147. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4148. {
  4149. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4150. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4151. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4152. }
  4153. // -------------------------------------------------------------------
  4154. // URI-Map Feature
  4155. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4156. {
  4157. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4158. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4159. // unused
  4160. (void)map;
  4161. }
  4162. // -------------------------------------------------------------------
  4163. // URID Feature
  4164. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4165. {
  4166. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4167. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4168. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4169. // Atom types
  4170. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4171. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4172. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4173. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4174. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4175. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4176. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4177. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4178. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4179. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4180. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4181. return CARLA_URI_MAP_ID_ATOM_INT;
  4182. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4183. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4184. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4185. return CARLA_URI_MAP_ID_ATOM_LONG;
  4186. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4187. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4188. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4189. return CARLA_URI_MAP_ID_ATOM_PATH;
  4190. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4191. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4192. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4193. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4194. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4195. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4196. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4197. return CARLA_URI_MAP_ID_ATOM_STRING;
  4198. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4199. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4200. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4201. return CARLA_URI_MAP_ID_ATOM_URI;
  4202. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4203. return CARLA_URI_MAP_ID_ATOM_URID;
  4204. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4205. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4206. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4207. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4208. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4209. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4210. // BufSize types
  4211. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4212. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4213. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4214. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4215. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4216. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4217. // Log types
  4218. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4219. return CARLA_URI_MAP_ID_LOG_ERROR;
  4220. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4221. return CARLA_URI_MAP_ID_LOG_NOTE;
  4222. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4223. return CARLA_URI_MAP_ID_LOG_TRACE;
  4224. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4225. return CARLA_URI_MAP_ID_LOG_WARNING;
  4226. // Time types
  4227. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4228. return CARLA_URI_MAP_ID_TIME_POSITION;
  4229. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4230. return CARLA_URI_MAP_ID_TIME_BAR;
  4231. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4232. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4233. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4234. return CARLA_URI_MAP_ID_TIME_BEAT;
  4235. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4236. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4237. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4238. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4239. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4240. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4241. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4242. return CARLA_URI_MAP_ID_TIME_FRAME;
  4243. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4244. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4245. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4246. return CARLA_URI_MAP_ID_TIME_SPEED;
  4247. // Others
  4248. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4249. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4250. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4251. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4252. // Custom types
  4253. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4254. }
  4255. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4256. {
  4257. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4258. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4259. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4260. // Atom types
  4261. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4262. return LV2_ATOM__Blank;
  4263. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4264. return LV2_ATOM__Bool;
  4265. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4266. return LV2_ATOM__Chunk;
  4267. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4268. return LV2_ATOM__Double;
  4269. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4270. return LV2_ATOM__Float;
  4271. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4272. return LV2_ATOM__Int;
  4273. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4274. return LV2_ATOM__Literal;
  4275. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4276. return LV2_ATOM__Long;
  4277. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4278. return LV2_ATOM__Object;
  4279. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4280. return LV2_ATOM__Path;
  4281. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4282. return LV2_ATOM__Property;
  4283. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4284. return LV2_ATOM__Resource;
  4285. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4286. return LV2_ATOM__Sequence;
  4287. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4288. return LV2_ATOM__String;
  4289. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4290. return LV2_ATOM__Tuple;
  4291. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4292. return LV2_ATOM__URI;
  4293. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4294. return LV2_ATOM__URID;
  4295. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4296. return LV2_ATOM__Vector;
  4297. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  4298. return nullptr; // custom
  4299. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4300. return LV2_ATOM__atomTransfer;
  4301. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4302. return LV2_ATOM__eventTransfer;
  4303. // BufSize types
  4304. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4305. return LV2_BUF_SIZE__maxBlockLength;
  4306. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4307. return LV2_BUF_SIZE__minBlockLength;
  4308. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4309. return LV2_BUF_SIZE__sequenceSize;
  4310. // Log types
  4311. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4312. return LV2_LOG__Error;
  4313. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4314. return LV2_LOG__Note;
  4315. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4316. return LV2_LOG__Trace;
  4317. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4318. return LV2_LOG__Warning;
  4319. // Time types
  4320. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4321. return LV2_TIME__Position;
  4322. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4323. return LV2_TIME__bar;
  4324. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4325. return LV2_TIME__barBeat;
  4326. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4327. return LV2_TIME__beat;
  4328. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4329. return LV2_TIME__beatUnit;
  4330. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4331. return LV2_TIME__beatsPerBar;
  4332. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4333. return LV2_TIME__beatsPerMinute;
  4334. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4335. return LV2_TIME__frame;
  4336. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4337. return LV2_TIME__framesPerSecond;
  4338. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4339. return LV2_TIME__speed;
  4340. // Others
  4341. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4342. return LV2_MIDI__MidiEvent;
  4343. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4344. return LV2_PARAMETERS__sampleRate;
  4345. // Custom types
  4346. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4347. }
  4348. // -------------------------------------------------------------------
  4349. // Worker Feature
  4350. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4351. {
  4352. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4353. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4354. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4355. }
  4356. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4357. {
  4358. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4359. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4360. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4361. }
  4362. // -------------------------------------------------------------------
  4363. // External UI Feature
  4364. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4365. {
  4366. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4367. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4368. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4369. }
  4370. // -------------------------------------------------------------------
  4371. // UI Port-Map Feature
  4372. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4373. {
  4374. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4375. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4376. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4377. }
  4378. // -------------------------------------------------------------------
  4379. // UI Resize Feature
  4380. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4381. {
  4382. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4383. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4384. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4385. }
  4386. // -------------------------------------------------------------------
  4387. // UI Extension
  4388. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4389. {
  4390. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4391. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4392. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4393. }
  4394. // -------------------------------------------------------------------
  4395. // Lilv State
  4396. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4397. {
  4398. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4399. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4400. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4401. }
  4402. // -------------------------------------------------------------------
  4403. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4404. };
  4405. // -------------------------------------------------------------------------------------------------------------------
  4406. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4407. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4408. {
  4409. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4410. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4411. const int32_t portIndex = argv[0]->i;
  4412. const char* const atomBuf = (const char*)&argv[1]->s;
  4413. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4414. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  4415. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4416. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  4417. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4418. return 0;
  4419. }
  4420. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4421. {
  4422. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4423. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4424. const int32_t urid = argv[0]->i;
  4425. const char* const uri = (const char*)&argv[1]->s;
  4426. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4427. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4428. return 0;
  4429. }
  4430. #undef lv2PluginPtr
  4431. CARLA_BACKEND_END_NAMESPACE
  4432. #endif // WANT_LV2
  4433. // -------------------------------------------------------------------------------------------------------------------
  4434. CARLA_BACKEND_START_NAMESPACE
  4435. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4436. {
  4437. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4438. #ifdef WANT_LV2
  4439. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4440. if (! plugin->init(init.filename, init.name, init.label))
  4441. {
  4442. delete plugin;
  4443. return nullptr;
  4444. }
  4445. plugin->reload();
  4446. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4447. {
  4448. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4449. delete plugin;
  4450. return nullptr;
  4451. }
  4452. return plugin;
  4453. #else
  4454. init.engine->setLastError("LV2 support not available");
  4455. return nullptr;
  4456. #endif
  4457. }
  4458. CARLA_BACKEND_END_NAMESPACE
  4459. // -------------------------------------------------------------------------------------------------------------------