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.

5579 lines
209KB

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