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.

5683 lines
213KB

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