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.

5723 lines
215KB

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