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.

5543 lines
208KB

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