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.

5319 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 kFeatureIdUiIdle = 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. // TODO
  2065. //LV2_Atom_Forge_Frame forgeFrame;
  2066. //lv2_atom_forge_blank(&fAtomForge, &forgeFrame, 1, CARLA_URI_MAP_ID_TIME_POSITION);
  2067. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED, 0);
  2068. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2069. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME, 0);
  2070. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2071. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2072. {
  2073. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR, 0);
  2074. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2075. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT, 0);
  2076. 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)));
  2077. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT, 0);
  2078. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2079. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT, 0);
  2080. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2081. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR, 0);
  2082. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2083. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE, 0);
  2084. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2085. }
  2086. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2087. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2088. CARLA_ASSERT(atom->size < 256);
  2089. }
  2090. pData->postRtEvents.trySplice();
  2091. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2092. CARLA_PROCESS_CONTINUE_CHECK;
  2093. }
  2094. // --------------------------------------------------------------------------------------------------------
  2095. // Event Input and Processing
  2096. if (fEventsIn.ctrl != nullptr)
  2097. {
  2098. // ----------------------------------------------------------------------------------------------------
  2099. // Message Input
  2100. if (fAtomQueueIn.tryLock())
  2101. {
  2102. if (! fAtomQueueIn.isEmpty())
  2103. {
  2104. const LV2_Atom* atom;
  2105. uint32_t portIndex;
  2106. const uint32_t j = fEventsIn.ctrlIndex;
  2107. for (; fAtomQueueIn.get(&atom, &portIndex);)
  2108. {
  2109. carla_debug("Event input message sent to plugin DSP, type %i:\"%s\", size:%i/%i",
  2110. atom->type, carla_lv2_urid_unmap(this, atom->type),
  2111. atom->size, lv2_atom_total_size(atom));
  2112. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2113. {
  2114. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2115. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2116. }
  2117. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2118. {
  2119. carla_stdout("Event input buffer full, at least 1 message lost");
  2120. continue;
  2121. }
  2122. }
  2123. }
  2124. fAtomQueueIn.unlock();
  2125. }
  2126. // ----------------------------------------------------------------------------------------------------
  2127. // MIDI Input (External)
  2128. if (pData->extNotes.mutex.tryLock())
  2129. {
  2130. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2131. {
  2132. // does not handle MIDI
  2133. pData->extNotes.data.clear();
  2134. }
  2135. else
  2136. {
  2137. const uint32_t j = fEventsIn.ctrlIndex;
  2138. for (; ! pData->extNotes.data.isEmpty();)
  2139. {
  2140. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  2141. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2142. uint8_t midiEvent[3];
  2143. midiEvent[0] = static_cast<uint8_t>(note.channel + (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF);
  2144. midiEvent[1] = note.note;
  2145. midiEvent[2] = note.velo;
  2146. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2147. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2148. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2149. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2150. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2151. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2152. }
  2153. }
  2154. pData->extNotes.mutex.unlock();
  2155. } // End of MIDI Input (External)
  2156. // ----------------------------------------------------------------------------------------------------
  2157. // Event Input (System)
  2158. bool allNotesOffSent = false;
  2159. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2160. uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2161. uint32_t startTime = 0;
  2162. uint32_t timeOffset = 0;
  2163. uint32_t nextBankId;
  2164. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2165. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2166. else
  2167. nextBankId = 0;
  2168. for (uint32_t i=0; i < numEvents; ++i)
  2169. {
  2170. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2171. if (event.time >= frames)
  2172. continue;
  2173. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2174. if (isSampleAccurate && event.time > timeOffset)
  2175. {
  2176. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset))
  2177. {
  2178. startTime = 0;
  2179. timeOffset = event.time;
  2180. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2181. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2182. else
  2183. nextBankId = 0;
  2184. // reset iters
  2185. const uint32_t j = fEventsIn.ctrlIndex;
  2186. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2187. {
  2188. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2189. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2190. }
  2191. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2192. {
  2193. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2194. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2195. }
  2196. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2197. {
  2198. fEventsIn.data[j].midi.event_count = 0;
  2199. fEventsIn.data[j].midi.size = 0;
  2200. evInMidiStates[j].position = event.time;
  2201. }
  2202. }
  2203. else
  2204. startTime += timeOffset;
  2205. }
  2206. switch (event.type)
  2207. {
  2208. case kEngineEventTypeNull:
  2209. break;
  2210. case kEngineEventTypeControl: {
  2211. const EngineControlEvent& ctrlEvent(event.ctrl);
  2212. switch (ctrlEvent.type)
  2213. {
  2214. case kEngineControlEventTypeNull:
  2215. break;
  2216. case kEngineControlEventTypeParameter: {
  2217. #ifndef BUILD_BRIDGE
  2218. // Control backend stuff
  2219. if (event.channel == pData->ctrlChannel)
  2220. {
  2221. float value;
  2222. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2223. {
  2224. value = ctrlEvent.value;
  2225. setDryWet(value, false, false);
  2226. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2227. break;
  2228. }
  2229. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2230. {
  2231. value = ctrlEvent.value*127.0f/100.0f;
  2232. setVolume(value, false, false);
  2233. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2234. break;
  2235. }
  2236. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2237. {
  2238. float left, right;
  2239. value = ctrlEvent.value/0.5f - 1.0f;
  2240. if (value < 0.0f)
  2241. {
  2242. left = -1.0f;
  2243. right = (value*2.0f)+1.0f;
  2244. }
  2245. else if (value > 0.0f)
  2246. {
  2247. left = (value*2.0f)-1.0f;
  2248. right = 1.0f;
  2249. }
  2250. else
  2251. {
  2252. left = -1.0f;
  2253. right = 1.0f;
  2254. }
  2255. setBalanceLeft(left, false, false);
  2256. setBalanceRight(right, false, false);
  2257. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2258. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2259. break;
  2260. }
  2261. }
  2262. #endif
  2263. // Control plugin parameters
  2264. uint32_t k;
  2265. for (k=0; k < pData->param.count; ++k)
  2266. {
  2267. if (pData->param.data[k].midiChannel != event.channel)
  2268. continue;
  2269. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2270. continue;
  2271. if (pData->param.data[k].type != PARAMETER_INPUT)
  2272. continue;
  2273. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2274. continue;
  2275. float value;
  2276. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2277. {
  2278. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2279. }
  2280. else
  2281. {
  2282. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2283. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2284. value = std::rint(value);
  2285. }
  2286. setParameterValue(k, value, false, false, false);
  2287. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2288. break;
  2289. }
  2290. // check if event is already handled
  2291. if (k != pData->param.count)
  2292. break;
  2293. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2294. {
  2295. uint8_t midiData[3];
  2296. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2297. midiData[1] = static_cast<uint8_t>(ctrlEvent.param);
  2298. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2299. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2300. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2301. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2302. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2303. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2304. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0.0, 3, midiData);
  2305. }
  2306. break;
  2307. } // case kEngineControlEventTypeParameter
  2308. case kEngineControlEventTypeMidiBank:
  2309. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2310. nextBankId = ctrlEvent.param;
  2311. break;
  2312. case kEngineControlEventTypeMidiProgram:
  2313. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2314. {
  2315. const uint32_t nextProgramId = ctrlEvent.param;
  2316. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2317. {
  2318. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2319. {
  2320. const int32_t index(static_cast<int32_t>(k));
  2321. setMidiProgram(index, false, false, false);
  2322. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2323. break;
  2324. }
  2325. }
  2326. }
  2327. break;
  2328. case kEngineControlEventTypeAllSoundOff:
  2329. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2330. {
  2331. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2332. uint8_t midiData[3];
  2333. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2334. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2335. midiData[2] = 0;
  2336. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2337. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2338. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2339. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2340. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2341. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2342. }
  2343. break;
  2344. case kEngineControlEventTypeAllNotesOff:
  2345. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2346. {
  2347. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2348. {
  2349. allNotesOffSent = true;
  2350. sendMidiAllNotesOffToCallback();
  2351. }
  2352. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2353. uint8_t midiData[3];
  2354. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2355. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2356. midiData[2] = 0;
  2357. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2358. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2359. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2360. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2361. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2362. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2363. }
  2364. break;
  2365. } // switch (ctrlEvent.type)
  2366. break;
  2367. } // case kEngineEventTypeControl
  2368. case kEngineEventTypeMidi:
  2369. {
  2370. const EngineMidiEvent& midiEvent(event.midi);
  2371. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  2372. uint8_t channel = event.channel;
  2373. uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2374. // Fix bad note-off (per LV2 spec)
  2375. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  2376. status = MIDI_STATUS_NOTE_OFF;
  2377. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2378. continue;
  2379. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2380. continue;
  2381. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2382. continue;
  2383. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2384. continue;
  2385. const uint32_t j = fEventsIn.ctrlIndex;
  2386. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2387. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2388. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2389. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2390. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2391. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiEvent.data);
  2392. if (status == MIDI_STATUS_NOTE_ON)
  2393. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2394. else if (status == MIDI_STATUS_NOTE_OFF)
  2395. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2396. break;
  2397. } // case kEngineEventTypeMidi
  2398. } // switch (event.type)
  2399. }
  2400. pData->postRtEvents.trySplice();
  2401. if (frames > timeOffset)
  2402. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2403. } // End of Event Input and Processing
  2404. // --------------------------------------------------------------------------------------------------------
  2405. // Plugin processing (no events)
  2406. else
  2407. {
  2408. processSingle(inBuffer, outBuffer, frames, 0);
  2409. } // End of Plugin processing (no events)
  2410. CARLA_PROCESS_CONTINUE_CHECK;
  2411. // --------------------------------------------------------------------------------------------------------
  2412. // MIDI Output
  2413. if (fEventsOut.ctrl != nullptr)
  2414. {
  2415. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2416. {
  2417. const LV2_Atom_Event* ev;
  2418. LV2_Atom_Buffer_Iterator iter;
  2419. uint8_t* data;
  2420. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2421. for (;;)
  2422. {
  2423. data = nullptr;
  2424. ev = lv2_atom_buffer_get(&iter, &data);
  2425. if (ev == nullptr || data == nullptr)
  2426. break;
  2427. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2428. {
  2429. if (fEventsOut.ctrl->port != nullptr)
  2430. {
  2431. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2432. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2433. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2434. }
  2435. }
  2436. else if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2437. {
  2438. fAtomQueueOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2439. }
  2440. lv2_atom_buffer_increment(&iter);
  2441. }
  2442. }
  2443. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2444. {
  2445. const LV2_Event* ev;
  2446. LV2_Event_Iterator iter;
  2447. uint8_t* data;
  2448. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2449. for (;;)
  2450. {
  2451. data = nullptr;
  2452. ev = lv2_event_get(&iter, &data);
  2453. if (ev == nullptr || data == nullptr)
  2454. break;
  2455. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2456. {
  2457. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2458. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2459. }
  2460. lv2_event_increment(&iter);
  2461. }
  2462. }
  2463. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2464. {
  2465. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2466. uint32_t eventSize;
  2467. double eventTime;
  2468. unsigned char* eventData;
  2469. for (;;)
  2470. {
  2471. eventSize = 0;
  2472. eventTime = 0.0;
  2473. eventData = nullptr;
  2474. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2475. if (eventData == nullptr || eventSize == 0)
  2476. break;
  2477. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2478. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2479. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2480. lv2midi_step(&state);
  2481. }
  2482. }
  2483. }
  2484. // --------------------------------------------------------------------------------------------------------
  2485. // Control Output
  2486. if (pData->event.portOut != nullptr)
  2487. {
  2488. uint8_t channel;
  2489. uint16_t param;
  2490. float value;
  2491. for (uint32_t k=0; k < pData->param.count; ++k)
  2492. {
  2493. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2494. continue;
  2495. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2496. if (pData->param.data[k].midiCC > 0)
  2497. {
  2498. channel = pData->param.data[k].midiChannel;
  2499. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2500. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2501. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2502. }
  2503. }
  2504. } // End of Control Output
  2505. CARLA_PROCESS_CONTINUE_CHECK;
  2506. // --------------------------------------------------------------------------------------------------------
  2507. // Final work
  2508. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2509. {
  2510. fExt.worker->end_run(fHandle);
  2511. if (fHandle2 != nullptr)
  2512. fExt.worker->end_run(fHandle2);
  2513. }
  2514. fFirstActive = false;
  2515. // --------------------------------------------------------------------------------------------------------
  2516. }
  2517. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2518. {
  2519. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2520. if (pData->audioIn.count > 0)
  2521. {
  2522. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  2523. }
  2524. if (pData->audioOut.count > 0)
  2525. {
  2526. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  2527. }
  2528. // --------------------------------------------------------------------------------------------------------
  2529. // Try lock, silence otherwise
  2530. if (pData->engine->isOffline())
  2531. {
  2532. pData->singleMutex.lock();
  2533. }
  2534. else if (! pData->singleMutex.tryLock())
  2535. {
  2536. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2537. {
  2538. for (uint32_t k=0; k < frames; ++k)
  2539. outBuffer[i][k+timeOffset] = 0.0f;
  2540. }
  2541. return false;
  2542. }
  2543. // --------------------------------------------------------------------------------------------------------
  2544. // Reset audio buffers
  2545. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2546. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2547. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2548. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  2549. // --------------------------------------------------------------------------------------------------------
  2550. // Run plugin
  2551. fDescriptor->run(fHandle, frames);
  2552. if (fHandle2 != nullptr)
  2553. fDescriptor->run(fHandle2, frames);
  2554. // --------------------------------------------------------------------------------------------------------
  2555. // Handle trigger parameters
  2556. for (uint32_t k=0; k < pData->param.count; ++k)
  2557. {
  2558. if (pData->param.data[k].type != PARAMETER_INPUT)
  2559. continue;
  2560. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2561. {
  2562. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2563. {
  2564. fParamBuffers[k] = pData->param.ranges[k].def;
  2565. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2566. }
  2567. }
  2568. }
  2569. pData->postRtEvents.trySplice();
  2570. #ifndef BUILD_BRIDGE
  2571. // --------------------------------------------------------------------------------------------------------
  2572. // Post-processing (dry/wet, volume and balance)
  2573. {
  2574. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2575. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2576. bool isPair;
  2577. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2578. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2579. {
  2580. // Dry/Wet
  2581. if (doDryWet)
  2582. {
  2583. for (uint32_t k=0; k < frames; ++k)
  2584. {
  2585. // TODO
  2586. //if (k < pData->latency && pData->latency < frames)
  2587. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  2588. //else
  2589. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2590. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  2591. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2592. }
  2593. }
  2594. // Balance
  2595. if (doBalance)
  2596. {
  2597. isPair = (i % 2 == 0);
  2598. if (isPair)
  2599. {
  2600. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2601. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  2602. }
  2603. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2604. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2605. for (uint32_t k=0; k < frames; ++k)
  2606. {
  2607. if (isPair)
  2608. {
  2609. // left
  2610. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2611. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2612. }
  2613. else
  2614. {
  2615. // right
  2616. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2617. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2618. }
  2619. }
  2620. }
  2621. // Volume (and buffer copy)
  2622. {
  2623. for (uint32_t k=0; k < frames; ++k)
  2624. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2625. }
  2626. }
  2627. } // End of Post-processing
  2628. #else // BUILD_BRIDGE
  2629. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2630. {
  2631. for (uint32_t k=0; k < frames; ++k)
  2632. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2633. }
  2634. #endif
  2635. // --------------------------------------------------------------------------------------------------------
  2636. pData->singleMutex.unlock();
  2637. return true;
  2638. }
  2639. void bufferSizeChanged(const uint32_t newBufferSize) override
  2640. {
  2641. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2642. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2643. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2644. {
  2645. if (fAudioInBuffers[i] != nullptr)
  2646. delete[] fAudioInBuffers[i];
  2647. fAudioInBuffers[i] = new float[newBufferSize];
  2648. }
  2649. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2650. {
  2651. if (fAudioOutBuffers[i] != nullptr)
  2652. delete[] fAudioOutBuffers[i];
  2653. fAudioOutBuffers[i] = new float[newBufferSize];
  2654. }
  2655. if (fHandle2 == nullptr)
  2656. {
  2657. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2658. {
  2659. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2660. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2661. }
  2662. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2663. {
  2664. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2665. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2666. }
  2667. }
  2668. else
  2669. {
  2670. if (pData->audioIn.count > 0)
  2671. {
  2672. CARLA_ASSERT(pData->audioIn.count == 2);
  2673. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2674. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2675. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2676. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2677. }
  2678. if (pData->audioOut.count > 0)
  2679. {
  2680. CARLA_ASSERT(pData->audioOut.count == 2);
  2681. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2682. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2683. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2684. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2685. }
  2686. }
  2687. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2688. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2689. {
  2690. fLv2Options.maxBufferSize = newBufferSizeInt;
  2691. if (fLv2Options.minBufferSize != 1)
  2692. fLv2Options.minBufferSize = newBufferSizeInt;
  2693. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2694. {
  2695. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2696. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2697. }
  2698. }
  2699. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2700. }
  2701. void sampleRateChanged(const double newSampleRate) override
  2702. {
  2703. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2704. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2705. if (fLv2Options.sampleRate != newSampleRate)
  2706. {
  2707. fLv2Options.sampleRate = newSampleRate;
  2708. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2709. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2710. }
  2711. for (uint32_t k=0; k < pData->param.count; ++k)
  2712. {
  2713. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2714. {
  2715. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2716. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2717. break;
  2718. }
  2719. }
  2720. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2721. }
  2722. void offlineModeChanged(const bool isOffline) override
  2723. {
  2724. for (uint32_t k=0; k < pData->param.count; ++k)
  2725. {
  2726. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  2727. {
  2728. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2729. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2730. break;
  2731. }
  2732. }
  2733. }
  2734. // -------------------------------------------------------------------
  2735. // Plugin buffers
  2736. void initBuffers() override
  2737. {
  2738. fEventsIn.initBuffers();
  2739. fEventsOut.initBuffers();
  2740. CarlaPlugin::initBuffers();
  2741. }
  2742. void clearBuffers() override
  2743. {
  2744. carla_debug("Lv2Plugin::clearBuffers() - start");
  2745. if (fAudioInBuffers != nullptr)
  2746. {
  2747. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2748. {
  2749. if (fAudioInBuffers[i] != nullptr)
  2750. {
  2751. delete[] fAudioInBuffers[i];
  2752. fAudioInBuffers[i] = nullptr;
  2753. }
  2754. }
  2755. delete[] fAudioInBuffers;
  2756. fAudioInBuffers = nullptr;
  2757. }
  2758. if (fAudioOutBuffers != nullptr)
  2759. {
  2760. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2761. {
  2762. if (fAudioOutBuffers[i] != nullptr)
  2763. {
  2764. delete[] fAudioOutBuffers[i];
  2765. fAudioOutBuffers[i] = nullptr;
  2766. }
  2767. }
  2768. delete[] fAudioOutBuffers;
  2769. fAudioOutBuffers = nullptr;
  2770. }
  2771. if (fParamBuffers != nullptr)
  2772. {
  2773. delete[] fParamBuffers;
  2774. fParamBuffers = nullptr;
  2775. }
  2776. fEventsIn.clear();
  2777. fEventsOut.clear();
  2778. CarlaPlugin::clearBuffers();
  2779. carla_debug("Lv2Plugin::clearBuffers() - end");
  2780. }
  2781. // -------------------------------------------------------------------
  2782. // OSC stuff
  2783. void updateOscData(const lo_address& source, const char* const url) override
  2784. {
  2785. CarlaPlugin::updateOscData(source, url);
  2786. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  2787. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i));
  2788. }
  2789. // -------------------------------------------------------------------
  2790. // Post-poned UI Stuff
  2791. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2792. {
  2793. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2794. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2795. if (fUi.type == UI::TYPE_OSC)
  2796. {
  2797. if (pData->osc.data.target != nullptr)
  2798. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  2799. }
  2800. else
  2801. {
  2802. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2803. {
  2804. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  2805. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  2806. }
  2807. }
  2808. }
  2809. void uiMidiProgramChange(const uint32_t index) noexcept override
  2810. {
  2811. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2812. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2813. if (fUi.type == UI::TYPE_OSC)
  2814. {
  2815. if (pData->osc.data.target != nullptr)
  2816. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2817. }
  2818. else
  2819. {
  2820. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2821. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2822. }
  2823. }
  2824. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2825. {
  2826. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2827. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2828. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2829. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2830. if (fUi.type == UI::TYPE_OSC)
  2831. {
  2832. if (pData->osc.data.target != nullptr)
  2833. {
  2834. uint8_t midiData[4] = { 0 };
  2835. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2836. midiData[2] = note;
  2837. midiData[3] = velo;
  2838. osc_send_midi(pData->osc.data, midiData);
  2839. }
  2840. }
  2841. else
  2842. {
  2843. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2844. {
  2845. LV2_Atom_MidiEvent midiEv;
  2846. midiEv.event.time.frames = 0;
  2847. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2848. midiEv.event.body.size = 3;
  2849. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2850. midiEv.data[1] = note;
  2851. midiEv.data[2] = velo;
  2852. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2853. }
  2854. }
  2855. }
  2856. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2857. {
  2858. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2859. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2860. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2861. if (fUi.type == UI::TYPE_OSC)
  2862. {
  2863. if (pData->osc.data.target != nullptr)
  2864. {
  2865. uint8_t midiData[4] = { 0 };
  2866. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2867. midiData[2] = note;
  2868. osc_send_midi(pData->osc.data, midiData);
  2869. }
  2870. }
  2871. else
  2872. {
  2873. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2874. {
  2875. LV2_Atom_MidiEvent midiEv;
  2876. midiEv.event.time.frames = 0;
  2877. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2878. midiEv.event.body.size = 3;
  2879. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2880. midiEv.data[1] = note;
  2881. midiEv.data[2] = 0;
  2882. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2883. }
  2884. }
  2885. }
  2886. // -------------------------------------------------------------------
  2887. bool isRealtimeSafe() const noexcept
  2888. {
  2889. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  2890. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2891. {
  2892. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  2893. return true;
  2894. }
  2895. return false;
  2896. }
  2897. bool needsFixedBuffer() const noexcept
  2898. {
  2899. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  2900. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  2901. {
  2902. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  2903. return true;
  2904. }
  2905. return false;
  2906. }
  2907. // -------------------------------------------------------------------
  2908. bool isUiBridgeable(const uint32_t uiId) const noexcept
  2909. {
  2910. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  2911. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  2912. if (std::strstr(rdfUi->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  2913. return false;
  2914. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  2915. {
  2916. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  2917. return false;
  2918. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2919. return false;
  2920. }
  2921. return true;
  2922. }
  2923. bool isUiResizable() const noexcept
  2924. {
  2925. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  2926. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  2927. {
  2928. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  2929. return false;
  2930. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2931. return false;
  2932. }
  2933. return true;
  2934. }
  2935. const char* getUiBridgeBinary(const LV2_Property type) const
  2936. {
  2937. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  2938. if (bridgeBinary.isEmpty())
  2939. return nullptr;
  2940. // test for local build
  2941. if (bridgeBinary.endsWith("/source/backend/"))
  2942. bridgeBinary += "../bridges/";
  2943. switch (type)
  2944. {
  2945. case LV2_UI_GTK2:
  2946. bridgeBinary += "carla-bridge-lv2-gtk2";
  2947. break;
  2948. case LV2_UI_GTK3:
  2949. bridgeBinary += "carla-bridge-lv2-gtk3";
  2950. break;
  2951. case LV2_UI_QT4:
  2952. bridgeBinary += "carla-bridge-lv2-qt4";
  2953. break;
  2954. case LV2_UI_QT5:
  2955. bridgeBinary += "carla-bridge-lv2-qt5";
  2956. break;
  2957. case LV2_UI_COCOA:
  2958. bridgeBinary += "carla-bridge-lv2-cocoa";
  2959. break;
  2960. case LV2_UI_WINDOWS:
  2961. bridgeBinary += "carla-bridge-lv2-windows";
  2962. break;
  2963. case LV2_UI_X11:
  2964. bridgeBinary += "carla-bridge-lv2-x11";
  2965. break;
  2966. case LV2_UI_EXTERNAL:
  2967. case LV2_UI_OLD_EXTERNAL:
  2968. bridgeBinary += "carla-bridge-lv2-external";
  2969. break;
  2970. default:
  2971. return nullptr;
  2972. }
  2973. #ifdef CARLA_OS_WIN
  2974. bridgeBinary += ".exe";
  2975. #endif
  2976. QFile file(bridgeBinary.getBuffer());
  2977. if (! file.exists())
  2978. return nullptr;
  2979. return bridgeBinary.dup();
  2980. }
  2981. // -------------------------------------------------------------------
  2982. void recheckExtensions()
  2983. {
  2984. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  2985. carla_debug("Lv2Plugin::recheckExtensions()");
  2986. fExt.options = nullptr;
  2987. fExt.programs = nullptr;
  2988. fExt.state = nullptr;
  2989. fExt.worker = nullptr;
  2990. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  2991. {
  2992. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  2993. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  2994. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  2995. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  2996. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  2997. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  2998. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  2999. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3000. else
  3001. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3002. }
  3003. if (fDescriptor->extension_data != nullptr)
  3004. {
  3005. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3006. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3007. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3008. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3009. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3010. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3011. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3012. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3013. // check if invalid
  3014. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3015. fExt.options = nullptr;
  3016. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3017. fExt.programs = nullptr;
  3018. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3019. fExt.state = nullptr;
  3020. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3021. fExt.worker = nullptr;
  3022. }
  3023. }
  3024. // -------------------------------------------------------------------
  3025. void updateUi()
  3026. {
  3027. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  3028. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  3029. carla_debug("Lv2Plugin::updateUi()");
  3030. fExt.uiidle = nullptr;
  3031. fExt.uiprograms = nullptr;
  3032. if (fUi.descriptor->extension_data != nullptr)
  3033. {
  3034. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3035. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3036. // check if invalid
  3037. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3038. fExt.uiidle = nullptr;
  3039. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3040. fExt.uiprograms = nullptr;
  3041. }
  3042. // update midi program
  3043. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3044. {
  3045. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3046. fExt.uiprograms->select_program(fUi.handle, curData.bank, curData.program);
  3047. }
  3048. // update control ports
  3049. if (fUi.descriptor->port_event != nullptr)
  3050. {
  3051. float value;
  3052. for (uint32_t i=0; i < pData->param.count; ++i)
  3053. {
  3054. value = getParameterValue(i);
  3055. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3056. }
  3057. }
  3058. }
  3059. // -------------------------------------------------------------------
  3060. LV2_URID getCustomURID(const char* const uri)
  3061. {
  3062. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3063. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3064. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3065. {
  3066. const char*& thisUri(fCustomURIDs.getAt(i));
  3067. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3068. return static_cast<LV2_URID>(i);
  3069. }
  3070. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3071. fCustomURIDs.append(carla_strdup(uri));
  3072. if (fUi.type == UI::TYPE_OSC && pData->osc.data.target != nullptr)
  3073. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3074. return urid;
  3075. }
  3076. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3077. {
  3078. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  3079. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  3080. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3081. return fCustomURIDs.getAt(urid);
  3082. }
  3083. // -------------------------------------------------------------------
  3084. void handleProgramChanged(const int32_t index)
  3085. {
  3086. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3087. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3088. if (index == -1)
  3089. {
  3090. const ScopedSingleProcessLocker spl(this, true);
  3091. return reloadPrograms(false);
  3092. }
  3093. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3094. {
  3095. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3096. {
  3097. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3098. if (pData->midiprog.data[index].name != nullptr)
  3099. delete[] pData->midiprog.data[index].name;
  3100. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3101. if (index == pData->midiprog.current)
  3102. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3103. else
  3104. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3105. }
  3106. }
  3107. }
  3108. // -------------------------------------------------------------------
  3109. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3110. {
  3111. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3112. carla_debug("Lv2Plugin::handleResizePort(%i, " P_SIZE ")", index, size);
  3113. // TODO
  3114. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3115. (void)index;
  3116. }
  3117. // -------------------------------------------------------------------
  3118. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3119. {
  3120. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3121. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3122. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3123. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3124. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3125. 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);
  3126. const char* const skey(carla_lv2_urid_unmap(this, key));
  3127. const char* const stype(carla_lv2_urid_unmap(this, type));
  3128. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3129. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3130. // Check if we already have this key
  3131. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3132. {
  3133. CustomData& data(it.getValue());
  3134. if (std::strcmp(data.key, skey) == 0)
  3135. {
  3136. // found it
  3137. if (data.value != nullptr)
  3138. delete[] data.value;
  3139. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3140. data.value = carla_strdup((const char*)value);
  3141. else
  3142. data.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3143. return LV2_STATE_SUCCESS;
  3144. }
  3145. }
  3146. // Otherwise store it
  3147. CustomData newData;
  3148. newData.type = carla_strdup(stype);
  3149. newData.key = carla_strdup(skey);
  3150. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3151. newData.value = carla_strdup((const char*)value);
  3152. else
  3153. newData.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3154. pData->custom.append(newData);
  3155. return LV2_STATE_SUCCESS;
  3156. }
  3157. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3158. {
  3159. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3160. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3161. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3162. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3163. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3164. const char* const skey(carla_lv2_urid_unmap(this, key));
  3165. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3166. const char* stype = nullptr;
  3167. const char* stringData = nullptr;
  3168. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3169. {
  3170. const CustomData& data(it.getValue());
  3171. if (std::strcmp(data.key, skey) == 0)
  3172. {
  3173. stype = data.type;
  3174. stringData = data.value;
  3175. break;
  3176. }
  3177. }
  3178. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3179. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3180. *type = carla_lv2_urid_map(this, stype);
  3181. *flags = LV2_STATE_IS_POD;
  3182. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3183. {
  3184. *size = std::strlen(stringData);
  3185. return stringData;
  3186. }
  3187. else
  3188. {
  3189. static QByteArray chunk; // FIXME
  3190. chunk = QByteArray::fromBase64(stringData);
  3191. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3192. *size = static_cast<size_t>(chunk.size());
  3193. return chunk.constData();
  3194. }
  3195. }
  3196. // -------------------------------------------------------------------
  3197. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3198. {
  3199. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3200. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3201. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3202. if (pData->engine->isOffline())
  3203. {
  3204. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3205. return LV2_WORKER_SUCCESS;
  3206. }
  3207. LV2_Atom atom;
  3208. atom.size = size;
  3209. atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3210. return fAtomQueueOut.putChunk(&atom, data, fEventsIn.ctrl->rindex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3211. }
  3212. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3213. {
  3214. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3215. LV2_Atom atom;
  3216. atom.size = size;
  3217. atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3218. return fAtomQueueIn.putChunk(&atom, data, fEventsIn.ctrl->rindex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3219. }
  3220. // -------------------------------------------------------------------
  3221. void handleExternalUiClosed()
  3222. {
  3223. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EXTERNAL,);
  3224. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3225. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3226. fUi.descriptor->cleanup(fUi.handle);
  3227. fUi.handle = nullptr;
  3228. fUi.widget = nullptr;
  3229. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3230. }
  3231. // -------------------------------------------------------------------
  3232. uint32_t handleUiPortMap(const char* const symbol) const noexcept
  3233. {
  3234. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3235. carla_debug("Lv2Plugin::handleUiPortMap(\"%s\")", symbol);
  3236. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3237. {
  3238. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3239. return i;
  3240. }
  3241. return LV2UI_INVALID_PORT_INDEX;
  3242. }
  3243. int handleUiResize(const int width, const int height)
  3244. {
  3245. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3246. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3247. carla_debug("Lv2Plugin::handleUiResize(%i, %i)", width, height);
  3248. return 0;
  3249. }
  3250. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3251. {
  3252. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3253. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3254. carla_debug("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p)", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3255. switch (format)
  3256. {
  3257. case 0:
  3258. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3259. for (uint32_t i=0; i < pData->param.count; ++i)
  3260. {
  3261. if (pData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3262. {
  3263. const float value(*(const float*)buffer);
  3264. if (fParamBuffers[i] != value)
  3265. setParameterValue(i, value, false, true, true);
  3266. break;
  3267. }
  3268. }
  3269. break;
  3270. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3271. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT:
  3272. CARLA_SAFE_ASSERT_RETURN(((const LV2_Atom*)buffer)->size == bufferSize,);
  3273. fAtomQueueIn.put((const LV2_Atom*)buffer, rindex);
  3274. break;
  3275. default:
  3276. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3277. break;
  3278. }
  3279. }
  3280. // -------------------------------------------------------------------
  3281. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3282. {
  3283. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3284. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3285. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3286. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3287. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\")", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3288. int32_t rindex = -1;
  3289. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3290. {
  3291. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3292. {
  3293. rindex = static_cast<int32_t>(i);
  3294. break;
  3295. }
  3296. }
  3297. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3298. switch (type)
  3299. {
  3300. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3301. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3302. for (uint32_t i=0; i < pData->param.count; ++i)
  3303. {
  3304. if (pData->param.data[i].rindex == rindex)
  3305. {
  3306. const float valuef(*(const float*)value);
  3307. setParameterValue(i, valuef, true, true, true);
  3308. break;
  3309. }
  3310. }
  3311. break;
  3312. default:
  3313. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3314. break;
  3315. }
  3316. }
  3317. // -------------------------------------------------------------------
  3318. public:
  3319. bool init(const char* const bundle, const char* const name, const char* const uri)
  3320. {
  3321. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3322. // ---------------------------------------------------------------
  3323. // first checks
  3324. if (pData->client != nullptr)
  3325. {
  3326. pData->engine->setLastError("Plugin client is already registered");
  3327. return false;
  3328. }
  3329. if (bundle == nullptr || bundle[0] == '\0')
  3330. {
  3331. pData->engine->setLastError("null bundle");
  3332. return false;
  3333. }
  3334. if (uri == nullptr || uri[0] == '\0')
  3335. {
  3336. pData->engine->setLastError("null uri");
  3337. return false;
  3338. }
  3339. // ---------------------------------------------------------------
  3340. // get plugin from lv2_rdf (lilv)
  3341. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3342. // Convert bundle filename to URI
  3343. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  3344. if (! qBundle.endsWith(OS_SEP_STR))
  3345. qBundle += OS_SEP_STR;
  3346. // Load bundle
  3347. Lilv::Node lilvBundle(lv2World.new_uri(qBundle.toUtf8().constData()));
  3348. lv2World.load_bundle(lilvBundle);
  3349. fRdfDescriptor = lv2_rdf_new(uri, true);
  3350. if (fRdfDescriptor == nullptr)
  3351. {
  3352. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3353. return false;
  3354. }
  3355. // ---------------------------------------------------------------
  3356. // open DLL
  3357. if (! pData->libOpen(fRdfDescriptor->Binary))
  3358. {
  3359. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3360. return false;
  3361. }
  3362. // ---------------------------------------------------------------
  3363. // get DLL main entry
  3364. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3365. if (descFn == nullptr)
  3366. {
  3367. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3368. return false;
  3369. }
  3370. // -----------------------------------------------------------
  3371. // get descriptor that matches URI
  3372. uint32_t i = 0;
  3373. while ((fDescriptor = descFn(i++)))
  3374. {
  3375. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3376. break;
  3377. }
  3378. if (fDescriptor == nullptr)
  3379. {
  3380. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3381. return false;
  3382. }
  3383. // ---------------------------------------------------------------
  3384. // check supported port-types and features
  3385. bool canContinue = true;
  3386. // Check supported ports
  3387. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3388. {
  3389. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3390. if (! is_lv2_port_supported(portTypes))
  3391. {
  3392. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3393. {
  3394. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3395. canContinue = false;
  3396. break;
  3397. }
  3398. }
  3399. }
  3400. // Check supported features
  3401. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3402. {
  3403. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  3404. {
  3405. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  3406. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  3407. {
  3408. canContinue = false;
  3409. pData->engine->setLastError(msg.toUtf8().constData());
  3410. break;
  3411. }
  3412. else
  3413. carla_stderr("%s", msg.toUtf8().constData());
  3414. }
  3415. }
  3416. if (! canContinue)
  3417. {
  3418. // error already set
  3419. return false;
  3420. }
  3421. // ---------------------------------------------------------------
  3422. // get info
  3423. if (name != nullptr && name[0] != '\0')
  3424. pData->name = pData->engine->getUniquePluginName(name);
  3425. else
  3426. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3427. pData->filename = carla_strdup(bundle);
  3428. // ---------------------------------------------------------------
  3429. // register client
  3430. pData->client = pData->engine->addClient(this);
  3431. if (pData->client == nullptr || ! pData->client->isOk())
  3432. {
  3433. pData->engine->setLastError("Failed to register plugin client");
  3434. return false;
  3435. }
  3436. // ---------------------------------------------------------------
  3437. // initialize options
  3438. fLv2Options.minBufferSize = 1;
  3439. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3440. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3441. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3442. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3443. {
  3444. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3445. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3446. {
  3447. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3448. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3449. }
  3450. }
  3451. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3452. // ---------------------------------------------------------------
  3453. // initialize features (part 1)
  3454. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3455. eventFt->callback_data = this;
  3456. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3457. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3458. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3459. logFt->handle = this;
  3460. logFt->printf = carla_lv2_log_printf;
  3461. logFt->vprintf = carla_lv2_log_vprintf;
  3462. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3463. stateMakePathFt->handle = this;
  3464. stateMakePathFt->path = carla_lv2_state_make_path;
  3465. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3466. stateMapPathFt->handle = this;
  3467. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3468. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3469. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3470. programsFt->handle = this;
  3471. programsFt->program_changed = carla_lv2_program_changed;
  3472. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3473. rsPortFt->data = this;
  3474. rsPortFt->resize = carla_lv2_resize_port;
  3475. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3476. lv2_rtmempool_init(rtMemPoolFt);
  3477. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3478. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3479. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3480. uriMapFt->callback_data = this;
  3481. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3482. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3483. uridMapFt->handle = this;
  3484. uridMapFt->map = carla_lv2_urid_map;
  3485. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3486. uridUnmapFt->handle = this;
  3487. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3488. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3489. workerFt->handle = this;
  3490. workerFt->schedule_work = carla_lv2_worker_schedule;
  3491. // ---------------------------------------------------------------
  3492. // initialize features (part 2)
  3493. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3494. fFeatures[j] = new LV2_Feature;
  3495. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3496. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3497. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3498. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3499. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3500. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3501. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3502. fFeatures[kFeatureIdEvent]->data = eventFt;
  3503. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3504. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3505. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3506. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3507. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3508. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3509. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3510. fFeatures[kFeatureIdLogs]->data = logFt;
  3511. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3512. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3513. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3514. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3515. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3516. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3517. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3518. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3519. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3520. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3521. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3522. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3523. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3524. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3525. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3526. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3527. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3528. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3529. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3530. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3531. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3532. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3533. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3534. fFeatures[kFeatureIdWorker]->data = workerFt;
  3535. // if a fixed buffer is not needed, skip its feature
  3536. if (! needsFixedBuffer())
  3537. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3538. // if power of 2 is not possible, skip its feature FIXME
  3539. //if (fLv2Options.maxBufferSize)
  3540. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3541. // ---------------------------------------------------------------
  3542. // initialize plugin
  3543. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3544. if (fHandle == nullptr)
  3545. {
  3546. pData->engine->setLastError("Plugin failed to initialize");
  3547. return false;
  3548. }
  3549. // ---------------------------------------------------------------
  3550. // load plugin settings
  3551. {
  3552. // set default options
  3553. pData->options = 0x0;
  3554. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3555. if (getMidiInCount() > 0 || needsFixedBuffer())
  3556. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3557. if (pData->engine->getOptions().forceStereo)
  3558. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3559. if (getMidiInCount() > 0)
  3560. {
  3561. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3562. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3563. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3564. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3565. }
  3566. // set identifier string
  3567. CarlaString identifier("LV2/");
  3568. identifier += uri;
  3569. pData->identifier = identifier.dup();
  3570. // load settings
  3571. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3572. // ignore settings, we need this anyway
  3573. if (getMidiInCount() > 0 || needsFixedBuffer())
  3574. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3575. }
  3576. // ---------------------------------------------------------------
  3577. // gui stuff
  3578. if (fRdfDescriptor->UICount != 0)
  3579. initUi();
  3580. return true;
  3581. }
  3582. // -------------------------------------------------------------------
  3583. void initUi()
  3584. {
  3585. // ---------------------------------------------------------------
  3586. // find more appropriate ui
  3587. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3588. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3589. #ifdef BUILD_BRIDGE
  3590. const bool preferUiBridges(false);
  3591. #else
  3592. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3593. #endif
  3594. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3595. {
  3596. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3597. const int ii(static_cast<int>(i));
  3598. switch (fRdfDescriptor->UIs[i].Type)
  3599. {
  3600. case LV2_UI_QT4:
  3601. if (isUiBridgeable(i))
  3602. eQt4 = ii;
  3603. break;
  3604. case LV2_UI_QT5:
  3605. if (isUiBridgeable(i))
  3606. eQt5 = ii;
  3607. break;
  3608. case LV2_UI_GTK2:
  3609. if (isUiBridgeable(i))
  3610. eGtk2 = ii;
  3611. break;
  3612. case LV2_UI_GTK3:
  3613. if (isUiBridgeable(i))
  3614. eGtk3 = ii;
  3615. break;
  3616. #if defined(CARLA_OS_HAIKU)
  3617. #elif defined(CARLA_OS_MAC)
  3618. case LV2_UI_COCOA:
  3619. if (isUiBridgeable(i) && preferUiBridges)
  3620. eCocoa = ii;
  3621. iCocoa = ii;
  3622. break;
  3623. #elif defined(CARLA_OS_WIN)
  3624. case LV2_UI_WINDOWS:
  3625. if (isUiBridgeable(i) && preferUiBridges)
  3626. eWindows = ii;
  3627. iWindows = ii;
  3628. break;
  3629. #else
  3630. case LV2_UI_X11:
  3631. if (isUiBridgeable(i) && preferUiBridges)
  3632. eX11 = ii;
  3633. iX11 = ii;
  3634. break;
  3635. #endif
  3636. case LV2_UI_EXTERNAL:
  3637. case LV2_UI_OLD_EXTERNAL:
  3638. if (isUiBridgeable(i))
  3639. eExt = ii;
  3640. iExt = ii;
  3641. break;
  3642. default:
  3643. break;
  3644. }
  3645. }
  3646. if (eQt4 >= 0)
  3647. iFinal = eQt4;
  3648. else if (eQt5 >= 0)
  3649. iFinal = eQt5;
  3650. else if (eGtk2 >= 0)
  3651. iFinal = eGtk2;
  3652. else if (eGtk3 >= 0)
  3653. iFinal = eGtk3;
  3654. else if (eCocoa >= 0)
  3655. iFinal = eCocoa;
  3656. else if (eWindows >= 0)
  3657. iFinal = eWindows;
  3658. else if (eX11 >= 0)
  3659. iFinal = eX11;
  3660. //else if (eExt >= 0) // TODO
  3661. // iFinal = eExt;
  3662. else if (iCocoa >= 0)
  3663. iFinal = iCocoa;
  3664. else if (iWindows >= 0)
  3665. iFinal = iWindows;
  3666. else if (iX11 >= 0)
  3667. iFinal = iX11;
  3668. else if (iExt >= 0)
  3669. iFinal = iExt;
  3670. if (iFinal < 0)
  3671. {
  3672. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3673. return;
  3674. }
  3675. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3676. // ---------------------------------------------------------------
  3677. // check supported ui features
  3678. bool canContinue = true;
  3679. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3680. {
  3681. if (! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3682. {
  3683. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3684. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type))
  3685. {
  3686. canContinue = false;
  3687. break;
  3688. }
  3689. }
  3690. }
  3691. if (! canContinue)
  3692. {
  3693. fUi.rdfDescriptor = nullptr;
  3694. return;
  3695. }
  3696. // ---------------------------------------------------------------
  3697. // initialize ui according to type
  3698. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3699. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  3700. {
  3701. // -----------------------------------------------------------
  3702. // initialize osc-bridge
  3703. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  3704. {
  3705. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  3706. fUi.type = UI::TYPE_OSC;
  3707. pData->osc.thread.setOscData(bridgeBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3708. delete[] bridgeBinary;
  3709. return;
  3710. }
  3711. }
  3712. // ---------------------------------------------------------------
  3713. // open UI DLL
  3714. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3715. {
  3716. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUi.rdfDescriptor->Binary));
  3717. fUi.rdfDescriptor = nullptr;
  3718. return;
  3719. }
  3720. // ---------------------------------------------------------------
  3721. // get UI DLL main entry
  3722. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  3723. if (uiDescFn == nullptr)
  3724. {
  3725. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3726. pData->uiLibClose();
  3727. fUi.rdfDescriptor = nullptr;
  3728. return;
  3729. }
  3730. // ---------------------------------------------------------------
  3731. // get UI descriptor that matches UI URI
  3732. uint32_t i = 0;
  3733. while ((fUi.descriptor = uiDescFn(i++)))
  3734. {
  3735. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3736. break;
  3737. }
  3738. if (fUi.descriptor == nullptr)
  3739. {
  3740. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3741. pData->uiLibClose();
  3742. fUi.rdfDescriptor = nullptr;
  3743. return;
  3744. }
  3745. // ---------------------------------------------------------------
  3746. // check if ui is usable
  3747. switch (uiType)
  3748. {
  3749. case LV2_UI_QT4:
  3750. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  3751. break;
  3752. case LV2_UI_QT5:
  3753. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  3754. break;
  3755. case LV2_UI_GTK2:
  3756. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  3757. break;
  3758. case LV2_UI_GTK3:
  3759. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  3760. break;
  3761. #if defined(CARLA_OS_HAIKU)
  3762. #elif defined(CARLA_OS_MAC)
  3763. case LV2_UI_COCOA:
  3764. carla_stdout("Will use LV2 Cocoa UI");
  3765. fUi.type = UI::TYPE_EMBED;
  3766. break;
  3767. #elif defined(CARLA_OS_WIN)
  3768. case LV2_UI_WINDOWS:
  3769. carla_stdout("Will use LV2 Windows UI");
  3770. fUi.type = UI::TYPE_EMBED;
  3771. break;
  3772. #else
  3773. case LV2_UI_X11:
  3774. carla_stdout("Will use LV2 X11 UI");
  3775. fUi.type = UI::TYPE_EMBED;
  3776. break;
  3777. #endif
  3778. case LV2_UI_EXTERNAL:
  3779. case LV2_UI_OLD_EXTERNAL:
  3780. carla_stdout("Will use LV2 External UI");
  3781. fUi.type = UI::TYPE_EXTERNAL;
  3782. break;
  3783. }
  3784. if (fUi.type == UI::TYPE_NULL)
  3785. {
  3786. pData->uiLibClose();
  3787. fUi.descriptor = nullptr;
  3788. fUi.rdfDescriptor = nullptr;
  3789. return;
  3790. }
  3791. // ---------------------------------------------------------------
  3792. // initialize ui features (part 1)
  3793. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  3794. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3795. uiDataFt->data_access = fDescriptor->extension_data;
  3796. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3797. uiPortMapFt->handle = this;
  3798. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3799. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3800. uiResizeFt->handle = this;
  3801. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3802. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3803. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3804. uiExternalHostFt->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  3805. // ---------------------------------------------------------------
  3806. // initialize ui features (part 2)
  3807. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  3808. fFeatures[j] = new LV2_Feature;
  3809. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3810. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3811. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3812. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3813. fFeatures[kFeatureIdUiIdle]->URI = LV2_UI__idle;
  3814. fFeatures[kFeatureIdUiIdle]->data = nullptr;
  3815. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  3816. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  3817. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  3818. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  3819. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  3820. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  3821. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3822. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3823. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3824. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3825. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  3826. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  3827. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3828. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3829. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  3830. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  3831. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3832. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3833. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3834. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3835. }
  3836. // -------------------------------------------------------------------
  3837. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  3838. {
  3839. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  3840. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  3841. fAtomQueueIn.put(atom, portIndex);
  3842. }
  3843. void handleUridMap(const LV2_URID urid, const char* const uri)
  3844. {
  3845. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  3846. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  3847. carla_debug("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count(), uri);
  3848. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  3849. fCustomURIDs.append(carla_strdup(uri));
  3850. }
  3851. // -------------------------------------------------------------------
  3852. private:
  3853. LV2_Handle fHandle;
  3854. LV2_Handle fHandle2;
  3855. LV2_Feature* fFeatures[kFeatureCountAll+1];
  3856. const LV2_Descriptor* fDescriptor;
  3857. const LV2_RDF_Descriptor* fRdfDescriptor;
  3858. float** fAudioInBuffers;
  3859. float** fAudioOutBuffers;
  3860. float* fParamBuffers;
  3861. Lv2AtomQueue fAtomQueueIn;
  3862. Lv2AtomQueue fAtomQueueOut;
  3863. LV2_Atom_Forge fAtomForge;
  3864. Lv2PluginEventData fEventsIn;
  3865. Lv2PluginEventData fEventsOut;
  3866. Lv2PluginOptions fLv2Options;
  3867. LinkedList<const char*> fCustomURIDs;
  3868. bool fFirstActive; // first process() call after activate()
  3869. EngineTimeInfo fLastTimeInfo;
  3870. struct Extensions {
  3871. const LV2_Options_Interface* options;
  3872. const LV2_State_Interface* state;
  3873. const LV2_Worker_Interface* worker;
  3874. const LV2_Programs_Interface* programs;
  3875. const LV2UI_Idle_Interface* uiidle;
  3876. const LV2_Programs_UI_Interface* uiprograms;
  3877. Extensions()
  3878. : options(nullptr),
  3879. state(nullptr),
  3880. worker(nullptr),
  3881. programs(nullptr),
  3882. uiidle(nullptr),
  3883. uiprograms(nullptr) {}
  3884. } fExt;
  3885. struct UI {
  3886. enum Type {
  3887. TYPE_NULL,
  3888. TYPE_EMBED,
  3889. TYPE_EXTERNAL,
  3890. TYPE_OSC
  3891. };
  3892. Type type;
  3893. LV2UI_Handle handle;
  3894. LV2UI_Widget widget;
  3895. const LV2UI_Descriptor* descriptor;
  3896. const LV2_RDF_UI* rdfDescriptor;
  3897. UI()
  3898. : type(TYPE_NULL),
  3899. handle(nullptr),
  3900. widget(nullptr),
  3901. descriptor(nullptr),
  3902. rdfDescriptor(nullptr) {}
  3903. ~UI()
  3904. {
  3905. CARLA_ASSERT(handle == nullptr);
  3906. CARLA_ASSERT(widget == nullptr);
  3907. CARLA_ASSERT(descriptor == nullptr);
  3908. CARLA_ASSERT(rdfDescriptor == nullptr);
  3909. }
  3910. } fUi;
  3911. // -------------------------------------------------------------------
  3912. // Event Feature
  3913. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3914. {
  3915. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  3916. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  3917. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  3918. return 0;
  3919. }
  3920. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3921. {
  3922. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  3923. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  3924. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  3925. return 0;
  3926. }
  3927. // -------------------------------------------------------------------
  3928. // Logs Feature
  3929. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  3930. {
  3931. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  3932. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  3933. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  3934. #ifndef DEBUG
  3935. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3936. return 0;
  3937. #endif
  3938. va_list args;
  3939. va_start(args, fmt);
  3940. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  3941. va_end(args);
  3942. return ret;
  3943. }
  3944. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  3945. {
  3946. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  3947. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  3948. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  3949. #ifndef DEBUG
  3950. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3951. return 0;
  3952. #endif
  3953. int ret = 0;
  3954. switch (type)
  3955. {
  3956. case CARLA_URI_MAP_ID_LOG_ERROR:
  3957. std::fprintf(stderr, "\x1b[31m");
  3958. ret = std::vfprintf(stderr, fmt, ap);
  3959. std::fprintf(stderr, "\x1b[0m");
  3960. break;
  3961. case CARLA_URI_MAP_ID_LOG_NOTE:
  3962. ret = std::vfprintf(stdout, fmt, ap);
  3963. break;
  3964. case CARLA_URI_MAP_ID_LOG_TRACE:
  3965. #ifdef DEBUG
  3966. std::fprintf(stdout, "\x1b[30;1m");
  3967. ret = std::vfprintf(stdout, fmt, ap);
  3968. std::fprintf(stdout, "\x1b[0m");
  3969. #endif
  3970. break;
  3971. case CARLA_URI_MAP_ID_LOG_WARNING:
  3972. ret = std::vfprintf(stderr, fmt, ap);
  3973. break;
  3974. default:
  3975. break;
  3976. }
  3977. return ret;
  3978. }
  3979. // -------------------------------------------------------------------
  3980. // Programs Feature
  3981. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  3982. {
  3983. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  3984. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  3985. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  3986. }
  3987. // -------------------------------------------------------------------
  3988. // Resize Port Feature
  3989. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  3990. {
  3991. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3992. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  3993. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  3994. }
  3995. // -------------------------------------------------------------------
  3996. // State Feature
  3997. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  3998. {
  3999. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4000. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4001. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4002. QDir dir;
  4003. dir.mkpath(path);
  4004. return strdup(path);
  4005. }
  4006. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4007. {
  4008. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4009. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4010. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4011. QDir dir(absolute_path);
  4012. return strdup(dir.canonicalPath().toUtf8().constData());
  4013. }
  4014. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4015. {
  4016. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4017. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4018. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4019. QDir dir(abstract_path);
  4020. return strdup(dir.absolutePath().toUtf8().constData());
  4021. }
  4022. 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)
  4023. {
  4024. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4025. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4026. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4027. }
  4028. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4029. {
  4030. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4031. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4032. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4033. }
  4034. // -------------------------------------------------------------------
  4035. // URI-Map Feature
  4036. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4037. {
  4038. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4039. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4040. // unused
  4041. (void)map;
  4042. }
  4043. // -------------------------------------------------------------------
  4044. // URID Feature
  4045. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4046. {
  4047. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4048. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4049. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4050. // Atom types
  4051. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4052. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4053. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4054. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4055. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4056. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4057. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4058. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4059. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4060. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4061. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4062. return CARLA_URI_MAP_ID_ATOM_INT;
  4063. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4064. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4065. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4066. return CARLA_URI_MAP_ID_ATOM_LONG;
  4067. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4068. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4069. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4070. return CARLA_URI_MAP_ID_ATOM_PATH;
  4071. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4072. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4073. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4074. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4075. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4076. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4077. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4078. return CARLA_URI_MAP_ID_ATOM_STRING;
  4079. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4080. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4081. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4082. return CARLA_URI_MAP_ID_ATOM_URI;
  4083. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4084. return CARLA_URI_MAP_ID_ATOM_URID;
  4085. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4086. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4087. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4088. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4089. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4090. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4091. // BufSize types
  4092. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4093. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4094. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4095. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4096. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4097. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4098. // Log types
  4099. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4100. return CARLA_URI_MAP_ID_LOG_ERROR;
  4101. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4102. return CARLA_URI_MAP_ID_LOG_NOTE;
  4103. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4104. return CARLA_URI_MAP_ID_LOG_TRACE;
  4105. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4106. return CARLA_URI_MAP_ID_LOG_WARNING;
  4107. // Time types
  4108. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4109. return CARLA_URI_MAP_ID_TIME_POSITION;
  4110. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4111. return CARLA_URI_MAP_ID_TIME_BAR;
  4112. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4113. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4114. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4115. return CARLA_URI_MAP_ID_TIME_BEAT;
  4116. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4117. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4118. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4119. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4120. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4121. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4122. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4123. return CARLA_URI_MAP_ID_TIME_FRAME;
  4124. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4125. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4126. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4127. return CARLA_URI_MAP_ID_TIME_SPEED;
  4128. // Others
  4129. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4130. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4131. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4132. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4133. // Custom types
  4134. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4135. }
  4136. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4137. {
  4138. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4139. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4140. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4141. // Atom types
  4142. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4143. return LV2_ATOM__Blank;
  4144. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4145. return LV2_ATOM__Bool;
  4146. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4147. return LV2_ATOM__Chunk;
  4148. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4149. return LV2_ATOM__Double;
  4150. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4151. return LV2_ATOM__Float;
  4152. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4153. return LV2_ATOM__Int;
  4154. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4155. return LV2_ATOM__Literal;
  4156. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4157. return LV2_ATOM__Long;
  4158. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4159. return LV2_ATOM__Object;
  4160. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4161. return LV2_ATOM__Path;
  4162. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4163. return LV2_ATOM__Property;
  4164. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4165. return LV2_ATOM__Resource;
  4166. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4167. return LV2_ATOM__Sequence;
  4168. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4169. return LV2_ATOM__String;
  4170. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4171. return LV2_ATOM__Tuple;
  4172. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4173. return LV2_ATOM__URI;
  4174. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4175. return LV2_ATOM__URID;
  4176. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4177. return LV2_ATOM__Vector;
  4178. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  4179. return nullptr; // custom
  4180. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4181. return LV2_ATOM__atomTransfer;
  4182. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4183. return LV2_ATOM__eventTransfer;
  4184. // BufSize types
  4185. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4186. return LV2_BUF_SIZE__maxBlockLength;
  4187. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4188. return LV2_BUF_SIZE__minBlockLength;
  4189. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4190. return LV2_BUF_SIZE__sequenceSize;
  4191. // Log types
  4192. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4193. return LV2_LOG__Error;
  4194. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4195. return LV2_LOG__Note;
  4196. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4197. return LV2_LOG__Trace;
  4198. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4199. return LV2_LOG__Warning;
  4200. // Time types
  4201. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4202. return LV2_TIME__Position;
  4203. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4204. return LV2_TIME__bar;
  4205. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4206. return LV2_TIME__barBeat;
  4207. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4208. return LV2_TIME__beat;
  4209. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4210. return LV2_TIME__beatUnit;
  4211. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4212. return LV2_TIME__beatsPerBar;
  4213. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4214. return LV2_TIME__beatsPerMinute;
  4215. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4216. return LV2_TIME__frame;
  4217. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4218. return LV2_TIME__framesPerSecond;
  4219. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4220. return LV2_TIME__speed;
  4221. // Others
  4222. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4223. return LV2_MIDI__MidiEvent;
  4224. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4225. return LV2_PARAMETERS__sampleRate;
  4226. // Custom types
  4227. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4228. }
  4229. // -------------------------------------------------------------------
  4230. // Worker Feature
  4231. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4232. {
  4233. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4234. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4235. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4236. }
  4237. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4238. {
  4239. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4240. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4241. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4242. }
  4243. // -------------------------------------------------------------------
  4244. // External UI Feature
  4245. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4246. {
  4247. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4248. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4249. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4250. }
  4251. // -------------------------------------------------------------------
  4252. // UI Port-Map Feature
  4253. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4254. {
  4255. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4256. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4257. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4258. }
  4259. // -------------------------------------------------------------------
  4260. // UI Resize Feature
  4261. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4262. {
  4263. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4264. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4265. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4266. }
  4267. // -------------------------------------------------------------------
  4268. // UI Extension
  4269. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4270. {
  4271. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4272. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4273. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4274. }
  4275. // -------------------------------------------------------------------
  4276. // Lilv State
  4277. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4278. {
  4279. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4280. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4281. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4282. }
  4283. // -------------------------------------------------------------------
  4284. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4285. };
  4286. // -------------------------------------------------------------------------------------------------------------------
  4287. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4288. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4289. {
  4290. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4291. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4292. const int32_t portIndex = argv[0]->i;
  4293. const char* const atomBuf = (const char*)&argv[1]->s;
  4294. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4295. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  4296. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4297. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  4298. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4299. return 0;
  4300. }
  4301. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4302. {
  4303. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4304. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4305. const int32_t urid = argv[0]->i;
  4306. const char* const uri = (const char*)&argv[1]->s;
  4307. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4308. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4309. return 0;
  4310. }
  4311. #undef lv2PluginPtr
  4312. CARLA_BACKEND_END_NAMESPACE
  4313. #endif // WANT_LV2
  4314. // -------------------------------------------------------------------------------------------------------------------
  4315. CARLA_BACKEND_START_NAMESPACE
  4316. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4317. {
  4318. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4319. #ifdef WANT_LV2
  4320. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4321. if (! plugin->init(init.filename, init.name, init.label))
  4322. {
  4323. delete plugin;
  4324. return nullptr;
  4325. }
  4326. plugin->reload();
  4327. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4328. {
  4329. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4330. delete plugin;
  4331. return nullptr;
  4332. }
  4333. return plugin;
  4334. #else
  4335. init.engine->setLastError("LV2 support not available");
  4336. return nullptr;
  4337. #endif
  4338. }
  4339. CARLA_BACKEND_END_NAMESPACE
  4340. // -------------------------------------------------------------------------------------------------------------------