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.

5662 lines
212KB

  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 && fExt.uishow == nullptr)
  964. {
  965. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  966. // unused
  967. (void)frontendWinId;
  968. }
  969. if (fUi.window != nullptr)
  970. {
  971. fUi.window->setTitle(fUi.title);
  972. fFeatures[kFeatureIdUiParent]->data = fUi.window->getPtr();
  973. }
  974. }
  975. #endif
  976. fUi.widget = nullptr;
  977. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  978. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  979. }
  980. CARLA_SAFE_ASSERT(fUi.handle != nullptr);
  981. CARLA_SAFE_ASSERT(fUi.type != UI::TYPE_EXTERNAL || fUi.widget != nullptr);
  982. if (fUi.handle == nullptr || (fUi.type == UI::TYPE_EXTERNAL && fUi.widget == nullptr))
  983. {
  984. fUi.widget = nullptr;
  985. if (fUi.handle != nullptr)
  986. {
  987. fUi.descriptor->cleanup(fUi.handle);
  988. fUi.handle = nullptr;
  989. }
  990. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  991. }
  992. updateUi();
  993. #ifndef LV2_UIS_ONLY_BRIDGES
  994. if (fUi.type == UI::TYPE_EMBED)
  995. {
  996. if (fUi.window != nullptr)
  997. fUi.window->show();
  998. else if (fExt.uishow != nullptr)
  999. fExt.uishow->show(fUi.handle);
  1000. }
  1001. else
  1002. #endif
  1003. {
  1004. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  1005. pData->tryTransient();
  1006. }
  1007. }
  1008. else
  1009. {
  1010. #ifndef LV2_UIS_ONLY_BRIDGES
  1011. if (fUi.type == UI::TYPE_EMBED)
  1012. {
  1013. if (fUi.window != nullptr)
  1014. fUi.window->hide();
  1015. else if (fExt.uishow != nullptr)
  1016. fExt.uishow->hide(fUi.handle);
  1017. }
  1018. else
  1019. #endif
  1020. {
  1021. CARLA_SAFE_ASSERT(fUi.widget != nullptr);
  1022. if (fUi.widget != nullptr)
  1023. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  1024. }
  1025. fUi.descriptor->cleanup(fUi.handle);
  1026. fUi.handle = nullptr;
  1027. fUi.widget = nullptr;
  1028. }
  1029. }
  1030. void idle() override
  1031. {
  1032. if (! fAtomQueueOut.isEmpty())
  1033. {
  1034. char dumpBuf[fAtomQueueOut.getSize()];
  1035. Lv2AtomQueue tmpQueue;
  1036. tmpQueue.copyAndDumpDataFromQueue(fAtomQueueOut, dumpBuf);
  1037. CARLA_SAFE_ASSERT(! tmpQueue.isEmpty());
  1038. uint32_t portIndex;
  1039. const LV2_Atom* atom;
  1040. const bool hasPortEvent(fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr);
  1041. for (; tmpQueue.get(&atom, &portIndex);)
  1042. {
  1043. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  1044. {
  1045. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1046. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1047. }
  1048. else if (fUi.type == UI::TYPE_OSC)
  1049. {
  1050. if (pData->osc.data.target != nullptr)
  1051. {
  1052. QByteArray chunk((const char*)atom, static_cast<int>(lv2_atom_total_size(atom)));
  1053. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.toBase64().constData());
  1054. }
  1055. }
  1056. else
  1057. {
  1058. if (hasPortEvent)
  1059. fUi.descriptor->port_event(fUi.handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1060. }
  1061. }
  1062. }
  1063. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  1064. {
  1065. if (fUi.type == UI::TYPE_EXTERNAL && fUi.widget != nullptr)
  1066. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  1067. #ifndef LV2_UIS_ONLY_BRIDGES
  1068. else if (fUi.type == UI::TYPE_EMBED && fUi.window != nullptr)
  1069. fUi.window->idle();
  1070. if (fExt.uiidle != nullptr && fExt.uiidle->idle(fUi.handle) != 0)
  1071. {
  1072. showCustomUI(false);
  1073. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1074. }
  1075. #endif
  1076. }
  1077. CarlaPlugin::idle();
  1078. }
  1079. // -------------------------------------------------------------------
  1080. // Plugin state
  1081. void reload() override
  1082. {
  1083. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1084. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1085. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1086. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1087. carla_debug("Lv2Plugin::reload() - start");
  1088. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1089. // Safely disable plugin for reload
  1090. const ScopedDisabler sd(this);
  1091. if (pData->active)
  1092. deactivate();
  1093. clearBuffers();
  1094. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1095. const uint32_t portCount(fRdfDescriptor->PortCount);
  1096. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1097. aIns = aOuts = cvIns = cvOuts = params = 0;
  1098. LinkedList<unsigned int> evIns, evOuts;
  1099. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1100. bool forcedStereoIn, forcedStereoOut;
  1101. forcedStereoIn = forcedStereoOut = false;
  1102. bool needsCtrlIn, needsCtrlOut;
  1103. needsCtrlIn = needsCtrlOut = false;
  1104. for (uint32_t i=0; i < portCount; ++i)
  1105. {
  1106. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1107. if (LV2_IS_PORT_AUDIO(portTypes))
  1108. {
  1109. if (LV2_IS_PORT_INPUT(portTypes))
  1110. aIns += 1;
  1111. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1112. aOuts += 1;
  1113. }
  1114. else if (LV2_IS_PORT_CV(portTypes))
  1115. {
  1116. if (LV2_IS_PORT_INPUT(portTypes))
  1117. cvIns += 1;
  1118. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1119. cvOuts += 1;
  1120. }
  1121. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1122. {
  1123. if (LV2_IS_PORT_INPUT(portTypes))
  1124. evIns.append(CARLA_EVENT_DATA_ATOM);
  1125. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1126. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1127. }
  1128. else if (LV2_IS_PORT_EVENT(portTypes))
  1129. {
  1130. if (LV2_IS_PORT_INPUT(portTypes))
  1131. evIns.append(CARLA_EVENT_DATA_EVENT);
  1132. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1133. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1134. }
  1135. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1136. {
  1137. if (LV2_IS_PORT_INPUT(portTypes))
  1138. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1139. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1140. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1141. }
  1142. else if (LV2_IS_PORT_CONTROL(portTypes))
  1143. params += 1;
  1144. }
  1145. recheckExtensions();
  1146. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1147. {
  1148. if (fHandle2 == nullptr)
  1149. {
  1150. try {
  1151. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1152. } catch(...) {}
  1153. }
  1154. if (fHandle2 != nullptr)
  1155. {
  1156. if (aIns == 1)
  1157. {
  1158. aIns = 2;
  1159. forcedStereoIn = true;
  1160. }
  1161. if (aOuts == 1)
  1162. {
  1163. aOuts = 2;
  1164. forcedStereoOut = true;
  1165. }
  1166. }
  1167. }
  1168. if (aIns > 0)
  1169. {
  1170. pData->audioIn.createNew(aIns);
  1171. fAudioInBuffers = new float*[aIns];
  1172. for (uint32_t i=0; i < aIns; ++i)
  1173. fAudioInBuffers[i] = nullptr;
  1174. }
  1175. if (aOuts > 0)
  1176. {
  1177. pData->audioOut.createNew(aOuts);
  1178. fAudioOutBuffers = new float*[aOuts];
  1179. needsCtrlIn = true;
  1180. for (uint32_t i=0; i < aOuts; ++i)
  1181. fAudioOutBuffers[i] = nullptr;
  1182. }
  1183. if (cvIns > 0)
  1184. {
  1185. fCvIn.createNew(cvIns);
  1186. fCvInBuffers = new float*[cvIns];
  1187. for (uint32_t i=0; i < cvIns; ++i)
  1188. fCvInBuffers[i] = nullptr;
  1189. }
  1190. if (cvOuts > 0)
  1191. {
  1192. fCvOut.createNew(cvOuts);
  1193. fCvOutBuffers = new float*[cvOuts];
  1194. for (uint32_t i=0; i < cvOuts; ++i)
  1195. fCvOutBuffers[i] = nullptr;
  1196. }
  1197. if (params > 0)
  1198. {
  1199. pData->param.createNew(params, true, false);
  1200. fParamBuffers = new float[params];
  1201. FLOAT_CLEAR(fParamBuffers, params);
  1202. }
  1203. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1204. {
  1205. fEventsIn.createNew(count);
  1206. for (uint32_t i=0; i < count; ++i)
  1207. {
  1208. const uint32_t& type(evIns.getAt(i));
  1209. if (type == CARLA_EVENT_DATA_ATOM)
  1210. {
  1211. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1212. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1213. }
  1214. else if (type == CARLA_EVENT_DATA_EVENT)
  1215. {
  1216. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1217. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1218. }
  1219. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1220. {
  1221. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1222. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1223. fEventsIn.data[i].midi.data = new unsigned char[eventBufferSize];
  1224. }
  1225. }
  1226. }
  1227. else
  1228. {
  1229. fEventsIn.createNew(1);
  1230. fEventsIn.ctrl = &fEventsIn.data[0];
  1231. }
  1232. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1233. {
  1234. fEventsOut.createNew(count);
  1235. for (uint32_t i=0; i < count; ++i)
  1236. {
  1237. const uint32_t& type(evOuts.getAt(i));
  1238. if (type == CARLA_EVENT_DATA_ATOM)
  1239. {
  1240. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1241. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1242. }
  1243. else if (type == CARLA_EVENT_DATA_EVENT)
  1244. {
  1245. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1246. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1247. }
  1248. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1249. {
  1250. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1251. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1252. fEventsOut.data[i].midi.data = new unsigned char[eventBufferSize];
  1253. }
  1254. }
  1255. }
  1256. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1257. CarlaString portName;
  1258. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1259. {
  1260. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1261. portName.clear();
  1262. 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))
  1263. {
  1264. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1265. {
  1266. portName = pData->name;
  1267. portName += ":";
  1268. }
  1269. portName += fRdfDescriptor->Ports[i].Name;
  1270. portName.truncate(portNameSize);
  1271. }
  1272. if (LV2_IS_PORT_AUDIO(portTypes))
  1273. {
  1274. if (LV2_IS_PORT_INPUT(portTypes))
  1275. {
  1276. uint32_t j = iAudioIn++;
  1277. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1278. pData->audioIn.ports[j].rindex = i;
  1279. if (forcedStereoIn)
  1280. {
  1281. portName += "_2";
  1282. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1283. pData->audioIn.ports[1].rindex = i;
  1284. }
  1285. }
  1286. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1287. {
  1288. uint32_t j = iAudioOut++;
  1289. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1290. pData->audioOut.ports[j].rindex = i;
  1291. if (forcedStereoOut)
  1292. {
  1293. portName += "_2";
  1294. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1295. pData->audioOut.ports[1].rindex = i;
  1296. }
  1297. }
  1298. else
  1299. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1300. }
  1301. else if (LV2_IS_PORT_CV(portTypes))
  1302. {
  1303. if (LV2_IS_PORT_INPUT(portTypes))
  1304. {
  1305. uint32_t j = iCvIn++;
  1306. fCvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1307. fCvIn.ports[j].rindex = i;
  1308. }
  1309. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1310. {
  1311. uint32_t j = iCvOut++;
  1312. fCvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1313. fCvOut.ports[j].rindex = i;
  1314. }
  1315. else
  1316. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1317. }
  1318. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1319. {
  1320. if (LV2_IS_PORT_INPUT(portTypes))
  1321. {
  1322. uint32_t j = iEvIn++;
  1323. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1324. if (fHandle2 != nullptr)
  1325. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1326. fEventsIn.data[j].rindex = i;
  1327. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1328. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1329. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1330. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1331. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1332. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1333. if (evIns.count() == 1)
  1334. {
  1335. fEventsIn.ctrl = &fEventsIn.data[j];
  1336. fEventsIn.ctrlIndex = j;
  1337. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1338. needsCtrlIn = true;
  1339. }
  1340. else
  1341. {
  1342. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1343. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1344. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1345. {
  1346. fEventsIn.ctrl = &fEventsIn.data[j];
  1347. fEventsIn.ctrlIndex = j;
  1348. }
  1349. }
  1350. }
  1351. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1352. {
  1353. uint32_t j = iEvOut++;
  1354. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1355. if (fHandle2 != nullptr)
  1356. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1357. fEventsOut.data[j].rindex = i;
  1358. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1359. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1360. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1361. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1362. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1363. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1364. if (evOuts.count() == 1)
  1365. {
  1366. fEventsOut.ctrl = &fEventsOut.data[j];
  1367. fEventsOut.ctrlIndex = j;
  1368. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1369. needsCtrlOut = true;
  1370. }
  1371. else
  1372. {
  1373. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1374. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1375. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1376. {
  1377. fEventsOut.ctrl = &fEventsOut.data[j];
  1378. fEventsOut.ctrlIndex = j;
  1379. }
  1380. }
  1381. }
  1382. else
  1383. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1384. }
  1385. else if (LV2_IS_PORT_EVENT(portTypes))
  1386. {
  1387. if (LV2_IS_PORT_INPUT(portTypes))
  1388. {
  1389. uint32_t j = iEvIn++;
  1390. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1391. if (fHandle2 != nullptr)
  1392. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1393. fEventsIn.data[j].rindex = i;
  1394. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1395. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1396. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1397. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1398. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1399. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1400. if (evIns.count() == 1)
  1401. {
  1402. fEventsIn.ctrl = &fEventsIn.data[j];
  1403. fEventsIn.ctrlIndex = j;
  1404. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1405. needsCtrlIn = true;
  1406. }
  1407. else
  1408. {
  1409. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1410. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1411. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1412. {
  1413. fEventsIn.ctrl = &fEventsIn.data[j];
  1414. fEventsIn.ctrlIndex = j;
  1415. }
  1416. }
  1417. }
  1418. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1419. {
  1420. uint32_t j = iEvOut++;
  1421. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1422. if (fHandle2 != nullptr)
  1423. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1424. fEventsOut.data[j].rindex = i;
  1425. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1426. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1427. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1428. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1429. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1430. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1431. if (evOuts.count() == 1)
  1432. {
  1433. fEventsOut.ctrl = &fEventsOut.data[j];
  1434. fEventsOut.ctrlIndex = j;
  1435. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1436. needsCtrlOut = true;
  1437. }
  1438. else
  1439. {
  1440. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1441. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1442. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1443. {
  1444. fEventsOut.ctrl = &fEventsOut.data[j];
  1445. fEventsOut.ctrlIndex = j;
  1446. }
  1447. }
  1448. }
  1449. else
  1450. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1451. }
  1452. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1453. {
  1454. if (LV2_IS_PORT_INPUT(portTypes))
  1455. {
  1456. uint32_t j = iEvIn++;
  1457. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1458. if (fHandle2 != nullptr)
  1459. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1460. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1461. fEventsIn.data[j].rindex = i;
  1462. if (evIns.count() == 1)
  1463. {
  1464. needsCtrlIn = true;
  1465. fEventsIn.ctrl = &fEventsIn.data[j];
  1466. fEventsIn.ctrlIndex = j;
  1467. }
  1468. else
  1469. {
  1470. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1471. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1472. {
  1473. fEventsIn.ctrl = &fEventsIn.data[j];
  1474. fEventsIn.ctrlIndex = j;
  1475. }
  1476. }
  1477. }
  1478. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1479. {
  1480. uint32_t j = iEvOut++;
  1481. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1482. if (fHandle2 != nullptr)
  1483. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1484. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1485. fEventsOut.data[j].rindex = i;
  1486. if (evOuts.count() == 1)
  1487. {
  1488. needsCtrlOut = true;
  1489. fEventsOut.ctrl = &fEventsOut.data[j];
  1490. fEventsOut.ctrlIndex = j;
  1491. }
  1492. else
  1493. {
  1494. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1495. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1496. {
  1497. fEventsOut.ctrl = &fEventsOut.data[j];
  1498. fEventsOut.ctrlIndex = j;
  1499. }
  1500. }
  1501. }
  1502. else
  1503. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1504. }
  1505. else if (LV2_IS_PORT_CONTROL(portTypes))
  1506. {
  1507. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1508. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1509. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1510. uint32_t j = iCtrl++;
  1511. pData->param.data[j].hints = 0x0;
  1512. pData->param.data[j].index = static_cast<int32_t>(j);
  1513. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1514. pData->param.data[j].midiCC = -1;
  1515. pData->param.data[j].midiChannel = 0;
  1516. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  1517. float min, max, def, step, stepSmall, stepLarge;
  1518. // min value
  1519. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1520. min = portPoints.Minimum;
  1521. else
  1522. min = 0.0f;
  1523. // max value
  1524. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1525. max = portPoints.Maximum;
  1526. else
  1527. max = 1.0f;
  1528. if (min > max)
  1529. max = min;
  1530. // stupid hack for ir.lv2 (broken plugin)
  1531. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1532. {
  1533. min = 0.0f;
  1534. max = (float)0xffffff;
  1535. }
  1536. if (max - min == 0.0f)
  1537. {
  1538. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1539. max = min + 0.1f;
  1540. }
  1541. // default value
  1542. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1543. {
  1544. def = portPoints.Default;
  1545. }
  1546. else
  1547. {
  1548. // no default value
  1549. if (min < 0.0f && max > 0.0f)
  1550. def = 0.0f;
  1551. else
  1552. def = min;
  1553. }
  1554. if (def < min)
  1555. def = min;
  1556. else if (def > max)
  1557. def = max;
  1558. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1559. {
  1560. min *= sampleRate;
  1561. max *= sampleRate;
  1562. def *= sampleRate;
  1563. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1564. }
  1565. if (LV2_IS_PORT_TOGGLED(portProps))
  1566. {
  1567. step = max - min;
  1568. stepSmall = step;
  1569. stepLarge = step;
  1570. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1571. }
  1572. else if (LV2_IS_PORT_INTEGER(portProps))
  1573. {
  1574. step = 1.0f;
  1575. stepSmall = 1.0f;
  1576. stepLarge = 10.0f;
  1577. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1578. }
  1579. else
  1580. {
  1581. float range = max - min;
  1582. step = range/100.0f;
  1583. stepSmall = range/1000.0f;
  1584. stepLarge = range/10.0f;
  1585. }
  1586. if (LV2_IS_PORT_INPUT(portTypes))
  1587. {
  1588. pData->param.data[j].type = PARAMETER_INPUT;
  1589. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1590. {
  1591. carla_stderr("Plugin has latency input port, this should not happen!");
  1592. }
  1593. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1594. {
  1595. def = sampleRate;
  1596. step = 1.0f;
  1597. stepSmall = 1.0f;
  1598. stepLarge = 1.0f;
  1599. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1600. }
  1601. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1602. {
  1603. pData->param.special[j] = PARAMETER_SPECIAL_LV2_FREEWHEEL;
  1604. }
  1605. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1606. {
  1607. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1608. }
  1609. else
  1610. {
  1611. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1612. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1613. needsCtrlIn = true;
  1614. }
  1615. // MIDI CC value
  1616. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1617. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1618. {
  1619. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1620. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  1621. }
  1622. }
  1623. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1624. {
  1625. pData->param.data[j].type = PARAMETER_OUTPUT;
  1626. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1627. {
  1628. min = 0.0f;
  1629. max = sampleRate;
  1630. def = 0.0f;
  1631. step = 1.0f;
  1632. stepSmall = 1.0f;
  1633. stepLarge = 1.0f;
  1634. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1635. }
  1636. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1637. {
  1638. def = sampleRate;
  1639. step = 1.0f;
  1640. stepSmall = 1.0f;
  1641. stepLarge = 1.0f;
  1642. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1643. }
  1644. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1645. {
  1646. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1647. }
  1648. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1649. {
  1650. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1651. }
  1652. else
  1653. {
  1654. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1655. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1656. needsCtrlOut = true;
  1657. }
  1658. }
  1659. else
  1660. {
  1661. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1662. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1663. }
  1664. // extra parameter hints
  1665. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1666. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1667. if (LV2_IS_PORT_TRIGGER(portProps))
  1668. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1669. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1670. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1671. if (LV2_IS_PORT_ENUMERATION(portProps))
  1672. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1673. // check if parameter is not enabled or automable
  1674. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1675. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1676. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1677. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1678. pData->param.ranges[j].min = min;
  1679. pData->param.ranges[j].max = max;
  1680. pData->param.ranges[j].def = def;
  1681. pData->param.ranges[j].step = step;
  1682. pData->param.ranges[j].stepSmall = stepSmall;
  1683. pData->param.ranges[j].stepLarge = stepLarge;
  1684. // Start parameters in their default values (except freewheel, which is off by default)
  1685. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1686. fParamBuffers[j] = min;
  1687. else
  1688. fParamBuffers[j] = def;
  1689. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1690. if (fHandle2 != nullptr)
  1691. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1692. }
  1693. else
  1694. {
  1695. // Port Type not supported, but it's optional anyway
  1696. fDescriptor->connect_port(fHandle, i, nullptr);
  1697. if (fHandle2 != nullptr)
  1698. fDescriptor->connect_port(fHandle2, i, nullptr);
  1699. }
  1700. }
  1701. if (needsCtrlIn)
  1702. {
  1703. portName.clear();
  1704. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1705. {
  1706. portName = pData->name;
  1707. portName += ":";
  1708. }
  1709. portName += "events-in";
  1710. portName.truncate(portNameSize);
  1711. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1712. }
  1713. if (needsCtrlOut)
  1714. {
  1715. portName.clear();
  1716. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1717. {
  1718. portName = pData->name;
  1719. portName += ":";
  1720. }
  1721. portName += "events-out";
  1722. portName.truncate(portNameSize);
  1723. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1724. }
  1725. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1726. fAtomQueueIn.createBuffer(eventBufferSize);
  1727. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1728. fAtomQueueOut.createBuffer(eventBufferSize);
  1729. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1730. fEventsIn.ctrl->port = pData->event.portIn;
  1731. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1732. fEventsOut.ctrl->port = pData->event.portOut;
  1733. if (forcedStereoIn || forcedStereoOut)
  1734. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1735. else
  1736. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1737. // plugin hints
  1738. pData->hints = 0x0;
  1739. if (isRealtimeSafe())
  1740. pData->hints |= PLUGIN_IS_RTSAFE;
  1741. if (fUi.type != UI::TYPE_NULL)
  1742. {
  1743. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1744. if (fUi.type == UI::TYPE_EMBED)
  1745. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1746. }
  1747. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1748. pData->hints |= PLUGIN_IS_SYNTH;
  1749. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1750. pData->hints |= PLUGIN_CAN_DRYWET;
  1751. if (aOuts > 0)
  1752. pData->hints |= PLUGIN_CAN_VOLUME;
  1753. if (aOuts >= 2 && aOuts % 2 == 0)
  1754. pData->hints |= PLUGIN_CAN_BALANCE;
  1755. // extra plugin hints
  1756. pData->extraHints = 0x0;
  1757. if (fExt.state != nullptr || fExt.worker != nullptr)
  1758. {
  1759. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1760. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1761. }
  1762. else
  1763. {
  1764. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1765. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1766. }
  1767. bufferSizeChanged(pData->engine->getBufferSize());
  1768. reloadPrograms(true);
  1769. // check latency
  1770. // TODO
  1771. evIns.clear();
  1772. evOuts.clear();
  1773. if (pData->active)
  1774. activate();
  1775. carla_debug("Lv2Plugin::reload() - end");
  1776. }
  1777. void reloadPrograms(const bool doInit) override
  1778. {
  1779. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  1780. const uint32_t oldCount = pData->midiprog.count;
  1781. const int32_t current = pData->midiprog.current;
  1782. // special LV2 programs handling
  1783. if (doInit)
  1784. {
  1785. pData->prog.clear();
  1786. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  1787. if (presetCount > 0)
  1788. {
  1789. pData->prog.createNew(presetCount);
  1790. for (uint32_t i=0; i < presetCount; ++i)
  1791. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1792. }
  1793. }
  1794. // Delete old programs
  1795. pData->midiprog.clear();
  1796. // Query new programs
  1797. uint32_t newCount = 0;
  1798. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1799. {
  1800. for (; fExt.programs->get_program(fHandle, newCount);)
  1801. ++newCount;
  1802. }
  1803. if (newCount > 0)
  1804. {
  1805. pData->midiprog.createNew(newCount);
  1806. // Update data
  1807. for (uint32_t i=0; i < newCount; ++i)
  1808. {
  1809. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1810. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  1811. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  1812. pData->midiprog.data[i].bank = pdesc->bank;
  1813. pData->midiprog.data[i].program = pdesc->program;
  1814. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1815. }
  1816. }
  1817. #ifndef BUILD_BRIDGE
  1818. // Update OSC Names
  1819. if (pData->engine->isOscControlRegistered())
  1820. {
  1821. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  1822. for (uint32_t i=0; i < newCount; ++i)
  1823. 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);
  1824. }
  1825. #endif
  1826. if (doInit)
  1827. {
  1828. if (newCount > 0)
  1829. {
  1830. setMidiProgram(0, false, false, false);
  1831. }
  1832. else
  1833. {
  1834. // load default state
  1835. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1836. {
  1837. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1838. if (fHandle2 != nullptr)
  1839. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1840. }
  1841. }
  1842. }
  1843. else
  1844. {
  1845. // Check if current program is invalid
  1846. bool programChanged = false;
  1847. if (newCount == oldCount+1)
  1848. {
  1849. // one midi program added, probably created by user
  1850. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1851. programChanged = true;
  1852. }
  1853. else if (current < 0 && newCount > 0)
  1854. {
  1855. // programs exist now, but not before
  1856. pData->midiprog.current = 0;
  1857. programChanged = true;
  1858. }
  1859. else if (current >= 0 && newCount == 0)
  1860. {
  1861. // programs existed before, but not anymore
  1862. pData->midiprog.current = -1;
  1863. programChanged = true;
  1864. }
  1865. else if (current >= static_cast<int32_t>(newCount))
  1866. {
  1867. // current midi program > count
  1868. pData->midiprog.current = 0;
  1869. programChanged = true;
  1870. }
  1871. else
  1872. {
  1873. // no change
  1874. pData->midiprog.current = current;
  1875. }
  1876. if (programChanged)
  1877. setMidiProgram(pData->midiprog.current, true, true, true);
  1878. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1879. }
  1880. }
  1881. // -------------------------------------------------------------------
  1882. // Plugin processing
  1883. void activate() noexcept override
  1884. {
  1885. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1886. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1887. if (fDescriptor->activate != nullptr)
  1888. {
  1889. try {
  1890. fDescriptor->activate(fHandle);
  1891. } catch(...) {}
  1892. if (fHandle2 != nullptr)
  1893. {
  1894. try {
  1895. fDescriptor->activate(fHandle2);
  1896. } catch(...) {}
  1897. }
  1898. }
  1899. fFirstActive = true;
  1900. }
  1901. void deactivate() noexcept override
  1902. {
  1903. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1904. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1905. if (fDescriptor->deactivate != nullptr)
  1906. {
  1907. try {
  1908. fDescriptor->deactivate(fHandle);
  1909. } catch(...) {}
  1910. if (fHandle2 != nullptr)
  1911. {
  1912. try {
  1913. fDescriptor->deactivate(fHandle2);
  1914. } catch(...) {}
  1915. }
  1916. }
  1917. }
  1918. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1919. {
  1920. // --------------------------------------------------------------------------------------------------------
  1921. // Check if active
  1922. if (! pData->active)
  1923. {
  1924. // disable any output sound
  1925. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1926. FLOAT_CLEAR(outBuffer[i], frames);
  1927. return;
  1928. }
  1929. // --------------------------------------------------------------------------------------------------------
  1930. // Event itenerators from different APIs (input)
  1931. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1932. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1933. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1934. for (uint32_t i=0; i < fEventsIn.count; ++i)
  1935. {
  1936. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1937. {
  1938. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1939. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1940. }
  1941. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1942. {
  1943. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1944. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1945. }
  1946. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1947. {
  1948. fEventsIn.data[i].midi.event_count = 0;
  1949. fEventsIn.data[i].midi.size = 0;
  1950. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  1951. evInMidiStates[i].frame_count = frames;
  1952. evInMidiStates[i].position = 0;
  1953. }
  1954. }
  1955. for (uint32_t i=0; i < fEventsOut.count; ++i)
  1956. {
  1957. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1958. {
  1959. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1960. }
  1961. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1962. {
  1963. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1964. }
  1965. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1966. {
  1967. // not needed
  1968. }
  1969. }
  1970. CARLA_PROCESS_CONTINUE_CHECK;
  1971. // --------------------------------------------------------------------------------------------------------
  1972. // Check if needs reset
  1973. if (pData->needsReset)
  1974. {
  1975. uint8_t midiData[3] = { 0 };
  1976. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1977. {
  1978. const uint32_t j = fEventsIn.ctrlIndex;
  1979. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1980. {
  1981. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  1982. {
  1983. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1984. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1985. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1986. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1987. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1988. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1989. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1990. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1991. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  1992. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1993. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1994. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1995. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1996. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1997. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1998. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  1999. }
  2000. }
  2001. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2002. {
  2003. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2004. {
  2005. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  2006. midiData[1] = k;
  2007. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2008. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2009. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2010. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2011. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2012. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  2013. }
  2014. }
  2015. }
  2016. if (pData->latency > 0)
  2017. {
  2018. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2019. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  2020. }
  2021. pData->needsReset = false;
  2022. }
  2023. // --------------------------------------------------------------------------------------------------------
  2024. // TimeInfo
  2025. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2026. if (fFirstActive || fLastTimeInfo != timeInfo)
  2027. {
  2028. bool doPostRt;
  2029. int32_t rindex;
  2030. // update input ports
  2031. for (uint32_t k=0; k < pData->param.count; ++k)
  2032. {
  2033. if (pData->param.data[k].type != PARAMETER_INPUT)
  2034. continue;
  2035. if (pData->param.special[k] != PARAMETER_SPECIAL_LV2_TIME)
  2036. continue;
  2037. doPostRt = false;
  2038. rindex = pData->param.data[k].rindex;
  2039. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2040. switch (fRdfDescriptor->Ports[rindex].Designation)
  2041. {
  2042. // Non-BBT
  2043. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2044. if (fLastTimeInfo.playing != timeInfo.playing)
  2045. {
  2046. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2047. doPostRt = true;
  2048. }
  2049. break;
  2050. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2051. if (fLastTimeInfo.frame != timeInfo.frame)
  2052. {
  2053. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2054. doPostRt = true;
  2055. }
  2056. break;
  2057. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2058. break;
  2059. // BBT
  2060. case LV2_PORT_DESIGNATION_TIME_BAR:
  2061. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2062. {
  2063. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2064. doPostRt = true;
  2065. }
  2066. break;
  2067. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2068. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2069. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2070. {
  2071. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2072. doPostRt = true;
  2073. }
  2074. break;
  2075. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2076. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2077. {
  2078. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2079. doPostRt = true;
  2080. }
  2081. break;
  2082. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2083. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2084. {
  2085. fParamBuffers[k] = timeInfo.bbt.beatType;
  2086. doPostRt = true;
  2087. }
  2088. break;
  2089. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2090. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2091. {
  2092. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2093. doPostRt = true;
  2094. }
  2095. break;
  2096. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2097. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2098. {
  2099. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2100. doPostRt = true;
  2101. }
  2102. break;
  2103. }
  2104. if (doPostRt)
  2105. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2106. }
  2107. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2108. {
  2109. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2110. continue;
  2111. uint8_t timeInfoBuf[256];
  2112. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2113. LV2_Atom_Forge_Frame forgeFrame;
  2114. lv2_atom_forge_object(&fAtomForge, &forgeFrame, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_TIME_POSITION);
  2115. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2116. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2117. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2118. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2119. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2120. {
  2121. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2122. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2123. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2124. 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)));
  2125. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2126. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2127. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2128. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2129. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2130. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2131. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2132. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2133. }
  2134. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2135. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2136. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2137. // send only deprecated blank object for now
  2138. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, CARLA_URI_MAP_ID_ATOM_BLANK, atom->size, LV2_ATOM_BODY_CONST(atom));
  2139. // for atom:object
  2140. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2141. }
  2142. pData->postRtEvents.trySplice();
  2143. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2144. CARLA_PROCESS_CONTINUE_CHECK;
  2145. }
  2146. // --------------------------------------------------------------------------------------------------------
  2147. // CV ports
  2148. float* cvInBuf[fCvIn.count /*> 0 ? fCvIn.count : 1*/];
  2149. float* cvOutBuf[fCvOut.count /*> 0 ? fCvOut.count : 1*/];
  2150. for (uint32_t i=0; i < fCvIn.count; ++i)
  2151. {
  2152. CARLA_SAFE_ASSERT_CONTINUE(fCvIn.ports[i].port != nullptr);
  2153. cvInBuf[i] = fCvIn.ports[i].port->getBuffer();
  2154. }
  2155. for (uint32_t i=0; i < fCvOut.count; ++i)
  2156. {
  2157. CARLA_SAFE_ASSERT_CONTINUE(fCvOut.ports[i].port != nullptr);
  2158. cvOutBuf[i] = fCvOut.ports[i].port->getBuffer();
  2159. }
  2160. // --------------------------------------------------------------------------------------------------------
  2161. // Event Input and Processing
  2162. if (fEventsIn.ctrl != nullptr)
  2163. {
  2164. // ----------------------------------------------------------------------------------------------------
  2165. // Message Input
  2166. if (fAtomQueueIn.tryLock())
  2167. {
  2168. if (! fAtomQueueIn.isEmpty())
  2169. {
  2170. const LV2_Atom* atom;
  2171. uint32_t j, portIndex;
  2172. for (; fAtomQueueIn.get(&atom, &portIndex);)
  2173. {
  2174. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2175. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2176. {
  2177. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2178. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2179. }
  2180. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2181. {
  2182. carla_stdout("Event input buffer full, at least 1 message lost");
  2183. continue;
  2184. }
  2185. }
  2186. }
  2187. fAtomQueueIn.unlock();
  2188. }
  2189. // ----------------------------------------------------------------------------------------------------
  2190. // MIDI Input (External)
  2191. if (pData->extNotes.mutex.tryLock())
  2192. {
  2193. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2194. {
  2195. // does not handle MIDI
  2196. pData->extNotes.data.clear();
  2197. }
  2198. else
  2199. {
  2200. const uint32_t j = fEventsIn.ctrlIndex;
  2201. for (; ! pData->extNotes.data.isEmpty();)
  2202. {
  2203. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  2204. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2205. uint8_t midiEvent[3];
  2206. midiEvent[0] = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2207. midiEvent[1] = note.note;
  2208. midiEvent[2] = note.velo;
  2209. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2210. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2211. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2212. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2213. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2214. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2215. }
  2216. }
  2217. pData->extNotes.mutex.unlock();
  2218. } // End of MIDI Input (External)
  2219. // ----------------------------------------------------------------------------------------------------
  2220. // Event Input (System)
  2221. bool allNotesOffSent = false;
  2222. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2223. uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2224. uint32_t startTime = 0;
  2225. uint32_t timeOffset = 0;
  2226. uint32_t nextBankId;
  2227. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2228. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2229. else
  2230. nextBankId = 0;
  2231. for (uint32_t i=0; i < numEvents; ++i)
  2232. {
  2233. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2234. if (event.time >= frames)
  2235. continue;
  2236. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2237. if (isSampleAccurate && event.time > timeOffset)
  2238. {
  2239. if (processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, event.time - timeOffset, timeOffset))
  2240. {
  2241. startTime = 0;
  2242. timeOffset = event.time;
  2243. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2244. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2245. else
  2246. nextBankId = 0;
  2247. // reset iters
  2248. const uint32_t j = fEventsIn.ctrlIndex;
  2249. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2250. {
  2251. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2252. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2253. }
  2254. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2255. {
  2256. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2257. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2258. }
  2259. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2260. {
  2261. fEventsIn.data[j].midi.event_count = 0;
  2262. fEventsIn.data[j].midi.size = 0;
  2263. evInMidiStates[j].position = event.time;
  2264. }
  2265. }
  2266. else
  2267. startTime += timeOffset;
  2268. }
  2269. switch (event.type)
  2270. {
  2271. case kEngineEventTypeNull:
  2272. break;
  2273. case kEngineEventTypeControl: {
  2274. const EngineControlEvent& ctrlEvent(event.ctrl);
  2275. switch (ctrlEvent.type)
  2276. {
  2277. case kEngineControlEventTypeNull:
  2278. break;
  2279. case kEngineControlEventTypeParameter: {
  2280. #ifndef BUILD_BRIDGE
  2281. // Control backend stuff
  2282. if (event.channel == pData->ctrlChannel)
  2283. {
  2284. float value;
  2285. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2286. {
  2287. value = ctrlEvent.value;
  2288. setDryWet(value, false, false);
  2289. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2290. break;
  2291. }
  2292. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2293. {
  2294. value = ctrlEvent.value*127.0f/100.0f;
  2295. setVolume(value, false, false);
  2296. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2297. break;
  2298. }
  2299. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2300. {
  2301. float left, right;
  2302. value = ctrlEvent.value/0.5f - 1.0f;
  2303. if (value < 0.0f)
  2304. {
  2305. left = -1.0f;
  2306. right = (value*2.0f)+1.0f;
  2307. }
  2308. else if (value > 0.0f)
  2309. {
  2310. left = (value*2.0f)-1.0f;
  2311. right = 1.0f;
  2312. }
  2313. else
  2314. {
  2315. left = -1.0f;
  2316. right = 1.0f;
  2317. }
  2318. setBalanceLeft(left, false, false);
  2319. setBalanceRight(right, false, false);
  2320. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2321. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2322. break;
  2323. }
  2324. }
  2325. #endif
  2326. // Control plugin parameters
  2327. uint32_t k;
  2328. for (k=0; k < pData->param.count; ++k)
  2329. {
  2330. if (pData->param.data[k].midiChannel != event.channel)
  2331. continue;
  2332. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2333. continue;
  2334. if (pData->param.data[k].type != PARAMETER_INPUT)
  2335. continue;
  2336. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2337. continue;
  2338. float value;
  2339. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2340. {
  2341. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2342. }
  2343. else
  2344. {
  2345. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2346. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2347. value = std::rint(value);
  2348. }
  2349. setParameterValue(k, value, false, false, false);
  2350. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2351. break;
  2352. }
  2353. // check if event is already handled
  2354. if (k != pData->param.count)
  2355. break;
  2356. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2357. {
  2358. uint8_t midiData[3];
  2359. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2360. midiData[1] = static_cast<uint8_t>(ctrlEvent.param);
  2361. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2362. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2363. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2364. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2365. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2366. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2367. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2368. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2369. }
  2370. break;
  2371. } // case kEngineControlEventTypeParameter
  2372. case kEngineControlEventTypeMidiBank:
  2373. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2374. nextBankId = ctrlEvent.param;
  2375. break;
  2376. case kEngineControlEventTypeMidiProgram:
  2377. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2378. {
  2379. const uint32_t nextProgramId = ctrlEvent.param;
  2380. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2381. {
  2382. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2383. {
  2384. const int32_t index(static_cast<int32_t>(k));
  2385. setMidiProgram(index, false, false, false);
  2386. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2387. break;
  2388. }
  2389. }
  2390. }
  2391. break;
  2392. case kEngineControlEventTypeAllSoundOff:
  2393. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2394. {
  2395. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2396. uint8_t midiData[3];
  2397. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2398. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2399. midiData[2] = 0;
  2400. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2401. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2402. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2403. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2404. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2405. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2406. }
  2407. break;
  2408. case kEngineControlEventTypeAllNotesOff:
  2409. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2410. {
  2411. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2412. {
  2413. allNotesOffSent = true;
  2414. sendMidiAllNotesOffToCallback();
  2415. }
  2416. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2417. uint8_t midiData[3];
  2418. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2419. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2420. midiData[2] = 0;
  2421. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2422. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2423. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2424. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2425. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2426. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2427. }
  2428. break;
  2429. } // switch (ctrlEvent.type)
  2430. break;
  2431. } // case kEngineEventTypeControl
  2432. case kEngineEventTypeMidi:
  2433. {
  2434. const EngineMidiEvent& midiEvent(event.midi);
  2435. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  2436. uint8_t channel = event.channel;
  2437. uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2438. // Fix bad note-off (per LV2 spec)
  2439. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  2440. status = MIDI_STATUS_NOTE_OFF;
  2441. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2442. continue;
  2443. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2444. continue;
  2445. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2446. continue;
  2447. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2448. continue;
  2449. const uint32_t j = fEventsIn.ctrlIndex;
  2450. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2451. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2452. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2453. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2454. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2455. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiEvent.data);
  2456. if (status == MIDI_STATUS_NOTE_ON)
  2457. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2458. else if (status == MIDI_STATUS_NOTE_OFF)
  2459. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2460. break;
  2461. } // case kEngineEventTypeMidi
  2462. } // switch (event.type)
  2463. }
  2464. pData->postRtEvents.trySplice();
  2465. if (frames > timeOffset)
  2466. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames - timeOffset, timeOffset);
  2467. } // End of Event Input and Processing
  2468. // --------------------------------------------------------------------------------------------------------
  2469. // Plugin processing (no events)
  2470. else
  2471. {
  2472. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames, 0);
  2473. } // End of Plugin processing (no events)
  2474. CARLA_PROCESS_CONTINUE_CHECK;
  2475. // --------------------------------------------------------------------------------------------------------
  2476. // MIDI Output
  2477. if (fEventsOut.ctrl != nullptr)
  2478. {
  2479. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2480. {
  2481. const LV2_Atom_Event* ev;
  2482. LV2_Atom_Buffer_Iterator iter;
  2483. uint8_t* data;
  2484. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2485. for (;;)
  2486. {
  2487. data = nullptr;
  2488. ev = lv2_atom_buffer_get(&iter, &data);
  2489. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2490. break;
  2491. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2492. {
  2493. if (fEventsOut.ctrl->port != nullptr)
  2494. {
  2495. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2496. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2497. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2498. }
  2499. }
  2500. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2501. {
  2502. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2503. fAtomQueueOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2504. }
  2505. lv2_atom_buffer_increment(&iter);
  2506. }
  2507. }
  2508. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2509. {
  2510. const LV2_Event* ev;
  2511. LV2_Event_Iterator iter;
  2512. uint8_t* data;
  2513. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2514. for (;;)
  2515. {
  2516. data = nullptr;
  2517. ev = lv2_event_get(&iter, &data);
  2518. if (ev == nullptr || data == nullptr)
  2519. break;
  2520. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2521. {
  2522. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2523. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2524. }
  2525. lv2_event_increment(&iter);
  2526. }
  2527. }
  2528. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2529. {
  2530. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2531. uint32_t eventSize;
  2532. double eventTime;
  2533. unsigned char* eventData;
  2534. for (;;)
  2535. {
  2536. eventSize = 0;
  2537. eventTime = 0.0;
  2538. eventData = nullptr;
  2539. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2540. if (eventData == nullptr || eventSize == 0)
  2541. break;
  2542. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2543. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2544. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2545. lv2midi_step(&state);
  2546. }
  2547. }
  2548. }
  2549. // --------------------------------------------------------------------------------------------------------
  2550. // Control Output
  2551. if (pData->event.portOut != nullptr)
  2552. {
  2553. uint8_t channel;
  2554. uint16_t param;
  2555. float value;
  2556. for (uint32_t k=0; k < pData->param.count; ++k)
  2557. {
  2558. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2559. continue;
  2560. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2561. if (pData->param.data[k].midiCC > 0)
  2562. {
  2563. channel = pData->param.data[k].midiChannel;
  2564. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2565. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2566. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2567. }
  2568. }
  2569. } // End of Control Output
  2570. CARLA_PROCESS_CONTINUE_CHECK;
  2571. // --------------------------------------------------------------------------------------------------------
  2572. // Final work
  2573. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2574. {
  2575. fExt.worker->end_run(fHandle);
  2576. if (fHandle2 != nullptr)
  2577. fExt.worker->end_run(fHandle2);
  2578. }
  2579. fFirstActive = false;
  2580. // --------------------------------------------------------------------------------------------------------
  2581. }
  2582. bool processSingle(float** const audioInBuf, float** const audioOutBuf, float** const cvInBuf, float** const cvOutBuf, const uint32_t frames, const uint32_t timeOffset)
  2583. {
  2584. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2585. if (pData->audioIn.count > 0)
  2586. {
  2587. CARLA_SAFE_ASSERT_RETURN(audioInBuf != nullptr, false);
  2588. }
  2589. if (pData->audioOut.count > 0)
  2590. {
  2591. CARLA_SAFE_ASSERT_RETURN(audioOutBuf != nullptr, false);
  2592. }
  2593. if (fCvIn.count > 0)
  2594. {
  2595. CARLA_SAFE_ASSERT_RETURN(cvInBuf != nullptr, false);
  2596. }
  2597. if (fCvOut.count > 0)
  2598. {
  2599. CARLA_SAFE_ASSERT_RETURN(cvOutBuf != nullptr, false);
  2600. }
  2601. // --------------------------------------------------------------------------------------------------------
  2602. // Try lock, silence otherwise
  2603. if (pData->engine->isOffline())
  2604. {
  2605. pData->singleMutex.lock();
  2606. }
  2607. else if (! pData->singleMutex.tryLock())
  2608. {
  2609. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2610. {
  2611. for (uint32_t k=0; k < frames; ++k)
  2612. audioOutBuf[i][k+timeOffset] = 0.0f;
  2613. }
  2614. return false;
  2615. }
  2616. // --------------------------------------------------------------------------------------------------------
  2617. // Set audio buffers
  2618. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2619. FLOAT_COPY(fAudioInBuffers[i], audioInBuf[i]+timeOffset, frames);
  2620. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2621. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  2622. // --------------------------------------------------------------------------------------------------------
  2623. // Set CV buffers
  2624. for (uint32_t i=0; i < fCvIn.count; ++i)
  2625. FLOAT_COPY(fCvInBuffers[i], cvInBuf[i]+timeOffset, frames);
  2626. for (uint32_t i=0; i < fCvOut.count; ++i)
  2627. FLOAT_CLEAR(fCvOutBuffers[i], frames);
  2628. // --------------------------------------------------------------------------------------------------------
  2629. // Run plugin
  2630. fDescriptor->run(fHandle, frames);
  2631. if (fHandle2 != nullptr)
  2632. fDescriptor->run(fHandle2, frames);
  2633. // --------------------------------------------------------------------------------------------------------
  2634. // Handle trigger parameters
  2635. for (uint32_t k=0; k < pData->param.count; ++k)
  2636. {
  2637. if (pData->param.data[k].type != PARAMETER_INPUT)
  2638. continue;
  2639. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2640. {
  2641. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2642. {
  2643. fParamBuffers[k] = pData->param.ranges[k].def;
  2644. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2645. }
  2646. }
  2647. }
  2648. pData->postRtEvents.trySplice();
  2649. #ifndef BUILD_BRIDGE
  2650. // --------------------------------------------------------------------------------------------------------
  2651. // Post-processing (dry/wet, volume and balance)
  2652. {
  2653. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2654. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2655. bool isPair;
  2656. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2657. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2658. {
  2659. // Dry/Wet
  2660. if (doDryWet)
  2661. {
  2662. for (uint32_t k=0; k < frames; ++k)
  2663. {
  2664. // TODO
  2665. //if (k < pData->latency && pData->latency < frames)
  2666. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  2667. //else
  2668. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2669. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  2670. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2671. }
  2672. }
  2673. // Balance
  2674. if (doBalance)
  2675. {
  2676. isPair = (i % 2 == 0);
  2677. if (isPair)
  2678. {
  2679. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2680. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  2681. }
  2682. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2683. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2684. for (uint32_t k=0; k < frames; ++k)
  2685. {
  2686. if (isPair)
  2687. {
  2688. // left
  2689. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2690. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2691. }
  2692. else
  2693. {
  2694. // right
  2695. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2696. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2697. }
  2698. }
  2699. }
  2700. // Volume (and buffer copy)
  2701. {
  2702. for (uint32_t k=0; k < frames; ++k)
  2703. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2704. }
  2705. }
  2706. } // End of Post-processing
  2707. #else // BUILD_BRIDGE
  2708. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2709. {
  2710. for (uint32_t k=0; k < frames; ++k)
  2711. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2712. }
  2713. #endif
  2714. // --------------------------------------------------------------------------------------------------------
  2715. pData->singleMutex.unlock();
  2716. return true;
  2717. }
  2718. void bufferSizeChanged(const uint32_t newBufferSize) override
  2719. {
  2720. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2721. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2722. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2723. {
  2724. if (fAudioInBuffers[i] != nullptr)
  2725. delete[] fAudioInBuffers[i];
  2726. fAudioInBuffers[i] = new float[newBufferSize];
  2727. }
  2728. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2729. {
  2730. if (fAudioOutBuffers[i] != nullptr)
  2731. delete[] fAudioOutBuffers[i];
  2732. fAudioOutBuffers[i] = new float[newBufferSize];
  2733. }
  2734. if (fHandle2 == nullptr)
  2735. {
  2736. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2737. {
  2738. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2739. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2740. }
  2741. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2742. {
  2743. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2744. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2745. }
  2746. }
  2747. else
  2748. {
  2749. if (pData->audioIn.count > 0)
  2750. {
  2751. CARLA_ASSERT(pData->audioIn.count == 2);
  2752. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2753. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2754. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2755. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2756. }
  2757. if (pData->audioOut.count > 0)
  2758. {
  2759. CARLA_ASSERT(pData->audioOut.count == 2);
  2760. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2761. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2762. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2763. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2764. }
  2765. }
  2766. for (uint32_t i=0; i < fCvIn.count; ++i)
  2767. {
  2768. if (fCvInBuffers[i] != nullptr)
  2769. delete[] fCvInBuffers[i];
  2770. fCvInBuffers[i] = new float[newBufferSize];
  2771. fDescriptor->connect_port(fHandle, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2772. if (fHandle2 != nullptr)
  2773. fDescriptor->connect_port(fHandle2, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2774. }
  2775. for (uint32_t i=0; i < fCvOut.count; ++i)
  2776. {
  2777. if (fCvOutBuffers[i] != nullptr)
  2778. delete[] fCvOutBuffers[i];
  2779. fCvOutBuffers[i] = new float[newBufferSize];
  2780. fDescriptor->connect_port(fHandle, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2781. if (fHandle2 != nullptr)
  2782. fDescriptor->connect_port(fHandle2, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2783. }
  2784. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2785. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2786. {
  2787. fLv2Options.maxBufferSize = newBufferSizeInt;
  2788. if (fLv2Options.minBufferSize != 1)
  2789. fLv2Options.minBufferSize = newBufferSizeInt;
  2790. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2791. {
  2792. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2793. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2794. }
  2795. }
  2796. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2797. }
  2798. void sampleRateChanged(const double newSampleRate) override
  2799. {
  2800. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2801. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2802. if (fLv2Options.sampleRate != newSampleRate)
  2803. {
  2804. fLv2Options.sampleRate = newSampleRate;
  2805. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2806. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2807. }
  2808. for (uint32_t k=0; k < pData->param.count; ++k)
  2809. {
  2810. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2811. {
  2812. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2813. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2814. break;
  2815. }
  2816. }
  2817. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2818. }
  2819. void offlineModeChanged(const bool isOffline) override
  2820. {
  2821. for (uint32_t k=0; k < pData->param.count; ++k)
  2822. {
  2823. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  2824. {
  2825. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2826. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2827. break;
  2828. }
  2829. }
  2830. }
  2831. // -------------------------------------------------------------------
  2832. // Plugin buffers
  2833. void initBuffers() override
  2834. {
  2835. fCvIn.initBuffers();
  2836. fCvOut.initBuffers();
  2837. fEventsIn.initBuffers();
  2838. fEventsOut.initBuffers();
  2839. CarlaPlugin::initBuffers();
  2840. }
  2841. void clearBuffers() override
  2842. {
  2843. carla_debug("Lv2Plugin::clearBuffers() - start");
  2844. if (fAudioInBuffers != nullptr)
  2845. {
  2846. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2847. {
  2848. if (fAudioInBuffers[i] != nullptr)
  2849. {
  2850. delete[] fAudioInBuffers[i];
  2851. fAudioInBuffers[i] = nullptr;
  2852. }
  2853. }
  2854. delete[] fAudioInBuffers;
  2855. fAudioInBuffers = nullptr;
  2856. }
  2857. if (fAudioOutBuffers != nullptr)
  2858. {
  2859. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2860. {
  2861. if (fAudioOutBuffers[i] != nullptr)
  2862. {
  2863. delete[] fAudioOutBuffers[i];
  2864. fAudioOutBuffers[i] = nullptr;
  2865. }
  2866. }
  2867. delete[] fAudioOutBuffers;
  2868. fAudioOutBuffers = nullptr;
  2869. }
  2870. if (fCvInBuffers != nullptr)
  2871. {
  2872. for (uint32_t i=0; i < fCvIn.count; ++i)
  2873. {
  2874. if (fCvInBuffers[i] != nullptr)
  2875. {
  2876. delete[] fCvInBuffers[i];
  2877. fCvInBuffers[i] = nullptr;
  2878. }
  2879. }
  2880. delete[] fCvInBuffers;
  2881. fCvInBuffers = nullptr;
  2882. }
  2883. if (fCvOutBuffers != nullptr)
  2884. {
  2885. for (uint32_t i=0; i < fCvOut.count; ++i)
  2886. {
  2887. if (fCvOutBuffers[i] != nullptr)
  2888. {
  2889. delete[] fCvOutBuffers[i];
  2890. fCvOutBuffers[i] = nullptr;
  2891. }
  2892. }
  2893. delete[] fCvOutBuffers;
  2894. fCvOutBuffers = nullptr;
  2895. }
  2896. if (fParamBuffers != nullptr)
  2897. {
  2898. delete[] fParamBuffers;
  2899. fParamBuffers = nullptr;
  2900. }
  2901. fCvIn.clear();
  2902. fCvOut.clear();
  2903. fEventsIn.clear();
  2904. fEventsOut.clear();
  2905. CarlaPlugin::clearBuffers();
  2906. carla_debug("Lv2Plugin::clearBuffers() - end");
  2907. }
  2908. // -------------------------------------------------------------------
  2909. // OSC stuff
  2910. bool updateOscDataExtra() override
  2911. {
  2912. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  2913. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i));
  2914. osc_send_lv2_urid_map(pData->osc.data, CARLA_URI_MAP_ID_NULL, "Complete");
  2915. return true;
  2916. }
  2917. // -------------------------------------------------------------------
  2918. // Post-poned UI Stuff
  2919. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2920. {
  2921. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2922. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2923. if (fUi.type == UI::TYPE_OSC)
  2924. {
  2925. if (pData->osc.data.target != nullptr)
  2926. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  2927. }
  2928. else
  2929. {
  2930. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2931. {
  2932. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  2933. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  2934. }
  2935. }
  2936. }
  2937. void uiMidiProgramChange(const uint32_t index) noexcept override
  2938. {
  2939. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2940. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2941. if (fUi.type == UI::TYPE_OSC)
  2942. {
  2943. if (pData->osc.data.target != nullptr)
  2944. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2945. }
  2946. else
  2947. {
  2948. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2949. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2950. }
  2951. }
  2952. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2953. {
  2954. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2955. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2956. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2957. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2958. if (fUi.type == UI::TYPE_OSC)
  2959. {
  2960. if (pData->osc.data.target != nullptr)
  2961. {
  2962. uint8_t midiData[4] = { 0 };
  2963. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2964. midiData[2] = note;
  2965. midiData[3] = velo;
  2966. osc_send_midi(pData->osc.data, midiData);
  2967. }
  2968. }
  2969. else
  2970. {
  2971. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2972. {
  2973. LV2_Atom_MidiEvent midiEv;
  2974. midiEv.event.time.frames = 0;
  2975. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2976. midiEv.event.body.size = 3;
  2977. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  2978. midiEv.data[1] = note;
  2979. midiEv.data[2] = velo;
  2980. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2981. }
  2982. }
  2983. }
  2984. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2985. {
  2986. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2987. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2988. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2989. if (fUi.type == UI::TYPE_OSC)
  2990. {
  2991. if (pData->osc.data.target != nullptr)
  2992. {
  2993. uint8_t midiData[4] = { 0 };
  2994. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  2995. midiData[2] = note;
  2996. osc_send_midi(pData->osc.data, midiData);
  2997. }
  2998. }
  2999. else
  3000. {
  3001. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3002. {
  3003. LV2_Atom_MidiEvent midiEv;
  3004. midiEv.event.time.frames = 0;
  3005. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3006. midiEv.event.body.size = 3;
  3007. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  3008. midiEv.data[1] = note;
  3009. midiEv.data[2] = 0;
  3010. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3011. }
  3012. }
  3013. }
  3014. // -------------------------------------------------------------------
  3015. bool isRealtimeSafe() const noexcept
  3016. {
  3017. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3018. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3019. {
  3020. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3021. return true;
  3022. }
  3023. return false;
  3024. }
  3025. bool needsFixedBuffer() const noexcept
  3026. {
  3027. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3028. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3029. {
  3030. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3031. return true;
  3032. }
  3033. return false;
  3034. }
  3035. // -------------------------------------------------------------------
  3036. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3037. {
  3038. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3039. #ifndef LV2_UIS_ONLY_INPROCESS
  3040. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  3041. if (std::strstr(rdfUi->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3042. return false;
  3043. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  3044. {
  3045. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3046. return false;
  3047. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3048. return false;
  3049. }
  3050. return true;
  3051. #else
  3052. return false;
  3053. #endif
  3054. }
  3055. bool isUiResizable() const noexcept
  3056. {
  3057. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  3058. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3059. {
  3060. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3061. return false;
  3062. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3063. return false;
  3064. }
  3065. return true;
  3066. }
  3067. const char* getUiBridgeBinary(const LV2_Property type) const
  3068. {
  3069. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3070. if (bridgeBinary.isEmpty())
  3071. return nullptr;
  3072. #ifdef CARLA_OS_LINUX
  3073. // test for local build
  3074. if (bridgeBinary.endsWith("/source/backend/"))
  3075. bridgeBinary += "../bridges/";
  3076. #endif
  3077. switch (type)
  3078. {
  3079. case LV2_UI_GTK2:
  3080. bridgeBinary += "carla-bridge-lv2-gtk2";
  3081. break;
  3082. case LV2_UI_GTK3:
  3083. bridgeBinary += "carla-bridge-lv2-gtk3";
  3084. break;
  3085. case LV2_UI_QT4:
  3086. bridgeBinary += "carla-bridge-lv2-qt4";
  3087. break;
  3088. case LV2_UI_QT5:
  3089. bridgeBinary += "carla-bridge-lv2-qt5";
  3090. break;
  3091. case LV2_UI_COCOA:
  3092. bridgeBinary += "carla-bridge-lv2-cocoa";
  3093. break;
  3094. case LV2_UI_WINDOWS:
  3095. bridgeBinary += "carla-bridge-lv2-windows";
  3096. break;
  3097. case LV2_UI_X11:
  3098. bridgeBinary += "carla-bridge-lv2-x11";
  3099. break;
  3100. case LV2_UI_EXTERNAL:
  3101. case LV2_UI_OLD_EXTERNAL:
  3102. bridgeBinary += "carla-bridge-lv2-external";
  3103. break;
  3104. default:
  3105. return nullptr;
  3106. }
  3107. #ifdef CARLA_OS_WIN
  3108. bridgeBinary += ".exe";
  3109. #endif
  3110. QFile file(bridgeBinary.buffer());
  3111. if (! file.exists())
  3112. return nullptr;
  3113. return bridgeBinary.dup();
  3114. }
  3115. // -------------------------------------------------------------------
  3116. void recheckExtensions()
  3117. {
  3118. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3119. carla_debug("Lv2Plugin::recheckExtensions()");
  3120. fExt.options = nullptr;
  3121. fExt.programs = nullptr;
  3122. fExt.state = nullptr;
  3123. fExt.worker = nullptr;
  3124. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3125. {
  3126. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->Extensions[i] != nullptr);
  3127. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3128. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3129. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3130. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3131. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3132. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3133. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3134. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3135. else
  3136. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3137. }
  3138. if (fDescriptor->extension_data != nullptr)
  3139. {
  3140. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3141. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3142. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3143. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3144. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3145. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3146. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3147. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3148. // check if invalid
  3149. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3150. fExt.options = nullptr;
  3151. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3152. fExt.programs = nullptr;
  3153. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3154. fExt.state = nullptr;
  3155. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3156. fExt.worker = nullptr;
  3157. }
  3158. }
  3159. // -------------------------------------------------------------------
  3160. void updateUi()
  3161. {
  3162. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  3163. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  3164. carla_debug("Lv2Plugin::updateUi()");
  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. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  3413. // plugins sometimes fail on this, not good...
  3414. CARLA_SAFE_ASSERT_INT2(bufferSize == lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom)+atom->size)), bufferSize, atom->size);
  3415. for (uint32_t i=0; i < fEventsIn.count; ++i)
  3416. {
  3417. if (fEventsIn.data[i].rindex != rindex)
  3418. continue;
  3419. index = i;
  3420. break;
  3421. }
  3422. // for bad plugins
  3423. if (index == LV2UI_INVALID_PORT_INDEX)
  3424. {
  3425. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  3426. index = fEventsIn.ctrlIndex;
  3427. }
  3428. fAtomQueueIn.put(atom, index);
  3429. } break;
  3430. default:
  3431. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3432. break;
  3433. }
  3434. }
  3435. // -------------------------------------------------------------------
  3436. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3437. {
  3438. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3439. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3440. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3441. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3442. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3443. int32_t rindex = -1;
  3444. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3445. {
  3446. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3447. {
  3448. rindex = static_cast<int32_t>(i);
  3449. break;
  3450. }
  3451. }
  3452. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3453. float paramValue;
  3454. switch (type)
  3455. {
  3456. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3457. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3458. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3459. break;
  3460. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3461. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3462. paramValue = static_cast<float>((*(const double*)value));
  3463. break;
  3464. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3465. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3466. paramValue = (*(const float*)value);
  3467. break;
  3468. case CARLA_URI_MAP_ID_ATOM_INT:
  3469. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3470. paramValue = static_cast<float>((*(const int32_t*)value));
  3471. break;
  3472. case CARLA_URI_MAP_ID_ATOM_LONG:
  3473. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3474. paramValue = static_cast<float>((*(const int64_t*)value));
  3475. break;
  3476. default:
  3477. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3478. return;
  3479. }
  3480. for (uint32_t i=0; i < pData->param.count; ++i)
  3481. {
  3482. if (pData->param.data[i].rindex == rindex)
  3483. {
  3484. setParameterValue(i, paramValue, true, true, true);
  3485. break;
  3486. }
  3487. }
  3488. }
  3489. // -------------------------------------------------------------------
  3490. public:
  3491. bool init(const char* const bundle, const char* const name, const char* const uri)
  3492. {
  3493. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3494. // ---------------------------------------------------------------
  3495. // first checks
  3496. if (pData->client != nullptr)
  3497. {
  3498. pData->engine->setLastError("Plugin client is already registered");
  3499. return false;
  3500. }
  3501. if (bundle == nullptr || bundle[0] == '\0')
  3502. {
  3503. pData->engine->setLastError("null bundle");
  3504. return false;
  3505. }
  3506. if (uri == nullptr || uri[0] == '\0')
  3507. {
  3508. pData->engine->setLastError("null uri");
  3509. return false;
  3510. }
  3511. // ---------------------------------------------------------------
  3512. // get plugin from lv2_rdf (lilv)
  3513. fRdfDescriptor = lv2_rdf_new(uri, true);
  3514. if (fRdfDescriptor == nullptr)
  3515. {
  3516. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3517. return false;
  3518. }
  3519. // ---------------------------------------------------------------
  3520. // open DLL
  3521. if (! pData->libOpen(fRdfDescriptor->Binary))
  3522. {
  3523. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3524. return false;
  3525. }
  3526. // ---------------------------------------------------------------
  3527. // try to get DLL main entry via new mode
  3528. if (const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)pData->libSymbol("lv2_lib_descriptor"))
  3529. {
  3530. // -----------------------------------------------------------
  3531. // all ok, get lib descriptor
  3532. const LV2_Lib_Descriptor* const libDesc = libDescFn(bundle, nullptr);
  3533. if (libDesc == nullptr)
  3534. {
  3535. pData->engine->setLastError("Could not find the LV2 Descriptor");
  3536. return false;
  3537. }
  3538. // -----------------------------------------------------------
  3539. // get descriptor that matches URI (new mode)
  3540. uint32_t i = 0;
  3541. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3542. {
  3543. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3544. break;
  3545. }
  3546. }
  3547. else
  3548. {
  3549. // -----------------------------------------------------------
  3550. // get DLL main entry (old mode)
  3551. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3552. if (descFn == nullptr)
  3553. {
  3554. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3555. return false;
  3556. }
  3557. // -----------------------------------------------------------
  3558. // get descriptor that matches URI (old mode)
  3559. uint32_t i = 0;
  3560. while ((fDescriptor = descFn(i++)))
  3561. {
  3562. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3563. break;
  3564. }
  3565. }
  3566. if (fDescriptor == nullptr)
  3567. {
  3568. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3569. return false;
  3570. }
  3571. // ---------------------------------------------------------------
  3572. // check supported port-types and features
  3573. bool canContinue = true;
  3574. // Check supported ports
  3575. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3576. {
  3577. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3578. if (! is_lv2_port_supported(portTypes))
  3579. {
  3580. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3581. {
  3582. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3583. canContinue = false;
  3584. break;
  3585. }
  3586. }
  3587. }
  3588. // Check supported features
  3589. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3590. {
  3591. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  3592. {
  3593. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  3594. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  3595. {
  3596. canContinue = false;
  3597. pData->engine->setLastError(msg.toUtf8().constData());
  3598. break;
  3599. }
  3600. else
  3601. carla_stderr("%s", msg.toUtf8().constData());
  3602. }
  3603. }
  3604. if (! canContinue)
  3605. {
  3606. // error already set
  3607. return false;
  3608. }
  3609. // ---------------------------------------------------------------
  3610. // get info
  3611. if (name != nullptr && name[0] != '\0')
  3612. pData->name = pData->engine->getUniquePluginName(name);
  3613. else
  3614. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3615. pData->filename = carla_strdup(bundle);
  3616. // ---------------------------------------------------------------
  3617. // register client
  3618. pData->client = pData->engine->addClient(this);
  3619. if (pData->client == nullptr || ! pData->client->isOk())
  3620. {
  3621. pData->engine->setLastError("Failed to register plugin client");
  3622. return false;
  3623. }
  3624. // ---------------------------------------------------------------
  3625. // initialize options
  3626. fLv2Options.minBufferSize = 1;
  3627. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3628. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3629. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3630. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3631. {
  3632. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3633. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3634. {
  3635. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3636. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3637. }
  3638. }
  3639. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3640. // ---------------------------------------------------------------
  3641. // initialize features (part 1)
  3642. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3643. eventFt->callback_data = this;
  3644. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3645. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3646. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3647. logFt->handle = this;
  3648. logFt->printf = carla_lv2_log_printf;
  3649. logFt->vprintf = carla_lv2_log_vprintf;
  3650. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3651. stateMakePathFt->handle = this;
  3652. stateMakePathFt->path = carla_lv2_state_make_path;
  3653. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3654. stateMapPathFt->handle = this;
  3655. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3656. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3657. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3658. programsFt->handle = this;
  3659. programsFt->program_changed = carla_lv2_program_changed;
  3660. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3661. rsPortFt->data = this;
  3662. rsPortFt->resize = carla_lv2_resize_port;
  3663. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3664. lv2_rtmempool_init(rtMemPoolFt);
  3665. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3666. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3667. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3668. uriMapFt->callback_data = this;
  3669. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3670. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3671. uridMapFt->handle = this;
  3672. uridMapFt->map = carla_lv2_urid_map;
  3673. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3674. uridUnmapFt->handle = this;
  3675. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3676. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3677. workerFt->handle = this;
  3678. workerFt->schedule_work = carla_lv2_worker_schedule;
  3679. // ---------------------------------------------------------------
  3680. // initialize features (part 2)
  3681. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3682. fFeatures[j] = new LV2_Feature;
  3683. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3684. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3685. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3686. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3687. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3688. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3689. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3690. fFeatures[kFeatureIdEvent]->data = eventFt;
  3691. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3692. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3693. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3694. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3695. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3696. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3697. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3698. fFeatures[kFeatureIdLogs]->data = logFt;
  3699. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3700. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3701. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3702. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3703. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3704. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3705. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3706. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3707. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3708. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3709. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3710. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3711. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3712. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3713. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3714. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3715. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3716. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3717. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3718. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3719. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3720. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3721. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3722. fFeatures[kFeatureIdWorker]->data = workerFt;
  3723. // if a fixed buffer is not needed, skip its feature
  3724. if (! needsFixedBuffer())
  3725. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3726. // if power of 2 is not possible, skip its feature FIXME
  3727. //if (fLv2Options.maxBufferSize)
  3728. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3729. // ---------------------------------------------------------------
  3730. // initialize plugin
  3731. try {
  3732. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3733. } catch(...) {}
  3734. if (fHandle == nullptr)
  3735. {
  3736. pData->engine->setLastError("Plugin failed to initialize");
  3737. return false;
  3738. }
  3739. // ---------------------------------------------------------------
  3740. // load plugin settings
  3741. {
  3742. // set default options
  3743. pData->options = 0x0;
  3744. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3745. if (getMidiInCount() > 0 || needsFixedBuffer())
  3746. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3747. if (pData->engine->getOptions().forceStereo)
  3748. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3749. if (getMidiInCount() > 0)
  3750. {
  3751. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3752. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3753. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3754. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3755. }
  3756. // set identifier string
  3757. CarlaString identifier("LV2/");
  3758. identifier += uri;
  3759. pData->identifier = identifier.dup();
  3760. // load settings
  3761. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3762. // ignore settings, we need this anyway
  3763. if (getMidiInCount() > 0 || needsFixedBuffer())
  3764. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3765. }
  3766. // ---------------------------------------------------------------
  3767. // gui stuff
  3768. if (fRdfDescriptor->UICount != 0)
  3769. initUi();
  3770. return true;
  3771. }
  3772. // -------------------------------------------------------------------
  3773. void initUi()
  3774. {
  3775. // ---------------------------------------------------------------
  3776. // find the most appropriate ui
  3777. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3778. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3779. #if defined(BUILD_BRIDGE)
  3780. const bool preferUiBridges(false);
  3781. #elif defined(LV2_UIS_ONLY_BRIDGES)
  3782. const bool preferUiBridges(true);
  3783. #else
  3784. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3785. #endif
  3786. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3787. {
  3788. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3789. const int ii(static_cast<int>(i));
  3790. switch (fRdfDescriptor->UIs[i].Type)
  3791. {
  3792. case LV2_UI_QT4:
  3793. if (isUiBridgeable(i))
  3794. eQt4 = ii;
  3795. break;
  3796. case LV2_UI_QT5:
  3797. if (isUiBridgeable(i))
  3798. eQt5 = ii;
  3799. break;
  3800. case LV2_UI_GTK2:
  3801. if (isUiBridgeable(i))
  3802. eGtk2 = ii;
  3803. break;
  3804. case LV2_UI_GTK3:
  3805. if (isUiBridgeable(i))
  3806. eGtk3 = ii;
  3807. break;
  3808. case LV2_UI_COCOA:
  3809. if (isUiBridgeable(i) && preferUiBridges)
  3810. eCocoa = ii;
  3811. iCocoa = ii;
  3812. break;
  3813. case LV2_UI_WINDOWS:
  3814. if (isUiBridgeable(i) && preferUiBridges)
  3815. eWindows = ii;
  3816. iWindows = ii;
  3817. break;
  3818. case LV2_UI_X11:
  3819. if (isUiBridgeable(i) && preferUiBridges)
  3820. eX11 = ii;
  3821. iX11 = ii;
  3822. break;
  3823. case LV2_UI_EXTERNAL:
  3824. case LV2_UI_OLD_EXTERNAL:
  3825. if (isUiBridgeable(i))
  3826. eExt = ii;
  3827. iExt = ii;
  3828. break;
  3829. default:
  3830. break;
  3831. }
  3832. }
  3833. if (eQt4 >= 0)
  3834. iFinal = eQt4;
  3835. else if (eQt5 >= 0)
  3836. iFinal = eQt5;
  3837. else if (eGtk2 >= 0)
  3838. iFinal = eGtk2;
  3839. else if (eGtk3 >= 0)
  3840. iFinal = eGtk3;
  3841. #ifdef CARLA_OS_MAC
  3842. else if (eCocoa >= 0)
  3843. iFinal = eCocoa;
  3844. #endif
  3845. #ifdef CARLA_OS_WIN
  3846. else if (eWindows >= 0)
  3847. iFinal = eWindows;
  3848. #endif
  3849. #ifdef HAVE_X11
  3850. else if (eX11 >= 0)
  3851. iFinal = eX11;
  3852. #endif
  3853. //else if (eExt >= 0) // TODO
  3854. // iFinal = eExt;
  3855. #ifndef LV2_UIS_ONLY_BRIDGES
  3856. # ifdef CARLA_OS_MAC
  3857. else if (iCocoa >= 0)
  3858. iFinal = iCocoa;
  3859. # endif
  3860. # ifdef CARLA_OS_WIN
  3861. else if (iWindows >= 0)
  3862. iFinal = iWindows;
  3863. # endif
  3864. # ifdef HAVE_X11
  3865. else if (iX11 >= 0)
  3866. iFinal = iX11;
  3867. # endif
  3868. #endif
  3869. else if (iExt >= 0)
  3870. iFinal = iExt;
  3871. if (iFinal < 0)
  3872. {
  3873. // no suitable UI found, see if there's one which supports ui:showInterface
  3874. bool hasShowInterface = false;
  3875. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  3876. {
  3877. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  3878. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  3879. {
  3880. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  3881. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  3882. continue;
  3883. iFinal = static_cast<int>(i);
  3884. hasShowInterface = true;
  3885. break;
  3886. }
  3887. }
  3888. if (! hasShowInterface)
  3889. {
  3890. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3891. return;
  3892. }
  3893. }
  3894. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3895. // ---------------------------------------------------------------
  3896. // check supported ui features
  3897. bool canContinue = true;
  3898. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3899. {
  3900. if (! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3901. {
  3902. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3903. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type))
  3904. {
  3905. canContinue = false;
  3906. break;
  3907. }
  3908. }
  3909. }
  3910. if (! canContinue)
  3911. {
  3912. fUi.rdfDescriptor = nullptr;
  3913. return;
  3914. }
  3915. // ---------------------------------------------------------------
  3916. // initialize ui according to type
  3917. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3918. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  3919. {
  3920. // -----------------------------------------------------------
  3921. // initialize osc-bridge
  3922. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  3923. {
  3924. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  3925. fUi.type = UI::TYPE_OSC;
  3926. pData->osc.thread.setOscData(bridgeBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3927. delete[] bridgeBinary;
  3928. return;
  3929. }
  3930. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3)
  3931. {
  3932. carla_stderr2("Failed to find UI bridge binary, cannot use UI");
  3933. fUi.rdfDescriptor = nullptr;
  3934. return;
  3935. }
  3936. }
  3937. #ifdef LV2_UIS_ONLY_BRIDGES
  3938. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  3939. fUi.rdfDescriptor = nullptr;
  3940. return;
  3941. #endif
  3942. // ---------------------------------------------------------------
  3943. // open UI DLL
  3944. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3945. {
  3946. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUi.rdfDescriptor->Binary));
  3947. fUi.rdfDescriptor = nullptr;
  3948. return;
  3949. }
  3950. // ---------------------------------------------------------------
  3951. // get UI DLL main entry
  3952. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  3953. if (uiDescFn == nullptr)
  3954. {
  3955. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3956. pData->uiLibClose();
  3957. fUi.rdfDescriptor = nullptr;
  3958. return;
  3959. }
  3960. // ---------------------------------------------------------------
  3961. // get UI descriptor that matches UI URI
  3962. uint32_t i = 0;
  3963. while ((fUi.descriptor = uiDescFn(i++)))
  3964. {
  3965. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3966. break;
  3967. }
  3968. if (fUi.descriptor == nullptr)
  3969. {
  3970. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3971. pData->uiLibClose();
  3972. fUi.rdfDescriptor = nullptr;
  3973. return;
  3974. }
  3975. // ---------------------------------------------------------------
  3976. // check if ui is usable
  3977. switch (uiType)
  3978. {
  3979. case LV2_UI_QT4:
  3980. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  3981. fUi.type = UI::TYPE_EMBED;
  3982. break;
  3983. case LV2_UI_QT5:
  3984. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  3985. fUi.type = UI::TYPE_EMBED;
  3986. break;
  3987. case LV2_UI_GTK2:
  3988. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  3989. fUi.type = UI::TYPE_EMBED;
  3990. break;
  3991. case LV2_UI_GTK3:
  3992. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  3993. fUi.type = UI::TYPE_EMBED;
  3994. break;
  3995. #ifdef CARLA_OS_MAC
  3996. case LV2_UI_COCOA:
  3997. carla_stdout("Will use LV2 Cocoa UI");
  3998. fUi.type = UI::TYPE_EMBED;
  3999. break;
  4000. #endif
  4001. #ifdef CARLA_OS_WIN
  4002. case LV2_UI_WINDOWS:
  4003. carla_stdout("Will use LV2 Windows UI");
  4004. fUi.type = UI::TYPE_EMBED;
  4005. break;
  4006. #endif
  4007. case LV2_UI_X11:
  4008. #ifdef HAVE_X11
  4009. carla_stdout("Will use LV2 X11 UI");
  4010. #else
  4011. carla_stdout("Will use LV2 X11 UI, NOT!");
  4012. #endif
  4013. fUi.type = UI::TYPE_EMBED;
  4014. break;
  4015. case LV2_UI_EXTERNAL:
  4016. case LV2_UI_OLD_EXTERNAL:
  4017. carla_stdout("Will use LV2 External UI");
  4018. fUi.type = UI::TYPE_EXTERNAL;
  4019. break;
  4020. }
  4021. if (fUi.type == UI::TYPE_NULL)
  4022. {
  4023. pData->uiLibClose();
  4024. fUi.descriptor = nullptr;
  4025. fUi.rdfDescriptor = nullptr;
  4026. return;
  4027. }
  4028. // ---------------------------------------------------------------
  4029. // initialize ui data
  4030. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  4031. fUi.title = carla_strdup(guiTitle.toUtf8().constData());
  4032. // ---------------------------------------------------------------
  4033. // initialize ui features (part 1)
  4034. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  4035. uiDataFt->data_access = fDescriptor->extension_data;
  4036. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  4037. uiPortMapFt->handle = this;
  4038. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  4039. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  4040. uiResizeFt->handle = this;
  4041. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  4042. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  4043. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  4044. uiExternalHostFt->plugin_human_id = fUi.title;
  4045. // ---------------------------------------------------------------
  4046. // initialize ui features (part 2)
  4047. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  4048. fFeatures[j] = new LV2_Feature;
  4049. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  4050. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  4051. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  4052. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  4053. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  4054. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  4055. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  4056. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  4057. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  4058. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  4059. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  4060. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  4061. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  4062. fFeatures[kFeatureIdUiParent]->data = nullptr;
  4063. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  4064. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  4065. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  4066. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  4067. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  4068. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  4069. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  4070. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  4071. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  4072. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  4073. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  4074. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  4075. // ---------------------------------------------------------------
  4076. // initialize ui extensions
  4077. if (fUi.descriptor->extension_data == nullptr)
  4078. return;
  4079. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  4080. fExt.uishow = (const LV2UI_Show_Interface*)fUi.descriptor->extension_data(LV2_UI__showInterface);
  4081. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  4082. // check if invalid
  4083. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  4084. fExt.uiidle = nullptr;
  4085. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  4086. fExt.uishow = nullptr;
  4087. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  4088. fExt.uiprograms = nullptr;
  4089. }
  4090. // -------------------------------------------------------------------
  4091. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  4092. {
  4093. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  4094. carla_stdout("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  4095. fAtomQueueIn.put(atom, portIndex);
  4096. }
  4097. void handleUridMap(const LV2_URID urid, const char* const uri)
  4098. {
  4099. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  4100. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  4101. carla_stdout("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count()-1, uri);
  4102. if (urid < fCustomURIDs.count())
  4103. {
  4104. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  4105. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr,);
  4106. if (std::strcmp(ourURI, uri) != 0)
  4107. {
  4108. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  4109. }
  4110. }
  4111. else
  4112. {
  4113. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  4114. fCustomURIDs.append(carla_strdup(uri));
  4115. }
  4116. }
  4117. // -------------------------------------------------------------------
  4118. private:
  4119. LV2_Handle fHandle;
  4120. LV2_Handle fHandle2;
  4121. LV2_Feature* fFeatures[kFeatureCountAll+1];
  4122. const LV2_Descriptor* fDescriptor;
  4123. const LV2_RDF_Descriptor* fRdfDescriptor;
  4124. float** fAudioInBuffers;
  4125. float** fAudioOutBuffers;
  4126. float** fCvInBuffers;
  4127. float** fCvOutBuffers;
  4128. float* fParamBuffers;
  4129. Lv2AtomQueue fAtomQueueIn;
  4130. Lv2AtomQueue fAtomQueueOut;
  4131. LV2_Atom_Forge fAtomForge;
  4132. PluginCVData fCvIn;
  4133. PluginCVData fCvOut;
  4134. Lv2PluginEventData fEventsIn;
  4135. Lv2PluginEventData fEventsOut;
  4136. Lv2PluginOptions fLv2Options;
  4137. LinkedList<const char*> fCustomURIDs;
  4138. bool fFirstActive; // first process() call after activate()
  4139. EngineTimeInfo fLastTimeInfo;
  4140. struct Extensions {
  4141. const LV2_Options_Interface* options;
  4142. const LV2_State_Interface* state;
  4143. const LV2_Worker_Interface* worker;
  4144. const LV2_Programs_Interface* programs;
  4145. const LV2UI_Idle_Interface* uiidle;
  4146. const LV2UI_Show_Interface* uishow;
  4147. const LV2_Programs_UI_Interface* uiprograms;
  4148. Extensions()
  4149. : options(nullptr),
  4150. state(nullptr),
  4151. worker(nullptr),
  4152. programs(nullptr),
  4153. uiidle(nullptr),
  4154. uishow(nullptr),
  4155. uiprograms(nullptr) {}
  4156. } fExt;
  4157. struct UI {
  4158. enum Type {
  4159. TYPE_NULL,
  4160. TYPE_EMBED,
  4161. TYPE_EXTERNAL,
  4162. TYPE_OSC
  4163. };
  4164. Type type;
  4165. LV2UI_Handle handle;
  4166. LV2UI_Widget widget;
  4167. const LV2UI_Descriptor* descriptor;
  4168. const LV2_RDF_UI* rdfDescriptor;
  4169. const char* title;
  4170. CarlaPluginUi* window;
  4171. UI()
  4172. : type(TYPE_NULL),
  4173. handle(nullptr),
  4174. widget(nullptr),
  4175. descriptor(nullptr),
  4176. rdfDescriptor(nullptr),
  4177. title(nullptr),
  4178. window(nullptr) {}
  4179. ~UI()
  4180. {
  4181. CARLA_ASSERT(handle == nullptr);
  4182. CARLA_ASSERT(widget == nullptr);
  4183. CARLA_ASSERT(descriptor == nullptr);
  4184. CARLA_ASSERT(rdfDescriptor == nullptr);
  4185. CARLA_ASSERT(title == nullptr);
  4186. CARLA_ASSERT(window == nullptr);
  4187. }
  4188. } fUi;
  4189. // -------------------------------------------------------------------
  4190. // Event Feature
  4191. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4192. {
  4193. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4194. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4195. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4196. return 0;
  4197. }
  4198. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4199. {
  4200. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4201. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4202. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4203. return 0;
  4204. }
  4205. // -------------------------------------------------------------------
  4206. // Logs Feature
  4207. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4208. {
  4209. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4210. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4211. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4212. #ifndef DEBUG
  4213. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4214. return 0;
  4215. #endif
  4216. va_list args;
  4217. va_start(args, fmt);
  4218. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4219. va_end(args);
  4220. return ret;
  4221. }
  4222. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4223. {
  4224. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4225. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4226. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4227. #ifndef DEBUG
  4228. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4229. return 0;
  4230. #endif
  4231. int ret = 0;
  4232. switch (type)
  4233. {
  4234. case CARLA_URI_MAP_ID_LOG_ERROR:
  4235. std::fprintf(stderr, "\x1b[31m");
  4236. ret = std::vfprintf(stderr, fmt, ap);
  4237. std::fprintf(stderr, "\x1b[0m");
  4238. break;
  4239. case CARLA_URI_MAP_ID_LOG_NOTE:
  4240. ret = std::vfprintf(stdout, fmt, ap);
  4241. break;
  4242. case CARLA_URI_MAP_ID_LOG_TRACE:
  4243. #ifdef DEBUG
  4244. std::fprintf(stdout, "\x1b[30;1m");
  4245. ret = std::vfprintf(stdout, fmt, ap);
  4246. std::fprintf(stdout, "\x1b[0m");
  4247. #endif
  4248. break;
  4249. case CARLA_URI_MAP_ID_LOG_WARNING:
  4250. ret = std::vfprintf(stderr, fmt, ap);
  4251. break;
  4252. default:
  4253. break;
  4254. }
  4255. return ret;
  4256. }
  4257. // -------------------------------------------------------------------
  4258. // Programs Feature
  4259. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4260. {
  4261. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4262. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4263. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4264. }
  4265. // -------------------------------------------------------------------
  4266. // Resize Port Feature
  4267. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4268. {
  4269. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4270. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4271. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  4272. }
  4273. // -------------------------------------------------------------------
  4274. // State Feature
  4275. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4276. {
  4277. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4278. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4279. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4280. QDir dir;
  4281. dir.mkpath(path);
  4282. return strdup(path);
  4283. }
  4284. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4285. {
  4286. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4287. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4288. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4289. QDir dir(absolute_path);
  4290. return strdup(dir.canonicalPath().toUtf8().constData());
  4291. }
  4292. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4293. {
  4294. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4295. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4296. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4297. QDir dir(abstract_path);
  4298. return strdup(dir.absolutePath().toUtf8().constData());
  4299. }
  4300. 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)
  4301. {
  4302. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4303. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4304. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4305. }
  4306. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4307. {
  4308. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4309. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4310. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4311. }
  4312. // -------------------------------------------------------------------
  4313. // URI-Map Feature
  4314. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4315. {
  4316. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4317. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4318. // unused
  4319. (void)map;
  4320. }
  4321. // -------------------------------------------------------------------
  4322. // URID Feature
  4323. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4324. {
  4325. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4326. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4327. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4328. // Atom types
  4329. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4330. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4331. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4332. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4333. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4334. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4335. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4336. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4337. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  4338. return CARLA_URI_MAP_ID_ATOM_EVENT;
  4339. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4340. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4341. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4342. return CARLA_URI_MAP_ID_ATOM_INT;
  4343. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4344. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4345. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4346. return CARLA_URI_MAP_ID_ATOM_LONG;
  4347. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  4348. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  4349. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4350. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4351. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4352. return CARLA_URI_MAP_ID_ATOM_PATH;
  4353. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4354. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4355. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4356. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4357. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4358. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4359. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  4360. return CARLA_URI_MAP_ID_ATOM_SOUND;
  4361. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4362. return CARLA_URI_MAP_ID_ATOM_STRING;
  4363. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4364. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4365. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4366. return CARLA_URI_MAP_ID_ATOM_URI;
  4367. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4368. return CARLA_URI_MAP_ID_ATOM_URID;
  4369. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4370. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4371. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4372. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4373. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4374. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4375. // BufSize types
  4376. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4377. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4378. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4379. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4380. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4381. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4382. // Log types
  4383. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4384. return CARLA_URI_MAP_ID_LOG_ERROR;
  4385. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4386. return CARLA_URI_MAP_ID_LOG_NOTE;
  4387. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4388. return CARLA_URI_MAP_ID_LOG_TRACE;
  4389. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4390. return CARLA_URI_MAP_ID_LOG_WARNING;
  4391. // Time types
  4392. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4393. return CARLA_URI_MAP_ID_TIME_POSITION;
  4394. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4395. return CARLA_URI_MAP_ID_TIME_BAR;
  4396. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4397. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4398. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4399. return CARLA_URI_MAP_ID_TIME_BEAT;
  4400. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4401. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4402. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4403. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4404. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4405. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4406. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4407. return CARLA_URI_MAP_ID_TIME_FRAME;
  4408. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4409. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4410. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4411. return CARLA_URI_MAP_ID_TIME_SPEED;
  4412. // Others
  4413. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4414. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4415. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4416. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4417. // Custom
  4418. if (std::strcmp(uri, URI_CARLA_WORKER) == 0)
  4419. return CARLA_URI_MAP_ID_ATOM_WORKER;
  4420. // Custom types
  4421. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4422. }
  4423. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4424. {
  4425. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4426. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4427. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4428. // Atom types
  4429. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4430. return LV2_ATOM__Blank;
  4431. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4432. return LV2_ATOM__Bool;
  4433. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4434. return LV2_ATOM__Chunk;
  4435. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4436. return LV2_ATOM__Double;
  4437. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  4438. return LV2_ATOM__Event;
  4439. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4440. return LV2_ATOM__Float;
  4441. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4442. return LV2_ATOM__Int;
  4443. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4444. return LV2_ATOM__Literal;
  4445. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4446. return LV2_ATOM__Long;
  4447. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  4448. return LV2_ATOM__Number;
  4449. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4450. return LV2_ATOM__Object;
  4451. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4452. return LV2_ATOM__Path;
  4453. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4454. return LV2_ATOM__Property;
  4455. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4456. return LV2_ATOM__Resource;
  4457. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4458. return LV2_ATOM__Sequence;
  4459. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  4460. return LV2_ATOM__Sound;
  4461. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4462. return LV2_ATOM__String;
  4463. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4464. return LV2_ATOM__Tuple;
  4465. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4466. return LV2_ATOM__URI;
  4467. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4468. return LV2_ATOM__URID;
  4469. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4470. return LV2_ATOM__Vector;
  4471. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  4472. return URI_CARLA_WORKER; // custom
  4473. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4474. return LV2_ATOM__atomTransfer;
  4475. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4476. return LV2_ATOM__eventTransfer;
  4477. // BufSize types
  4478. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4479. return LV2_BUF_SIZE__maxBlockLength;
  4480. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4481. return LV2_BUF_SIZE__minBlockLength;
  4482. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4483. return LV2_BUF_SIZE__sequenceSize;
  4484. // Log types
  4485. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4486. return LV2_LOG__Error;
  4487. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4488. return LV2_LOG__Note;
  4489. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4490. return LV2_LOG__Trace;
  4491. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4492. return LV2_LOG__Warning;
  4493. // Time types
  4494. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4495. return LV2_TIME__Position;
  4496. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4497. return LV2_TIME__bar;
  4498. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4499. return LV2_TIME__barBeat;
  4500. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4501. return LV2_TIME__beat;
  4502. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4503. return LV2_TIME__beatUnit;
  4504. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4505. return LV2_TIME__beatsPerBar;
  4506. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4507. return LV2_TIME__beatsPerMinute;
  4508. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4509. return LV2_TIME__frame;
  4510. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4511. return LV2_TIME__framesPerSecond;
  4512. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4513. return LV2_TIME__speed;
  4514. // Others
  4515. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4516. return LV2_MIDI__MidiEvent;
  4517. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4518. return LV2_PARAMETERS__sampleRate;
  4519. // Custom types
  4520. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4521. }
  4522. // -------------------------------------------------------------------
  4523. // Worker Feature
  4524. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4525. {
  4526. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4527. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4528. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4529. }
  4530. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4531. {
  4532. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4533. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4534. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4535. }
  4536. // -------------------------------------------------------------------
  4537. // External UI Feature
  4538. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4539. {
  4540. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4541. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4542. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4543. }
  4544. // -------------------------------------------------------------------
  4545. // UI Port-Map Feature
  4546. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4547. {
  4548. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4549. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4550. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4551. }
  4552. // -------------------------------------------------------------------
  4553. // UI Resize Feature
  4554. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4555. {
  4556. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4557. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4558. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4559. }
  4560. // -------------------------------------------------------------------
  4561. // UI Extension
  4562. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4563. {
  4564. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4565. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4566. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4567. }
  4568. // -------------------------------------------------------------------
  4569. // Lilv State
  4570. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4571. {
  4572. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4573. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4574. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4575. }
  4576. // -------------------------------------------------------------------
  4577. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4578. };
  4579. // -------------------------------------------------------------------------------------------------------------------
  4580. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4581. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4582. {
  4583. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4584. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4585. const int32_t portIndex = argv[0]->i;
  4586. const char* const atomBuf = (const char*)&argv[1]->s;
  4587. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4588. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  4589. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4590. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  4591. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4592. return 0;
  4593. }
  4594. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4595. {
  4596. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4597. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4598. const int32_t urid = argv[0]->i;
  4599. const char* const uri = (const char*)&argv[1]->s;
  4600. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4601. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4602. return 0;
  4603. }
  4604. #undef lv2PluginPtr
  4605. CARLA_BACKEND_END_NAMESPACE
  4606. #endif // WANT_LV2
  4607. // -------------------------------------------------------------------------------------------------------------------
  4608. CARLA_BACKEND_START_NAMESPACE
  4609. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4610. {
  4611. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  4612. #ifdef WANT_LV2
  4613. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4614. if (! plugin->init(init.filename, init.name, init.label))
  4615. {
  4616. delete plugin;
  4617. return nullptr;
  4618. }
  4619. plugin->reload();
  4620. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4621. {
  4622. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4623. delete plugin;
  4624. return nullptr;
  4625. }
  4626. return plugin;
  4627. #else
  4628. init.engine->setLastError("LV2 support not available");
  4629. return nullptr;
  4630. #endif
  4631. }
  4632. CARLA_BACKEND_END_NAMESPACE
  4633. // -------------------------------------------------------------------------------------------------------------------