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.

5551 lines
208KB

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