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.

5540 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. #include "CarlaLv2Utils.hpp"
  24. #include "CarlaMathUtils.hpp"
  25. #include "CarlaPluginUi.hpp"
  26. #include "Lv2AtomQueue.hpp"
  27. #include "../engine/CarlaEngineOsc.hpp"
  28. extern "C" {
  29. #include "rtmempool/rtmempool-lv2.h"
  30. }
  31. #include <QtCore/QDir>
  32. #include <QtCore/QFile>
  33. #include <QtCore/QUrl>
  34. // -----------------------------------------------------
  35. CARLA_BACKEND_START_NAMESPACE
  36. #if 0
  37. }
  38. #endif
  39. // Maximum default buffer size
  40. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  41. // Extra Plugin Hints
  42. const unsigned int PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  43. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  44. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  45. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  46. // Extra Parameter Hints
  47. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  48. const unsigned int PARAMETER_IS_TRIGGER = 0x2000;
  49. // LV2 Event Data/Types
  50. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  51. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  52. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  53. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  54. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  55. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  56. // LV2 URI Map Ids
  57. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  58. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  77. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  78. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  79. const uint32_t CARLA_URI_MAP_ID_ATOM_WORKER = 22; // custom
  80. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 23;
  81. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 24;
  82. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 25;
  83. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 26;
  84. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 27;
  85. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 28;
  86. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 29;
  87. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 30;
  88. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 31;
  89. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 32; // base type
  90. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 33; // values
  91. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 34;
  92. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 35;
  93. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 36;
  94. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 37;
  95. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 38;
  96. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 39;
  97. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 40;
  98. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 41;
  99. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 42;
  100. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 43;
  101. const uint32_t CARLA_URI_MAP_ID_COUNT = 44;
  102. // LV2 Feature Ids
  103. const uint32_t kFeatureIdBufSizeBounded = 0;
  104. const uint32_t kFeatureIdBufSizeFixed = 1;
  105. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  106. const uint32_t kFeatureIdEvent = 3;
  107. const uint32_t kFeatureIdHardRtCapable = 4;
  108. const uint32_t kFeatureIdInPlaceBroken = 5;
  109. const uint32_t kFeatureIdIsLive = 6;
  110. const uint32_t kFeatureIdLogs = 7;
  111. const uint32_t kFeatureIdOptions = 8;
  112. const uint32_t kFeatureIdPrograms = 9;
  113. const uint32_t kFeatureIdResizePort = 10;
  114. const uint32_t kFeatureIdRtMemPool = 11;
  115. const uint32_t kFeatureIdRtMemPoolOld = 12;
  116. const uint32_t kFeatureIdStateMakePath = 13;
  117. const uint32_t kFeatureIdStateMapPath = 14;
  118. const uint32_t kFeatureIdStrictBounds = 15;
  119. const uint32_t kFeatureIdUriMap = 16;
  120. const uint32_t kFeatureIdUridMap = 17;
  121. const uint32_t kFeatureIdUridUnmap = 18;
  122. const uint32_t kFeatureIdWorker = 19;
  123. const uint32_t kFeatureCountPlugin = 20;
  124. const uint32_t kFeatureIdUiDataAccess = 20;
  125. const uint32_t kFeatureIdUiInstanceAccess = 21;
  126. const uint32_t kFeatureIdUiIdleInterface = 22;
  127. const uint32_t kFeatureIdUiFixedSize = 23;
  128. const uint32_t kFeatureIdUiMakeResident = 24;
  129. const uint32_t kFeatureIdUiNoUserResize = 25;
  130. const uint32_t kFeatureIdUiParent = 26;
  131. const uint32_t kFeatureIdUiPortMap = 27;
  132. const uint32_t kFeatureIdUiPortSubscribe = 28;
  133. const uint32_t kFeatureIdUiResize = 29;
  134. const uint32_t kFeatureIdUiTouch = 30;
  135. const uint32_t kFeatureIdExternalUi = 31;
  136. const uint32_t kFeatureIdExternalUiOld = 32;
  137. const uint32_t kFeatureCountAll = 33;
  138. // -----------------------------------------------------
  139. struct Lv2EventData {
  140. uint32_t type;
  141. uint32_t rindex;
  142. CarlaEngineEventPort* port;
  143. union {
  144. LV2_Atom_Buffer* atom;
  145. LV2_Event_Buffer* event;
  146. LV2_MIDI midi;
  147. };
  148. Lv2EventData()
  149. : type(0x0),
  150. rindex(0),
  151. port(nullptr) {}
  152. ~Lv2EventData()
  153. {
  154. if (port != nullptr)
  155. {
  156. delete port;
  157. port = nullptr;
  158. }
  159. const uint32_t rtype(type);
  160. type = 0x0;
  161. if (rtype & CARLA_EVENT_DATA_ATOM)
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  164. std::free(atom);
  165. atom = nullptr;
  166. }
  167. else if (rtype & CARLA_EVENT_DATA_EVENT)
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  170. std::free(event);
  171. event = nullptr;
  172. }
  173. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  174. {
  175. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  176. delete[] midi.data;
  177. midi.data = nullptr;
  178. }
  179. }
  180. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  181. };
  182. struct Lv2PluginEventData {
  183. uint32_t count;
  184. Lv2EventData* data;
  185. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  186. uint32_t ctrlIndex;
  187. Lv2PluginEventData()
  188. : count(0),
  189. data(nullptr),
  190. ctrl(nullptr),
  191. ctrlIndex(0) {}
  192. ~Lv2PluginEventData()
  193. {
  194. CARLA_ASSERT_INT(count == 0, count);
  195. CARLA_ASSERT(data == nullptr);
  196. CARLA_ASSERT(ctrl == nullptr);
  197. CARLA_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  198. }
  199. void createNew(const uint32_t newCount)
  200. {
  201. CARLA_SAFE_ASSERT_INT(count == 0, count);
  202. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  203. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  204. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  205. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  206. data = new Lv2EventData[newCount];
  207. count = newCount;
  208. }
  209. void clear()
  210. {
  211. if (data != nullptr)
  212. {
  213. for (uint32_t i=0; i < count; ++i)
  214. {
  215. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  216. data[i].port = nullptr;
  217. }
  218. delete[] data;
  219. data = nullptr;
  220. }
  221. count = 0;
  222. ctrl = nullptr;
  223. ctrlIndex = 0;
  224. }
  225. void initBuffers() const noexcept
  226. {
  227. for (uint32_t i=0; i < count; ++i)
  228. {
  229. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  230. data[i].port->initBuffer();
  231. }
  232. }
  233. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginEventData)
  234. };
  235. // -----------------------------------------------------
  236. struct Lv2PluginOptions {
  237. enum OptIndex {
  238. MaxBlockLenth = 0,
  239. MinBlockLenth,
  240. SequenceSize,
  241. SampleRate,
  242. Null
  243. };
  244. int maxBufferSize;
  245. int minBufferSize;
  246. int sequenceSize;
  247. double sampleRate;
  248. LV2_Options_Option opts[5];
  249. Lv2PluginOptions()
  250. : maxBufferSize(0),
  251. minBufferSize(0),
  252. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  253. sampleRate(0.0)
  254. {
  255. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  256. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  257. optMaxBlockLenth.subject = 0;
  258. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  259. optMaxBlockLenth.size = sizeof(int);
  260. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  261. optMaxBlockLenth.value = &maxBufferSize;
  262. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  263. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  264. optMinBlockLenth.subject = 0;
  265. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  266. optMinBlockLenth.size = sizeof(int);
  267. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  268. optMinBlockLenth.value = &minBufferSize;
  269. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  270. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  271. optSequenceSize.subject = 0;
  272. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  273. optSequenceSize.size = sizeof(int);
  274. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  275. optSequenceSize.value = &sequenceSize;
  276. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  277. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  278. optSampleRate.subject = 0;
  279. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  280. optSampleRate.size = sizeof(double);
  281. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  282. optSampleRate.value = &sampleRate;
  283. LV2_Options_Option& optNull(opts[Null]);
  284. optNull.context = LV2_OPTIONS_INSTANCE;
  285. optNull.subject = 0;
  286. optNull.key = CARLA_URI_MAP_ID_NULL;
  287. optNull.size = 0;
  288. optNull.type = CARLA_URI_MAP_ID_NULL;
  289. optNull.value = nullptr;
  290. }
  291. };
  292. // -----------------------------------------------------
  293. class Lv2Plugin : public CarlaPlugin,
  294. CarlaPluginUi::CloseCallback
  295. {
  296. public:
  297. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  298. : CarlaPlugin(engine, id),
  299. fHandle(nullptr),
  300. fHandle2(nullptr),
  301. fDescriptor(nullptr),
  302. fRdfDescriptor(nullptr),
  303. fAudioInBuffers(nullptr),
  304. fAudioOutBuffers(nullptr),
  305. fCvInBuffers(nullptr),
  306. fCvOutBuffers(nullptr),
  307. fParamBuffers(nullptr),
  308. fFirstActive(true)
  309. {
  310. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  311. carla_fill<LV2_Feature*>(fFeatures, kFeatureCountAll+1, nullptr);
  312. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  313. fCustomURIDs.append(nullptr);
  314. #if defined(__clang__)
  315. # pragma clang diagnostic push
  316. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  317. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  318. # pragma GCC diagnostic push
  319. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  320. #endif
  321. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  322. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  323. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  324. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  325. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  326. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  327. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  328. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  329. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  330. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  331. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  332. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  333. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  334. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  335. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  336. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  337. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  338. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  339. #if defined(__clang__)
  340. # pragma clang diagnostic pop
  341. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  342. # pragma GCC diagnostic pop
  343. #endif
  344. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  345. }
  346. ~Lv2Plugin() override
  347. {
  348. carla_debug("Lv2Plugin::~Lv2Plugin()");
  349. // close UI
  350. if (fUi.type != UI::TYPE_NULL)
  351. {
  352. showCustomUI(false);
  353. if (fUi.type == UI::TYPE_OSC)
  354. {
  355. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  356. }
  357. else
  358. {
  359. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  360. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  361. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  362. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  363. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  364. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  365. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  366. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  367. fUi.descriptor = nullptr;
  368. pData->uiLibClose();
  369. }
  370. if (fUi.title != nullptr)
  371. {
  372. delete[] fUi.title;
  373. fUi.title = nullptr;
  374. }
  375. #ifndef LV2_UIS_ONLY_BRIDGES
  376. if (fUi.window != nullptr)
  377. {
  378. delete fUi.window;
  379. fUi.window = nullptr;
  380. }
  381. #endif
  382. fUi.rdfDescriptor = nullptr;
  383. }
  384. pData->singleMutex.lock();
  385. pData->masterMutex.lock();
  386. if (pData->client != nullptr && pData->client->isActive())
  387. pData->client->deactivate();
  388. if (pData->active)
  389. {
  390. deactivate();
  391. pData->active = false;
  392. }
  393. if (fDescriptor != nullptr)
  394. {
  395. if (fDescriptor->cleanup != nullptr)
  396. {
  397. if (fHandle != nullptr)
  398. fDescriptor->cleanup(fHandle);
  399. if (fHandle2 != nullptr)
  400. fDescriptor->cleanup(fHandle2);
  401. }
  402. fHandle = nullptr;
  403. fHandle2 = nullptr;
  404. fDescriptor = nullptr;
  405. }
  406. if (fRdfDescriptor != nullptr)
  407. {
  408. delete fRdfDescriptor;
  409. fRdfDescriptor = nullptr;
  410. }
  411. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  412. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  413. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  414. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  415. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  416. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  417. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  418. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  419. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  420. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  421. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  422. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  423. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  424. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  425. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  426. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  427. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  428. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  429. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  430. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  431. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  432. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  433. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  434. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  435. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  436. {
  437. if (fFeatures[i] != nullptr)
  438. {
  439. delete fFeatures[i];
  440. fFeatures[i] = nullptr;
  441. }
  442. }
  443. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  444. {
  445. const char* const uri(it.getValue());
  446. if (uri != nullptr)
  447. delete[] uri;
  448. }
  449. fCustomURIDs.clear();
  450. clearBuffers();
  451. }
  452. // -------------------------------------------------------------------
  453. // Information (base)
  454. PluginType getType() const noexcept override
  455. {
  456. return PLUGIN_LV2;
  457. }
  458. PluginCategory getCategory() const noexcept override
  459. {
  460. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  461. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  462. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  463. if (LV2_IS_DELAY(cat1, cat2))
  464. return PLUGIN_CATEGORY_DELAY;
  465. if (LV2_IS_DISTORTION(cat1, cat2))
  466. return PLUGIN_CATEGORY_OTHER;
  467. if (LV2_IS_DYNAMICS(cat1, cat2))
  468. return PLUGIN_CATEGORY_DYNAMICS;
  469. if (LV2_IS_EQ(cat1, cat2))
  470. return PLUGIN_CATEGORY_EQ;
  471. if (LV2_IS_FILTER(cat1, cat2))
  472. return PLUGIN_CATEGORY_FILTER;
  473. if (LV2_IS_GENERATOR(cat1, cat2))
  474. return PLUGIN_CATEGORY_SYNTH;
  475. if (LV2_IS_MODULATOR(cat1, cat2))
  476. return PLUGIN_CATEGORY_MODULATOR;
  477. if (LV2_IS_REVERB(cat1, cat2))
  478. return PLUGIN_CATEGORY_DELAY;
  479. if (LV2_IS_SIMULATOR(cat1, cat2))
  480. return PLUGIN_CATEGORY_OTHER;
  481. if (LV2_IS_SPATIAL(cat1, cat2))
  482. return PLUGIN_CATEGORY_OTHER;
  483. if (LV2_IS_SPECTRAL(cat1, cat2))
  484. return PLUGIN_CATEGORY_UTILITY;
  485. if (LV2_IS_UTILITY(cat1, cat2))
  486. return PLUGIN_CATEGORY_UTILITY;
  487. return CarlaPlugin::getCategory();
  488. }
  489. int64_t getUniqueId() const noexcept override
  490. {
  491. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  492. return static_cast<int64_t>(fRdfDescriptor->UniqueID);
  493. }
  494. // -------------------------------------------------------------------
  495. // Information (count)
  496. uint32_t getMidiInCount() const noexcept override
  497. {
  498. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  499. uint32_t count = 0;
  500. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  501. {
  502. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  503. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  504. ++count;
  505. }
  506. return count;
  507. }
  508. uint32_t getMidiOutCount() const noexcept override
  509. {
  510. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  511. uint32_t count = 0;
  512. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  513. {
  514. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  515. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  516. ++count;
  517. }
  518. return count;
  519. }
  520. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  521. {
  522. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  523. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  524. const int32_t rindex(pData->param.data[parameterId].rindex);
  525. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  526. {
  527. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  528. return port->ScalePointCount;
  529. }
  530. return 0;
  531. }
  532. // -------------------------------------------------------------------
  533. // Information (current data)
  534. // nothing
  535. // -------------------------------------------------------------------
  536. // Information (per-plugin data)
  537. unsigned int getOptionsAvailable() const noexcept override
  538. {
  539. const bool hasMidiIn(getMidiInCount() > 0);
  540. unsigned int options = 0x0;
  541. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  542. if (! (hasMidiIn || needsFixedBuffer()))
  543. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  544. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  545. {
  546. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  547. options |= PLUGIN_OPTION_FORCE_STEREO;
  548. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  549. options |= PLUGIN_OPTION_FORCE_STEREO;
  550. }
  551. if (hasMidiIn)
  552. {
  553. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  554. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  555. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  556. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  557. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  558. }
  559. return options;
  560. }
  561. float getParameterValue(const uint32_t parameterId) const noexcept override
  562. {
  563. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  564. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  565. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  566. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  567. return fParamBuffers[parameterId];
  568. }
  569. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  570. {
  571. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  572. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  573. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  574. const int32_t rindex(pData->param.data[parameterId].rindex);
  575. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  576. {
  577. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  578. if (scalePointId < port->ScalePointCount)
  579. {
  580. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  581. return portScalePoint->Value;
  582. }
  583. }
  584. return 0.0f;
  585. }
  586. void getLabel(char* const strBuf) const noexcept override
  587. {
  588. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  589. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  590. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  591. }
  592. void getMaker(char* const strBuf) const noexcept override
  593. {
  594. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  595. if (fRdfDescriptor->Author != nullptr)
  596. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  597. else
  598. CarlaPlugin::getMaker(strBuf);
  599. }
  600. void getCopyright(char* const strBuf) const noexcept override
  601. {
  602. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  603. if (fRdfDescriptor->License != nullptr)
  604. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  605. else
  606. CarlaPlugin::getCopyright(strBuf);
  607. }
  608. void getRealName(char* const strBuf) const noexcept override
  609. {
  610. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  611. if (fRdfDescriptor->Name != nullptr)
  612. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  613. else
  614. CarlaPlugin::getRealName(strBuf);
  615. }
  616. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  617. {
  618. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  619. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  620. const int32_t rindex(pData->param.data[parameterId].rindex);
  621. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  622. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  623. else
  624. CarlaPlugin::getParameterName(parameterId, strBuf);
  625. }
  626. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  627. {
  628. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  629. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  630. const int32_t rindex(pData->param.data[parameterId].rindex);
  631. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  632. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  633. else
  634. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  635. }
  636. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  637. {
  638. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  639. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  640. const int32_t rindex(pData->param.data[parameterId].rindex);
  641. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  642. {
  643. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  644. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  645. {
  646. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  647. return;
  648. }
  649. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  650. {
  651. switch (port->Unit.Unit)
  652. {
  653. case LV2_PORT_UNIT_BAR:
  654. std::strncpy(strBuf, "bars", STR_MAX);
  655. return;
  656. case LV2_PORT_UNIT_BEAT:
  657. std::strncpy(strBuf, "beats", STR_MAX);
  658. return;
  659. case LV2_PORT_UNIT_BPM:
  660. std::strncpy(strBuf, "BPM", STR_MAX);
  661. return;
  662. case LV2_PORT_UNIT_CENT:
  663. std::strncpy(strBuf, "ct", STR_MAX);
  664. return;
  665. case LV2_PORT_UNIT_CM:
  666. std::strncpy(strBuf, "cm", STR_MAX);
  667. return;
  668. case LV2_PORT_UNIT_COEF:
  669. std::strncpy(strBuf, "(coef)", STR_MAX);
  670. return;
  671. case LV2_PORT_UNIT_DB:
  672. std::strncpy(strBuf, "dB", STR_MAX);
  673. return;
  674. case LV2_PORT_UNIT_DEGREE:
  675. std::strncpy(strBuf, "deg", STR_MAX);
  676. return;
  677. case LV2_PORT_UNIT_FRAME:
  678. std::strncpy(strBuf, "frames", STR_MAX);
  679. return;
  680. case LV2_PORT_UNIT_HZ:
  681. std::strncpy(strBuf, "Hz", STR_MAX);
  682. return;
  683. case LV2_PORT_UNIT_INCH:
  684. std::strncpy(strBuf, "in", STR_MAX);
  685. return;
  686. case LV2_PORT_UNIT_KHZ:
  687. std::strncpy(strBuf, "kHz", STR_MAX);
  688. return;
  689. case LV2_PORT_UNIT_KM:
  690. std::strncpy(strBuf, "km", STR_MAX);
  691. return;
  692. case LV2_PORT_UNIT_M:
  693. std::strncpy(strBuf, "m", STR_MAX);
  694. return;
  695. case LV2_PORT_UNIT_MHZ:
  696. std::strncpy(strBuf, "MHz", STR_MAX);
  697. return;
  698. case LV2_PORT_UNIT_MIDINOTE:
  699. std::strncpy(strBuf, "note", STR_MAX);
  700. return;
  701. case LV2_PORT_UNIT_MILE:
  702. std::strncpy(strBuf, "mi", STR_MAX);
  703. return;
  704. case LV2_PORT_UNIT_MIN:
  705. std::strncpy(strBuf, "min", STR_MAX);
  706. return;
  707. case LV2_PORT_UNIT_MM:
  708. std::strncpy(strBuf, "mm", STR_MAX);
  709. return;
  710. case LV2_PORT_UNIT_MS:
  711. std::strncpy(strBuf, "ms", STR_MAX);
  712. return;
  713. case LV2_PORT_UNIT_OCT:
  714. std::strncpy(strBuf, "oct", STR_MAX);
  715. return;
  716. case LV2_PORT_UNIT_PC:
  717. std::strncpy(strBuf, "%", STR_MAX);
  718. return;
  719. case LV2_PORT_UNIT_S:
  720. std::strncpy(strBuf, "s", STR_MAX);
  721. return;
  722. case LV2_PORT_UNIT_SEMITONE:
  723. std::strncpy(strBuf, "semi", STR_MAX);
  724. return;
  725. }
  726. }
  727. }
  728. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  729. }
  730. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  731. {
  732. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  733. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  734. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  735. const int32_t rindex(pData->param.data[parameterId].rindex);
  736. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  737. {
  738. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  739. if (scalePointId < port->ScalePointCount)
  740. {
  741. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  742. if (portScalePoint->Label != nullptr)
  743. {
  744. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  745. return;
  746. }
  747. }
  748. }
  749. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  750. }
  751. // -------------------------------------------------------------------
  752. // Set data (state)
  753. void prepareForSave() override
  754. {
  755. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  756. if (fExt.state != nullptr && fExt.state->save != nullptr)
  757. {
  758. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  759. if (fHandle2 != nullptr)
  760. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  761. }
  762. }
  763. // -------------------------------------------------------------------
  764. // Set data (internal stuff)
  765. void setName(const char* const newName) override
  766. {
  767. CarlaPlugin::setName(newName);
  768. if (fUi.title == nullptr)
  769. return;
  770. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  771. delete[] fUi.title;
  772. fUi.title = carla_strdup(guiTitle.toUtf8().constData());
  773. #ifndef LV2_UIS_ONLY_BRIDGES
  774. if (fUi.window != nullptr)
  775. fUi.window->setTitle(fUi.title);
  776. #endif
  777. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  778. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fUi.title;
  779. }
  780. // -------------------------------------------------------------------
  781. // Set data (plugin-specific stuff)
  782. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  783. {
  784. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  785. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  786. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  787. fParamBuffers[parameterId] = fixedValue;
  788. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  789. }
  790. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  791. {
  792. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  793. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  794. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  795. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  796. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  797. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  798. // we should only call state restore once
  799. // so inject this in CarlaPlugin::loadSaveState
  800. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "CarlaLoadLv2StateNow") == 0 && std::strcmp(value, "true") == 0)
  801. {
  802. if (fExt.state == nullptr)
  803. return;
  804. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  805. {
  806. const ScopedSingleProcessLocker spl(this, true);
  807. try {
  808. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  809. } catch(...) {}
  810. if (fHandle2 != nullptr)
  811. {
  812. try {
  813. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  814. } catch(...) {}
  815. }
  816. }
  817. switch (status)
  818. {
  819. case LV2_STATE_SUCCESS:
  820. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  821. break;
  822. case LV2_STATE_ERR_UNKNOWN:
  823. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  824. break;
  825. case LV2_STATE_ERR_BAD_TYPE:
  826. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  827. break;
  828. case LV2_STATE_ERR_BAD_FLAGS:
  829. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  830. break;
  831. case LV2_STATE_ERR_NO_FEATURE:
  832. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  833. break;
  834. case LV2_STATE_ERR_NO_PROPERTY:
  835. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  836. break;
  837. }
  838. return;
  839. }
  840. CarlaPlugin::setCustomData(type, key, value, sendGui);
  841. }
  842. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  843. {
  844. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  845. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  846. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  847. {
  848. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fRdfDescriptor->Presets[index].URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  849. {
  850. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  851. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  852. if (fHandle2 != nullptr)
  853. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  854. }
  855. }
  856. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  857. }
  858. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  859. {
  860. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  861. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  862. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  863. {
  864. const uint32_t bank(pData->midiprog.data[index].bank);
  865. const uint32_t program(pData->midiprog.data[index].program);
  866. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  867. try {
  868. fExt.programs->select_program(fHandle, bank, program);
  869. } catch(...) {}
  870. if (fHandle2 != nullptr)
  871. {
  872. try {
  873. fExt.programs->select_program(fHandle2, bank, program);
  874. } catch(...) {}
  875. }
  876. }
  877. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  878. }
  879. // -------------------------------------------------------------------
  880. // Set ui stuff
  881. void showCustomUI(const bool yesNo) override
  882. {
  883. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  884. if (! yesNo)
  885. pData->transientTryCounter = 0;
  886. if (fUi.type == UI::TYPE_OSC)
  887. {
  888. if (yesNo)
  889. {
  890. pData->osc.data.free();
  891. pData->osc.thread.startThread();
  892. }
  893. else
  894. {
  895. if (pData->osc.data.target != nullptr)
  896. {
  897. osc_send_hide(pData->osc.data);
  898. osc_send_quit(pData->osc.data);
  899. pData->osc.data.free();
  900. }
  901. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  902. }
  903. return;
  904. }
  905. // take some precautions
  906. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  907. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr,);
  908. if (yesNo)
  909. {
  910. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->instantiate != nullptr,);
  911. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->cleanup != nullptr,);
  912. }
  913. else
  914. {
  915. if (fUi.handle == nullptr)
  916. return;
  917. }
  918. if (yesNo)
  919. {
  920. if (fUi.handle == nullptr)
  921. {
  922. #ifndef LV2_UIS_ONLY_BRIDGES
  923. if (fUi.type == UI::TYPE_EMBED)
  924. {
  925. const char* msg = nullptr;
  926. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  927. switch (fUi.rdfDescriptor->Type)
  928. {
  929. case LV2_UI_GTK2:
  930. case LV2_UI_GTK3:
  931. case LV2_UI_QT4:
  932. case LV2_UI_QT5:
  933. case LV2_UI_EXTERNAL:
  934. case LV2_UI_OLD_EXTERNAL:
  935. msg = "Invalid UI type";
  936. break;
  937. case LV2_UI_COCOA:
  938. # ifdef CARLA_OS_MAC
  939. fUi.window = CarlaPluginUi::newCocoa(this, frontendWinId);
  940. # else
  941. msg = "UI is for MacOS only";
  942. # endif
  943. break;
  944. case LV2_UI_WINDOWS:
  945. # ifdef CARLA_OS_WIN
  946. fUi.window = CarlaPluginUi::newWindows(this, frontendWinId);
  947. # else
  948. msg = "UI is for Windows only";
  949. # endif
  950. break;
  951. case LV2_UI_X11:
  952. # ifdef HAVE_X11
  953. fUi.window = CarlaPluginUi::newX11(this, frontendWinId);
  954. # else
  955. msg = "UI is only for systems with X11";
  956. # endif
  957. break;
  958. default:
  959. msg = "Unknown UI type";
  960. break;
  961. }
  962. if (fUi.window == nullptr)
  963. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  964. fUi.window->setTitle(fUi.title);
  965. fFeatures[kFeatureIdUiParent]->data = fUi.window->getPtr();
  966. }
  967. #endif
  968. fUi.widget = nullptr;
  969. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  970. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  971. }
  972. CARLA_SAFE_ASSERT(fUi.handle != nullptr);
  973. CARLA_SAFE_ASSERT(fUi.type != UI::TYPE_EXTERNAL || fUi.widget != nullptr);
  974. if (fUi.handle == nullptr || (fUi.type == UI::TYPE_EXTERNAL && fUi.widget == nullptr))
  975. {
  976. fUi.widget = nullptr;
  977. if (fUi.handle != nullptr)
  978. {
  979. fUi.descriptor->cleanup(fUi.handle);
  980. fUi.handle = nullptr;
  981. }
  982. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  983. }
  984. updateUi();
  985. #ifndef LV2_UIS_ONLY_BRIDGES
  986. if (fUi.type == UI::TYPE_EMBED)
  987. {
  988. fUi.window->show();
  989. }
  990. else
  991. #endif
  992. {
  993. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  994. pData->tryTransient();
  995. }
  996. }
  997. else
  998. {
  999. #ifndef LV2_UIS_ONLY_BRIDGES
  1000. if (fUi.type == UI::TYPE_EMBED)
  1001. {
  1002. CARLA_SAFE_ASSERT(fUi.window != nullptr);
  1003. if (fUi.window != nullptr)
  1004. fUi.window->hide();
  1005. }
  1006. else
  1007. #endif
  1008. {
  1009. CARLA_SAFE_ASSERT(fUi.widget != nullptr);
  1010. if (fUi.widget != nullptr)
  1011. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  1012. }
  1013. fUi.descriptor->cleanup(fUi.handle);
  1014. fUi.handle = nullptr;
  1015. fUi.widget = nullptr;
  1016. }
  1017. }
  1018. void idle() override
  1019. {
  1020. if (! fAtomQueueOut.isEmpty())
  1021. {
  1022. char dumpBuf[fAtomQueueOut.getSize()];
  1023. Lv2AtomQueue tmpQueue;
  1024. tmpQueue.copyAndDumpDataFromQueue(fAtomQueueOut, dumpBuf);
  1025. CARLA_SAFE_ASSERT(! tmpQueue.isEmpty());
  1026. uint32_t portIndex;
  1027. const LV2_Atom* atom;
  1028. const bool hasPortEvent(fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr);
  1029. for (; tmpQueue.get(&atom, &portIndex);)
  1030. {
  1031. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  1032. {
  1033. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1034. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1035. }
  1036. else if (fUi.type == UI::TYPE_OSC)
  1037. {
  1038. if (pData->osc.data.target != nullptr)
  1039. {
  1040. QByteArray chunk((const char*)atom, static_cast<int>(lv2_atom_total_size(atom)));
  1041. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.toBase64().constData());
  1042. }
  1043. }
  1044. else
  1045. {
  1046. if (hasPortEvent)
  1047. fUi.descriptor->port_event(fUi.handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1048. }
  1049. }
  1050. }
  1051. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  1052. {
  1053. if (fUi.type == UI::TYPE_EXTERNAL && fUi.widget != nullptr)
  1054. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  1055. #ifndef LV2_UIS_ONLY_BRIDGES
  1056. else if (fUi.type == UI::TYPE_EMBED && fUi.window != nullptr)
  1057. fUi.window->idle();
  1058. if (fExt.uiidle != nullptr && fExt.uiidle->idle(fUi.handle) != 0)
  1059. {
  1060. showCustomUI(false);
  1061. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1062. }
  1063. #endif
  1064. }
  1065. CarlaPlugin::idle();
  1066. }
  1067. // -------------------------------------------------------------------
  1068. // Plugin state
  1069. void reload() override
  1070. {
  1071. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1072. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1073. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1074. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1075. carla_debug("Lv2Plugin::reload() - start");
  1076. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1077. // Safely disable plugin for reload
  1078. const ScopedDisabler sd(this);
  1079. if (pData->active)
  1080. deactivate();
  1081. clearBuffers();
  1082. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1083. const uint32_t portCount(fRdfDescriptor->PortCount);
  1084. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1085. aIns = aOuts = cvIns = cvOuts = params = 0;
  1086. LinkedList<unsigned int> evIns, evOuts;
  1087. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1088. bool forcedStereoIn, forcedStereoOut;
  1089. forcedStereoIn = forcedStereoOut = false;
  1090. bool needsCtrlIn, needsCtrlOut;
  1091. needsCtrlIn = needsCtrlOut = false;
  1092. for (uint32_t i=0; i < portCount; ++i)
  1093. {
  1094. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1095. if (LV2_IS_PORT_AUDIO(portTypes))
  1096. {
  1097. if (LV2_IS_PORT_INPUT(portTypes))
  1098. aIns += 1;
  1099. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1100. aOuts += 1;
  1101. }
  1102. else if (LV2_IS_PORT_CV(portTypes))
  1103. {
  1104. if (LV2_IS_PORT_INPUT(portTypes))
  1105. cvIns += 1;
  1106. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1107. cvOuts += 1;
  1108. }
  1109. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1110. {
  1111. if (LV2_IS_PORT_INPUT(portTypes))
  1112. evIns.append(CARLA_EVENT_DATA_ATOM);
  1113. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1114. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1115. }
  1116. else if (LV2_IS_PORT_EVENT(portTypes))
  1117. {
  1118. if (LV2_IS_PORT_INPUT(portTypes))
  1119. evIns.append(CARLA_EVENT_DATA_EVENT);
  1120. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1121. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1122. }
  1123. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1124. {
  1125. if (LV2_IS_PORT_INPUT(portTypes))
  1126. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1127. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1128. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1129. }
  1130. else if (LV2_IS_PORT_CONTROL(portTypes))
  1131. params += 1;
  1132. }
  1133. recheckExtensions();
  1134. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1135. {
  1136. if (fHandle2 == nullptr)
  1137. {
  1138. try {
  1139. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1140. } catch(...) {}
  1141. }
  1142. if (fHandle2 != nullptr)
  1143. {
  1144. if (aIns == 1)
  1145. {
  1146. aIns = 2;
  1147. forcedStereoIn = true;
  1148. }
  1149. if (aOuts == 1)
  1150. {
  1151. aOuts = 2;
  1152. forcedStereoOut = true;
  1153. }
  1154. }
  1155. }
  1156. if (aIns > 0)
  1157. {
  1158. pData->audioIn.createNew(aIns);
  1159. fAudioInBuffers = new float*[aIns];
  1160. for (uint32_t i=0; i < aIns; ++i)
  1161. fAudioInBuffers[i] = nullptr;
  1162. }
  1163. if (aOuts > 0)
  1164. {
  1165. pData->audioOut.createNew(aOuts);
  1166. fAudioOutBuffers = new float*[aOuts];
  1167. needsCtrlIn = true;
  1168. for (uint32_t i=0; i < aOuts; ++i)
  1169. fAudioOutBuffers[i] = nullptr;
  1170. }
  1171. if (cvIns > 0)
  1172. {
  1173. fCvIn.createNew(cvIns);
  1174. fCvInBuffers = new float*[cvIns];
  1175. for (uint32_t i=0; i < cvIns; ++i)
  1176. fCvInBuffers[i] = nullptr;
  1177. }
  1178. if (cvOuts > 0)
  1179. {
  1180. fCvOut.createNew(cvOuts);
  1181. fCvOutBuffers = new float*[cvOuts];
  1182. for (uint32_t i=0; i < cvOuts; ++i)
  1183. fCvOutBuffers[i] = nullptr;
  1184. }
  1185. if (params > 0)
  1186. {
  1187. pData->param.createNew(params, true);
  1188. fParamBuffers = new float[params];
  1189. FLOAT_CLEAR(fParamBuffers, params);
  1190. }
  1191. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1192. {
  1193. fEventsIn.createNew(count);
  1194. for (uint32_t i=0; i < count; ++i)
  1195. {
  1196. const uint32_t& type(evIns.getAt(i));
  1197. if (type == CARLA_EVENT_DATA_ATOM)
  1198. {
  1199. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1200. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1201. }
  1202. else if (type == CARLA_EVENT_DATA_EVENT)
  1203. {
  1204. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1205. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1206. }
  1207. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1208. {
  1209. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1210. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1211. fEventsIn.data[i].midi.data = new unsigned char[eventBufferSize];
  1212. }
  1213. }
  1214. }
  1215. else
  1216. {
  1217. fEventsIn.createNew(1);
  1218. fEventsIn.ctrl = &fEventsIn.data[0];
  1219. }
  1220. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1221. {
  1222. fEventsOut.createNew(count);
  1223. for (uint32_t i=0; i < count; ++i)
  1224. {
  1225. const uint32_t& type(evOuts.getAt(i));
  1226. if (type == CARLA_EVENT_DATA_ATOM)
  1227. {
  1228. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1229. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1230. }
  1231. else if (type == CARLA_EVENT_DATA_EVENT)
  1232. {
  1233. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1234. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1235. }
  1236. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1237. {
  1238. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1239. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1240. fEventsOut.data[i].midi.data = new unsigned char[eventBufferSize];
  1241. }
  1242. }
  1243. }
  1244. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1245. CarlaString portName;
  1246. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1247. {
  1248. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1249. portName.clear();
  1250. 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))
  1251. {
  1252. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1253. {
  1254. portName = pData->name;
  1255. portName += ":";
  1256. }
  1257. portName += fRdfDescriptor->Ports[i].Name;
  1258. portName.truncate(portNameSize);
  1259. }
  1260. if (LV2_IS_PORT_AUDIO(portTypes))
  1261. {
  1262. if (LV2_IS_PORT_INPUT(portTypes))
  1263. {
  1264. uint32_t j = iAudioIn++;
  1265. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1266. pData->audioIn.ports[j].rindex = i;
  1267. if (forcedStereoIn)
  1268. {
  1269. portName += "_2";
  1270. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1271. pData->audioIn.ports[1].rindex = i;
  1272. }
  1273. }
  1274. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1275. {
  1276. uint32_t j = iAudioOut++;
  1277. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1278. pData->audioOut.ports[j].rindex = i;
  1279. if (forcedStereoOut)
  1280. {
  1281. portName += "_2";
  1282. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1283. pData->audioOut.ports[1].rindex = i;
  1284. }
  1285. }
  1286. else
  1287. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1288. }
  1289. else if (LV2_IS_PORT_CV(portTypes))
  1290. {
  1291. if (LV2_IS_PORT_INPUT(portTypes))
  1292. {
  1293. uint32_t j = iCvIn++;
  1294. fCvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1295. fCvIn.ports[j].rindex = i;
  1296. }
  1297. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1298. {
  1299. uint32_t j = iCvOut++;
  1300. fCvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1301. fCvOut.ports[j].rindex = i;
  1302. }
  1303. else
  1304. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1305. }
  1306. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1307. {
  1308. if (LV2_IS_PORT_INPUT(portTypes))
  1309. {
  1310. uint32_t j = iEvIn++;
  1311. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1312. if (fHandle2 != nullptr)
  1313. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1314. fEventsIn.data[j].rindex = i;
  1315. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1316. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1317. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1318. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1319. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1320. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1321. if (evIns.count() == 1)
  1322. {
  1323. fEventsIn.ctrl = &fEventsIn.data[j];
  1324. fEventsIn.ctrlIndex = j;
  1325. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1326. needsCtrlIn = true;
  1327. }
  1328. else
  1329. {
  1330. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1331. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1332. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1333. {
  1334. fEventsIn.ctrl = &fEventsIn.data[j];
  1335. fEventsIn.ctrlIndex = j;
  1336. }
  1337. }
  1338. }
  1339. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1340. {
  1341. uint32_t j = iEvOut++;
  1342. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1343. if (fHandle2 != nullptr)
  1344. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1345. fEventsOut.data[j].rindex = i;
  1346. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1347. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1348. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1349. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1350. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1351. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1352. if (evOuts.count() == 1)
  1353. {
  1354. fEventsOut.ctrl = &fEventsOut.data[j];
  1355. fEventsOut.ctrlIndex = j;
  1356. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1357. needsCtrlOut = true;
  1358. }
  1359. else
  1360. {
  1361. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1362. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1363. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1364. {
  1365. fEventsOut.ctrl = &fEventsOut.data[j];
  1366. fEventsOut.ctrlIndex = j;
  1367. }
  1368. }
  1369. }
  1370. else
  1371. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1372. }
  1373. else if (LV2_IS_PORT_EVENT(portTypes))
  1374. {
  1375. if (LV2_IS_PORT_INPUT(portTypes))
  1376. {
  1377. uint32_t j = iEvIn++;
  1378. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1379. if (fHandle2 != nullptr)
  1380. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1381. fEventsIn.data[j].rindex = i;
  1382. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1383. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1384. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1385. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1386. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1387. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1388. if (evIns.count() == 1)
  1389. {
  1390. fEventsIn.ctrl = &fEventsIn.data[j];
  1391. fEventsIn.ctrlIndex = j;
  1392. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1393. needsCtrlIn = true;
  1394. }
  1395. else
  1396. {
  1397. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1398. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1399. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1400. {
  1401. fEventsIn.ctrl = &fEventsIn.data[j];
  1402. fEventsIn.ctrlIndex = j;
  1403. }
  1404. }
  1405. }
  1406. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1407. {
  1408. uint32_t j = iEvOut++;
  1409. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1410. if (fHandle2 != nullptr)
  1411. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1412. fEventsOut.data[j].rindex = i;
  1413. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1414. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1415. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1416. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1417. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1418. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1419. if (evOuts.count() == 1)
  1420. {
  1421. fEventsOut.ctrl = &fEventsOut.data[j];
  1422. fEventsOut.ctrlIndex = j;
  1423. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1424. needsCtrlOut = true;
  1425. }
  1426. else
  1427. {
  1428. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1429. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1430. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1431. {
  1432. fEventsOut.ctrl = &fEventsOut.data[j];
  1433. fEventsOut.ctrlIndex = j;
  1434. }
  1435. }
  1436. }
  1437. else
  1438. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1439. }
  1440. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1441. {
  1442. if (LV2_IS_PORT_INPUT(portTypes))
  1443. {
  1444. uint32_t j = iEvIn++;
  1445. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1446. if (fHandle2 != nullptr)
  1447. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1448. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1449. fEventsIn.data[j].rindex = i;
  1450. if (evIns.count() == 1)
  1451. {
  1452. needsCtrlIn = true;
  1453. fEventsIn.ctrl = &fEventsIn.data[j];
  1454. fEventsIn.ctrlIndex = j;
  1455. }
  1456. else
  1457. {
  1458. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1459. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1460. {
  1461. fEventsIn.ctrl = &fEventsIn.data[j];
  1462. fEventsIn.ctrlIndex = j;
  1463. }
  1464. }
  1465. }
  1466. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1467. {
  1468. uint32_t j = iEvOut++;
  1469. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1470. if (fHandle2 != nullptr)
  1471. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1472. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1473. fEventsOut.data[j].rindex = i;
  1474. if (evOuts.count() == 1)
  1475. {
  1476. needsCtrlOut = true;
  1477. fEventsOut.ctrl = &fEventsOut.data[j];
  1478. fEventsOut.ctrlIndex = j;
  1479. }
  1480. else
  1481. {
  1482. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1483. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1484. {
  1485. fEventsOut.ctrl = &fEventsOut.data[j];
  1486. fEventsOut.ctrlIndex = j;
  1487. }
  1488. }
  1489. }
  1490. else
  1491. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1492. }
  1493. else if (LV2_IS_PORT_CONTROL(portTypes))
  1494. {
  1495. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1496. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1497. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1498. uint32_t j = iCtrl++;
  1499. pData->param.data[j].hints = 0x0;
  1500. pData->param.data[j].index = static_cast<int32_t>(j);
  1501. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1502. pData->param.data[j].midiCC = -1;
  1503. pData->param.data[j].midiChannel = 0;
  1504. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  1505. float min, max, def, step, stepSmall, stepLarge;
  1506. // min value
  1507. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1508. min = portPoints.Minimum;
  1509. else
  1510. min = 0.0f;
  1511. // max value
  1512. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1513. max = portPoints.Maximum;
  1514. else
  1515. max = 1.0f;
  1516. if (min > max)
  1517. max = min;
  1518. // stupid hack for ir.lv2 (broken plugin)
  1519. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1520. {
  1521. min = 0.0f;
  1522. max = (float)0xffffff;
  1523. }
  1524. if (max - min == 0.0f)
  1525. {
  1526. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1527. max = min + 0.1f;
  1528. }
  1529. // default value
  1530. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1531. {
  1532. def = portPoints.Default;
  1533. }
  1534. else
  1535. {
  1536. // no default value
  1537. if (min < 0.0f && max > 0.0f)
  1538. def = 0.0f;
  1539. else
  1540. def = min;
  1541. }
  1542. if (def < min)
  1543. def = min;
  1544. else if (def > max)
  1545. def = max;
  1546. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1547. {
  1548. min *= sampleRate;
  1549. max *= sampleRate;
  1550. def *= sampleRate;
  1551. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1552. }
  1553. if (LV2_IS_PORT_TOGGLED(portProps))
  1554. {
  1555. step = max - min;
  1556. stepSmall = step;
  1557. stepLarge = step;
  1558. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1559. }
  1560. else if (LV2_IS_PORT_INTEGER(portProps))
  1561. {
  1562. step = 1.0f;
  1563. stepSmall = 1.0f;
  1564. stepLarge = 10.0f;
  1565. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1566. }
  1567. else
  1568. {
  1569. float range = max - min;
  1570. step = range/100.0f;
  1571. stepSmall = range/1000.0f;
  1572. stepLarge = range/10.0f;
  1573. }
  1574. if (LV2_IS_PORT_INPUT(portTypes))
  1575. {
  1576. pData->param.data[j].type = PARAMETER_INPUT;
  1577. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1578. {
  1579. carla_stderr("Plugin has latency input port, this should not happen!");
  1580. }
  1581. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1582. {
  1583. def = sampleRate;
  1584. step = 1.0f;
  1585. stepSmall = 1.0f;
  1586. stepLarge = 1.0f;
  1587. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1588. }
  1589. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1590. {
  1591. pData->param.special[j] = PARAMETER_SPECIAL_LV2_FREEWHEEL;
  1592. }
  1593. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1594. {
  1595. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1596. }
  1597. else
  1598. {
  1599. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1600. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1601. needsCtrlIn = true;
  1602. }
  1603. // MIDI CC value
  1604. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1605. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1606. {
  1607. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1608. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  1609. }
  1610. }
  1611. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1612. {
  1613. pData->param.data[j].type = PARAMETER_OUTPUT;
  1614. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1615. {
  1616. min = 0.0f;
  1617. max = sampleRate;
  1618. def = 0.0f;
  1619. step = 1.0f;
  1620. stepSmall = 1.0f;
  1621. stepLarge = 1.0f;
  1622. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1623. }
  1624. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1625. {
  1626. def = sampleRate;
  1627. step = 1.0f;
  1628. stepSmall = 1.0f;
  1629. stepLarge = 1.0f;
  1630. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1631. }
  1632. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1633. {
  1634. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1635. }
  1636. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1637. {
  1638. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1639. }
  1640. else
  1641. {
  1642. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1643. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1644. needsCtrlOut = true;
  1645. }
  1646. }
  1647. else
  1648. {
  1649. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1650. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1651. }
  1652. // extra parameter hints
  1653. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1654. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1655. if (LV2_IS_PORT_TRIGGER(portProps))
  1656. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1657. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1658. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1659. if (LV2_IS_PORT_ENUMERATION(portProps))
  1660. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1661. // check if parameter is not enabled or automable
  1662. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1663. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1664. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1665. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1666. pData->param.ranges[j].min = min;
  1667. pData->param.ranges[j].max = max;
  1668. pData->param.ranges[j].def = def;
  1669. pData->param.ranges[j].step = step;
  1670. pData->param.ranges[j].stepSmall = stepSmall;
  1671. pData->param.ranges[j].stepLarge = stepLarge;
  1672. // Start parameters in their default values (except freewheel, which is off by default)
  1673. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1674. fParamBuffers[j] = min;
  1675. else
  1676. fParamBuffers[j] = def;
  1677. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1678. if (fHandle2 != nullptr)
  1679. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1680. }
  1681. else
  1682. {
  1683. // Port Type not supported, but it's optional anyway
  1684. fDescriptor->connect_port(fHandle, i, nullptr);
  1685. if (fHandle2 != nullptr)
  1686. fDescriptor->connect_port(fHandle2, i, nullptr);
  1687. }
  1688. }
  1689. if (needsCtrlIn)
  1690. {
  1691. portName.clear();
  1692. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1693. {
  1694. portName = pData->name;
  1695. portName += ":";
  1696. }
  1697. portName += "events-in";
  1698. portName.truncate(portNameSize);
  1699. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1700. }
  1701. if (needsCtrlOut)
  1702. {
  1703. portName.clear();
  1704. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1705. {
  1706. portName = pData->name;
  1707. portName += ":";
  1708. }
  1709. portName += "events-out";
  1710. portName.truncate(portNameSize);
  1711. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1712. }
  1713. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1714. fAtomQueueIn.createBuffer(eventBufferSize);
  1715. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1716. fAtomQueueOut.createBuffer(eventBufferSize);
  1717. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1718. fEventsIn.ctrl->port = pData->event.portIn;
  1719. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1720. fEventsOut.ctrl->port = pData->event.portOut;
  1721. if (forcedStereoIn || forcedStereoOut)
  1722. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1723. else
  1724. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1725. // plugin hints
  1726. pData->hints = 0x0;
  1727. if (isRealtimeSafe())
  1728. pData->hints |= PLUGIN_IS_RTSAFE;
  1729. if (fUi.type != UI::TYPE_NULL)
  1730. {
  1731. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1732. if (fUi.type == UI::TYPE_EMBED)
  1733. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1734. }
  1735. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1736. pData->hints |= PLUGIN_IS_SYNTH;
  1737. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1738. pData->hints |= PLUGIN_CAN_DRYWET;
  1739. if (aOuts > 0)
  1740. pData->hints |= PLUGIN_CAN_VOLUME;
  1741. if (aOuts >= 2 && aOuts % 2 == 0)
  1742. pData->hints |= PLUGIN_CAN_BALANCE;
  1743. // extra plugin hints
  1744. pData->extraHints = 0x0;
  1745. if (fExt.state != nullptr || fExt.worker != nullptr)
  1746. {
  1747. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1748. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1749. }
  1750. else
  1751. {
  1752. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1753. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1754. }
  1755. bufferSizeChanged(pData->engine->getBufferSize());
  1756. reloadPrograms(true);
  1757. // check latency
  1758. // TODO
  1759. evIns.clear();
  1760. evOuts.clear();
  1761. if (pData->active)
  1762. activate();
  1763. carla_debug("Lv2Plugin::reload() - end");
  1764. }
  1765. void reloadPrograms(const bool doInit) override
  1766. {
  1767. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  1768. const uint32_t oldCount = pData->midiprog.count;
  1769. const int32_t current = pData->midiprog.current;
  1770. // special LV2 programs handling
  1771. if (doInit)
  1772. {
  1773. pData->prog.clear();
  1774. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  1775. if (presetCount > 0)
  1776. {
  1777. pData->prog.createNew(presetCount);
  1778. for (uint32_t i=0; i < presetCount; ++i)
  1779. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1780. }
  1781. }
  1782. // Delete old programs
  1783. pData->midiprog.clear();
  1784. // Query new programs
  1785. uint32_t newCount = 0;
  1786. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1787. {
  1788. for (; fExt.programs->get_program(fHandle, newCount);)
  1789. ++newCount;
  1790. }
  1791. if (newCount > 0)
  1792. {
  1793. pData->midiprog.createNew(newCount);
  1794. // Update data
  1795. for (uint32_t i=0; i < newCount; ++i)
  1796. {
  1797. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1798. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  1799. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  1800. pData->midiprog.data[i].bank = pdesc->bank;
  1801. pData->midiprog.data[i].program = pdesc->program;
  1802. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1803. }
  1804. }
  1805. #ifndef BUILD_BRIDGE
  1806. // Update OSC Names
  1807. if (pData->engine->isOscControlRegistered())
  1808. {
  1809. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  1810. for (uint32_t i=0; i < newCount; ++i)
  1811. 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);
  1812. }
  1813. #endif
  1814. if (doInit)
  1815. {
  1816. if (newCount > 0)
  1817. {
  1818. setMidiProgram(0, false, false, false);
  1819. }
  1820. else
  1821. {
  1822. // load default state
  1823. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1824. {
  1825. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1826. if (fHandle2 != nullptr)
  1827. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1828. }
  1829. }
  1830. }
  1831. else
  1832. {
  1833. // Check if current program is invalid
  1834. bool programChanged = false;
  1835. if (newCount == oldCount+1)
  1836. {
  1837. // one midi program added, probably created by user
  1838. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1839. programChanged = true;
  1840. }
  1841. else if (current < 0 && newCount > 0)
  1842. {
  1843. // programs exist now, but not before
  1844. pData->midiprog.current = 0;
  1845. programChanged = true;
  1846. }
  1847. else if (current >= 0 && newCount == 0)
  1848. {
  1849. // programs existed before, but not anymore
  1850. pData->midiprog.current = -1;
  1851. programChanged = true;
  1852. }
  1853. else if (current >= static_cast<int32_t>(newCount))
  1854. {
  1855. // current midi program > count
  1856. pData->midiprog.current = 0;
  1857. programChanged = true;
  1858. }
  1859. else
  1860. {
  1861. // no change
  1862. pData->midiprog.current = current;
  1863. }
  1864. if (programChanged)
  1865. setMidiProgram(pData->midiprog.current, true, true, true);
  1866. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1867. }
  1868. }
  1869. // -------------------------------------------------------------------
  1870. // Plugin processing
  1871. void activate() noexcept override
  1872. {
  1873. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1874. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1875. if (fDescriptor->activate != nullptr)
  1876. {
  1877. try {
  1878. fDescriptor->activate(fHandle);
  1879. } catch(...) {}
  1880. if (fHandle2 != nullptr)
  1881. {
  1882. try {
  1883. fDescriptor->activate(fHandle2);
  1884. } catch(...) {}
  1885. }
  1886. }
  1887. fFirstActive = true;
  1888. }
  1889. void deactivate() noexcept override
  1890. {
  1891. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1892. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1893. if (fDescriptor->deactivate != nullptr)
  1894. {
  1895. try {
  1896. fDescriptor->deactivate(fHandle);
  1897. } catch(...) {}
  1898. if (fHandle2 != nullptr)
  1899. {
  1900. try {
  1901. fDescriptor->deactivate(fHandle2);
  1902. } catch(...) {}
  1903. }
  1904. }
  1905. }
  1906. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1907. {
  1908. // --------------------------------------------------------------------------------------------------------
  1909. // Check if active
  1910. if (! pData->active)
  1911. {
  1912. // disable any output sound
  1913. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1914. FLOAT_CLEAR(outBuffer[i], frames);
  1915. return;
  1916. }
  1917. // --------------------------------------------------------------------------------------------------------
  1918. // Event itenerators from different APIs (input)
  1919. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1920. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1921. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1922. for (uint32_t i=0; i < fEventsIn.count; ++i)
  1923. {
  1924. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1925. {
  1926. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1927. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1928. }
  1929. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1930. {
  1931. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1932. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1933. }
  1934. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1935. {
  1936. fEventsIn.data[i].midi.event_count = 0;
  1937. fEventsIn.data[i].midi.size = 0;
  1938. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  1939. evInMidiStates[i].frame_count = frames;
  1940. evInMidiStates[i].position = 0;
  1941. }
  1942. }
  1943. for (uint32_t i=0; i < fEventsOut.count; ++i)
  1944. {
  1945. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1946. {
  1947. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1948. }
  1949. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1950. {
  1951. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1952. }
  1953. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1954. {
  1955. // not needed
  1956. }
  1957. }
  1958. CARLA_PROCESS_CONTINUE_CHECK;
  1959. // --------------------------------------------------------------------------------------------------------
  1960. // Check if needs reset
  1961. if (pData->needsReset)
  1962. {
  1963. uint8_t midiData[3] = { 0 };
  1964. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1965. {
  1966. const uint32_t j = fEventsIn.ctrlIndex;
  1967. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1968. {
  1969. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  1970. {
  1971. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1972. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1973. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1974. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1975. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1976. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1977. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1978. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1979. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1980. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1981. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1982. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1983. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1984. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1985. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1986. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1987. }
  1988. }
  1989. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1990. {
  1991. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1992. {
  1993. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  1994. midiData[1] = k;
  1995. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1996. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1997. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1998. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1999. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2000. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  2001. }
  2002. }
  2003. }
  2004. if (pData->latency > 0)
  2005. {
  2006. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2007. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  2008. }
  2009. pData->needsReset = false;
  2010. }
  2011. // --------------------------------------------------------------------------------------------------------
  2012. // TimeInfo
  2013. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2014. if (fFirstActive || fLastTimeInfo != timeInfo)
  2015. {
  2016. bool doPostRt;
  2017. int32_t rindex;
  2018. // update input ports
  2019. for (uint32_t k=0; k < pData->param.count; ++k)
  2020. {
  2021. if (pData->param.data[k].type != PARAMETER_INPUT)
  2022. continue;
  2023. if (pData->param.special[k] != PARAMETER_SPECIAL_LV2_TIME)
  2024. continue;
  2025. doPostRt = false;
  2026. rindex = pData->param.data[k].rindex;
  2027. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2028. switch (fRdfDescriptor->Ports[rindex].Designation)
  2029. {
  2030. // Non-BBT
  2031. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2032. if (fLastTimeInfo.playing != timeInfo.playing)
  2033. {
  2034. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2035. doPostRt = true;
  2036. }
  2037. break;
  2038. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2039. if (fLastTimeInfo.frame != timeInfo.frame)
  2040. {
  2041. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2042. doPostRt = true;
  2043. }
  2044. break;
  2045. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2046. break;
  2047. // BBT
  2048. case LV2_PORT_DESIGNATION_TIME_BAR:
  2049. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2050. {
  2051. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2052. doPostRt = true;
  2053. }
  2054. break;
  2055. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2056. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2057. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2058. {
  2059. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2060. doPostRt = true;
  2061. }
  2062. break;
  2063. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2064. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2065. {
  2066. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2067. doPostRt = true;
  2068. }
  2069. break;
  2070. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2071. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2072. {
  2073. fParamBuffers[k] = timeInfo.bbt.beatType;
  2074. doPostRt = true;
  2075. }
  2076. break;
  2077. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2078. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2079. {
  2080. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2081. doPostRt = true;
  2082. }
  2083. break;
  2084. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2085. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2086. {
  2087. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2088. doPostRt = true;
  2089. }
  2090. break;
  2091. }
  2092. if (doPostRt)
  2093. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2094. }
  2095. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2096. {
  2097. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2098. continue;
  2099. uint8_t timeInfoBuf[256];
  2100. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2101. LV2_Atom_Forge_Frame forgeFrame;
  2102. lv2_atom_forge_object(&fAtomForge, &forgeFrame, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_TIME_POSITION);
  2103. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2104. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2105. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2106. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2107. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2108. {
  2109. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2110. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2111. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2112. lv2_atom_forge_float(&fAtomForge, static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat)));
  2113. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2114. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2115. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2116. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2117. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2118. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2119. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2120. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2121. }
  2122. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2123. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2124. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2125. // send only deprecated blank object for now
  2126. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, CARLA_URI_MAP_ID_ATOM_BLANK, atom->size, LV2_ATOM_BODY_CONST(atom));
  2127. // for atom:object
  2128. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2129. }
  2130. pData->postRtEvents.trySplice();
  2131. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2132. CARLA_PROCESS_CONTINUE_CHECK;
  2133. }
  2134. // --------------------------------------------------------------------------------------------------------
  2135. // CV ports
  2136. float* cvInBuf[fCvIn.count /*> 0 ? fCvIn.count : 1*/];
  2137. float* cvOutBuf[fCvOut.count /*> 0 ? fCvOut.count : 1*/];
  2138. for (uint32_t i=0; i < fCvIn.count; ++i)
  2139. {
  2140. CARLA_SAFE_ASSERT_CONTINUE(fCvIn.ports[i].port != nullptr);
  2141. cvInBuf[i] = fCvIn.ports[i].port->getBuffer();
  2142. }
  2143. for (uint32_t i=0; i < fCvOut.count; ++i)
  2144. {
  2145. CARLA_SAFE_ASSERT_CONTINUE(fCvOut.ports[i].port != nullptr);
  2146. cvOutBuf[i] = fCvOut.ports[i].port->getBuffer();
  2147. }
  2148. // --------------------------------------------------------------------------------------------------------
  2149. // Event Input and Processing
  2150. if (fEventsIn.ctrl != nullptr)
  2151. {
  2152. // ----------------------------------------------------------------------------------------------------
  2153. // Message Input
  2154. if (fAtomQueueIn.tryLock())
  2155. {
  2156. if (! fAtomQueueIn.isEmpty())
  2157. {
  2158. const LV2_Atom* atom;
  2159. uint32_t portIndex;
  2160. const uint32_t j = fEventsIn.ctrlIndex;
  2161. for (; fAtomQueueIn.get(&atom, &portIndex);)
  2162. {
  2163. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2164. {
  2165. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2166. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2167. }
  2168. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2169. {
  2170. carla_stdout("Event input buffer full, at least 1 message lost");
  2171. continue;
  2172. }
  2173. }
  2174. }
  2175. fAtomQueueIn.unlock();
  2176. }
  2177. // ----------------------------------------------------------------------------------------------------
  2178. // MIDI Input (External)
  2179. if (pData->extNotes.mutex.tryLock())
  2180. {
  2181. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2182. {
  2183. // does not handle MIDI
  2184. pData->extNotes.data.clear();
  2185. }
  2186. else
  2187. {
  2188. const uint32_t j = fEventsIn.ctrlIndex;
  2189. for (; ! pData->extNotes.data.isEmpty();)
  2190. {
  2191. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  2192. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2193. uint8_t midiEvent[3];
  2194. midiEvent[0] = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2195. midiEvent[1] = note.note;
  2196. midiEvent[2] = note.velo;
  2197. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2198. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2199. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2200. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2201. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2202. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2203. }
  2204. }
  2205. pData->extNotes.mutex.unlock();
  2206. } // End of MIDI Input (External)
  2207. // ----------------------------------------------------------------------------------------------------
  2208. // Event Input (System)
  2209. bool allNotesOffSent = false;
  2210. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2211. uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2212. uint32_t startTime = 0;
  2213. uint32_t timeOffset = 0;
  2214. uint32_t nextBankId;
  2215. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2216. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2217. else
  2218. nextBankId = 0;
  2219. for (uint32_t i=0; i < numEvents; ++i)
  2220. {
  2221. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2222. if (event.time >= frames)
  2223. continue;
  2224. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2225. if (isSampleAccurate && event.time > timeOffset)
  2226. {
  2227. if (processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, event.time - timeOffset, timeOffset))
  2228. {
  2229. startTime = 0;
  2230. timeOffset = event.time;
  2231. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2232. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2233. else
  2234. nextBankId = 0;
  2235. // reset iters
  2236. const uint32_t j = fEventsIn.ctrlIndex;
  2237. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2238. {
  2239. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2240. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2241. }
  2242. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2243. {
  2244. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2245. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2246. }
  2247. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2248. {
  2249. fEventsIn.data[j].midi.event_count = 0;
  2250. fEventsIn.data[j].midi.size = 0;
  2251. evInMidiStates[j].position = event.time;
  2252. }
  2253. }
  2254. else
  2255. startTime += timeOffset;
  2256. }
  2257. switch (event.type)
  2258. {
  2259. case kEngineEventTypeNull:
  2260. break;
  2261. case kEngineEventTypeControl: {
  2262. const EngineControlEvent& ctrlEvent(event.ctrl);
  2263. switch (ctrlEvent.type)
  2264. {
  2265. case kEngineControlEventTypeNull:
  2266. break;
  2267. case kEngineControlEventTypeParameter: {
  2268. #ifndef BUILD_BRIDGE
  2269. // Control backend stuff
  2270. if (event.channel == pData->ctrlChannel)
  2271. {
  2272. float value;
  2273. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2274. {
  2275. value = ctrlEvent.value;
  2276. setDryWet(value, false, false);
  2277. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2278. break;
  2279. }
  2280. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2281. {
  2282. value = ctrlEvent.value*127.0f/100.0f;
  2283. setVolume(value, false, false);
  2284. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2285. break;
  2286. }
  2287. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2288. {
  2289. float left, right;
  2290. value = ctrlEvent.value/0.5f - 1.0f;
  2291. if (value < 0.0f)
  2292. {
  2293. left = -1.0f;
  2294. right = (value*2.0f)+1.0f;
  2295. }
  2296. else if (value > 0.0f)
  2297. {
  2298. left = (value*2.0f)-1.0f;
  2299. right = 1.0f;
  2300. }
  2301. else
  2302. {
  2303. left = -1.0f;
  2304. right = 1.0f;
  2305. }
  2306. setBalanceLeft(left, false, false);
  2307. setBalanceRight(right, false, false);
  2308. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2309. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2310. break;
  2311. }
  2312. }
  2313. #endif
  2314. // Control plugin parameters
  2315. uint32_t k;
  2316. for (k=0; k < pData->param.count; ++k)
  2317. {
  2318. if (pData->param.data[k].midiChannel != event.channel)
  2319. continue;
  2320. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2321. continue;
  2322. if (pData->param.data[k].type != PARAMETER_INPUT)
  2323. continue;
  2324. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2325. continue;
  2326. float value;
  2327. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2328. {
  2329. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2330. }
  2331. else
  2332. {
  2333. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2334. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2335. value = std::rint(value);
  2336. }
  2337. setParameterValue(k, value, false, false, false);
  2338. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2339. break;
  2340. }
  2341. // check if event is already handled
  2342. if (k != pData->param.count)
  2343. break;
  2344. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2345. {
  2346. uint8_t midiData[3];
  2347. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2348. midiData[1] = static_cast<uint8_t>(ctrlEvent.param);
  2349. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2350. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2351. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2352. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2353. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2354. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2355. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2356. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2357. }
  2358. break;
  2359. } // case kEngineControlEventTypeParameter
  2360. case kEngineControlEventTypeMidiBank:
  2361. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2362. nextBankId = ctrlEvent.param;
  2363. break;
  2364. case kEngineControlEventTypeMidiProgram:
  2365. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2366. {
  2367. const uint32_t nextProgramId = ctrlEvent.param;
  2368. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2369. {
  2370. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2371. {
  2372. const int32_t index(static_cast<int32_t>(k));
  2373. setMidiProgram(index, false, false, false);
  2374. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2375. break;
  2376. }
  2377. }
  2378. }
  2379. break;
  2380. case kEngineControlEventTypeAllSoundOff:
  2381. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2382. {
  2383. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2384. uint8_t midiData[3];
  2385. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2386. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2387. midiData[2] = 0;
  2388. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2389. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2390. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2391. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2392. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2393. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2394. }
  2395. break;
  2396. case kEngineControlEventTypeAllNotesOff:
  2397. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2398. {
  2399. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2400. {
  2401. allNotesOffSent = true;
  2402. sendMidiAllNotesOffToCallback();
  2403. }
  2404. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2405. uint8_t midiData[3];
  2406. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2407. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2408. midiData[2] = 0;
  2409. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2410. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2411. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2412. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2413. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2414. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2415. }
  2416. break;
  2417. } // switch (ctrlEvent.type)
  2418. break;
  2419. } // case kEngineEventTypeControl
  2420. case kEngineEventTypeMidi:
  2421. {
  2422. const EngineMidiEvent& midiEvent(event.midi);
  2423. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  2424. uint8_t channel = event.channel;
  2425. uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2426. // Fix bad note-off (per LV2 spec)
  2427. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  2428. status = MIDI_STATUS_NOTE_OFF;
  2429. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2430. continue;
  2431. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2432. continue;
  2433. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2434. continue;
  2435. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2436. continue;
  2437. const uint32_t j = fEventsIn.ctrlIndex;
  2438. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2439. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2440. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2441. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2442. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2443. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiEvent.data);
  2444. if (status == MIDI_STATUS_NOTE_ON)
  2445. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2446. else if (status == MIDI_STATUS_NOTE_OFF)
  2447. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2448. break;
  2449. } // case kEngineEventTypeMidi
  2450. } // switch (event.type)
  2451. }
  2452. pData->postRtEvents.trySplice();
  2453. if (frames > timeOffset)
  2454. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames - timeOffset, timeOffset);
  2455. } // End of Event Input and Processing
  2456. // --------------------------------------------------------------------------------------------------------
  2457. // Plugin processing (no events)
  2458. else
  2459. {
  2460. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames, 0);
  2461. } // End of Plugin processing (no events)
  2462. CARLA_PROCESS_CONTINUE_CHECK;
  2463. // --------------------------------------------------------------------------------------------------------
  2464. // MIDI Output
  2465. if (fEventsOut.ctrl != nullptr)
  2466. {
  2467. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2468. {
  2469. const LV2_Atom_Event* ev;
  2470. LV2_Atom_Buffer_Iterator iter;
  2471. uint8_t* data;
  2472. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2473. for (;;)
  2474. {
  2475. data = nullptr;
  2476. ev = lv2_atom_buffer_get(&iter, &data);
  2477. if (ev == nullptr || data == nullptr)
  2478. break;
  2479. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2480. {
  2481. if (fEventsOut.ctrl->port != nullptr)
  2482. {
  2483. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2484. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2485. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2486. }
  2487. }
  2488. else if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2489. {
  2490. fAtomQueueOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2491. }
  2492. lv2_atom_buffer_increment(&iter);
  2493. }
  2494. }
  2495. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2496. {
  2497. const LV2_Event* ev;
  2498. LV2_Event_Iterator iter;
  2499. uint8_t* data;
  2500. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2501. for (;;)
  2502. {
  2503. data = nullptr;
  2504. ev = lv2_event_get(&iter, &data);
  2505. if (ev == nullptr || data == nullptr)
  2506. break;
  2507. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2508. {
  2509. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2510. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2511. }
  2512. lv2_event_increment(&iter);
  2513. }
  2514. }
  2515. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2516. {
  2517. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2518. uint32_t eventSize;
  2519. double eventTime;
  2520. unsigned char* eventData;
  2521. for (;;)
  2522. {
  2523. eventSize = 0;
  2524. eventTime = 0.0;
  2525. eventData = nullptr;
  2526. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2527. if (eventData == nullptr || eventSize == 0)
  2528. break;
  2529. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2530. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2531. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2532. lv2midi_step(&state);
  2533. }
  2534. }
  2535. }
  2536. // --------------------------------------------------------------------------------------------------------
  2537. // Control Output
  2538. if (pData->event.portOut != nullptr)
  2539. {
  2540. uint8_t channel;
  2541. uint16_t param;
  2542. float value;
  2543. for (uint32_t k=0; k < pData->param.count; ++k)
  2544. {
  2545. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2546. continue;
  2547. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2548. if (pData->param.data[k].midiCC > 0)
  2549. {
  2550. channel = pData->param.data[k].midiChannel;
  2551. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2552. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2553. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2554. }
  2555. }
  2556. } // End of Control Output
  2557. CARLA_PROCESS_CONTINUE_CHECK;
  2558. // --------------------------------------------------------------------------------------------------------
  2559. // Final work
  2560. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2561. {
  2562. fExt.worker->end_run(fHandle);
  2563. if (fHandle2 != nullptr)
  2564. fExt.worker->end_run(fHandle2);
  2565. }
  2566. fFirstActive = false;
  2567. // --------------------------------------------------------------------------------------------------------
  2568. }
  2569. bool processSingle(float** const audioInBuf, float** const audioOutBuf, float** const cvInBuf, float** const cvOutBuf, const uint32_t frames, const uint32_t timeOffset)
  2570. {
  2571. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2572. if (pData->audioIn.count > 0)
  2573. {
  2574. CARLA_SAFE_ASSERT_RETURN(audioInBuf != nullptr, false);
  2575. }
  2576. if (pData->audioOut.count > 0)
  2577. {
  2578. CARLA_SAFE_ASSERT_RETURN(audioOutBuf != nullptr, false);
  2579. }
  2580. if (fCvIn.count > 0)
  2581. {
  2582. CARLA_SAFE_ASSERT_RETURN(cvInBuf != nullptr, false);
  2583. }
  2584. if (fCvOut.count > 0)
  2585. {
  2586. CARLA_SAFE_ASSERT_RETURN(cvOutBuf != nullptr, false);
  2587. }
  2588. // --------------------------------------------------------------------------------------------------------
  2589. // Try lock, silence otherwise
  2590. if (pData->engine->isOffline())
  2591. {
  2592. pData->singleMutex.lock();
  2593. }
  2594. else if (! pData->singleMutex.tryLock())
  2595. {
  2596. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2597. {
  2598. for (uint32_t k=0; k < frames; ++k)
  2599. audioOutBuf[i][k+timeOffset] = 0.0f;
  2600. }
  2601. return false;
  2602. }
  2603. // --------------------------------------------------------------------------------------------------------
  2604. // Set audio buffers
  2605. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2606. FLOAT_COPY(fAudioInBuffers[i], audioInBuf[i]+timeOffset, frames);
  2607. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2608. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  2609. // --------------------------------------------------------------------------------------------------------
  2610. // Set CV buffers
  2611. for (uint32_t i=0; i < fCvIn.count; ++i)
  2612. FLOAT_COPY(fCvInBuffers[i], cvInBuf[i]+timeOffset, frames);
  2613. for (uint32_t i=0; i < fCvOut.count; ++i)
  2614. FLOAT_CLEAR(fCvOutBuffers[i], frames);
  2615. // --------------------------------------------------------------------------------------------------------
  2616. // Run plugin
  2617. fDescriptor->run(fHandle, frames);
  2618. if (fHandle2 != nullptr)
  2619. fDescriptor->run(fHandle2, frames);
  2620. // --------------------------------------------------------------------------------------------------------
  2621. // Handle trigger parameters
  2622. for (uint32_t k=0; k < pData->param.count; ++k)
  2623. {
  2624. if (pData->param.data[k].type != PARAMETER_INPUT)
  2625. continue;
  2626. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2627. {
  2628. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2629. {
  2630. fParamBuffers[k] = pData->param.ranges[k].def;
  2631. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2632. }
  2633. }
  2634. }
  2635. pData->postRtEvents.trySplice();
  2636. #ifndef BUILD_BRIDGE
  2637. // --------------------------------------------------------------------------------------------------------
  2638. // Post-processing (dry/wet, volume and balance)
  2639. {
  2640. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2641. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2642. bool isPair;
  2643. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2644. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2645. {
  2646. // Dry/Wet
  2647. if (doDryWet)
  2648. {
  2649. for (uint32_t k=0; k < frames; ++k)
  2650. {
  2651. // TODO
  2652. //if (k < pData->latency && pData->latency < frames)
  2653. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  2654. //else
  2655. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2656. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  2657. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2658. }
  2659. }
  2660. // Balance
  2661. if (doBalance)
  2662. {
  2663. isPair = (i % 2 == 0);
  2664. if (isPair)
  2665. {
  2666. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2667. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  2668. }
  2669. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2670. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2671. for (uint32_t k=0; k < frames; ++k)
  2672. {
  2673. if (isPair)
  2674. {
  2675. // left
  2676. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2677. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2678. }
  2679. else
  2680. {
  2681. // right
  2682. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2683. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2684. }
  2685. }
  2686. }
  2687. // Volume (and buffer copy)
  2688. {
  2689. for (uint32_t k=0; k < frames; ++k)
  2690. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2691. }
  2692. }
  2693. } // End of Post-processing
  2694. #else // BUILD_BRIDGE
  2695. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2696. {
  2697. for (uint32_t k=0; k < frames; ++k)
  2698. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2699. }
  2700. #endif
  2701. // --------------------------------------------------------------------------------------------------------
  2702. pData->singleMutex.unlock();
  2703. return true;
  2704. }
  2705. void bufferSizeChanged(const uint32_t newBufferSize) override
  2706. {
  2707. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2708. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2709. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2710. {
  2711. if (fAudioInBuffers[i] != nullptr)
  2712. delete[] fAudioInBuffers[i];
  2713. fAudioInBuffers[i] = new float[newBufferSize];
  2714. }
  2715. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2716. {
  2717. if (fAudioOutBuffers[i] != nullptr)
  2718. delete[] fAudioOutBuffers[i];
  2719. fAudioOutBuffers[i] = new float[newBufferSize];
  2720. }
  2721. if (fHandle2 == nullptr)
  2722. {
  2723. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2724. {
  2725. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2726. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2727. }
  2728. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2729. {
  2730. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2731. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2732. }
  2733. }
  2734. else
  2735. {
  2736. if (pData->audioIn.count > 0)
  2737. {
  2738. CARLA_ASSERT(pData->audioIn.count == 2);
  2739. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2740. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2741. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2742. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2743. }
  2744. if (pData->audioOut.count > 0)
  2745. {
  2746. CARLA_ASSERT(pData->audioOut.count == 2);
  2747. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2748. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2749. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2750. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2751. }
  2752. }
  2753. for (uint32_t i=0; i < fCvIn.count; ++i)
  2754. {
  2755. if (fCvInBuffers[i] != nullptr)
  2756. delete[] fCvInBuffers[i];
  2757. fCvInBuffers[i] = new float[newBufferSize];
  2758. fDescriptor->connect_port(fHandle, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2759. if (fHandle2 != nullptr)
  2760. fDescriptor->connect_port(fHandle2, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2761. }
  2762. for (uint32_t i=0; i < fCvOut.count; ++i)
  2763. {
  2764. if (fCvOutBuffers[i] != nullptr)
  2765. delete[] fCvOutBuffers[i];
  2766. fCvOutBuffers[i] = new float[newBufferSize];
  2767. fDescriptor->connect_port(fHandle, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2768. if (fHandle2 != nullptr)
  2769. fDescriptor->connect_port(fHandle2, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2770. }
  2771. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2772. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2773. {
  2774. fLv2Options.maxBufferSize = newBufferSizeInt;
  2775. if (fLv2Options.minBufferSize != 1)
  2776. fLv2Options.minBufferSize = newBufferSizeInt;
  2777. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2778. {
  2779. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2780. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2781. }
  2782. }
  2783. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2784. }
  2785. void sampleRateChanged(const double newSampleRate) override
  2786. {
  2787. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2788. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2789. if (fLv2Options.sampleRate != newSampleRate)
  2790. {
  2791. fLv2Options.sampleRate = newSampleRate;
  2792. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2793. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2794. }
  2795. for (uint32_t k=0; k < pData->param.count; ++k)
  2796. {
  2797. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2798. {
  2799. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2800. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2801. break;
  2802. }
  2803. }
  2804. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2805. }
  2806. void offlineModeChanged(const bool isOffline) override
  2807. {
  2808. for (uint32_t k=0; k < pData->param.count; ++k)
  2809. {
  2810. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  2811. {
  2812. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2813. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2814. break;
  2815. }
  2816. }
  2817. }
  2818. // -------------------------------------------------------------------
  2819. // Plugin buffers
  2820. void initBuffers() override
  2821. {
  2822. fCvIn.initBuffers();
  2823. fCvOut.initBuffers();
  2824. fEventsIn.initBuffers();
  2825. fEventsOut.initBuffers();
  2826. CarlaPlugin::initBuffers();
  2827. }
  2828. void clearBuffers() override
  2829. {
  2830. carla_debug("Lv2Plugin::clearBuffers() - start");
  2831. if (fAudioInBuffers != nullptr)
  2832. {
  2833. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2834. {
  2835. if (fAudioInBuffers[i] != nullptr)
  2836. {
  2837. delete[] fAudioInBuffers[i];
  2838. fAudioInBuffers[i] = nullptr;
  2839. }
  2840. }
  2841. delete[] fAudioInBuffers;
  2842. fAudioInBuffers = nullptr;
  2843. }
  2844. if (fAudioOutBuffers != nullptr)
  2845. {
  2846. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2847. {
  2848. if (fAudioOutBuffers[i] != nullptr)
  2849. {
  2850. delete[] fAudioOutBuffers[i];
  2851. fAudioOutBuffers[i] = nullptr;
  2852. }
  2853. }
  2854. delete[] fAudioOutBuffers;
  2855. fAudioOutBuffers = nullptr;
  2856. }
  2857. if (fCvInBuffers != nullptr)
  2858. {
  2859. for (uint32_t i=0; i < fCvIn.count; ++i)
  2860. {
  2861. if (fCvInBuffers[i] != nullptr)
  2862. {
  2863. delete[] fCvInBuffers[i];
  2864. fCvInBuffers[i] = nullptr;
  2865. }
  2866. }
  2867. delete[] fCvInBuffers;
  2868. fCvInBuffers = nullptr;
  2869. }
  2870. if (fCvOutBuffers != nullptr)
  2871. {
  2872. for (uint32_t i=0; i < fCvOut.count; ++i)
  2873. {
  2874. if (fCvOutBuffers[i] != nullptr)
  2875. {
  2876. delete[] fCvOutBuffers[i];
  2877. fCvOutBuffers[i] = nullptr;
  2878. }
  2879. }
  2880. delete[] fCvOutBuffers;
  2881. fCvOutBuffers = nullptr;
  2882. }
  2883. if (fParamBuffers != nullptr)
  2884. {
  2885. delete[] fParamBuffers;
  2886. fParamBuffers = nullptr;
  2887. }
  2888. fCvIn.clear();
  2889. fCvOut.clear();
  2890. fEventsIn.clear();
  2891. fEventsOut.clear();
  2892. CarlaPlugin::clearBuffers();
  2893. carla_debug("Lv2Plugin::clearBuffers() - end");
  2894. }
  2895. // -------------------------------------------------------------------
  2896. // OSC stuff
  2897. void updateOscData(const lo_address& source, const char* const url) override
  2898. {
  2899. CarlaPlugin::updateOscData(source, url);
  2900. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  2901. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i));
  2902. }
  2903. // -------------------------------------------------------------------
  2904. // Post-poned UI Stuff
  2905. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2906. {
  2907. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2908. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2909. if (fUi.type == UI::TYPE_OSC)
  2910. {
  2911. if (pData->osc.data.target != nullptr)
  2912. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  2913. }
  2914. else
  2915. {
  2916. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2917. {
  2918. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  2919. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  2920. }
  2921. }
  2922. }
  2923. void uiMidiProgramChange(const uint32_t index) noexcept override
  2924. {
  2925. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2926. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2927. if (fUi.type == UI::TYPE_OSC)
  2928. {
  2929. if (pData->osc.data.target != nullptr)
  2930. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2931. }
  2932. else
  2933. {
  2934. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2935. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2936. }
  2937. }
  2938. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2939. {
  2940. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2941. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2942. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2943. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2944. if (fUi.type == UI::TYPE_OSC)
  2945. {
  2946. if (pData->osc.data.target != nullptr)
  2947. {
  2948. uint8_t midiData[4] = { 0 };
  2949. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2950. midiData[2] = note;
  2951. midiData[3] = velo;
  2952. osc_send_midi(pData->osc.data, midiData);
  2953. }
  2954. }
  2955. else
  2956. {
  2957. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2958. {
  2959. LV2_Atom_MidiEvent midiEv;
  2960. midiEv.event.time.frames = 0;
  2961. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2962. midiEv.event.body.size = 3;
  2963. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2964. midiEv.data[1] = note;
  2965. midiEv.data[2] = velo;
  2966. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2967. }
  2968. }
  2969. }
  2970. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2971. {
  2972. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2973. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2974. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2975. if (fUi.type == UI::TYPE_OSC)
  2976. {
  2977. if (pData->osc.data.target != nullptr)
  2978. {
  2979. uint8_t midiData[4] = { 0 };
  2980. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2981. midiData[2] = note;
  2982. osc_send_midi(pData->osc.data, midiData);
  2983. }
  2984. }
  2985. else
  2986. {
  2987. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2988. {
  2989. LV2_Atom_MidiEvent midiEv;
  2990. midiEv.event.time.frames = 0;
  2991. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2992. midiEv.event.body.size = 3;
  2993. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2994. midiEv.data[1] = note;
  2995. midiEv.data[2] = 0;
  2996. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2997. }
  2998. }
  2999. }
  3000. // -------------------------------------------------------------------
  3001. bool isRealtimeSafe() const noexcept
  3002. {
  3003. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3004. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3005. {
  3006. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3007. return true;
  3008. }
  3009. return false;
  3010. }
  3011. bool needsFixedBuffer() const noexcept
  3012. {
  3013. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3014. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3015. {
  3016. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3017. return true;
  3018. }
  3019. return false;
  3020. }
  3021. // -------------------------------------------------------------------
  3022. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3023. {
  3024. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3025. #ifndef LV2_UIS_ONLY_INPROCESS
  3026. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  3027. if (std::strstr(rdfUi->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3028. return false;
  3029. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  3030. {
  3031. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3032. return false;
  3033. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3034. return false;
  3035. }
  3036. return true;
  3037. #else
  3038. return false;
  3039. #endif
  3040. }
  3041. bool isUiResizable() const noexcept
  3042. {
  3043. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  3044. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3045. {
  3046. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3047. return false;
  3048. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3049. return false;
  3050. }
  3051. return true;
  3052. }
  3053. const char* getUiBridgeBinary(const LV2_Property type) const
  3054. {
  3055. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3056. if (bridgeBinary.isEmpty())
  3057. return nullptr;
  3058. #ifdef CARLA_OS_LINUX
  3059. // test for local build
  3060. if (bridgeBinary.endsWith("/source/backend/"))
  3061. bridgeBinary += "../bridges/";
  3062. #endif
  3063. switch (type)
  3064. {
  3065. case LV2_UI_GTK2:
  3066. bridgeBinary += "carla-bridge-lv2-gtk2";
  3067. break;
  3068. case LV2_UI_GTK3:
  3069. bridgeBinary += "carla-bridge-lv2-gtk3";
  3070. break;
  3071. case LV2_UI_QT4:
  3072. bridgeBinary += "carla-bridge-lv2-qt4";
  3073. break;
  3074. case LV2_UI_QT5:
  3075. bridgeBinary += "carla-bridge-lv2-qt5";
  3076. break;
  3077. case LV2_UI_COCOA:
  3078. bridgeBinary += "carla-bridge-lv2-cocoa";
  3079. break;
  3080. case LV2_UI_WINDOWS:
  3081. bridgeBinary += "carla-bridge-lv2-windows";
  3082. break;
  3083. case LV2_UI_X11:
  3084. bridgeBinary += "carla-bridge-lv2-x11";
  3085. break;
  3086. case LV2_UI_EXTERNAL:
  3087. case LV2_UI_OLD_EXTERNAL:
  3088. bridgeBinary += "carla-bridge-lv2-external";
  3089. break;
  3090. default:
  3091. return nullptr;
  3092. }
  3093. #ifdef CARLA_OS_WIN
  3094. bridgeBinary += ".exe";
  3095. #endif
  3096. QFile file(bridgeBinary.buffer());
  3097. if (! file.exists())
  3098. return nullptr;
  3099. return bridgeBinary.dup();
  3100. }
  3101. // -------------------------------------------------------------------
  3102. void recheckExtensions()
  3103. {
  3104. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3105. carla_debug("Lv2Plugin::recheckExtensions()");
  3106. fExt.options = nullptr;
  3107. fExt.programs = nullptr;
  3108. fExt.state = nullptr;
  3109. fExt.worker = nullptr;
  3110. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3111. {
  3112. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3113. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3114. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3115. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3116. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3117. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3118. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3119. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3120. else
  3121. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3122. }
  3123. if (fDescriptor->extension_data != nullptr)
  3124. {
  3125. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3126. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3127. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3128. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3129. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3130. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3131. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3132. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3133. // check if invalid
  3134. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3135. fExt.options = nullptr;
  3136. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3137. fExt.programs = nullptr;
  3138. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3139. fExt.state = nullptr;
  3140. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3141. fExt.worker = nullptr;
  3142. }
  3143. }
  3144. // -------------------------------------------------------------------
  3145. void updateUi()
  3146. {
  3147. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  3148. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  3149. carla_debug("Lv2Plugin::updateUi()");
  3150. fExt.uiidle = nullptr;
  3151. fExt.uiprograms = nullptr;
  3152. if (fUi.descriptor->extension_data != nullptr)
  3153. {
  3154. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3155. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3156. // check if invalid
  3157. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3158. fExt.uiidle = nullptr;
  3159. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3160. fExt.uiprograms = nullptr;
  3161. }
  3162. // update midi program
  3163. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3164. {
  3165. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3166. fExt.uiprograms->select_program(fUi.handle, curData.bank, curData.program);
  3167. }
  3168. // update control ports
  3169. if (fUi.descriptor->port_event != nullptr)
  3170. {
  3171. float value;
  3172. for (uint32_t i=0; i < pData->param.count; ++i)
  3173. {
  3174. value = getParameterValue(i);
  3175. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3176. }
  3177. }
  3178. }
  3179. // -------------------------------------------------------------------
  3180. LV2_URID getCustomURID(const char* const uri)
  3181. {
  3182. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3183. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3184. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3185. {
  3186. const char* const thisUri(fCustomURIDs.getAt(i));
  3187. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3188. return static_cast<LV2_URID>(i);
  3189. }
  3190. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3191. fCustomURIDs.append(carla_strdup(uri));
  3192. if (fUi.type == UI::TYPE_OSC && pData->osc.data.target != nullptr)
  3193. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3194. return urid;
  3195. }
  3196. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3197. {
  3198. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  3199. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  3200. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3201. return fCustomURIDs.getAt(urid);
  3202. }
  3203. // -------------------------------------------------------------------
  3204. void handleProgramChanged(const int32_t index)
  3205. {
  3206. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3207. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3208. if (index == -1)
  3209. {
  3210. const ScopedSingleProcessLocker spl(this, true);
  3211. return reloadPrograms(false);
  3212. }
  3213. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3214. {
  3215. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3216. {
  3217. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3218. if (pData->midiprog.data[index].name != nullptr)
  3219. delete[] pData->midiprog.data[index].name;
  3220. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3221. if (index == pData->midiprog.current)
  3222. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3223. else
  3224. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3225. }
  3226. }
  3227. }
  3228. // -------------------------------------------------------------------
  3229. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3230. {
  3231. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3232. carla_debug("Lv2Plugin::handleResizePort(%i, " P_SIZE ")", index, size);
  3233. // TODO
  3234. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3235. (void)index;
  3236. }
  3237. // -------------------------------------------------------------------
  3238. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3239. {
  3240. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3241. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3242. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3243. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3244. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3245. 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);
  3246. const char* const skey(carla_lv2_urid_unmap(this, key));
  3247. const char* const stype(carla_lv2_urid_unmap(this, type));
  3248. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3249. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3250. // Check if we already have this key
  3251. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3252. {
  3253. CustomData& data(it.getValue());
  3254. if (std::strcmp(data.key, skey) == 0)
  3255. {
  3256. // found it
  3257. if (data.value != nullptr)
  3258. delete[] data.value;
  3259. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3260. data.value = carla_strdup((const char*)value);
  3261. else
  3262. data.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3263. return LV2_STATE_SUCCESS;
  3264. }
  3265. }
  3266. // Otherwise store it
  3267. CustomData newData;
  3268. newData.type = carla_strdup(stype);
  3269. newData.key = carla_strdup(skey);
  3270. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3271. newData.value = carla_strdup((const char*)value);
  3272. else
  3273. newData.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3274. pData->custom.append(newData);
  3275. return LV2_STATE_SUCCESS;
  3276. }
  3277. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3278. {
  3279. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3280. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3281. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3282. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3283. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3284. const char* const skey(carla_lv2_urid_unmap(this, key));
  3285. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3286. const char* stype = nullptr;
  3287. const char* stringData = nullptr;
  3288. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3289. {
  3290. const CustomData& data(it.getValue());
  3291. if (std::strcmp(data.key, skey) == 0)
  3292. {
  3293. stype = data.type;
  3294. stringData = data.value;
  3295. break;
  3296. }
  3297. }
  3298. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3299. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3300. *type = carla_lv2_urid_map(this, stype);
  3301. *flags = LV2_STATE_IS_POD;
  3302. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3303. {
  3304. *size = std::strlen(stringData);
  3305. return stringData;
  3306. }
  3307. else
  3308. {
  3309. static QByteArray chunk; // FIXME
  3310. chunk = QByteArray::fromBase64(stringData);
  3311. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3312. *size = static_cast<size_t>(chunk.size());
  3313. return chunk.constData();
  3314. }
  3315. }
  3316. // -------------------------------------------------------------------
  3317. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3318. {
  3319. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3320. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3321. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3322. if (pData->engine->isOffline())
  3323. {
  3324. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3325. return LV2_WORKER_SUCCESS;
  3326. }
  3327. LV2_Atom atom;
  3328. atom.size = size;
  3329. atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3330. return fAtomQueueOut.putChunk(&atom, data, fEventsIn.ctrl->rindex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3331. }
  3332. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3333. {
  3334. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3335. LV2_Atom atom;
  3336. atom.size = size;
  3337. atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3338. return fAtomQueueIn.putChunk(&atom, data, fEventsIn.ctrl->rindex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3339. }
  3340. // -------------------------------------------------------------------
  3341. void handleExternalUiClosed()
  3342. {
  3343. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EXTERNAL,);
  3344. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3345. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3346. fUi.descriptor->cleanup(fUi.handle);
  3347. fUi.handle = nullptr;
  3348. fUi.widget = nullptr;
  3349. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3350. }
  3351. void handlePluginUiClosed() override
  3352. {
  3353. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EMBED,);
  3354. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr,);
  3355. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3356. fUi.window->hide();
  3357. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3358. fUi.descriptor->cleanup(fUi.handle);
  3359. fUi.handle = nullptr;
  3360. fUi.widget = nullptr;
  3361. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3362. }
  3363. // -------------------------------------------------------------------
  3364. uint32_t handleUiPortMap(const char* const symbol) const noexcept
  3365. {
  3366. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3367. carla_debug("Lv2Plugin::handleUiPortMap(\"%s\")", symbol);
  3368. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3369. {
  3370. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3371. return i;
  3372. }
  3373. return LV2UI_INVALID_PORT_INDEX;
  3374. }
  3375. int handleUiResize(const int width, const int height)
  3376. {
  3377. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr, 1);
  3378. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3379. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3380. carla_debug("Lv2Plugin::handleUiResize(%i, %i)", width, height);
  3381. fUi.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3382. return 0;
  3383. }
  3384. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3385. {
  3386. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3387. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3388. carla_debug("Lv2Plugin::handleUiWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3389. switch (format)
  3390. {
  3391. case CARLA_URI_MAP_ID_NULL:
  3392. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3393. for (uint32_t i=0; i < pData->param.count; ++i)
  3394. {
  3395. if (pData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3396. {
  3397. const float value(*(const float*)buffer);
  3398. if (fParamBuffers[i] != value)
  3399. setParameterValue(i, value, false, true, true);
  3400. break;
  3401. }
  3402. }
  3403. break;
  3404. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3405. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT:
  3406. // plugins sometimes fails on this, not good...
  3407. CARLA_SAFE_ASSERT_INT2(((const LV2_Atom*)buffer)->size == bufferSize, ((const LV2_Atom*)buffer)->size, bufferSize);
  3408. fAtomQueueIn.put((const LV2_Atom*)buffer, rindex);
  3409. break;
  3410. default:
  3411. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3412. break;
  3413. }
  3414. }
  3415. // -------------------------------------------------------------------
  3416. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3417. {
  3418. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3419. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3420. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3421. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3422. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3423. int32_t rindex = -1;
  3424. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3425. {
  3426. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3427. {
  3428. rindex = static_cast<int32_t>(i);
  3429. break;
  3430. }
  3431. }
  3432. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3433. float paramValue;
  3434. switch (type)
  3435. {
  3436. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3437. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3438. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3439. break;
  3440. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3441. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3442. paramValue = static_cast<float>((*(const double*)value));
  3443. break;
  3444. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3445. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3446. paramValue = (*(const float*)value);
  3447. break;
  3448. case CARLA_URI_MAP_ID_ATOM_INT:
  3449. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3450. paramValue = static_cast<float>((*(const int32_t*)value));
  3451. break;
  3452. case CARLA_URI_MAP_ID_ATOM_LONG:
  3453. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3454. paramValue = static_cast<float>((*(const int64_t*)value));
  3455. break;
  3456. default:
  3457. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3458. return;
  3459. }
  3460. for (uint32_t i=0; i < pData->param.count; ++i)
  3461. {
  3462. if (pData->param.data[i].rindex == rindex)
  3463. {
  3464. setParameterValue(i, paramValue, true, true, true);
  3465. break;
  3466. }
  3467. }
  3468. }
  3469. // -------------------------------------------------------------------
  3470. public:
  3471. bool init(const char* const bundle, const char* const name, const char* const uri)
  3472. {
  3473. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3474. // ---------------------------------------------------------------
  3475. // first checks
  3476. if (pData->client != nullptr)
  3477. {
  3478. pData->engine->setLastError("Plugin client is already registered");
  3479. return false;
  3480. }
  3481. if (bundle == nullptr || bundle[0] == '\0')
  3482. {
  3483. pData->engine->setLastError("null bundle");
  3484. return false;
  3485. }
  3486. if (uri == nullptr || uri[0] == '\0')
  3487. {
  3488. pData->engine->setLastError("null uri");
  3489. return false;
  3490. }
  3491. // ---------------------------------------------------------------
  3492. // get plugin from lv2_rdf (lilv)
  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 (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\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  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. // -------------------------------------------------------------------------------------------------------------------