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.

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