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.

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