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.

5318 lines
201KB

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