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.

5602 lines
210KB

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