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.

5497 lines
206KB

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