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.

5556 lines
208KB

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