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.

5544 lines
208KB

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