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.

5714 lines
214KB

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