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